-
Notifications
You must be signed in to change notification settings - Fork 0
/
statgraph.pl
executable file
·201 lines (173 loc) · 6.56 KB
/
statgraph.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#!/usr/bin/perl
# statgraph: A simple host resource graphing tool
# Copyright (C) 2004-2011 Ben Charlton <ben@spod.cx>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; see the file COPYING. If not, write to the Free
# Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301 USA, or see http://www.gnu.org
use strict;
use StatGraph;
## TODO: configure with getopt
my $config = "statgraph.conf";
## Get configuration
open CONFIG, $config || die "Cannot open $config: $!";
my @CONFIG = (<CONFIG>);
close CONFIG;
my %CONFIG = confparse(\@CONFIG);
my %HOSTS = %{$CONFIG{hosts}};
%CONFIG = %{$CONFIG{config}};
## Set default configuration options if they've not been specified
my $defaultport = $CONFIG{defaultport} || 27001;
my $rrdlocation = $CONFIG{rrdlocation} || "rrd/";
my $pid = $$;
my $parent = 0;
my @kids = ();
my $host;
## Main loop - run once for each host
FORK: foreach my $currhost (keys %HOSTS) {
my $newpid = fork();
if ( not defined $newpid ) {
# if return value of fork() is undef, something went wrong
die "fork didn't work: $!\n";
}
elsif ( $newpid == 0 ) {
# if return value is 0, this is the child process
$parent = $pid; # which has a parent called $pid
$pid = $$; # and which will have a process ID of its very own
@kids = (); # the child doesn't want this baggage from the parent
$host = $currhost;
last FORK; # and we don't want the child making babies either
}
else {
# the parent process is returned the PID of the newborn by fork()
push @kids, $newpid;
}
}
if ( $parent ) {
# if I have a parent, i.e. if I'm the child process
my %results;
my %ignore;
foreach (split(/\s+/, $HOSTS{$host}{ignore})) {
$ignore{$_} = 1;
}
## exec method
if ($HOSTS{$host}{method} eq 'exec') {
unless (defined $HOSTS{$host}{execcommand}) {
die "execcommand not specified for $host, skipping...";
}
%results = get_exec_results($HOSTS{$host}{execcommand}, "$rrdlocation$host.txt");
## Network socket connection method
} elsif ($HOSTS{$host}{method} eq 'connect') {
unless (defined $HOSTS{$host}{hostname}) {
die "hostname not specified for $host, skipping...";
}
my $port = $HOSTS{$host}{port} || $defaultport;
%results = get_net_results($HOSTS{$host}{hostname}, $port, "$rrdlocation$host.txt");
## Unknown method
} else {
die "Unknown method specified for $host, skipping...";
}
## Simple check that we've got reasonable statgrab data
unless ($results{const}{0} eq '0') {
die "Bad statgrab results";
} else {
print "got result for $host\n";
}
## Update RRDs
foreach ('cpu', 'load', 'mem', 'page', 'proc', 'user', 'swap') {
unless (-e "$rrdlocation$host.$_.rrd") {
create_rrd($rrdlocation, $_, $host, '');
}
}
update_rrd("$rrdlocation$host.cpu.rrd", sprintf("N:%s:%s:%s:%s:%s:%s:%s",
$results{cpu}{idle},
$results{cpu}{iowait},
$results{cpu}{kernel},
$results{cpu}{nice},
$results{cpu}{swap},
$results{cpu}{total},
$results{cpu}{user}));
update_rrd("$rrdlocation$host.load.rrd", sprintf("N:%s:%s:%s",
$results{load}{min1},
$results{load}{min5},
$results{load}{min15}));
update_rrd("$rrdlocation$host.mem.rrd", sprintf("N:%s:%s:%s:%s",
$results{mem}{cache},
$results{mem}{free},
$results{mem}{total},
$results{mem}{used}));
update_rrd("$rrdlocation$host.page.rrd", sprintf("N:%s:%s",
$results{page}{in},
$results{page}{out}));
update_rrd("$rrdlocation$host.proc.rrd", sprintf("N:%s:%s:%s:%s:%s",
$results{proc}{running},
$results{proc}{sleeping},
$results{proc}{stopped},
$results{proc}{total},
$results{proc}{zombie}));
update_rrd("$rrdlocation$host.user.rrd", sprintf("N:%s",
$results{user}{num}));
update_rrd("$rrdlocation$host.swap.rrd", sprintf("N:%s:%s:%s",
$results{swap}{free},
$results{swap}{total},
$results{swap}{used}));
## net device RRDs
foreach my $dev (keys %{ $results{net} }) {
unless (defined $ignore{"net.$dev"}) {
unless (-e "$rrdlocation$host.net.$dev.rrd") {
create_rrd($rrdlocation, 'net', $host, $dev);
}
update_rrd("$rrdlocation$host.net.$dev.rrd", sprintf("N:%s:%s:%s:%s:%s:%s",
$results{net}{$dev}{rx} || 0,
$results{net}{$dev}{tx} || 0,
$results{net}{$dev}{ipackets} || 0,
$results{net}{$dev}{opackets} || 0,
$results{net}{$dev}{ierrors} || 0,
$results{net}{$dev}{oerrors} || 0));
}
}
## disk device RRDs
foreach my $dev (keys %{ $results{disk} }) {
unless (defined $ignore{"disk.$dev"}) {
unless (-e "$rrdlocation$host.disk.$dev.rrd") {
create_rrd($rrdlocation, 'disk', $host, $dev);
}
update_rrd("$rrdlocation$host.disk.$dev.rrd", sprintf("N:%s:%s",
$results{disk}{$dev}{read_bytes},
$results{disk}{$dev}{write_bytes}));
}
}
## fs device RRDs
foreach my $dev (keys %{ $results{fs} }) {
unless (defined $ignore{"fs.$dev"}) {
unless (-e "$rrdlocation$host.fs.$dev.rrd") {
create_rrd($rrdlocation, 'fs', $host, $dev);
}
update_rrd("$rrdlocation$host.fs.$dev.rrd", sprintf("N:%s:%s:%s:%s",
$results{fs}{$dev}{used},
$results{fs}{$dev}{size},
$results{fs}{$dev}{used_inodes},
$results{fs}{$dev}{total_inodes}));
}
}
}
else {
# parent process needs to preside over the death of its kids
while ( my $kid = shift @kids ) {
my $reaped = waitpid( $kid, 0 );
unless ( $reaped == $kid ) {
warn "Something's up: $?\n";
}
}
}