]>
git.zerfleddert.de Git - upsgraph/blob - upsgraph.pl
55c6a62a85998e88fcd82e1239113e2d57e645c2
3 #Due to memory leak in Debian squeeze (Bug #545519)
7 if ((@ARGV != 1) && (@ARGV != 2)) {
8 print STDERR
"Syntax: ${0} configfile [uid]\n";
21 $UPSGRAPH::outdir
= "";
22 $UPSGRAPH::daysCovered
= 370;
24 $UPSGRAPH::stepsPerHour
= (60 * 60) / $UPSGRAPH::step
;
25 $UPSGRAPH::keep
= ($UPSGRAPH::daysCovered
*24*60*60)/$UPSGRAPH::step
;
26 $UPSGRAPH::keepHours
= ($UPSGRAPH::daysCovered
*24*60*60)/$UPSGRAPH::stepsPerHour/$UPSGRAPH::step
;
27 $UPSGRAPH::hosts
= ();
28 $UPSGRAPH::regenerateOnStart
= 1; #when set, regenerate graphs on script startup
30 do $ARGV[0] or die "can't read config: $!";
32 my $outdir = $UPSGRAPH::outdir
;
33 my $step = $UPSGRAPH::step
;
34 my $stepsPerHour = $UPSGRAPH::stepsPerHour
;
35 my $keep = $UPSGRAPH::keep
;
36 my $keepHours = $UPSGRAPH::keepHours
;
37 my $hosts = $UPSGRAPH::hosts
;
44 $rrd_result = RRDs
::error
;
46 $rrd_result = system("rrdtool", "update", @args);
55 @rrd_out = RRDs
::graph
(@args);
56 $rrd_result = RRDs
::error
;
60 open(RRDFD
, '-|', 'rrdtool', 'graph', @args);
67 if ($rrd_result == 0) {
69 push @rrd_out, split(/x/, $rrd_stdout);
82 my @cmd = ("${newrrd}", "--step=${step}");
84 if (defined($start)) {
85 push @cmd, "--start=${start}";
88 push @cmd, "DS:${field}:GAUGE:600:" .
89 $vars->{$field}->{'min'} . ":" .
90 $vars->{$field}->{'max'};
92 push @cmd, "RRA:AVERAGE:0.5:1:${keep}";
93 push @cmd, "RRA:MIN:0.4:${stepsPerHour}:${keepHours}";
94 push @cmd, "RRA:MAX:0.4:${stepsPerHour}:${keepHours}";
95 push @cmd, "RRA:AVERAGE:0.5:${stepsPerHour}:${keepHours}";
99 print "Error while creating: " . RRDs
::error
. "\n";
106 my $community = shift;
109 (my $session, my $error) = Net
::SNMP
->session(Hostname
=> $address,
110 Community
=> $community);
113 print STDERR
"session error: $error";
117 $session->translate(0);
119 my $result = $session->get_request($oid);
123 return undef if (!defined($result));
132 my $sock = IO
::Socket
::INET
->new(PeerAddr
=> $address,
137 return undef if (!$sock);
139 my $select = IO
::Select
->new($sock);
143 if ($select->can_read(1)) {
144 chomp($value) if (sysread($sock, $value, 4096) > 0);
158 sub fetch_tcp_multi
(@
) {
161 my $delimiter = shift;
164 my $sock = IO
::Socket
::INET
->new(PeerAddr
=> $address,
169 return undef if (!$sock);
171 my $select = IO
::Select
->new($sock);
173 while($select->can_read(1)) {
174 if (sysread($sock, my $buf, 16384) > 0) {
176 foreach my $line (split(/\n/, $buf)) {
177 (my $key, my $value) = split(/${delimiter}/, $line);
179 $values{$key} = $value;
191 sub fetch_iotcore
(@
) {
194 my $ua = LWP
::UserAgent
->new;
197 my $resp = $ua->get($url);
198 return undef if (!$resp->is_success);
200 my $pdin = decode_json
($resp->decoded_content);
201 return undef if (!defined($pdin));
203 my $value = hex($pdin->{'data'}->{'value'});
213 my $dataPoints = shift;
215 push @args, "CDEF:prev${mode}1=PREV(${dataSrc})";
216 for (my $i = 1; $i < $dataPoints - 1; ++$i) {
219 push @args, "CDEF:prev${mode}${next}=PREV(prev${mode}${prev})";
223 if ($mode ne 'AVG') {
224 for (my $i = 1; $i < $dataPoints; ++$i) {
225 $dayCons .= "prev${mode}${i},";
226 $consFunc .= ",${mode}";
229 for (my $i = 1; $i < $dataPoints; ++$i) {
230 $dayCons .= "prev${mode}${i},";
232 $consFunc = ",${dataPoints},${mode}";
234 push @args, "CDEF:day${mode}=${dayCons}${dataSrc}${consFunc}";
235 push @args, "CDEF:fillCalDay${mode}0=COUNT,${dataPoints},%,0,EQ,day${mode},UNKN,IF";
236 for (my $i = 1; $i < $dataPoints; ++$i) {
239 push @args, "CDEF:fillCalDay${mode}${next}=PREV(fillCalDay${mode}${prev})";
243 for (my $i = 0; $i < $dataPoints; ++$i) {
244 $fillPoint .= "COUNT,${dataPoints},%,${i},EQ,fillCalDay${mode}${i},";
247 push @args, "CDEF:${mode}Curve=${fillPoint}UNKN${if}";
248 my $forwardShift = (24*60*60) * ($dataPoints - 1) / $dataPoints;
249 push @args, "SHIFT:${mode}Curve:-${forwardShift}";
250 push @args, "LINE1:${mode}Curve#${color}:${label}";
259 foreach my $subGraph (@
$cfg) {
260 if ($subGraph eq 'avg') {
261 push @graphDef, "LINE1:${varname}-avg#FF0000";
262 } elsif ($subGraph eq 'day-min') {
263 push @graphDef, @
{dayGraphFunc
("${varname}-min", 'MIN', '0000ff', 'Day Minimum Temperature', $dpPerDay)};
264 } elsif ($subGraph eq 'day-max') {
265 push @graphDef, @
{dayGraphFunc
("${varname}-max", 'MAX', '00ff00', 'Day Maximum Temperature', $dpPerDay)};
266 } elsif ($subGraph eq 'day-avg') {
267 push @graphDef, @
{dayGraphFunc
("${varname}-houravg", 'AVG', 'ff0000', 'Day Average Temperature', $dpPerDay)};
275 print STDERR
"Running as root, please provide UID as 2th argument!\n";
279 print "Running as root, switching to ".$ARGV[1]."\n";
283 foreach my $host (@
$hosts) {
284 my $rrdfile = $host->{'rrdfile'};
286 foreach my $var (keys(%{$host->{'vars'}})) {
287 $host->{'vars'}->{$var}->{'min'} = 'U' if (!defined($host->{'vars'}->{$var}->{'min'}));
288 $host->{'vars'}->{$var}->{'max'} = 'U' if (!defined($host->{'vars'}->{$var}->{'max'}));
291 if (-e
"${rrdfile}") {
292 print "Reading old ${rrdfile} to preserve data...\n";
294 my $rrdinfo = RRDs
::info
("${rrdfile}");
296 print "Error while getting info: " . RRDs
::error
. "\n";
300 (my $start, my $ostep, my $names, my $data) =
301 RRDs
::fetch
("${rrdfile}",
302 "-s " . (time() - ($rrdinfo->{'rra[0].rows'} * $rrdinfo->{'step'})),
306 print "Error while fetching data: " . RRDs
::error
. "\n";
310 foreach my $field (@
$names) {
311 if (! -e
"${rrdfile}.${field}") {
312 rrdcreate
("${rrdfile}.${field}",
315 (${start
}-${ostep
}));
320 foreach my $line (@
$data) {
321 foreach my $field (@
$names) {
322 my $val = shift (@
$line);
323 $val = 'U' if (!defined($val));
325 RRDs
::update
("${rrdfile}.${field}", "${pos}:${val}");
327 print "Can't insert data: " . RRDs
::error
. "\n";
335 if ((($pos-$start)/$ostep) == $#$data) {
340 rename("${rrdfile}", "${rrdfile}.old") or die "Can't rename old file: $!\n";
343 foreach my $field (@
{$host->{'fields'}}) {
344 if (! -e
"${rrdfile}.${field}") {
345 print "Creating ${rrdfile}.${field}...\n";
346 rrdcreate
("${rrdfile}.${field}",
351 my $rrdinfo = RRDs
::info
("${rrdfile}.${field}");
353 print "Error while getting info: " . RRDs
::error
. "\n";
357 my $limitsChanged = 0;
358 if (defined($rrdinfo->{"ds[${field}].min"})) {
359 if ($rrdinfo->{"ds[${field}].min"} ne $host->{'vars'}->{$field}->{'min'}) {
361 RRDs
::tune
("${rrdfile}.${field}","-i",$field.":".$host->{'vars'}->{$field}->{'min'});
364 if ($host->{'vars'}->{$field}->{'min'} ne 'U') {
366 RRDs
::tune
("${rrdfile}.${field}","-i",$field.":".$host->{'vars'}->{$field}->{'min'});
371 print "Error while setting min: " . RRDs
::error
. "\n";
375 if (defined($rrdinfo->{"ds[${field}].max"})) {
376 if ($rrdinfo->{"ds[${field}].max"} ne $host->{'vars'}->{$field}->{'max'}) {
378 RRDs
::tune
("${rrdfile}.${field}","-a",$field.":".$host->{'vars'}->{$field}->{'max'});
381 if ($host->{'vars'}->{$field}->{'max'} ne 'U') {
383 RRDs
::tune
("${rrdfile}.${field}","-a",$field.":".$host->{'vars'}->{$field}->{'max'});
388 print "Error while setting max: " . RRDs
::error
. "\n";
392 if ($rrdinfo->{'rra[0].rows'} != $keep ||
393 !defined($rrdinfo->{'rra[3].rows'}) || $rrdinfo->{'rra[3].rows'} != $keepHours ||
394 $limitsChanged == 1) {
396 print "Resizing ${rrdfile}.${field} from " . $rrdinfo->{'rra[0].rows'} .
397 " to ${keep} samples.\n";
399 (my $start, my $ostep, my $names, my $data) =
400 RRDs
::fetch
("${rrdfile}.${field}",
401 "-s " . (time() - ($rrdinfo->{'rra[0].rows'} * $rrdinfo->{'step'})),
405 print "Error while fetching data: " . RRDs
::error
. "\n";
409 rrdcreate
("${rrdfile}.${field}.new",
412 (${start
}-${ostep
}));
414 print "Preserving data in file ${rrdfile}.${field} since " . localtime($start) . "\n";
417 foreach my $line (@
$data) {
418 my $vline = "${pos}";
420 foreach my $val (@
$line) {
421 $val = 'U' if (!defined($val));
424 RRDs
::update
("${rrdfile}.${field}.new", $vline) or die "Can't insert data\n";
427 print "Error while updating: " . RRDs
::error
. "\n";
432 if ((($pos-$start)/$ostep) == $#$data) {
437 rename("${rrdfile}.${field}", "${rrdfile}.${field}.old") or die "Can't rename old file: $!\n";
438 rename("${rrdfile}.${field}.new", "${rrdfile}.${field}") or die "Can't rename new file: $!\n";
440 $rrdinfo = RRDs
::info
("${rrdfile}.${field}");
442 print "Error while getting info: " . RRDs
::error
. "\n";
446 if ($rrdinfo->{'rra[0].rows'} != $keep) {
456 die "fork failed!" if (!defined($child));
458 exit 0 if ($child != 0);
460 my $first = $UPSGRAPH::regenerateOnStart
;
462 open(HTML
, ">${outdir}/index.html.new");
464 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>';
465 print HTML
'<body bgcolor="#ffffff">';
467 foreach my $host (@
$hosts) {
468 print HTML
"[<a href=\"#".${host
}->{'name'}."\">".${host
}->{'name'}."</a>] ";
472 foreach my $host (@
$hosts) {
474 print HTML
"<a name=\"".${host
}->{'name'}."\"></a>\n";
475 my $vars = $host->{'vars'};
476 my $rrdfile = $host->{'rrdfile'};
477 my $hostname = $host->{'name'};
478 my %multi_values = ();
480 foreach my $var (@
{$host->{'fields'}}) {
481 delete $vars->{$var}->{'value'};
485 if ((!defined($vars->{$var}->{'proto'})) ||
486 ($vars->{$var}->{'proto'} eq '') ||
487 ($vars->{$var}->{'proto'} eq 'snmp')) {
488 $result = fetch_snmp
($host->{'address'}, $host->{'community'}, $vars->{$var}->{'oid'});
489 } elsif ($vars->{$var}->{'proto'} eq 'tcp') {
490 $result = fetch_tcp
($host->{'address'}, $vars->{$var}->{'port'});
491 } elsif ($vars->{$var}->{'proto'} eq 'tcp_multi') {
492 if (defined($multi_values{$vars->{$var}->{'multi_id'}})) {
493 $result = $multi_values{$vars->{$var}->{'multi_id'}}
495 my %values = fetch_tcp_multi
($host->{'address'}, $vars->{$var}->{'port'}, $vars->{$var}->{'multi_delimiter'});
496 @multi_values{keys %values} = values %values;
497 $result = $multi_values{$vars->{$var}->{'multi_id'}};
499 } elsif ($vars->{$var}->{'proto'} eq 'iotcore') {
500 $result = fetch_iotcore
($vars->{$var}->{'url'});
503 next unless (defined $result);
505 $vars->{$var}->{'value'} = $result;
506 if (defined($vars->{$var}->{'factor'})) {
507 $vars->{$var}->{'value'} *= $vars->{$var}->{'factor'};
511 foreach my $var (@
{$host->{'fields'}}) {
512 if (!(defined($vars->{$var}->{'value'}))) {
513 $vars->{$var}->{'value'} = 'U';
515 rrd_update
("${rrdfile}.${var}", "N:" . $vars->{$var}->{'value'});
518 print "Error while updating: " . $rrd_result . "\n";
521 foreach my $var (@
{$host->{'fields'}}) {
522 my $graphWidth = 365 * 3;
524 if (defined $vars->{$var}->{'graph'}) {
525 $graphConfig = $vars->{$var}->{'graph'};
526 foreach my $subGraph (qw(day week year)) {
527 if (!defined($graphConfig->{$subGraph})) {
528 $graphConfig->{$subGraph} = [ 'avg'];
539 my @graphdef = ('-P', "-A", "-t", $hostname." - ".$vars->{$var}->{'name'});
540 push @graphdef, "--lazy" if !$first;
542 push @graphdef, "DEF:${var}-avg=${rrdfile}.${var}:${var}:AVERAGE";
543 push @graphdef, "DEF:${var}-min=${rrdfile}.${var}:${var}:MIN";
544 push @graphdef, "DEF:${var}-max=${rrdfile}.${var}:${var}:MAX";
545 push @graphdef, "DEF:${var}-houravg=${rrdfile}.${var}:${var}:AVERAGE:step=3600";
546 push @graphdef, "LINE1:${var}-avg#FF0000";
547 push @graphdef, "VDEF:cur=${var}-avg,LAST";
548 push @graphdef, 'GPRINT:cur:Current\\: <span foreground="#FF0000">%.2lf</span>\\r';
551 $mtime=(stat("${outdir}/${hostname}.${var}.png.work"))[9];
553 (my $averages, my $width, my $height) =
554 rrd_graph
("${outdir}/${hostname}.${var}.png.work",
555 "-w", "720", @graphdef);
562 print "Error while graphing: " . $rrd_result . "\n";
564 my $newmtime=(stat("${outdir}/${hostname}.${var}.png.work"))[9];
565 if ((!defined($mtime)) || ($newmtime != $mtime)) {
566 copy
("${outdir}/${hostname}.${var}.png.work", "${outdir}/${hostname}.${var}.png.new");
567 rename("${outdir}/${hostname}.${var}.png.new", "${outdir}/${hostname}.${var}.png");
571 print HTML
"<a href=\"${hostname}.${var}.html\"><img src=\"${hostname}.${var}.png\" width=\"${width}\" height=\"${height}\" border=\"0\"></a><br>\n";
573 open (HTML2
, ">${outdir}/${hostname}.${var}.html.new");
574 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>';
575 print HTML2
'<body bgcolor="#ffffff">';
577 push @graphdef, "VDEF:min=${var}-min,MINIMUM";
578 push @graphdef, "GPRINT:min:Minimum\\: %.2lf";
580 push @graphdef, "VDEF:avg=${var}-avg,AVERAGE";
581 push @graphdef, "GPRINT:avg:Average\\: %.2lf";
583 push @graphdef, "VDEF:max=${var}-max,MAXIMUM";
584 push @graphdef, "GPRINT:max:Maximum\\: %.2lf";
586 push @graphdef, "VDEF:cur=${var}-avg,LAST";
587 push @graphdef, "GPRINT:cur:Current\\: %.2lf";
589 my @dayGraphDef = @graphdef;
590 push @dayGraphDef, @
{cfgToGraphDef
($graphConfig->{'day'}, ${var
}, 24)};
592 $mtime=(stat("${outdir}/${hostname}.${var}.long.png.work"))[9];
593 ($averages, $width, $height) =
594 rrd_graph
("${outdir}/${hostname}.${var}.long.png.work",
595 "-w", $graphWidth, @dayGraphDef);
598 print "Error while graphing: " . $rrd_result . "\n";
600 my $newmtime=(stat("${outdir}/${hostname}.${var}.long.png.work"))[9];
601 if ((!defined($mtime)) || ($newmtime != $mtime)) {
602 copy
("${outdir}/${hostname}.${var}.long.png.work", "${outdir}/${hostname}.${var}.long.png.new");
603 rename("${outdir}/${hostname}.${var}.long.png.new", "${outdir}/${hostname}.${var}.long.png");
607 print HTML2
"<img src=\"${hostname}.${var}.long.png\" width=\"${width}\" height=\"${height}\"><br>\n";
609 $mtime=(stat("${outdir}/${hostname}.${var}.week.png.work"))[9];
611 my @weekGraphDef = @graphdef;
612 push @weekGraphDef, @
{cfgToGraphDef
($graphConfig->{'week'}, ${var
}, 24)};
614 ($averages, $width, $height) =
615 rrd_graph
("${outdir}/${hostname}.${var}.week.png.work",
616 "-w", "$graphWidth", "-e", "now", "-s", "00:00-8d", @weekGraphDef);
619 print "Error while graphing: " . $rrd_result . "\n";
621 my $newmtime=(stat("${outdir}/${hostname}.${var}.week.png.work"))[9];
622 if ((!defined($mtime)) || ($newmtime != $mtime)) {
623 copy
("${outdir}/${hostname}.${var}.week.png.work", "${outdir}/${hostname}.${var}.week.png.new");
624 rename("${outdir}/${hostname}.${var}.week.png.new", "${outdir}/${hostname}.${var}.week.png");
628 print HTML2
"<img src=\"${hostname}.${var}.week.png\" width=\"${width}\" height=\"${height}\"><br>\n";
630 $mtime=(stat("${outdir}/${hostname}.${var}.year.png.work"))[9];
632 my @yearGraphDef = @graphdef;
633 push @yearGraphDef, @
{cfgToGraphDef
($graphConfig->{'year'}, ${var
}, 3)};
635 ($averages, $width, $height) =
636 rrd_graph
("${outdir}/${hostname}.${var}.year.png.work",
637 "-w", "$graphWidth", "-e", "00:00", "-s", "end-365d", @yearGraphDef);
640 print "Error while graphing: " . $rrd_result . "\n";
642 my $newmtime=(stat("${outdir}/${hostname}.${var}.year.png.work"))[9];
643 if ((!defined($mtime)) || ($newmtime != $mtime)) {
644 copy
("${outdir}/${hostname}.${var}.year.png.work", "${outdir}/${hostname}.${var}.year.png.new");
645 rename("${outdir}/${hostname}.${var}.year.png.new", "${outdir}/${hostname}.${var}.year.png");
649 print HTML2
"<img src=\"${hostname}.${var}.year.png\" width=\"${width}\" height=\"${height}\"><br>\n";
651 print HTML2
"</body></html>\n";
653 rename("${outdir}/${hostname}.${var}.html.new", "${outdir}/${hostname}.${var}.html");
657 print HTML
"</body></html>\n";
658 print HTML
"<br>Generated on: " . localtime(time());
659 print HTML
' by <a href="http://git.zerfleddert.de/cgi-bin/gitweb.cgi/upsgraph">upsgraph</a>.';
663 rename("${outdir}/index.html.new", "${outdir}/index.html");