]>
git.zerfleddert.de Git - upsgraph/blob - upsgraph.pl
4f660902158c975715a696fec3bf325ead762615
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;
192 my $dataPoints = shift;
194 push @args, "CDEF:prev${mode}1=PREV(${dataSrc})";
195 for (my $i = 1; $i < $dataPoints - 1; ++$i) {
198 push @args, "CDEF:prev${mode}${next}=PREV(prev${mode}${prev})";
202 for (my $i = 1; $i < $dataPoints; ++$i) {
203 $dayCons .= "prev${mode}${i},";
204 $consFunc .= ",${mode}";
206 push @args, "CDEF:day${mode}=${dayCons}${dataSrc}${consFunc}";
207 push @args, "CDEF:fillCalDay${mode}0=COUNT,${dataPoints},%,0,EQ,day${mode},UNKN,IF";
208 for (my $i = 1; $i < $dataPoints; ++$i) {
211 push @args, "CDEF:fillCalDay${mode}${next}=PREV(fillCalDay${mode}${prev})";
215 for (my $i = 0; $i < $dataPoints; ++$i) {
216 $fillPoint .= "COUNT,${dataPoints},%,${i},EQ,fillCalDay${mode}${i},";
219 push @args, "CDEF:${mode}Curve=${fillPoint}UNKN${if}";
220 my $forwardShift = (24*60*60) * ($dataPoints - 1) / $dataPoints;
221 push @args, "SHIFT:${mode}Curve:-${forwardShift}";
222 push @args, "LINE1:${mode}Curve#${color}:${label}";
231 foreach my $subGraph (@
$cfg) {
232 if ($subGraph eq 'avg') {
233 push @graphDef, "LINE1:${varname}-avg#FF0000";
234 } elsif ($subGraph eq 'day-min') {
235 push @graphDef, @
{dayGraphFunc
("${varname}-min", 'MIN', '0000ff', 'Day Minimum Temperature', $dpPerDay)};
236 } elsif ($subGraph eq 'day-max') {
237 push @graphDef, @
{dayGraphFunc
("${varname}-max", 'MAX', '00ff00', 'Day Maximum Temperature', $dpPerDay)};
245 print STDERR
"Running as root, please provide UID as 2th argument!\n";
249 print "Running as root, switching to ".$ARGV[1]."\n";
253 foreach my $host (@
$hosts) {
254 my $rrdfile = $host->{'rrdfile'};
256 foreach my $var (keys(%{$host->{'vars'}})) {
257 $host->{'vars'}->{$var}->{'min'} = 'U' if (!defined($host->{'vars'}->{$var}->{'min'}));
258 $host->{'vars'}->{$var}->{'max'} = 'U' if (!defined($host->{'vars'}->{$var}->{'max'}));
261 if (-e
"${rrdfile}") {
262 print "Reading old ${rrdfile} to preserve data...\n";
264 my $rrdinfo = RRDs
::info
("${rrdfile}");
266 print "Error while getting info: " . RRDs
::error
. "\n";
270 (my $start, my $ostep, my $names, my $data) =
271 RRDs
::fetch
("${rrdfile}",
272 "-s " . (time() - ($rrdinfo->{'rra[0].rows'} * $rrdinfo->{'step'})),
276 print "Error while fetching data: " . RRDs
::error
. "\n";
280 foreach my $field (@
$names) {
281 if (! -e
"${rrdfile}.${field}") {
282 rrdcreate
("${rrdfile}.${field}",
285 (${start
}-${ostep
}));
290 foreach my $line (@
$data) {
291 foreach my $field (@
$names) {
292 my $val = shift (@
$line);
293 $val = 'U' if (!defined($val));
295 RRDs
::update
("${rrdfile}.${field}", "${pos}:${val}");
297 print "Can't insert data: " . RRDs
::error
. "\n";
305 if ((($pos-$start)/$ostep) == $#$data) {
310 rename("${rrdfile}", "${rrdfile}.old") or die "Can't rename old file: $!\n";
313 foreach my $field (@
{$host->{'fields'}}) {
314 if (! -e
"${rrdfile}.${field}") {
315 print "Creating ${rrdfile}.${field}...\n";
316 rrdcreate
("${rrdfile}.${field}",
321 my $rrdinfo = RRDs
::info
("${rrdfile}.${field}");
323 print "Error while getting info: " . RRDs
::error
. "\n";
327 if (defined($rrdinfo->{"ds[${field}].min"})) {
328 if ($rrdinfo->{"ds[${field}].min"} ne $host->{'vars'}->{$field}->{'min'}) {
329 RRDs
::tune
("${rrdfile}.${field}","-i",$field.":".$host->{'vars'}->{$field}->{'min'});
332 if ($host->{'vars'}->{$field}->{'min'} ne 'U') {
333 RRDs
::tune
("${rrdfile}.${field}","-i",$field.":".$host->{'vars'}->{$field}->{'min'});
338 print "Error while setting min: " . RRDs
::error
. "\n";
342 if (defined($rrdinfo->{"ds[${field}].max"})) {
343 if ($rrdinfo->{"ds[${field}].max"} ne $host->{'vars'}->{$field}->{'max'}) {
344 RRDs
::tune
("${rrdfile}.${field}","-a",$field.":".$host->{'vars'}->{$field}->{'max'});
347 if ($host->{'vars'}->{$field}->{'max'} ne 'U') {
348 RRDs
::tune
("${rrdfile}.${field}","-a",$field.":".$host->{'vars'}->{$field}->{'max'});
353 print "Error while setting max: " . RRDs
::error
. "\n";
357 if ($rrdinfo->{'rra[0].rows'} != $keep ||
358 !defined($rrdinfo->{'rra[1].rows'}) || $rrdinfo->{'rra[1].rows'} != $keepHours) {
360 print "Resizing ${rrdfile}.${field} from " . $rrdinfo->{'rra[0].rows'} .
361 " to ${keep} samples.\n";
363 (my $start, my $ostep, my $names, my $data) =
364 RRDs
::fetch
("${rrdfile}.${field}",
365 "-s " . (time() - ($rrdinfo->{'rra[0].rows'} * $rrdinfo->{'step'})),
369 print "Error while fetching data: " . RRDs
::error
. "\n";
373 rrdcreate
("${rrdfile}.${field}.new",
376 (${start
}-${ostep
}));
378 print "Preserving data in file ${rrdfile}.${field} since " . localtime($start) . "\n";
381 foreach my $line (@
$data) {
382 my $vline = "${pos}";
384 foreach my $val (@
$line) {
385 $val = 'U' if (!defined($val));
388 RRDs
::update
("${rrdfile}.${field}.new", $vline) or die "Can't insert data\n";
391 print "Error while updating: " . RRDs
::error
. "\n";
396 if ((($pos-$start)/$ostep) == $#$data) {
401 rename("${rrdfile}.${field}", "${rrdfile}.${field}.old") or die "Can't rename old file: $!\n";
402 rename("${rrdfile}.${field}.new", "${rrdfile}.${field}") or die "Can't rename new file: $!\n";
404 $rrdinfo = RRDs
::info
("${rrdfile}.${field}");
406 print "Error while getting info: " . RRDs
::error
. "\n";
410 if ($rrdinfo->{'rra[0].rows'} != $keep) {
420 die "fork failed!" if (!defined($child));
422 exit 0 if ($child != 0);
425 open(HTML
, ">${outdir}/index.html.new");
427 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>';
428 print HTML
'<body bgcolor="#ffffff">';
430 foreach my $host (@
$hosts) {
431 print HTML
"[<a href=\"#".${host
}->{'name'}."\">".${host
}->{'name'}."</a>] ";
435 foreach my $host (@
$hosts) {
437 print HTML
"<a name=\"".${host
}->{'name'}."\"></a>\n";
438 my $vars = $host->{'vars'};
439 my $rrdfile = $host->{'rrdfile'};
440 my $hostname = $host->{'name'};
441 my %multi_values = ();
443 foreach my $var (@
{$host->{'fields'}}) {
444 delete $vars->{$var}->{'value'};
448 if ((!defined($vars->{$var}->{'proto'})) ||
449 ($vars->{$var}->{'proto'} eq '') ||
450 ($vars->{$var}->{'proto'} eq 'snmp')) {
451 $result = fetch_snmp
($host->{'address'}, $host->{'community'}, $vars->{$var}->{'oid'});
452 } elsif ($vars->{$var}->{'proto'} eq 'tcp') {
453 $result = fetch_tcp
($host->{'address'}, $vars->{$var}->{'port'});
454 } elsif ($vars->{$var}->{'proto'} eq 'tcp_multi') {
455 if (defined($multi_values{$vars->{$var}->{'multi_id'}})) {
456 $result = $multi_values{$vars->{$var}->{'multi_id'}}
458 my %values = fetch_tcp_multi
($host->{'address'}, $vars->{$var}->{'port'}, $vars->{$var}->{'multi_delimiter'});
459 @multi_values{keys %values} = values %values;
460 $result = $multi_values{$vars->{$var}->{'multi_id'}};
464 next unless (defined $result);
466 $vars->{$var}->{'value'} = $result;
467 if (defined($vars->{$var}->{'factor'})) {
468 $vars->{$var}->{'value'} *= $vars->{$var}->{'factor'};
472 foreach my $var (@
{$host->{'fields'}}) {
473 if (!(defined($vars->{$var}->{'value'}))) {
474 $vars->{$var}->{'value'} = 'U';
476 rrd_update
("${rrdfile}.${var}", "N:" . $vars->{$var}->{'value'});
479 print "Error while updating: " . $rrd_result . "\n";
482 foreach my $var (@
{$host->{'fields'}}) {
483 my $graphWidth = 365 * 3;
485 if (defined $vars->{$var}->{'graph'}) {
486 $graphConfig = $vars->{$var}->{'graph'};
487 foreach my $subGraph (qw(day week year)) {
488 if (!defined($graphConfig->{$subGraph})) {
489 $graphConfig->{$subGraph} = [ 'avg'];
500 my @graphdef = ('-P', "--lazy", "-t", $hostname." - ".$vars->{$var}->{'name'});
502 push @graphdef, "DEF:${var}-avg=${rrdfile}.${var}:${var}:AVERAGE";
503 push @graphdef, "DEF:${var}-min=${rrdfile}.${var}:${var}:MIN";
504 push @graphdef, "DEF:${var}-max=${rrdfile}.${var}:${var}:MAX";
505 push @graphdef, "LINE1:${var}-avg#FF0000";
506 push @graphdef, "VDEF:cur=${var}-avg,LAST";
507 push @graphdef, 'GPRINT:cur:Current\\: <span foreground="#FF0000">%.2lf</span>\\r';
510 $mtime=(stat("${outdir}/${hostname}.${var}.png.work"))[9];
512 (my $averages, my $width, my $height) =
513 rrd_graph
("${outdir}/${hostname}.${var}.png.work",
514 "-w", "720", @graphdef);
521 print "Error while graphing: " . $rrd_result . "\n";
523 my $newmtime=(stat("${outdir}/${hostname}.${var}.png.work"))[9];
524 if ((!defined($mtime)) || ($newmtime != $mtime)) {
525 copy
("${outdir}/${hostname}.${var}.png.work", "${outdir}/${hostname}.${var}.png.new");
526 rename("${outdir}/${hostname}.${var}.png.new", "${outdir}/${hostname}.${var}.png");
530 print HTML
"<a href=\"${hostname}.${var}.html\"><img src=\"${hostname}.${var}.png\" width=\"${width}\" height=\"${height}\" border=\"0\"></a><br>\n";
532 open (HTML2
, ">${outdir}/${hostname}.${var}.html.new");
533 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>';
534 print HTML2
'<body bgcolor="#ffffff">';
536 push @graphdef, "VDEF:min=${var}-min,MINIMUM";
537 push @graphdef, "GPRINT:min:Minimum\\: %.2lf";
539 push @graphdef, "VDEF:avg=${var}-avg,AVERAGE";
540 push @graphdef, "GPRINT:avg:Average\\: %.2lf";
542 push @graphdef, "VDEF:max=${var}-max,MAXIMUM";
543 push @graphdef, "GPRINT:max:Maximum\\: %.2lf";
545 push @graphdef, "VDEF:cur=${var}-avg,LAST";
546 push @graphdef, "GPRINT:cur:Current\\: %.2lf";
548 my @dayGraphDef = @graphdef;
549 push @dayGraphDef, @
{cfgToGraphDef
($graphConfig->{'day'}, ${var
}, 24)};
551 $mtime=(stat("${outdir}/${hostname}.${var}.long.png.work"))[9];
552 ($averages, $width, $height) =
553 rrd_graph
("${outdir}/${hostname}.${var}.long.png.work",
554 "-w", $graphWidth, @dayGraphDef);
557 print "Error while graphing: " . $rrd_result . "\n";
559 my $newmtime=(stat("${outdir}/${hostname}.${var}.long.png.work"))[9];
560 if ((!defined($mtime)) || ($newmtime != $mtime)) {
561 copy
("${outdir}/${hostname}.${var}.long.png.work", "${outdir}/${hostname}.${var}.long.png.new");
562 rename("${outdir}/${hostname}.${var}.long.png.new", "${outdir}/${hostname}.${var}.long.png");
566 print HTML2
"<img src=\"${hostname}.${var}.long.png\" width=\"${width}\" height=\"${height}\"><br>\n";
568 $mtime=(stat("${outdir}/${hostname}.${var}.week.png.work"))[9];
570 my @weekGraphDef = @graphdef;
571 push @weekGraphDef, @
{cfgToGraphDef
($graphConfig->{'week'}, ${var
}, 24)};
573 ($averages, $width, $height) =
574 rrd_graph
("${outdir}/${hostname}.${var}.week.png.work",
575 "-w", "$graphWidth", "-e", "now", "-s", "00:00-8d", @weekGraphDef);
578 print "Error while graphing: " . $rrd_result . "\n";
580 my $newmtime=(stat("${outdir}/${hostname}.${var}.week.png.work"))[9];
581 if ((!defined($mtime)) || ($newmtime != $mtime)) {
582 copy
("${outdir}/${hostname}.${var}.week.png.work", "${outdir}/${hostname}.${var}.week.png.new");
583 rename("${outdir}/${hostname}.${var}.week.png.new", "${outdir}/${hostname}.${var}.week.png");
587 print HTML2
"<img src=\"${hostname}.${var}.week.png\" width=\"${width}\" height=\"${height}\"><br>\n";
589 $mtime=(stat("${outdir}/${hostname}.${var}.year.png.work"))[9];
591 my @yearGraphDef = @graphdef;
592 push @yearGraphDef, @
{cfgToGraphDef
($graphConfig->{'year'}, ${var
}, 3)};
594 ($averages, $width, $height) =
595 rrd_graph
("${outdir}/${hostname}.${var}.year.png.work",
596 "-w", "$graphWidth", "-e", "00:00", "-s", "end-365d", @yearGraphDef);
599 print "Error while graphing: " . $rrd_result . "\n";
601 my $newmtime=(stat("${outdir}/${hostname}.${var}.year.png.work"))[9];
602 if ((!defined($mtime)) || ($newmtime != $mtime)) {
603 copy
("${outdir}/${hostname}.${var}.year.png.work", "${outdir}/${hostname}.${var}.year.png.new");
604 rename("${outdir}/${hostname}.${var}.year.png.new", "${outdir}/${hostname}.${var}.year.png");
608 print HTML2
"<img src=\"${hostname}.${var}.year.png\" width=\"${width}\" height=\"${height}\"><br>\n";
610 print HTML2
"</body></html>\n";
612 rename("${outdir}/${hostname}.${var}.html.new", "${outdir}/${hostname}.${var}.html");
616 print HTML
"</body></html>\n";
617 print HTML
"<br>Generated on: " . localtime(time());
618 print HTML
' by <a href="http://git.zerfleddert.de/cgi-bin/gitweb.cgi/upsgraph">upsgraph</a>.';
622 rename("${outdir}/index.html.new", "${outdir}/index.html");