]>
git.zerfleddert.de Git - upsgraph/blob - upsgraph.pl
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}";
228 print STDERR
"Running as root, please provide UID as 2th argument!\n";
232 print "Running as root, switching to ".$ARGV[1]."\n";
236 foreach my $host (@
$hosts) {
237 my $rrdfile = $host->{'rrdfile'};
239 foreach my $var (keys(%{$host->{'vars'}})) {
240 $host->{'vars'}->{$var}->{'min'} = 'U' if (!defined($host->{'vars'}->{$var}->{'min'}));
241 $host->{'vars'}->{$var}->{'max'} = 'U' if (!defined($host->{'vars'}->{$var}->{'max'}));
244 if (-e
"${rrdfile}") {
245 print "Reading old ${rrdfile} to preserve data...\n";
247 my $rrdinfo = RRDs
::info
("${rrdfile}");
249 print "Error while getting info: " . RRDs
::error
. "\n";
253 (my $start, my $ostep, my $names, my $data) =
254 RRDs
::fetch
("${rrdfile}",
255 "-s " . (time() - ($rrdinfo->{'rra[0].rows'} * $rrdinfo->{'step'})),
259 print "Error while fetching data: " . RRDs
::error
. "\n";
263 foreach my $field (@
$names) {
264 if (! -e
"${rrdfile}.${field}") {
265 rrdcreate
("${rrdfile}.${field}",
268 (${start
}-${ostep
}));
273 foreach my $line (@
$data) {
274 foreach my $field (@
$names) {
275 my $val = shift (@
$line);
276 $val = 'U' if (!defined($val));
278 RRDs
::update
("${rrdfile}.${field}", "${pos}:${val}");
280 print "Can't insert data: " . RRDs
::error
. "\n";
288 if ((($pos-$start)/$ostep) == $#$data) {
293 rename("${rrdfile}", "${rrdfile}.old") or die "Can't rename old file: $!\n";
296 foreach my $field (@
{$host->{'fields'}}) {
297 if (! -e
"${rrdfile}.${field}") {
298 print "Creating ${rrdfile}.${field}...\n";
299 rrdcreate
("${rrdfile}.${field}",
304 my $rrdinfo = RRDs
::info
("${rrdfile}.${field}");
306 print "Error while getting info: " . RRDs
::error
. "\n";
310 if (defined($rrdinfo->{"ds[${field}].min"})) {
311 if ($rrdinfo->{"ds[${field}].min"} ne $host->{'vars'}->{$field}->{'min'}) {
312 RRDs
::tune
("${rrdfile}.${field}","-i",$field.":".$host->{'vars'}->{$field}->{'min'});
315 if ($host->{'vars'}->{$field}->{'min'} ne 'U') {
316 RRDs
::tune
("${rrdfile}.${field}","-i",$field.":".$host->{'vars'}->{$field}->{'min'});
321 print "Error while setting min: " . RRDs
::error
. "\n";
325 if (defined($rrdinfo->{"ds[${field}].max"})) {
326 if ($rrdinfo->{"ds[${field}].max"} ne $host->{'vars'}->{$field}->{'max'}) {
327 RRDs
::tune
("${rrdfile}.${field}","-a",$field.":".$host->{'vars'}->{$field}->{'max'});
330 if ($host->{'vars'}->{$field}->{'max'} ne 'U') {
331 RRDs
::tune
("${rrdfile}.${field}","-a",$field.":".$host->{'vars'}->{$field}->{'max'});
336 print "Error while setting max: " . RRDs
::error
. "\n";
340 if ($rrdinfo->{'rra[0].rows'} != $keep ||
341 !defined($rrdinfo->{'rra[1].rows'}) || $rrdinfo->{'rra[1].rows'} != $keepHours) {
343 print "Resizing ${rrdfile}.${field} from " . $rrdinfo->{'rra[0].rows'} .
344 " to ${keep} samples.\n";
346 (my $start, my $ostep, my $names, my $data) =
347 RRDs
::fetch
("${rrdfile}.${field}",
348 "-s " . (time() - ($rrdinfo->{'rra[0].rows'} * $rrdinfo->{'step'})),
352 print "Error while fetching data: " . RRDs
::error
. "\n";
356 rrdcreate
("${rrdfile}.${field}.new",
359 (${start
}-${ostep
}));
361 print "Preserving data in file ${rrdfile}.${field} since " . localtime($start) . "\n";
364 foreach my $line (@
$data) {
365 my $vline = "${pos}";
367 foreach my $val (@
$line) {
368 $val = 'U' if (!defined($val));
371 RRDs
::update
("${rrdfile}.${field}.new", $vline) or die "Can't insert data\n";
374 print "Error while updating: " . RRDs
::error
. "\n";
379 if ((($pos-$start)/$ostep) == $#$data) {
384 rename("${rrdfile}.${field}", "${rrdfile}.${field}.old") or die "Can't rename old file: $!\n";
385 rename("${rrdfile}.${field}.new", "${rrdfile}.${field}") or die "Can't rename new file: $!\n";
387 $rrdinfo = RRDs
::info
("${rrdfile}.${field}");
389 print "Error while getting info: " . RRDs
::error
. "\n";
393 if ($rrdinfo->{'rra[0].rows'} != $keep) {
403 die "fork failed!" if (!defined($child));
405 exit 0 if ($child != 0);
408 open(HTML
, ">${outdir}/index.html.new");
410 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>';
411 print HTML
'<body bgcolor="#ffffff">';
413 foreach my $host (@
$hosts) {
414 print HTML
"[<a href=\"#".${host
}->{'name'}."\">".${host
}->{'name'}."</a>] ";
418 foreach my $host (@
$hosts) {
420 print HTML
"<a name=\"".${host
}->{'name'}."\"></a>\n";
421 my $vars = $host->{'vars'};
422 my $rrdfile = $host->{'rrdfile'};
423 my $hostname = $host->{'name'};
424 my %multi_values = ();
426 foreach my $var (@
{$host->{'fields'}}) {
427 delete $vars->{$var}->{'value'};
431 if ((!defined($vars->{$var}->{'proto'})) ||
432 ($vars->{$var}->{'proto'} eq '') ||
433 ($vars->{$var}->{'proto'} eq 'snmp')) {
434 $result = fetch_snmp
($host->{'address'}, $host->{'community'}, $vars->{$var}->{'oid'});
435 } elsif ($vars->{$var}->{'proto'} eq 'tcp') {
436 $result = fetch_tcp
($host->{'address'}, $vars->{$var}->{'port'});
437 } elsif ($vars->{$var}->{'proto'} eq 'tcp_multi') {
438 if (defined($multi_values{$vars->{$var}->{'multi_id'}})) {
439 $result = $multi_values{$vars->{$var}->{'multi_id'}}
441 my %values = fetch_tcp_multi
($host->{'address'}, $vars->{$var}->{'port'}, $vars->{$var}->{'multi_delimiter'});
442 @multi_values{keys %values} = values %values;
443 $result = $multi_values{$vars->{$var}->{'multi_id'}};
447 next unless (defined $result);
449 $vars->{$var}->{'value'} = $result;
450 if (defined($vars->{$var}->{'factor'})) {
451 $vars->{$var}->{'value'} *= $vars->{$var}->{'factor'};
455 foreach my $var (@
{$host->{'fields'}}) {
456 if (!(defined($vars->{$var}->{'value'}))) {
457 $vars->{$var}->{'value'} = 'U';
459 rrd_update
("${rrdfile}.${var}", "N:" . $vars->{$var}->{'value'});
462 print "Error while updating: " . $rrd_result . "\n";
465 foreach my $var (@
{$host->{'fields'}}) {
466 my @graphdef = ('-P', "--lazy", "-t", $hostname." - ".$vars->{$var}->{'name'}, "DEF:${var}=${rrdfile}.${var}:${var}:AVERAGE", "LINE1:${var}#FF0000");
468 push @graphdef, "DEF:${var}-min=${rrdfile}.${var}:${var}:MIN", "LINE1:${var}-min#0000FF";
469 push @graphdef, "DEF:${var}-max=${rrdfile}.${var}:${var}:MAX", "LINE1:${var}-max#00FF00";
470 push @graphdef, "VDEF:cur=${var},LAST";
471 push @graphdef, 'GPRINT:cur:Current\\: <span foreground="#FF0000">%.2lf</span>\\r';
474 $mtime=(stat("${outdir}/${hostname}.${var}.png.work"))[9];
476 (my $averages, my $width, my $height) =
477 rrd_graph
("${outdir}/${hostname}.${var}.png.work",
478 "-w", "720", @graphdef);
484 print "Error while graphing: " . $rrd_result . "\n";
486 my $newmtime=(stat("${outdir}/${hostname}.${var}.png.work"))[9];
487 if ((!defined($mtime)) || ($newmtime != $mtime)) {
488 copy
("${outdir}/${hostname}.${var}.png.work", "${outdir}/${hostname}.${var}.png.new");
489 rename("${outdir}/${hostname}.${var}.png.new", "${outdir}/${hostname}.${var}.png");
493 print HTML
"<a href=\"${hostname}.${var}.html\"><img src=\"${hostname}.${var}.png\" width=\"${width}\" height=\"${height}\" border=\"0\"></a><br>\n";
495 open (HTML2
, ">${outdir}/${hostname}.${var}.html.new");
496 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>';
497 print HTML2
'<body bgcolor="#ffffff">';
499 push @graphdef, "VDEF:min=${var}-min,MINIMUM";
500 push @graphdef, "GPRINT:min:Minimum\\: %.2lf";
502 push @graphdef, "VDEF:avg=${var},AVERAGE";
503 push @graphdef, "GPRINT:avg:Average\\: %.2lf";
505 push @graphdef, "VDEF:max=${var}-max,MAXIMUM";
506 push @graphdef, "GPRINT:max:Maximum\\: %.2lf";
508 push @graphdef, "VDEF:cur=${var},LAST";
509 push @graphdef, "GPRINT:cur:Current\\: %.2lf";
511 $mtime=(stat("${outdir}/${hostname}.${var}.long.png.work"))[9];
512 ($averages, $width, $height) =
513 rrd_graph
("${outdir}/${hostname}.${var}.long.png.work",
514 "-w", "1008", @graphdef);
517 print "Error while graphing: " . $rrd_result . "\n";
519 my $newmtime=(stat("${outdir}/${hostname}.${var}.long.png.work"))[9];
520 if ((!defined($mtime)) || ($newmtime != $mtime)) {
521 copy
("${outdir}/${hostname}.${var}.long.png.work", "${outdir}/${hostname}.${var}.long.png.new");
522 rename("${outdir}/${hostname}.${var}.long.png.new", "${outdir}/${hostname}.${var}.long.png");
526 print HTML2
"<img src=\"${hostname}.${var}.long.png\" width=\"${width}\" height=\"${height}\"><br>\n";
528 $mtime=(stat("${outdir}/${hostname}.${var}.week.png.work"))[9];
529 ($averages, $width, $height) =
530 rrd_graph
("${outdir}/${hostname}.${var}.week.png.work",
531 "-w", "1008", "-e", "now", "-s", "end-1w", @graphdef);
534 print "Error while graphing: " . $rrd_result . "\n";
536 my $newmtime=(stat("${outdir}/${hostname}.${var}.week.png.work"))[9];
537 if ((!defined($mtime)) || ($newmtime != $mtime)) {
538 copy
("${outdir}/${hostname}.${var}.week.png.work", "${outdir}/${hostname}.${var}.week.png.new");
539 rename("${outdir}/${hostname}.${var}.week.png.new", "${outdir}/${hostname}.${var}.week.png");
543 print HTML2
"<img src=\"${hostname}.${var}.week.png\" width=\"${width}\" height=\"${height}\"><br>\n";
545 $mtime=(stat("${outdir}/${hostname}.${var}.year.png.work"))[9];
546 ($averages, $width, $height) =
547 rrd_graph
("${outdir}/${hostname}.${var}.year.png.work",
548 "-w", "1008", "-e", "now", "-s", "end-1y", @graphdef);
551 print "Error while graphing: " . $rrd_result . "\n";
553 my $newmtime=(stat("${outdir}/${hostname}.${var}.year.png.work"))[9];
554 if ((!defined($mtime)) || ($newmtime != $mtime)) {
555 copy
("${outdir}/${hostname}.${var}.year.png.work", "${outdir}/${hostname}.${var}.year.png.new");
556 rename("${outdir}/${hostname}.${var}.year.png.new", "${outdir}/${hostname}.${var}.year.png");
560 print HTML2
"<img src=\"${hostname}.${var}.year.png\" width=\"${width}\" height=\"${height}\"><br>\n";
562 print HTML2
"</body></html>\n";
564 rename("${outdir}/${hostname}.${var}.html.new", "${outdir}/${hostname}.${var}.html");
568 print HTML
"</body></html>\n";
569 print HTML
"<br>Generated on: " . localtime(time());
570 print HTML
' by <a href="http://git.zerfleddert.de/cgi-bin/gitweb.cgi/upsgraph">upsgraph</a>.';
574 rename("${outdir}/index.html.new", "${outdir}/index.html");