]>
git.zerfleddert.de Git - upsgraph/blob - upsgraph.pl
cade9b25c20b2f10323c4c21b533b32cecda79dd
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
= ();
26 $UPSGRAPH::regenerateOnStart
= 1; #when set, regenerate graphs on script startup
28 do $ARGV[0] or die "can't read config: $!";
30 my $outdir = $UPSGRAPH::outdir
;
31 my $step = $UPSGRAPH::step
;
32 my $stepsPerHour = $UPSGRAPH::stepsPerHour
;
33 my $keep = $UPSGRAPH::keep
;
34 my $keepHours = $UPSGRAPH::keepHours
;
35 my $hosts = $UPSGRAPH::hosts
;
42 $rrd_result = RRDs
::error
;
44 $rrd_result = system("rrdtool", "update", @args);
53 @rrd_out = RRDs
::graph
(@args);
54 $rrd_result = RRDs
::error
;
58 open(RRDFD
, '-|', 'rrdtool', 'graph', @args);
65 if ($rrd_result == 0) {
67 push @rrd_out, split(/x/, $rrd_stdout);
80 my @cmd = ("${newrrd}", "--step=${step}");
82 if (defined($start)) {
83 push @cmd, "--start=${start}";
86 push @cmd, "DS:${field}:GAUGE:600:" .
87 $vars->{$field}->{'min'} . ":" .
88 $vars->{$field}->{'max'};
90 push @cmd, "RRA:AVERAGE:0.5:1:${keep}";
91 push @cmd, "RRA:MIN:0.4:${stepsPerHour}:${keepHours}";
92 push @cmd, "RRA:MAX:0.4:${stepsPerHour}:${keepHours}";
96 print "Error while creating: " . RRDs
::error
. "\n";
103 my $community = shift;
106 (my $session, my $error) = Net
::SNMP
->session(Hostname
=> $address,
107 Community
=> $community);
110 print STDERR
"session error: $error";
114 $session->translate(0);
116 my $result = $session->get_request($oid);
120 return undef if (!defined($result));
129 my $sock = IO
::Socket
::INET
->new(PeerAddr
=> $address,
134 return undef if (!$sock);
136 my $select = IO
::Select
->new($sock);
140 if ($select->can_read(1)) {
141 chomp($value) if (sysread($sock, $value, 4096) > 0);
155 sub fetch_tcp_multi
(@
) {
158 my $delimiter = shift;
161 my $sock = IO
::Socket
::INET
->new(PeerAddr
=> $address,
166 return undef if (!$sock);
168 my $select = IO
::Select
->new($sock);
170 while($select->can_read(1)) {
171 if (sysread($sock, my $buf, 16384) > 0) {
173 foreach my $line (split(/\n/, $buf)) {
174 (my $key, my $value) = split(/${delimiter}/, $line);
176 $values{$key} = $value;
193 my $dataPoints = shift;
195 push @args, "CDEF:prev${mode}1=PREV(${dataSrc})";
196 for (my $i = 1; $i < $dataPoints - 1; ++$i) {
199 push @args, "CDEF:prev${mode}${next}=PREV(prev${mode}${prev})";
203 for (my $i = 1; $i < $dataPoints; ++$i) {
204 $dayCons .= "prev${mode}${i},";
205 $consFunc .= ",${mode}";
207 push @args, "CDEF:day${mode}=${dayCons}${dataSrc}${consFunc}";
208 push @args, "CDEF:fillCalDay${mode}0=COUNT,${dataPoints},%,0,EQ,day${mode},UNKN,IF";
209 for (my $i = 1; $i < $dataPoints; ++$i) {
212 push @args, "CDEF:fillCalDay${mode}${next}=PREV(fillCalDay${mode}${prev})";
216 for (my $i = 0; $i < $dataPoints; ++$i) {
217 $fillPoint .= "COUNT,${dataPoints},%,${i},EQ,fillCalDay${mode}${i},";
220 push @args, "CDEF:${mode}Curve=${fillPoint}UNKN${if}";
221 my $forwardShift = (24*60*60) * ($dataPoints - 1) / $dataPoints;
222 push @args, "SHIFT:${mode}Curve:-${forwardShift}";
223 push @args, "LINE1:${mode}Curve#${color}:${label}";
232 foreach my $subGraph (@
$cfg) {
233 if ($subGraph eq 'avg') {
234 push @graphDef, "LINE1:${varname}-avg#FF0000";
235 } elsif ($subGraph eq 'day-min') {
236 push @graphDef, @
{dayGraphFunc
("${varname}-min", 'MIN', '0000ff', 'Day Minimum Temperature', $dpPerDay)};
237 } elsif ($subGraph eq 'day-max') {
238 push @graphDef, @
{dayGraphFunc
("${varname}-max", 'MAX', '00ff00', 'Day Maximum Temperature', $dpPerDay)};
246 print STDERR
"Running as root, please provide UID as 2th argument!\n";
250 print "Running as root, switching to ".$ARGV[1]."\n";
254 foreach my $host (@
$hosts) {
255 my $rrdfile = $host->{'rrdfile'};
257 foreach my $var (keys(%{$host->{'vars'}})) {
258 $host->{'vars'}->{$var}->{'min'} = 'U' if (!defined($host->{'vars'}->{$var}->{'min'}));
259 $host->{'vars'}->{$var}->{'max'} = 'U' if (!defined($host->{'vars'}->{$var}->{'max'}));
262 if (-e
"${rrdfile}") {
263 print "Reading old ${rrdfile} to preserve data...\n";
265 my $rrdinfo = RRDs
::info
("${rrdfile}");
267 print "Error while getting info: " . RRDs
::error
. "\n";
271 (my $start, my $ostep, my $names, my $data) =
272 RRDs
::fetch
("${rrdfile}",
273 "-s " . (time() - ($rrdinfo->{'rra[0].rows'} * $rrdinfo->{'step'})),
277 print "Error while fetching data: " . RRDs
::error
. "\n";
281 foreach my $field (@
$names) {
282 if (! -e
"${rrdfile}.${field}") {
283 rrdcreate
("${rrdfile}.${field}",
286 (${start
}-${ostep
}));
291 foreach my $line (@
$data) {
292 foreach my $field (@
$names) {
293 my $val = shift (@
$line);
294 $val = 'U' if (!defined($val));
296 RRDs
::update
("${rrdfile}.${field}", "${pos}:${val}");
298 print "Can't insert data: " . RRDs
::error
. "\n";
306 if ((($pos-$start)/$ostep) == $#$data) {
311 rename("${rrdfile}", "${rrdfile}.old") or die "Can't rename old file: $!\n";
314 foreach my $field (@
{$host->{'fields'}}) {
315 if (! -e
"${rrdfile}.${field}") {
316 print "Creating ${rrdfile}.${field}...\n";
317 rrdcreate
("${rrdfile}.${field}",
322 my $rrdinfo = RRDs
::info
("${rrdfile}.${field}");
324 print "Error while getting info: " . RRDs
::error
. "\n";
328 my $limitsChanged = 0;
329 if (defined($rrdinfo->{"ds[${field}].min"})) {
330 if ($rrdinfo->{"ds[${field}].min"} ne $host->{'vars'}->{$field}->{'min'}) {
332 RRDs
::tune
("${rrdfile}.${field}","-i",$field.":".$host->{'vars'}->{$field}->{'min'});
335 if ($host->{'vars'}->{$field}->{'min'} ne 'U') {
337 RRDs
::tune
("${rrdfile}.${field}","-i",$field.":".$host->{'vars'}->{$field}->{'min'});
342 print "Error while setting min: " . RRDs
::error
. "\n";
346 if (defined($rrdinfo->{"ds[${field}].max"})) {
347 if ($rrdinfo->{"ds[${field}].max"} ne $host->{'vars'}->{$field}->{'max'}) {
349 RRDs
::tune
("${rrdfile}.${field}","-a",$field.":".$host->{'vars'}->{$field}->{'max'});
352 if ($host->{'vars'}->{$field}->{'max'} ne 'U') {
354 RRDs
::tune
("${rrdfile}.${field}","-a",$field.":".$host->{'vars'}->{$field}->{'max'});
359 print "Error while setting max: " . RRDs
::error
. "\n";
363 if ($rrdinfo->{'rra[0].rows'} != $keep ||
364 !defined($rrdinfo->{'rra[1].rows'}) || $rrdinfo->{'rra[1].rows'} != $keepHours ||
365 $limitsChanged == 1) {
367 print "Resizing ${rrdfile}.${field} from " . $rrdinfo->{'rra[0].rows'} .
368 " to ${keep} samples.\n";
370 (my $start, my $ostep, my $names, my $data) =
371 RRDs
::fetch
("${rrdfile}.${field}",
372 "-s " . (time() - ($rrdinfo->{'rra[0].rows'} * $rrdinfo->{'step'})),
376 print "Error while fetching data: " . RRDs
::error
. "\n";
380 rrdcreate
("${rrdfile}.${field}.new",
383 (${start
}-${ostep
}));
385 print "Preserving data in file ${rrdfile}.${field} since " . localtime($start) . "\n";
388 foreach my $line (@
$data) {
389 my $vline = "${pos}";
391 foreach my $val (@
$line) {
392 $val = 'U' if (!defined($val));
395 RRDs
::update
("${rrdfile}.${field}.new", $vline) or die "Can't insert data\n";
398 print "Error while updating: " . RRDs
::error
. "\n";
403 if ((($pos-$start)/$ostep) == $#$data) {
408 rename("${rrdfile}.${field}", "${rrdfile}.${field}.old") or die "Can't rename old file: $!\n";
409 rename("${rrdfile}.${field}.new", "${rrdfile}.${field}") or die "Can't rename new file: $!\n";
411 $rrdinfo = RRDs
::info
("${rrdfile}.${field}");
413 print "Error while getting info: " . RRDs
::error
. "\n";
417 if ($rrdinfo->{'rra[0].rows'} != $keep) {
427 die "fork failed!" if (!defined($child));
429 exit 0 if ($child != 0);
431 my $first = $UPSGRAPH::regenerateOnStart
;
433 open(HTML
, ">${outdir}/index.html.new");
435 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>';
436 print HTML
'<body bgcolor="#ffffff">';
438 foreach my $host (@
$hosts) {
439 print HTML
"[<a href=\"#".${host
}->{'name'}."\">".${host
}->{'name'}."</a>] ";
443 foreach my $host (@
$hosts) {
445 print HTML
"<a name=\"".${host
}->{'name'}."\"></a>\n";
446 my $vars = $host->{'vars'};
447 my $rrdfile = $host->{'rrdfile'};
448 my $hostname = $host->{'name'};
449 my %multi_values = ();
451 foreach my $var (@
{$host->{'fields'}}) {
452 delete $vars->{$var}->{'value'};
456 if ((!defined($vars->{$var}->{'proto'})) ||
457 ($vars->{$var}->{'proto'} eq '') ||
458 ($vars->{$var}->{'proto'} eq 'snmp')) {
459 $result = fetch_snmp
($host->{'address'}, $host->{'community'}, $vars->{$var}->{'oid'});
460 } elsif ($vars->{$var}->{'proto'} eq 'tcp') {
461 $result = fetch_tcp
($host->{'address'}, $vars->{$var}->{'port'});
462 } elsif ($vars->{$var}->{'proto'} eq 'tcp_multi') {
463 if (defined($multi_values{$vars->{$var}->{'multi_id'}})) {
464 $result = $multi_values{$vars->{$var}->{'multi_id'}}
466 my %values = fetch_tcp_multi
($host->{'address'}, $vars->{$var}->{'port'}, $vars->{$var}->{'multi_delimiter'});
467 @multi_values{keys %values} = values %values;
468 $result = $multi_values{$vars->{$var}->{'multi_id'}};
472 next unless (defined $result);
474 $vars->{$var}->{'value'} = $result;
475 if (defined($vars->{$var}->{'factor'})) {
476 $vars->{$var}->{'value'} *= $vars->{$var}->{'factor'};
480 foreach my $var (@
{$host->{'fields'}}) {
481 if (!(defined($vars->{$var}->{'value'}))) {
482 $vars->{$var}->{'value'} = 'U';
484 rrd_update
("${rrdfile}.${var}", "N:" . $vars->{$var}->{'value'});
487 print "Error while updating: " . $rrd_result . "\n";
490 foreach my $var (@
{$host->{'fields'}}) {
491 my $graphWidth = 365 * 3;
493 if (defined $vars->{$var}->{'graph'}) {
494 $graphConfig = $vars->{$var}->{'graph'};
495 foreach my $subGraph (qw(day week year)) {
496 if (!defined($graphConfig->{$subGraph})) {
497 $graphConfig->{$subGraph} = [ 'avg'];
508 my @graphdef = ('-P', "-t", $hostname." - ".$vars->{$var}->{'name'});
509 push @graphdef, "--lazy" if !$first;
511 push @graphdef, "DEF:${var}-avg=${rrdfile}.${var}:${var}:AVERAGE";
512 push @graphdef, "DEF:${var}-min=${rrdfile}.${var}:${var}:MIN";
513 push @graphdef, "DEF:${var}-max=${rrdfile}.${var}:${var}:MAX";
514 push @graphdef, "LINE1:${var}-avg#FF0000";
515 push @graphdef, "VDEF:cur=${var}-avg,LAST";
516 push @graphdef, 'GPRINT:cur:Current\\: <span foreground="#FF0000">%.2lf</span>\\r';
519 $mtime=(stat("${outdir}/${hostname}.${var}.png.work"))[9];
521 (my $averages, my $width, my $height) =
522 rrd_graph
("${outdir}/${hostname}.${var}.png.work",
523 "-w", "720", @graphdef);
530 print "Error while graphing: " . $rrd_result . "\n";
532 my $newmtime=(stat("${outdir}/${hostname}.${var}.png.work"))[9];
533 if ((!defined($mtime)) || ($newmtime != $mtime)) {
534 copy
("${outdir}/${hostname}.${var}.png.work", "${outdir}/${hostname}.${var}.png.new");
535 rename("${outdir}/${hostname}.${var}.png.new", "${outdir}/${hostname}.${var}.png");
539 print HTML
"<a href=\"${hostname}.${var}.html\"><img src=\"${hostname}.${var}.png\" width=\"${width}\" height=\"${height}\" border=\"0\"></a><br>\n";
541 open (HTML2
, ">${outdir}/${hostname}.${var}.html.new");
542 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>';
543 print HTML2
'<body bgcolor="#ffffff">';
545 push @graphdef, "VDEF:min=${var}-min,MINIMUM";
546 push @graphdef, "GPRINT:min:Minimum\\: %.2lf";
548 push @graphdef, "VDEF:avg=${var}-avg,AVERAGE";
549 push @graphdef, "GPRINT:avg:Average\\: %.2lf";
551 push @graphdef, "VDEF:max=${var}-max,MAXIMUM";
552 push @graphdef, "GPRINT:max:Maximum\\: %.2lf";
554 push @graphdef, "VDEF:cur=${var}-avg,LAST";
555 push @graphdef, "GPRINT:cur:Current\\: %.2lf";
557 my @dayGraphDef = @graphdef;
558 push @dayGraphDef, @
{cfgToGraphDef
($graphConfig->{'day'}, ${var
}, 24)};
560 $mtime=(stat("${outdir}/${hostname}.${var}.long.png.work"))[9];
561 ($averages, $width, $height) =
562 rrd_graph
("${outdir}/${hostname}.${var}.long.png.work",
563 "-w", $graphWidth, @dayGraphDef);
566 print "Error while graphing: " . $rrd_result . "\n";
568 my $newmtime=(stat("${outdir}/${hostname}.${var}.long.png.work"))[9];
569 if ((!defined($mtime)) || ($newmtime != $mtime)) {
570 copy
("${outdir}/${hostname}.${var}.long.png.work", "${outdir}/${hostname}.${var}.long.png.new");
571 rename("${outdir}/${hostname}.${var}.long.png.new", "${outdir}/${hostname}.${var}.long.png");
575 print HTML2
"<img src=\"${hostname}.${var}.long.png\" width=\"${width}\" height=\"${height}\"><br>\n";
577 $mtime=(stat("${outdir}/${hostname}.${var}.week.png.work"))[9];
579 my @weekGraphDef = @graphdef;
580 push @weekGraphDef, @
{cfgToGraphDef
($graphConfig->{'week'}, ${var
}, 24)};
582 ($averages, $width, $height) =
583 rrd_graph
("${outdir}/${hostname}.${var}.week.png.work",
584 "-w", "$graphWidth", "-e", "now", "-s", "00:00-8d", @weekGraphDef);
587 print "Error while graphing: " . $rrd_result . "\n";
589 my $newmtime=(stat("${outdir}/${hostname}.${var}.week.png.work"))[9];
590 if ((!defined($mtime)) || ($newmtime != $mtime)) {
591 copy
("${outdir}/${hostname}.${var}.week.png.work", "${outdir}/${hostname}.${var}.week.png.new");
592 rename("${outdir}/${hostname}.${var}.week.png.new", "${outdir}/${hostname}.${var}.week.png");
596 print HTML2
"<img src=\"${hostname}.${var}.week.png\" width=\"${width}\" height=\"${height}\"><br>\n";
598 $mtime=(stat("${outdir}/${hostname}.${var}.year.png.work"))[9];
600 my @yearGraphDef = @graphdef;
601 push @yearGraphDef, @
{cfgToGraphDef
($graphConfig->{'year'}, ${var
}, 3)};
603 ($averages, $width, $height) =
604 rrd_graph
("${outdir}/${hostname}.${var}.year.png.work",
605 "-w", "$graphWidth", "-e", "00:00", "-s", "end-365d", @yearGraphDef);
608 print "Error while graphing: " . $rrd_result . "\n";
610 my $newmtime=(stat("${outdir}/${hostname}.${var}.year.png.work"))[9];
611 if ((!defined($mtime)) || ($newmtime != $mtime)) {
612 copy
("${outdir}/${hostname}.${var}.year.png.work", "${outdir}/${hostname}.${var}.year.png.new");
613 rename("${outdir}/${hostname}.${var}.year.png.new", "${outdir}/${hostname}.${var}.year.png");
617 print HTML2
"<img src=\"${hostname}.${var}.year.png\" width=\"${width}\" height=\"${height}\"><br>\n";
619 print HTML2
"</body></html>\n";
621 rename("${outdir}/${hostname}.${var}.html.new", "${outdir}/${hostname}.${var}.html");
625 print HTML
"</body></html>\n";
626 print HTML
"<br>Generated on: " . localtime(time());
627 print HTML
' by <a href="http://git.zerfleddert.de/cgi-bin/gitweb.cgi/upsgraph">upsgraph</a>.';
631 rename("${outdir}/index.html.new", "${outdir}/index.html");