]>
git.zerfleddert.de Git - upsgraph/blob - upsgraph.pl
010d11445a9af2766dc1a5c2d9e67387798c13b5
3 #Due to memory leak in Debian squeeze (Bug #545519)
7 if ((@ARGV != 1) && (@ARGV != 2)) {
8 print STDERR
"Syntax: ${0} configfile [uid]\n";
19 $UPSGRAPH::outdir
= "";
21 $UPSGRAPH::keep
= (370*24*60*60)/$UPSGRAPH::step
;
22 $UPSGRAPH::hosts
= ();
24 do $ARGV[0] or die "can't read config: $!";
26 my $outdir = $UPSGRAPH::outdir
;
27 my $step = $UPSGRAPH::step
;
28 my $keep = $UPSGRAPH::keep
;
29 my $hosts = $UPSGRAPH::hosts
;
36 $rrd_result = RRDs
::error
;
38 $rrd_result = system("rrdtool", "update", @args);
47 @rrd_out = RRDs
::graph
(@args);
48 $rrd_result = RRDs
::error
;
52 open(RRDFD
, '-|', 'rrdtool', 'graph', @args);
59 if ($rrd_result == 0) {
61 push @rrd_out, split(/x/, $rrd_stdout);
74 my @cmd = ("${newrrd}", "--step=${step}");
76 if (defined($start)) {
77 push @cmd, "--start=${start}";
80 push @cmd, "DS:${field}:GAUGE:600:" .
81 $vars->{$field}->{'min'} . ":" .
82 $vars->{$field}->{'max'} . " ";
84 push @cmd, "RRA:AVERAGE:0.5:1:${keep}";
88 print "Error while creating: " . RRDs
::error
. "\n";
95 my $community = shift;
98 (my $session, my $error) = Net
::SNMP
->session(Hostname
=> $address,
99 Community
=> $community);
102 print STDERR
"session error: $error";
106 $session->translate(0);
108 my $result = $session->get_request($oid);
112 return undef if (!defined($result));
121 my $sock = IO
::Socket
::INET
->new(PeerAddr
=> $address,
126 return undef if (!$sock);
128 my $select = IO
::Select
->new($sock);
132 if ($select->can_read(1)) {
133 chomp($value) if (sysread($sock, $value, 4096) > 0);
147 sub fetch_tcp_multi
(@
) {
150 my $delimiter = shift;
153 my $sock = IO
::Socket
::INET
->new(PeerAddr
=> $address,
158 return undef if (!$sock);
160 my $select = IO
::Select
->new($sock);
162 while($select->can_read(1)) {
163 if (sysread($sock, my $buf, 16384) > 0) {
165 foreach my $line (split(/\n/, $buf)) {
166 (my $key, my $value) = split(/${delimiter}/, $line);
168 $values{$key} = $value;
182 print STDERR
"Running as root, please provide UID as 2th argument!\n";
186 print "Running as root, switching to ".$ARGV[1]."\n";
190 foreach my $host (@
$hosts) {
191 my $rrdfile = $host->{'rrdfile'};
193 foreach my $var (keys(%{$host->{'vars'}})) {
194 $host->{'vars'}->{$var}->{'min'} = 'U' if (!defined($host->{'vars'}->{$var}->{'min'}));
195 $host->{'vars'}->{$var}->{'max'} = 'U' if (!defined($host->{'vars'}->{$var}->{'max'}));
198 if (-e
"${rrdfile}") {
199 print "Reading old ${rrdfile} to preserve data...\n";
201 my $rrdinfo = RRDs
::info
("${rrdfile}");
203 print "Error while getting info: " . RRDs
::error
. "\n";
207 (my $start, my $ostep, my $names, my $data) =
208 RRDs
::fetch
("${rrdfile}",
209 "-s " . (time() - ($rrdinfo->{'rra[0].rows'} * $rrdinfo->{'step'})),
213 print "Error while fetching data: " . RRDs
::error
. "\n";
217 foreach my $field (@
$names) {
218 if (! -e
"${rrdfile}.${field}") {
219 rrdcreate
("${rrdfile}.${field}",
222 (${start
}-${ostep
}));
227 foreach my $line (@
$data) {
228 foreach my $field (@
$names) {
229 my $val = shift (@
$line);
230 $val = 'U' if (!defined($val));
232 RRDs
::update
("${rrdfile}.${field}", "${pos}:${val}");
234 print "Can't insert data: " . RRDs
::error
. "\n";
242 if ((($pos-$start)/$ostep) == $#$data) {
247 rename("${rrdfile}", "${rrdfile}.old") or die "Can't rename old file: $!\n";
250 foreach my $field (@
{$host->{'fields'}}) {
251 if (! -e
"${rrdfile}.${field}") {
252 print "Creating ${rrdfile}.${field}...\n";
253 rrdcreate
("${rrdfile}.${field}",
258 my $rrdinfo = RRDs
::info
("${rrdfile}.${field}");
260 print "Error while getting info: " . RRDs
::error
. "\n";
264 if (defined($rrdinfo->{"ds[${field}].min"})) {
265 if ($rrdinfo->{"ds[${field}].min"} ne $host->{'vars'}->{$field}->{'min'}) {
266 RRDs
::tune
("${rrdfile}.${field}","-i",$field.":".$host->{'vars'}->{$field}->{'min'});
269 if ($host->{'vars'}->{$field}->{'min'} ne 'U') {
270 RRDs
::tune
("${rrdfile}.${field}","-i",$field.":".$host->{'vars'}->{$field}->{'min'});
275 print "Error while setting min: " . RRDs
::error
. "\n";
279 if (defined($rrdinfo->{"ds[${field}].max"})) {
280 if ($rrdinfo->{"ds[${field}].max"} ne $host->{'vars'}->{$field}->{'max'}) {
281 RRDs
::tune
("${rrdfile}.${field}","-a",$field.":".$host->{'vars'}->{$field}->{'max'});
284 if ($host->{'vars'}->{$field}->{'max'} ne 'U') {
285 RRDs
::tune
("${rrdfile}.${field}","-a",$field.":".$host->{'vars'}->{$field}->{'max'});
290 print "Error while setting max: " . RRDs
::error
. "\n";
294 if ($rrdinfo->{'rra[0].rows'} != $keep) {
295 print "Resizing ${rrdfile}.${field} from " . $rrdinfo->{'rra[0].rows'} .
296 " to ${keep} samples.\n";
298 (my $start, my $ostep, my $names, my $data) =
299 RRDs
::fetch
("${rrdfile}.${field}",
300 "-s " . (time() - ($rrdinfo->{'rra[0].rows'} * $rrdinfo->{'step'})),
304 print "Error while fetching data: " . RRDs
::error
. "\n";
308 rrdcreate
("${rrdfile}.${field}.new",
311 (${start
}-${ostep
}));
313 print "Preserving data since " . localtime($start) . "\n";
316 foreach my $line (@
$data) {
317 my $vline = "${pos}";
319 foreach my $val (@
$line) {
320 $val = 'U' if (!defined($val));
323 RRDs
::update
("${rrdfile}.${field}.new", $vline) or die "Can't insert data\n";
326 print "Error while updating: " . RRDs
::error
. "\n";
331 if ((($pos-$start)/$ostep) == $#$data) {
336 rename("${rrdfile}.${field}", "${rrdfile}.${field}.old") or die "Can't rename old file: $!\n";
337 rename("${rrdfile}.${field}.new", "${rrdfile}.${field}") or die "Can't rename new file: $!\n";
339 $rrdinfo = RRDs
::info
("${rrdfile}.${field}");
341 print "Error while getting info: " . RRDs
::error
. "\n";
345 if ($rrdinfo->{'rra[0].rows'} != $keep) {
355 die "fork failed!" if (!defined($child));
357 exit 0 if ($child != 0);
360 open(HTML
, ">${outdir}/index.html.new");
362 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>';
363 print HTML
'<body bgcolor="#ffffff">';
365 foreach my $host (@
$hosts) {
366 print HTML
"[<a href=\"#".${host
}->{'name'}."\">".${host
}->{'name'}."</a>] ";
370 foreach my $host (@
$hosts) {
372 print HTML
"<a name=\"".${host
}->{'name'}."\"></a>\n";
373 my $vars = $host->{'vars'};
374 my $rrdfile = $host->{'rrdfile'};
375 my $hostname = $host->{'name'};
376 my %multi_values = ();
378 foreach my $var (@
{$host->{'fields'}}) {
379 delete $vars->{$var}->{'value'};
383 if ((!defined($vars->{$var}->{'proto'})) ||
384 ($vars->{$var}->{'proto'} eq '') ||
385 ($vars->{$var}->{'proto'} eq 'snmp')) {
386 $result = fetch_snmp
($host->{'address'}, $host->{'community'}, $vars->{$var}->{'oid'});
387 } elsif ($vars->{$var}->{'proto'} eq 'tcp') {
388 $result = fetch_tcp
($host->{'address'}, $vars->{$var}->{'port'});
389 } elsif ($vars->{$var}->{'proto'} eq 'tcp_multi') {
390 if (defined($multi_values{$vars->{$var}->{'multi_id'}})) {
391 $result = $multi_values{$vars->{$var}->{'multi_id'}}
393 my %values = fetch_tcp_multi
($host->{'address'}, $vars->{$var}->{'port'}, $vars->{$var}->{'multi_delimiter'});
394 @multi_values{keys %values} = values %values;
395 $result = $multi_values{$vars->{$var}->{'multi_id'}};
399 next unless (defined $result);
401 $vars->{$var}->{'value'} = $result;
402 if (defined($vars->{$var}->{'factor'})) {
403 $vars->{$var}->{'value'} *= $vars->{$var}->{'factor'};
407 foreach my $var (@
{$host->{'fields'}}) {
408 if (!(defined($vars->{$var}->{'value'}))) {
409 $vars->{$var}->{'value'} = 'U';
411 rrd_update
("${rrdfile}.${var}", "N:" . $vars->{$var}->{'value'});
414 print "Error while updating: " . $rrd_result . "\n";
417 foreach my $var (@
{$host->{'fields'}}) {
418 my @graphdef = ('-P', "--lazy", "-t", $hostname." - ".$vars->{$var}->{'name'}, "DEF:${var}=${rrdfile}.${var}:${var}:AVERAGE", "LINE1:${var}#FF0000");
420 push @graphdef, "VDEF:cur=${var},LAST";
421 push @graphdef, 'GPRINT:cur:Current\\: <span foreground="#FF0000">%.2lf</span>\\r';
424 $mtime=(stat("${outdir}/${hostname}.${var}.png.work"))[9];
426 (my $averages, my $width, my $height) =
427 rrd_graph
("${outdir}/${hostname}.${var}.png.work",
428 "-w", "720", @graphdef);
434 print "Error while graphing: " . $rrd_result . "\n";
436 my $newmtime=(stat("${outdir}/${hostname}.${var}.png.work"))[9];
437 if ((!defined($mtime)) || ($newmtime != $mtime)) {
438 copy
("${outdir}/${hostname}.${var}.png.work", "${outdir}/${hostname}.${var}.png.new");
439 rename("${outdir}/${hostname}.${var}.png.new", "${outdir}/${hostname}.${var}.png");
443 print HTML
"<a href=\"${hostname}.${var}.html\"><img src=\"${hostname}.${var}.png\" width=\"${width}\" height=\"${height}\" border=\"0\"></a><br>\n";
445 open (HTML2
, ">${outdir}/${hostname}.${var}.html.new");
446 print HTML2
'<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>' . $vars->{$var}->{'name'} . '</title></head>';
447 print HTML2
'<body bgcolor="#ffffff">';
449 push @graphdef, "VDEF:min=${var},MINIMUM";
450 push @graphdef, "GPRINT:min:Minimum\\: %.2lf";
452 push @graphdef, "VDEF:avg=${var},AVERAGE";
453 push @graphdef, "GPRINT:avg:Average\\: %.2lf";
455 push @graphdef, "VDEF:max=${var},MAXIMUM";
456 push @graphdef, "GPRINT:max:Maximum\\: %.2lf";
458 push @graphdef, "VDEF:cur=${var},LAST";
459 push @graphdef, "GPRINT:cur:Current\\: %.2lf";
461 $mtime=(stat("${outdir}/${hostname}.${var}.long.png.work"))[9];
462 ($averages, $width, $height) =
463 rrd_graph
("${outdir}/${hostname}.${var}.long.png.work",
464 "-w", "1008", @graphdef);
467 print "Error while graphing: " . $rrd_result . "\n";
469 my $newmtime=(stat("${outdir}/${hostname}.${var}.long.png.work"))[9];
470 if ((!defined($mtime)) || ($newmtime != $mtime)) {
471 copy
("${outdir}/${hostname}.${var}.long.png.work", "${outdir}/${hostname}.${var}.long.png.new");
472 rename("${outdir}/${hostname}.${var}.long.png.new", "${outdir}/${hostname}.${var}.long.png");
476 print HTML2
"<img src=\"${hostname}.${var}.long.png\" width=\"${width}\" height=\"${height}\"><br>";
478 $mtime=(stat("${outdir}/${hostname}.${var}.week.png.work"))[9];
479 ($averages, $width, $height) =
480 rrd_graph
("${outdir}/${hostname}.${var}.week.png.work",
481 "-w", "1008", "-e", "now", "-s", "end-1w", @graphdef);
484 print "Error while graphing: " . $rrd_result . "\n";
486 my $newmtime=(stat("${outdir}/${hostname}.${var}.week.png.work"))[9];
487 if ((!defined($mtime)) || ($newmtime != $mtime)) {
488 copy
("${outdir}/${hostname}.${var}.week.png.work", "${outdir}/${hostname}.${var}.week.png.new");
489 rename("${outdir}/${hostname}.${var}.week.png.new", "${outdir}/${hostname}.${var}.week.png");
493 print HTML2
"<img src=\"${hostname}.${var}.week.png\" width=\"${width}\" height=\"${height}\"><br>";
495 $mtime=(stat("${outdir}/${hostname}.${var}.year.png.work"))[9];
496 ($averages, $width, $height) =
497 rrd_graph
("${outdir}/${hostname}.${var}.year.png.work",
498 "-w", "1008", "-e", "now", "-s", "end-1y", @graphdef);
501 print "Error while graphing: " . $rrd_result . "\n";
503 my $newmtime=(stat("${outdir}/${hostname}.${var}.year.png.work"))[9];
504 if ((!defined($mtime)) || ($newmtime != $mtime)) {
505 copy
("${outdir}/${hostname}.${var}.year.png.work", "${outdir}/${hostname}.${var}.year.png.new");
506 rename("${outdir}/${hostname}.${var}.year.png.new", "${outdir}/${hostname}.${var}.year.png");
510 print HTML2
"<img src=\"${hostname}.${var}.year.png\" width=\"${width}\" height=\"${height}\"><br>";
512 print HTML2
"</body></html>\n";
514 rename("${outdir}/${hostname}.${var}.html.new", "${outdir}/${hostname}.${var}.html");
518 print HTML
"</body></html>\n";
519 print HTML
"<br>Generated on: " . localtime(time());
520 print HTML
' by <a href="http://git.zerfleddert.de/cgi-bin/gitweb.cgi/upsgraph">upsgraph</a>.';
524 rename("${outdir}/index.html.new", "${outdir}/index.html");