]>
git.zerfleddert.de Git - upsgraph/blob - upsgraph.pl
adf33c968e96bfd7e71c5f612cae759f9e40a3d8
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}";
93 push @cmd, "RRA:AVERAGE:0.5:${stepsPerHour}:${keepHours}";
97 print "Error while creating: " . RRDs
::error
. "\n";
104 my $community = shift;
107 (my $session, my $error) = Net
::SNMP
->session(Hostname
=> $address,
108 Community
=> $community);
111 print STDERR
"session error: $error";
115 $session->translate(0);
117 my $result = $session->get_request($oid);
121 return undef if (!defined($result));
130 my $sock = IO
::Socket
::INET
->new(PeerAddr
=> $address,
135 return undef if (!$sock);
137 my $select = IO
::Select
->new($sock);
141 if ($select->can_read(1)) {
142 chomp($value) if (sysread($sock, $value, 4096) > 0);
156 sub fetch_tcp_multi
(@
) {
159 my $delimiter = shift;
162 my $sock = IO
::Socket
::INET
->new(PeerAddr
=> $address,
167 return undef if (!$sock);
169 my $select = IO
::Select
->new($sock);
171 while($select->can_read(1)) {
172 if (sysread($sock, my $buf, 16384) > 0) {
174 foreach my $line (split(/\n/, $buf)) {
175 (my $key, my $value) = split(/${delimiter}/, $line);
177 $values{$key} = $value;
194 my $dataPoints = shift;
196 push @args, "CDEF:prev${mode}1=PREV(${dataSrc})";
197 for (my $i = 1; $i < $dataPoints - 1; ++$i) {
200 push @args, "CDEF:prev${mode}${next}=PREV(prev${mode}${prev})";
204 if ($mode ne 'AVG') {
205 for (my $i = 1; $i < $dataPoints; ++$i) {
206 $dayCons .= "prev${mode}${i},";
207 $consFunc .= ",${mode}";
210 for (my $i = 1; $i < $dataPoints; ++$i) {
211 $dayCons .= "prev${mode}${i},";
213 $consFunc = ",${dataPoints},${mode}";
215 push @args, "CDEF:day${mode}=${dayCons}${dataSrc}${consFunc}";
216 push @args, "CDEF:fillCalDay${mode}0=COUNT,${dataPoints},%,0,EQ,day${mode},UNKN,IF";
217 for (my $i = 1; $i < $dataPoints; ++$i) {
220 push @args, "CDEF:fillCalDay${mode}${next}=PREV(fillCalDay${mode}${prev})";
224 for (my $i = 0; $i < $dataPoints; ++$i) {
225 $fillPoint .= "COUNT,${dataPoints},%,${i},EQ,fillCalDay${mode}${i},";
228 push @args, "CDEF:${mode}Curve=${fillPoint}UNKN${if}";
229 my $forwardShift = (24*60*60) * ($dataPoints - 1) / $dataPoints;
230 push @args, "SHIFT:${mode}Curve:-${forwardShift}";
231 push @args, "LINE1:${mode}Curve#${color}:${label}";
240 foreach my $subGraph (@
$cfg) {
241 if ($subGraph eq 'avg') {
242 push @graphDef, "LINE1:${varname}-avg#FF0000";
243 } elsif ($subGraph eq 'day-min') {
244 push @graphDef, @
{dayGraphFunc
("${varname}-min", 'MIN', '0000ff', 'Day Minimum Temperature', $dpPerDay)};
245 } elsif ($subGraph eq 'day-max') {
246 push @graphDef, @
{dayGraphFunc
("${varname}-max", 'MAX', '00ff00', 'Day Maximum Temperature', $dpPerDay)};
247 } elsif ($subGraph eq 'day-avg') {
248 push @graphDef, @
{dayGraphFunc
("${varname}-houravg", 'AVG', 'ff0000', 'Day Average Temperature', $dpPerDay)};
256 print STDERR
"Running as root, please provide UID as 2th argument!\n";
260 print "Running as root, switching to ".$ARGV[1]."\n";
264 foreach my $host (@
$hosts) {
265 my $rrdfile = $host->{'rrdfile'};
267 foreach my $var (keys(%{$host->{'vars'}})) {
268 $host->{'vars'}->{$var}->{'min'} = 'U' if (!defined($host->{'vars'}->{$var}->{'min'}));
269 $host->{'vars'}->{$var}->{'max'} = 'U' if (!defined($host->{'vars'}->{$var}->{'max'}));
272 if (-e
"${rrdfile}") {
273 print "Reading old ${rrdfile} to preserve data...\n";
275 my $rrdinfo = RRDs
::info
("${rrdfile}");
277 print "Error while getting info: " . RRDs
::error
. "\n";
281 (my $start, my $ostep, my $names, my $data) =
282 RRDs
::fetch
("${rrdfile}",
283 "-s " . (time() - ($rrdinfo->{'rra[0].rows'} * $rrdinfo->{'step'})),
287 print "Error while fetching data: " . RRDs
::error
. "\n";
291 foreach my $field (@
$names) {
292 if (! -e
"${rrdfile}.${field}") {
293 rrdcreate
("${rrdfile}.${field}",
296 (${start
}-${ostep
}));
301 foreach my $line (@
$data) {
302 foreach my $field (@
$names) {
303 my $val = shift (@
$line);
304 $val = 'U' if (!defined($val));
306 RRDs
::update
("${rrdfile}.${field}", "${pos}:${val}");
308 print "Can't insert data: " . RRDs
::error
. "\n";
316 if ((($pos-$start)/$ostep) == $#$data) {
321 rename("${rrdfile}", "${rrdfile}.old") or die "Can't rename old file: $!\n";
324 foreach my $field (@
{$host->{'fields'}}) {
325 if (! -e
"${rrdfile}.${field}") {
326 print "Creating ${rrdfile}.${field}...\n";
327 rrdcreate
("${rrdfile}.${field}",
332 my $rrdinfo = RRDs
::info
("${rrdfile}.${field}");
334 print "Error while getting info: " . RRDs
::error
. "\n";
338 my $limitsChanged = 0;
339 if (defined($rrdinfo->{"ds[${field}].min"})) {
340 if ($rrdinfo->{"ds[${field}].min"} ne $host->{'vars'}->{$field}->{'min'}) {
342 RRDs
::tune
("${rrdfile}.${field}","-i",$field.":".$host->{'vars'}->{$field}->{'min'});
345 if ($host->{'vars'}->{$field}->{'min'} ne 'U') {
347 RRDs
::tune
("${rrdfile}.${field}","-i",$field.":".$host->{'vars'}->{$field}->{'min'});
352 print "Error while setting min: " . RRDs
::error
. "\n";
356 if (defined($rrdinfo->{"ds[${field}].max"})) {
357 if ($rrdinfo->{"ds[${field}].max"} ne $host->{'vars'}->{$field}->{'max'}) {
359 RRDs
::tune
("${rrdfile}.${field}","-a",$field.":".$host->{'vars'}->{$field}->{'max'});
362 if ($host->{'vars'}->{$field}->{'max'} ne 'U') {
364 RRDs
::tune
("${rrdfile}.${field}","-a",$field.":".$host->{'vars'}->{$field}->{'max'});
369 print "Error while setting max: " . RRDs
::error
. "\n";
373 if ($rrdinfo->{'rra[0].rows'} != $keep ||
374 !defined($rrdinfo->{'rra[3].rows'}) || $rrdinfo->{'rra[3].rows'} != $keepHours ||
375 $limitsChanged == 1) {
377 print "Resizing ${rrdfile}.${field} from " . $rrdinfo->{'rra[0].rows'} .
378 " to ${keep} samples.\n";
380 (my $start, my $ostep, my $names, my $data) =
381 RRDs
::fetch
("${rrdfile}.${field}",
382 "-s " . (time() - ($rrdinfo->{'rra[0].rows'} * $rrdinfo->{'step'})),
386 print "Error while fetching data: " . RRDs
::error
. "\n";
390 rrdcreate
("${rrdfile}.${field}.new",
393 (${start
}-${ostep
}));
395 print "Preserving data in file ${rrdfile}.${field} since " . localtime($start) . "\n";
398 foreach my $line (@
$data) {
399 my $vline = "${pos}";
401 foreach my $val (@
$line) {
402 $val = 'U' if (!defined($val));
405 RRDs
::update
("${rrdfile}.${field}.new", $vline) or die "Can't insert data\n";
408 print "Error while updating: " . RRDs
::error
. "\n";
413 if ((($pos-$start)/$ostep) == $#$data) {
418 rename("${rrdfile}.${field}", "${rrdfile}.${field}.old") or die "Can't rename old file: $!\n";
419 rename("${rrdfile}.${field}.new", "${rrdfile}.${field}") or die "Can't rename new file: $!\n";
421 $rrdinfo = RRDs
::info
("${rrdfile}.${field}");
423 print "Error while getting info: " . RRDs
::error
. "\n";
427 if ($rrdinfo->{'rra[0].rows'} != $keep) {
437 die "fork failed!" if (!defined($child));
439 exit 0 if ($child != 0);
441 my $first = $UPSGRAPH::regenerateOnStart
;
443 open(HTML
, ">${outdir}/index.html.new");
445 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>';
446 print HTML
'<body bgcolor="#ffffff">';
448 foreach my $host (@
$hosts) {
449 print HTML
"[<a href=\"#".${host
}->{'name'}."\">".${host
}->{'name'}."</a>] ";
453 foreach my $host (@
$hosts) {
455 print HTML
"<a name=\"".${host
}->{'name'}."\"></a>\n";
456 my $vars = $host->{'vars'};
457 my $rrdfile = $host->{'rrdfile'};
458 my $hostname = $host->{'name'};
459 my %multi_values = ();
461 foreach my $var (@
{$host->{'fields'}}) {
462 delete $vars->{$var}->{'value'};
466 if ((!defined($vars->{$var}->{'proto'})) ||
467 ($vars->{$var}->{'proto'} eq '') ||
468 ($vars->{$var}->{'proto'} eq 'snmp')) {
469 $result = fetch_snmp
($host->{'address'}, $host->{'community'}, $vars->{$var}->{'oid'});
470 } elsif ($vars->{$var}->{'proto'} eq 'tcp') {
471 $result = fetch_tcp
($host->{'address'}, $vars->{$var}->{'port'});
472 } elsif ($vars->{$var}->{'proto'} eq 'tcp_multi') {
473 if (defined($multi_values{$vars->{$var}->{'multi_id'}})) {
474 $result = $multi_values{$vars->{$var}->{'multi_id'}}
476 my %values = fetch_tcp_multi
($host->{'address'}, $vars->{$var}->{'port'}, $vars->{$var}->{'multi_delimiter'});
477 @multi_values{keys %values} = values %values;
478 $result = $multi_values{$vars->{$var}->{'multi_id'}};
482 next unless (defined $result);
484 $vars->{$var}->{'value'} = $result;
485 if (defined($vars->{$var}->{'factor'})) {
486 $vars->{$var}->{'value'} *= $vars->{$var}->{'factor'};
490 foreach my $var (@
{$host->{'fields'}}) {
491 if (!(defined($vars->{$var}->{'value'}))) {
492 $vars->{$var}->{'value'} = 'U';
494 rrd_update
("${rrdfile}.${var}", "N:" . $vars->{$var}->{'value'});
497 print "Error while updating: " . $rrd_result . "\n";
500 foreach my $var (@
{$host->{'fields'}}) {
501 my $graphWidth = 365 * 3;
503 if (defined $vars->{$var}->{'graph'}) {
504 $graphConfig = $vars->{$var}->{'graph'};
505 foreach my $subGraph (qw(day week year)) {
506 if (!defined($graphConfig->{$subGraph})) {
507 $graphConfig->{$subGraph} = [ 'avg'];
518 my @graphdef = ('-P', "-A", "-t", $hostname." - ".$vars->{$var}->{'name'});
519 push @graphdef, "--lazy" if !$first;
521 push @graphdef, "DEF:${var}-avg=${rrdfile}.${var}:${var}:AVERAGE";
522 push @graphdef, "DEF:${var}-min=${rrdfile}.${var}:${var}:MIN";
523 push @graphdef, "DEF:${var}-max=${rrdfile}.${var}:${var}:MAX";
524 push @graphdef, "DEF:${var}-houravg=${rrdfile}.${var}:${var}:AVERAGE:step=3600";
525 push @graphdef, "LINE1:${var}-avg#FF0000";
526 push @graphdef, "VDEF:cur=${var}-avg,LAST";
527 push @graphdef, 'GPRINT:cur:Current\\: <span foreground="#FF0000">%.2lf</span>\\r';
530 $mtime=(stat("${outdir}/${hostname}.${var}.png.work"))[9];
532 (my $averages, my $width, my $height) =
533 rrd_graph
("${outdir}/${hostname}.${var}.png.work",
534 "-w", "720", @graphdef);
541 print "Error while graphing: " . $rrd_result . "\n";
543 my $newmtime=(stat("${outdir}/${hostname}.${var}.png.work"))[9];
544 if ((!defined($mtime)) || ($newmtime != $mtime)) {
545 copy
("${outdir}/${hostname}.${var}.png.work", "${outdir}/${hostname}.${var}.png.new");
546 rename("${outdir}/${hostname}.${var}.png.new", "${outdir}/${hostname}.${var}.png");
550 print HTML
"<a href=\"${hostname}.${var}.html\"><img src=\"${hostname}.${var}.png\" width=\"${width}\" height=\"${height}\" border=\"0\"></a><br>\n";
552 open (HTML2
, ">${outdir}/${hostname}.${var}.html.new");
553 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>';
554 print HTML2
'<body bgcolor="#ffffff">';
556 push @graphdef, "VDEF:min=${var}-min,MINIMUM";
557 push @graphdef, "GPRINT:min:Minimum\\: %.2lf";
559 push @graphdef, "VDEF:avg=${var}-avg,AVERAGE";
560 push @graphdef, "GPRINT:avg:Average\\: %.2lf";
562 push @graphdef, "VDEF:max=${var}-max,MAXIMUM";
563 push @graphdef, "GPRINT:max:Maximum\\: %.2lf";
565 push @graphdef, "VDEF:cur=${var}-avg,LAST";
566 push @graphdef, "GPRINT:cur:Current\\: %.2lf";
568 my @dayGraphDef = @graphdef;
569 push @dayGraphDef, @
{cfgToGraphDef
($graphConfig->{'day'}, ${var
}, 24)};
571 $mtime=(stat("${outdir}/${hostname}.${var}.long.png.work"))[9];
572 ($averages, $width, $height) =
573 rrd_graph
("${outdir}/${hostname}.${var}.long.png.work",
574 "-w", $graphWidth, @dayGraphDef);
577 print "Error while graphing: " . $rrd_result . "\n";
579 my $newmtime=(stat("${outdir}/${hostname}.${var}.long.png.work"))[9];
580 if ((!defined($mtime)) || ($newmtime != $mtime)) {
581 copy
("${outdir}/${hostname}.${var}.long.png.work", "${outdir}/${hostname}.${var}.long.png.new");
582 rename("${outdir}/${hostname}.${var}.long.png.new", "${outdir}/${hostname}.${var}.long.png");
586 print HTML2
"<img src=\"${hostname}.${var}.long.png\" width=\"${width}\" height=\"${height}\"><br>\n";
588 $mtime=(stat("${outdir}/${hostname}.${var}.week.png.work"))[9];
590 my @weekGraphDef = @graphdef;
591 push @weekGraphDef, @
{cfgToGraphDef
($graphConfig->{'week'}, ${var
}, 24)};
593 ($averages, $width, $height) =
594 rrd_graph
("${outdir}/${hostname}.${var}.week.png.work",
595 "-w", "$graphWidth", "-e", "now", "-s", "00:00-8d", @weekGraphDef);
598 print "Error while graphing: " . $rrd_result . "\n";
600 my $newmtime=(stat("${outdir}/${hostname}.${var}.week.png.work"))[9];
601 if ((!defined($mtime)) || ($newmtime != $mtime)) {
602 copy
("${outdir}/${hostname}.${var}.week.png.work", "${outdir}/${hostname}.${var}.week.png.new");
603 rename("${outdir}/${hostname}.${var}.week.png.new", "${outdir}/${hostname}.${var}.week.png");
607 print HTML2
"<img src=\"${hostname}.${var}.week.png\" width=\"${width}\" height=\"${height}\"><br>\n";
609 $mtime=(stat("${outdir}/${hostname}.${var}.year.png.work"))[9];
611 my @yearGraphDef = @graphdef;
612 push @yearGraphDef, @
{cfgToGraphDef
($graphConfig->{'year'}, ${var
}, 3)};
614 ($averages, $width, $height) =
615 rrd_graph
("${outdir}/${hostname}.${var}.year.png.work",
616 "-w", "$graphWidth", "-e", "00:00", "-s", "end-365d", @yearGraphDef);
619 print "Error while graphing: " . $rrd_result . "\n";
621 my $newmtime=(stat("${outdir}/${hostname}.${var}.year.png.work"))[9];
622 if ((!defined($mtime)) || ($newmtime != $mtime)) {
623 copy
("${outdir}/${hostname}.${var}.year.png.work", "${outdir}/${hostname}.${var}.year.png.new");
624 rename("${outdir}/${hostname}.${var}.year.png.new", "${outdir}/${hostname}.${var}.year.png");
628 print HTML2
"<img src=\"${hostname}.${var}.year.png\" width=\"${width}\" height=\"${height}\"><br>\n";
630 print HTML2
"</body></html>\n";
632 rename("${outdir}/${hostname}.${var}.html.new", "${outdir}/${hostname}.${var}.html");
636 print HTML
"</body></html>\n";
637 print HTML
"<br>Generated on: " . localtime(time());
638 print HTML
' by <a href="http://git.zerfleddert.de/cgi-bin/gitweb.cgi/upsgraph">upsgraph</a>.';
642 rename("${outdir}/index.html.new", "${outdir}/index.html");