]>
git.zerfleddert.de Git - upsgraph/blob - upsgraph.pl
1e335acd1aa613b9a151140a4cf965b04a967203
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
= "";
20 $UPSGRAPH::daysCovered
= 370;
22 $UPSGRAPH::stepsPerHour
= (60 * 60) / $UPSGRAPH::step
;
23 $UPSGRAPH::keep
= ($UPSGRAPH::daysCovered
*24*60*60)/$UPSGRAPH::step
;
24 $UPSGRAPH::keepHours
= ($UPSGRAPH::daysCovered
*24*60*60)/$UPSGRAPH::stepsPerHour/$UPSGRAPH::step
;
25 $UPSGRAPH::hosts
= ();
27 do $ARGV[0] or die "can't read config: $!";
29 my $outdir = $UPSGRAPH::outdir
;
30 my $step = $UPSGRAPH::step
;
31 my $stepsPerHour = $UPSGRAPH::stepsPerHour
;
32 my $keep = $UPSGRAPH::keep
;
33 my $keepHours = $UPSGRAPH::keepHours
;
34 my $hosts = $UPSGRAPH::hosts
;
41 $rrd_result = RRDs
::error
;
43 $rrd_result = system("rrdtool", "update", @args);
52 @rrd_out = RRDs
::graph
(@args);
53 $rrd_result = RRDs
::error
;
57 open(RRDFD
, '-|', 'rrdtool', 'graph', @args);
64 if ($rrd_result == 0) {
66 push @rrd_out, split(/x/, $rrd_stdout);
79 my @cmd = ("${newrrd}", "--step=${step}");
81 if (defined($start)) {
82 push @cmd, "--start=${start}";
85 push @cmd, "DS:${field}:GAUGE:600:" .
86 $vars->{$field}->{'min'} . ":" .
87 $vars->{$field}->{'max'};
89 push @cmd, "RRA:AVERAGE:0.5:1:${keep}";
90 push @cmd, "RRA:MIN:0.4:${stepsPerHour}:${keepHours}";
91 push @cmd, "RRA:MAX:0.4:${stepsPerHour}:${keepHours}";
95 print "Error while creating: " . RRDs
::error
. "\n";
102 my $community = shift;
105 (my $session, my $error) = Net
::SNMP
->session(Hostname
=> $address,
106 Community
=> $community);
109 print STDERR
"session error: $error";
113 $session->translate(0);
115 my $result = $session->get_request($oid);
119 return undef if (!defined($result));
128 my $sock = IO
::Socket
::INET
->new(PeerAddr
=> $address,
133 return undef if (!$sock);
135 my $select = IO
::Select
->new($sock);
139 if ($select->can_read(1)) {
140 chomp($value) if (sysread($sock, $value, 4096) > 0);
154 sub fetch_tcp_multi
(@
) {
157 my $delimiter = shift;
160 my $sock = IO
::Socket
::INET
->new(PeerAddr
=> $address,
165 return undef if (!$sock);
167 my $select = IO
::Select
->new($sock);
169 while($select->can_read(1)) {
170 if (sysread($sock, my $buf, 16384) > 0) {
172 foreach my $line (split(/\n/, $buf)) {
173 (my $key, my $value) = split(/${delimiter}/, $line);
175 $values{$key} = $value;
189 print STDERR
"Running as root, please provide UID as 2th argument!\n";
193 print "Running as root, switching to ".$ARGV[1]."\n";
197 foreach my $host (@
$hosts) {
198 my $rrdfile = $host->{'rrdfile'};
200 foreach my $var (keys(%{$host->{'vars'}})) {
201 $host->{'vars'}->{$var}->{'min'} = 'U' if (!defined($host->{'vars'}->{$var}->{'min'}));
202 $host->{'vars'}->{$var}->{'max'} = 'U' if (!defined($host->{'vars'}->{$var}->{'max'}));
205 if (-e
"${rrdfile}") {
206 print "Reading old ${rrdfile} to preserve data...\n";
208 my $rrdinfo = RRDs
::info
("${rrdfile}");
210 print "Error while getting info: " . RRDs
::error
. "\n";
214 (my $start, my $ostep, my $names, my $data) =
215 RRDs
::fetch
("${rrdfile}",
216 "-s " . (time() - ($rrdinfo->{'rra[0].rows'} * $rrdinfo->{'step'})),
220 print "Error while fetching data: " . RRDs
::error
. "\n";
224 foreach my $field (@
$names) {
225 if (! -e
"${rrdfile}.${field}") {
226 rrdcreate
("${rrdfile}.${field}",
229 (${start
}-${ostep
}));
234 foreach my $line (@
$data) {
235 foreach my $field (@
$names) {
236 my $val = shift (@
$line);
237 $val = 'U' if (!defined($val));
239 RRDs
::update
("${rrdfile}.${field}", "${pos}:${val}");
241 print "Can't insert data: " . RRDs
::error
. "\n";
249 if ((($pos-$start)/$ostep) == $#$data) {
254 rename("${rrdfile}", "${rrdfile}.old") or die "Can't rename old file: $!\n";
257 foreach my $field (@
{$host->{'fields'}}) {
258 if (! -e
"${rrdfile}.${field}") {
259 print "Creating ${rrdfile}.${field}...\n";
260 rrdcreate
("${rrdfile}.${field}",
265 my $rrdinfo = RRDs
::info
("${rrdfile}.${field}");
267 print "Error while getting info: " . RRDs
::error
. "\n";
271 if (defined($rrdinfo->{"ds[${field}].min"})) {
272 if ($rrdinfo->{"ds[${field}].min"} ne $host->{'vars'}->{$field}->{'min'}) {
273 RRDs
::tune
("${rrdfile}.${field}","-i",$field.":".$host->{'vars'}->{$field}->{'min'});
276 if ($host->{'vars'}->{$field}->{'min'} ne 'U') {
277 RRDs
::tune
("${rrdfile}.${field}","-i",$field.":".$host->{'vars'}->{$field}->{'min'});
282 print "Error while setting min: " . RRDs
::error
. "\n";
286 if (defined($rrdinfo->{"ds[${field}].max"})) {
287 if ($rrdinfo->{"ds[${field}].max"} ne $host->{'vars'}->{$field}->{'max'}) {
288 RRDs
::tune
("${rrdfile}.${field}","-a",$field.":".$host->{'vars'}->{$field}->{'max'});
291 if ($host->{'vars'}->{$field}->{'max'} ne 'U') {
292 RRDs
::tune
("${rrdfile}.${field}","-a",$field.":".$host->{'vars'}->{$field}->{'max'});
297 print "Error while setting max: " . RRDs
::error
. "\n";
301 if ($rrdinfo->{'rra[0].rows'} != $keep ||
302 !defined($rrdinfo->{'rra[1].rows'}) || $rrdinfo->{'rra[1].rows'} != $keepHours) {
304 print "Resizing ${rrdfile}.${field} from " . $rrdinfo->{'rra[0].rows'} .
305 " to ${keep} samples.\n";
307 (my $start, my $ostep, my $names, my $data) =
308 RRDs
::fetch
("${rrdfile}.${field}",
309 "-s " . (time() - ($rrdinfo->{'rra[0].rows'} * $rrdinfo->{'step'})),
313 print "Error while fetching data: " . RRDs
::error
. "\n";
317 rrdcreate
("${rrdfile}.${field}.new",
320 (${start
}-${ostep
}));
322 print "Preserving data in file ${rrdfile}.${field} since " . localtime($start) . "\n";
325 foreach my $line (@
$data) {
326 my $vline = "${pos}";
328 foreach my $val (@
$line) {
329 $val = 'U' if (!defined($val));
332 RRDs
::update
("${rrdfile}.${field}.new", $vline) or die "Can't insert data\n";
335 print "Error while updating: " . RRDs
::error
. "\n";
340 if ((($pos-$start)/$ostep) == $#$data) {
345 rename("${rrdfile}.${field}", "${rrdfile}.${field}.old") or die "Can't rename old file: $!\n";
346 rename("${rrdfile}.${field}.new", "${rrdfile}.${field}") or die "Can't rename new file: $!\n";
348 $rrdinfo = RRDs
::info
("${rrdfile}.${field}");
350 print "Error while getting info: " . RRDs
::error
. "\n";
354 if ($rrdinfo->{'rra[0].rows'} != $keep) {
364 die "fork failed!" if (!defined($child));
366 exit 0 if ($child != 0);
369 open(HTML
, ">${outdir}/index.html.new");
371 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>';
372 print HTML
'<body bgcolor="#ffffff">';
374 foreach my $host (@
$hosts) {
375 print HTML
"[<a href=\"#".${host
}->{'name'}."\">".${host
}->{'name'}."</a>] ";
379 foreach my $host (@
$hosts) {
381 print HTML
"<a name=\"".${host
}->{'name'}."\"></a>\n";
382 my $vars = $host->{'vars'};
383 my $rrdfile = $host->{'rrdfile'};
384 my $hostname = $host->{'name'};
385 my %multi_values = ();
387 foreach my $var (@
{$host->{'fields'}}) {
388 delete $vars->{$var}->{'value'};
392 if ((!defined($vars->{$var}->{'proto'})) ||
393 ($vars->{$var}->{'proto'} eq '') ||
394 ($vars->{$var}->{'proto'} eq 'snmp')) {
395 $result = fetch_snmp
($host->{'address'}, $host->{'community'}, $vars->{$var}->{'oid'});
396 } elsif ($vars->{$var}->{'proto'} eq 'tcp') {
397 $result = fetch_tcp
($host->{'address'}, $vars->{$var}->{'port'});
398 } elsif ($vars->{$var}->{'proto'} eq 'tcp_multi') {
399 if (defined($multi_values{$vars->{$var}->{'multi_id'}})) {
400 $result = $multi_values{$vars->{$var}->{'multi_id'}}
402 my %values = fetch_tcp_multi
($host->{'address'}, $vars->{$var}->{'port'}, $vars->{$var}->{'multi_delimiter'});
403 @multi_values{keys %values} = values %values;
404 $result = $multi_values{$vars->{$var}->{'multi_id'}};
408 next unless (defined $result);
410 $vars->{$var}->{'value'} = $result;
411 if (defined($vars->{$var}->{'factor'})) {
412 $vars->{$var}->{'value'} *= $vars->{$var}->{'factor'};
416 foreach my $var (@
{$host->{'fields'}}) {
417 if (!(defined($vars->{$var}->{'value'}))) {
418 $vars->{$var}->{'value'} = 'U';
420 rrd_update
("${rrdfile}.${var}", "N:" . $vars->{$var}->{'value'});
423 print "Error while updating: " . $rrd_result . "\n";
426 foreach my $var (@
{$host->{'fields'}}) {
427 my @graphdef = ('-P', "--lazy", "-t", $hostname." - ".$vars->{$var}->{'name'}, "DEF:${var}=${rrdfile}.${var}:${var}:AVERAGE", "LINE1:${var}#FF0000");
429 push @graphdef, "DEF:${var}-min=${rrdfile}.${var}:${var}:MIN", "LINE1:${var}-min#0000FF";
430 push @graphdef, "DEF:${var}-max=${rrdfile}.${var}:${var}:MAX", "LINE1:${var}-max#00FF00";
431 push @graphdef, "VDEF:cur=${var},LAST";
432 push @graphdef, 'GPRINT:cur:Current\\: <span foreground="#FF0000">%.2lf</span>\\r';
435 $mtime=(stat("${outdir}/${hostname}.${var}.png.work"))[9];
437 (my $averages, my $width, my $height) =
438 rrd_graph
("${outdir}/${hostname}.${var}.png.work",
439 "-w", "720", @graphdef);
445 print "Error while graphing: " . $rrd_result . "\n";
447 my $newmtime=(stat("${outdir}/${hostname}.${var}.png.work"))[9];
448 if ((!defined($mtime)) || ($newmtime != $mtime)) {
449 copy
("${outdir}/${hostname}.${var}.png.work", "${outdir}/${hostname}.${var}.png.new");
450 rename("${outdir}/${hostname}.${var}.png.new", "${outdir}/${hostname}.${var}.png");
454 print HTML
"<a href=\"${hostname}.${var}.html\"><img src=\"${hostname}.${var}.png\" width=\"${width}\" height=\"${height}\" border=\"0\"></a><br>\n";
456 open (HTML2
, ">${outdir}/${hostname}.${var}.html.new");
457 print HTML2
'<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><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>';
458 print HTML2
'<body bgcolor="#ffffff">';
460 push @graphdef, "VDEF:min=${var}-min,MINIMUM";
461 push @graphdef, "GPRINT:min:Minimum\\: %.2lf";
463 push @graphdef, "VDEF:avg=${var},AVERAGE";
464 push @graphdef, "GPRINT:avg:Average\\: %.2lf";
466 push @graphdef, "VDEF:max=${var}-max,MAXIMUM";
467 push @graphdef, "GPRINT:max:Maximum\\: %.2lf";
469 push @graphdef, "VDEF:cur=${var},LAST";
470 push @graphdef, "GPRINT:cur:Current\\: %.2lf";
472 $mtime=(stat("${outdir}/${hostname}.${var}.long.png.work"))[9];
473 ($averages, $width, $height) =
474 rrd_graph
("${outdir}/${hostname}.${var}.long.png.work",
475 "-w", "1008", @graphdef);
478 print "Error while graphing: " . $rrd_result . "\n";
480 my $newmtime=(stat("${outdir}/${hostname}.${var}.long.png.work"))[9];
481 if ((!defined($mtime)) || ($newmtime != $mtime)) {
482 copy
("${outdir}/${hostname}.${var}.long.png.work", "${outdir}/${hostname}.${var}.long.png.new");
483 rename("${outdir}/${hostname}.${var}.long.png.new", "${outdir}/${hostname}.${var}.long.png");
487 print HTML2
"<img src=\"${hostname}.${var}.long.png\" width=\"${width}\" height=\"${height}\"><br>\n";
489 $mtime=(stat("${outdir}/${hostname}.${var}.week.png.work"))[9];
490 ($averages, $width, $height) =
491 rrd_graph
("${outdir}/${hostname}.${var}.week.png.work",
492 "-w", "1008", "-e", "now", "-s", "end-1w", @graphdef);
495 print "Error while graphing: " . $rrd_result . "\n";
497 my $newmtime=(stat("${outdir}/${hostname}.${var}.week.png.work"))[9];
498 if ((!defined($mtime)) || ($newmtime != $mtime)) {
499 copy
("${outdir}/${hostname}.${var}.week.png.work", "${outdir}/${hostname}.${var}.week.png.new");
500 rename("${outdir}/${hostname}.${var}.week.png.new", "${outdir}/${hostname}.${var}.week.png");
504 print HTML2
"<img src=\"${hostname}.${var}.week.png\" width=\"${width}\" height=\"${height}\"><br>\n";
506 $mtime=(stat("${outdir}/${hostname}.${var}.year.png.work"))[9];
507 ($averages, $width, $height) =
508 rrd_graph
("${outdir}/${hostname}.${var}.year.png.work",
509 "-w", "1008", "-e", "now", "-s", "end-1y", @graphdef);
512 print "Error while graphing: " . $rrd_result . "\n";
514 my $newmtime=(stat("${outdir}/${hostname}.${var}.year.png.work"))[9];
515 if ((!defined($mtime)) || ($newmtime != $mtime)) {
516 copy
("${outdir}/${hostname}.${var}.year.png.work", "${outdir}/${hostname}.${var}.year.png.new");
517 rename("${outdir}/${hostname}.${var}.year.png.new", "${outdir}/${hostname}.${var}.year.png");
521 print HTML2
"<img src=\"${hostname}.${var}.year.png\" width=\"${width}\" height=\"${height}\"><br>\n";
523 print HTML2
"</body></html>\n";
525 rename("${outdir}/${hostname}.${var}.html.new", "${outdir}/${hostname}.${var}.html");
529 print HTML
"</body></html>\n";
530 print HTML
"<br>Generated on: " . localtime(time());
531 print HTML
' by <a href="http://git.zerfleddert.de/cgi-bin/gitweb.cgi/upsgraph">upsgraph</a>.';
535 rename("${outdir}/index.html.new", "${outdir}/index.html");