]>
git.zerfleddert.de Git - upsgraph/blob - upsgraph.pl
e846a23b42eeecca4219fbcc561ea5ed9e225207
3 if ((@ARGV != 1) && (@ARGV != 2)) {
4 print STDERR
"Syntax: ${0} configfile [uid]\n";
14 $UPSGRAPH::outdir
= "";
16 $UPSGRAPH::keep
= (370*24*60*60)/$UPSGRAPH::step
;
17 $UPSGRAPH::hosts
= ();
19 do $ARGV[0] or die "can't read config: $!";
21 my $outdir = $UPSGRAPH::outdir
;
22 my $step = $UPSGRAPH::step
;
23 my $keep = $UPSGRAPH::keep
;
24 my $hosts = $UPSGRAPH::hosts
;
32 my @cmd = ("${newrrd}", "--step=${step}");
34 if (defined($start)) {
35 push @cmd, "--start=${start}";
38 push @cmd, "DS:${field}:GAUGE:600:" .
39 $vars->{$field}->{'min'} . ":" .
40 $vars->{$field}->{'max'} . " ";
42 push @cmd, "RRA:AVERAGE:0.5:1:${keep}";
46 print "Error while creating: " . RRDs
::error
. "\n";
53 my $community = shift;
56 (my $session, my $error) = Net
::SNMP
->session(Hostname
=> $address,
57 Community
=> $community);
60 print STDERR
"session error: $error";
63 $session->translate(0);
65 my $result = $session->get_request($oid);
69 return undef if (!defined($result));
78 my $sock = IO
::Socket
::INET
->new(PeerAddr
=> $address,
83 return undef if (!$sock);
85 chomp(my $value = <$sock>);
100 print STDERR
"Running as root, please provide UID as 2th argument!\n";
104 print "Running as root, switching to ".$ARGV[1]."\n";
108 foreach my $host (@
$hosts) {
109 my $rrdfile = $host->{'rrdfile'};
111 foreach my $var (keys(%{$host->{'vars'}})) {
112 $host->{'vars'}->{$var}->{'min'} = 'U' if (!defined($host->{'vars'}->{$var}->{'min'}));
113 $host->{'vars'}->{$var}->{'max'} = 'U' if (!defined($host->{'vars'}->{$var}->{'max'}));
116 if (-e
"${rrdfile}") {
117 print "Reading old ${rrdfile} to preserve data...\n";
119 my $rrdinfo = RRDs
::info
("${rrdfile}");
121 print "Error while getting info: " . RRDs
::error
. "\n";
125 (my $start, my $ostep, my $names, my $data) =
126 RRDs
::fetch
("${rrdfile}",
127 "-s " . (time() - ($rrdinfo->{'rra[0].rows'} * $rrdinfo->{'step'})),
131 print "Error while fetching data: " . RRDs
::error
. "\n";
135 foreach my $field (@
$names) {
136 if (! -e
"${rrdfile}.${field}") {
137 rrdcreate
("${rrdfile}.${field}",
140 (${start
}-${ostep
}));
145 foreach my $line (@
$data) {
146 foreach my $field (@
$names) {
147 my $val = shift (@
$line);
148 $val = 'U' if (!defined($val));
150 RRDs
::update
("${rrdfile}.${field}", "${pos}:${val}");
152 print "Can't insert data: " . RRDs
::error
. "\n";
160 if ((($pos-$start)/$ostep) == $#$data) {
165 rename("${rrdfile}", "${rrdfile}.old") or die "Can't rename old file: $!\n";
168 foreach my $field (@
{$host->{'fields'}}) {
169 if (! -e
"${rrdfile}.${field}") {
170 print "Creating ${rrdfile}.${field}...\n";
171 rrdcreate
("${rrdfile}.${field}",
176 my $rrdinfo = RRDs
::info
("${rrdfile}.${field}");
178 print "Error while getting info: " . RRDs
::error
. "\n";
182 if (defined($rrdinfo->{"ds[${field}].min"})) {
183 if ($rrdinfo->{"ds[${field}].min"} ne $host->{'vars'}->{$field}->{'min'}) {
184 RRDs
::tune
("${rrdfile}.${field}","-i",$field.":".$host->{'vars'}->{$field}->{'min'});
187 if ($host->{'vars'}->{$field}->{'min'} ne 'U') {
188 RRDs
::tune
("${rrdfile}.${field}","-i",$field.":".$host->{'vars'}->{$field}->{'min'});
193 print "Error while setting min: " . RRDs
::error
. "\n";
197 if (defined($rrdinfo->{"ds[${field}].max"})) {
198 if ($rrdinfo->{"ds[${field}].max"} ne $host->{'vars'}->{$field}->{'max'}) {
199 RRDs
::tune
("${rrdfile}.${field}","-a",$field.":".$host->{'vars'}->{$field}->{'max'});
202 if ($host->{'vars'}->{$field}->{'max'} ne 'U') {
203 RRDs
::tune
("${rrdfile}.${field}","-a",$field.":".$host->{'vars'}->{$field}->{'max'});
208 print "Error while setting max: " . RRDs
::error
. "\n";
212 if ($rrdinfo->{'rra[0].rows'} != $keep) {
213 print "Resizing ${rrdfile}.${field} from " . $rrdinfo->{'rra[0].rows'} .
214 " to ${keep} samples.\n";
216 (my $start, my $ostep, my $names, my $data) =
217 RRDs
::fetch
("${rrdfile}.${field}",
218 "-s " . (time() - ($rrdinfo->{'rra[0].rows'} * $rrdinfo->{'step'})),
222 print "Error while fetching data: " . RRDs
::error
. "\n";
226 rrdcreate
("${rrdfile}.${field}.new",
229 (${start
}-${ostep
}));
231 print "Preserving data since " . localtime($start) . "\n";
234 foreach my $line (@
$data) {
235 my $vline = "${pos}";
237 foreach my $val (@
$line) {
238 $val = 'U' if (!defined($val));
241 RRDs
::update
("${rrdfile}.${field}.new", $vline) or die "Can't insert data\n";
244 print "Error while updating: " . RRDs
::error
. "\n";
249 if ((($pos-$start)/$ostep) == $#$data) {
254 rename("${rrdfile}.${field}", "${rrdfile}.${field}.old") or die "Can't rename old file: $!\n";
255 rename("${rrdfile}.${field}.new", "${rrdfile}.${field}") or die "Can't rename new file: $!\n";
257 $rrdinfo = RRDs
::info
("${rrdfile}.${field}");
259 print "Error while getting info: " . RRDs
::error
. "\n";
263 if ($rrdinfo->{'rra[0].rows'} != $keep) {
273 die "fork failed!" if (!defined($child));
275 exit 0 if ($child != 0);
278 open(HTML
, ">${outdir}/index.html.new");
280 print HTML
'<html><head><meta http-equiv="refresh" content="60"/><meta http-equiv="cache-control" content="no-cache"/><meta http-equiv="pragma" content="no-cache"/><meta http_equiv="expires" content="Sat, 26 Jul 1997 05:00:00 GMT"/><title>Status</title></head>';
281 print HTML
'<body bgcolor="#ffffff">';
283 foreach my $host (@
$hosts) {
284 print HTML
"[<a href=\"#".${host
}->{'name'}."\">".${host
}->{'name'}."</a>] ";
288 foreach my $host (@
$hosts) {
290 print HTML
"<a name=\"".${host
}->{'name'}."\"></a>\n";
291 my $vars = $host->{'vars'};
292 my $rrdfile = $host->{'rrdfile'};
293 my $hostname = $host->{'name'};
295 foreach my $var (@
{$host->{'fields'}}) {
296 delete $vars->{$var}->{'value'};
300 if ((!defined($vars->{$var}->{'proto'})) ||
301 ($vars->{$var}->{'proto'} eq '') ||
302 ($vars->{$var}->{'proto'} eq 'snmp')) {
303 $result = fetch_snmp
($host->{'address'}, $host->{'community'}, $vars->{$var}->{'oid'});
304 } elsif ($vars->{$var}->{'proto'} eq 'tcp') {
305 $result = fetch_tcp
($host->{'address'}, $vars->{$var}->{'port'});
308 next unless (defined $result);
310 $vars->{$var}->{'value'} = $result;
311 if (defined($vars->{$var}->{'factor'})) {
312 $vars->{$var}->{'value'} *= $vars->{$var}->{'factor'};
316 foreach my $var (@
{$host->{'fields'}}) {
317 if (!(defined($vars->{$var}->{'value'}))) {
318 $vars->{$var}->{'value'} = 'U';
320 RRDs
::update
("${rrdfile}.${var}", "N:" . $vars->{$var}->{'value'});
323 print "Error while updating: " . RRDs
::error
. "\n";
326 foreach my $var (@
{$host->{'fields'}}) {
327 my @graphdef = ("--lazy", "-t", $hostname." - ".$vars->{$var}->{'name'}, "DEF:${var}=${rrdfile}.${var}:${var}:AVERAGE", "LINE1:${var}#FF0000");
328 (my $averages, my $width, my $height) =
329 RRDs
::graph
("${outdir}/${hostname}.${var}.png.work",
330 "-w", "720", @graphdef);
333 print "Error while graphing: " . RRDs
::error
. "\n";
335 copy
("${outdir}/${hostname}.${var}.png.work", "${outdir}/${hostname}.${var}.png.new");
336 rename("${outdir}/${hostname}.${var}.png.new", "${outdir}/${hostname}.${var}.png");
339 print HTML
"<a href=\"${hostname}.${var}.html\"><img src=\"${hostname}.${var}.png\" width=\"${width}\" height=\"${height}\" border=\"0\"></a><br>\n";
341 open (HTML2
, ">${outdir}/${hostname}.${var}.html.new");
342 print HTML2
"<html><head><title>" . $vars->{$var}->{'name'} . "</title></head>";
343 print HTML2
'<body bgcolor="#ffffff">';
346 push @graphdef, "VDEF:min=${var},MINIMUM";
347 push @graphdef, "GPRINT:min:Minimum\\: %.2lf";
349 push @graphdef, "VDEF:avg=${var},AVERAGE";
350 push @graphdef, "GPRINT:avg:Average\\: %.2lf";
352 push @graphdef, "VDEF:max=${var},MAXIMUM";
353 push @graphdef, "GPRINT:max:Maximum\\: %.2lf";
355 push @graphdef, "VDEF:cur=${var},LAST";
356 push @graphdef, "GPRINT:cur:Current\\: %.2lf";
358 ($averages, $width, $height) =
359 RRDs
::graph
("${outdir}/${hostname}.${var}.long.png.work",
360 "-w", "1008", @graphdef);
363 print "Error while graphing: " . RRDs
::error
. "\n";
365 copy
("${outdir}/${hostname}.${var}.long.png.work", "${outdir}/${hostname}.${var}.long.png.new");
366 rename("${outdir}/${hostname}.${var}.long.png.new", "${outdir}/${hostname}.${var}.long.png");
369 print HTML2
"<img src=\"${hostname}.${var}.long.png\" width=\"${width}\" height=\"${height}\"><br>";
371 ($averages, $width, $height) =
372 RRDs
::graph
("${outdir}/${hostname}.${var}.week.png.work",
373 "-w", "1008", "-e", "now", "-s", "end-1w", @graphdef);
376 print "Error while graphing: " . RRDs
::error
. "\n";
378 copy
("${outdir}/${hostname}.${var}.week.png.work", "${outdir}/${hostname}.${var}.week.png.new");
379 rename("${outdir}/${hostname}.${var}.week.png.new", "${outdir}/${hostname}.${var}.week.png");
382 print HTML2
"<img src=\"${hostname}.${var}.week.png\" width=\"${width}\" height=\"${height}\"><br>";
384 ($averages, $width, $height) =
385 RRDs
::graph
("${outdir}/${hostname}.${var}.year.png.work",
386 "-w", "1008", "-e", "now", "-s", "end-1y", @graphdef);
389 print "Error while graphing: " . RRDs
::error
. "\n";
391 copy
("${outdir}/${hostname}.${var}.year.png.work", "${outdir}/${hostname}.${var}.year.png.new");
392 rename("${outdir}/${hostname}.${var}.year.png.new", "${outdir}/${hostname}.${var}.year.png");
395 print HTML2
"<img src=\"${hostname}.${var}.year.png\" width=\"${width}\" height=\"${height}\"><br>";
397 print HTML2
"</body></html>\n";
399 rename("${outdir}/${hostname}.${var}.html.new", "${outdir}/${hostname}.${var}.html");
403 print HTML
"</body></html>\n";
404 print HTML
"<br>Generated on: " . localtime(time());
405 print HTML
' by <a href="http://git.zerfleddert.de/cgi-bin/gitweb.cgi/upsgraph">upsgraph</a>.';
409 rename("${outdir}/index.html.new", "${outdir}/index.html");