]>
git.zerfleddert.de Git - upsgraph/blob - upsgraph.pl
e291cc73abe6f7efd6f014346e2e790e4d72bf65
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
;
39 print "Updating: ". Dumper
(\
@_);
42 $rrd_result = RRDs
::error
;
44 $rrd_result = system("rrdtool", "update", @args);
51 print "Graphing: " . Dumper
(\
@_);
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";
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 for (my $i = 1; $i < $dataPoints; ++$i) {
205 $dayCons .= "prev${mode}${i},";
206 $consFunc .= ",${mode}";
208 push @args, "CDEF:day${mode}=${dayCons}${dataSrc}${consFunc}";
209 push @args, "CDEF:fillCalDay${mode}0=COUNT,${dataPoints},%,0,EQ,day${mode},UNKN,IF";
210 for (my $i = 1; $i < $dataPoints; ++$i) {
213 push @args, "CDEF:fillCalDay${mode}${next}=PREV(fillCalDay${mode}${prev})";
217 for (my $i = 0; $i < $dataPoints; ++$i) {
218 $fillPoint .= "COUNT,${dataPoints},%,${i},EQ,fillCalDay${mode}${i},";
221 push @args, "CDEF:${mode}Curve=${fillPoint}UNKN${if}";
222 my $forwardShift = (24*60*60) * ($dataPoints - 1) / $dataPoints;
223 push @args, "SHIFT:${mode}Curve:-${forwardShift}";
224 push @args, "LINE1:${mode}Curve#${color}:${label}";
233 foreach my $subGraph (@
$cfg) {
234 if ($subGraph eq 'avg') {
235 push @graphDef, "LINE1:${varname}-avg#FF0000";
236 } elsif ($subGraph eq 'day-min') {
237 push @graphDef, @
{dayGraphFunc
("${varname}-min", 'MIN', '0000ff', 'Day Minimum Temperature', $dpPerDay)};
238 } elsif ($subGraph eq 'day-max') {
239 push @graphDef, @
{dayGraphFunc
("${varname}-max", 'MAX', '00ff00', 'Day Maximum Temperature', $dpPerDay)};
247 print STDERR
"Running as root, please provide UID as 2th argument!\n";
251 print "Running as root, switching to ".$ARGV[1]."\n";
255 foreach my $host (@
$hosts) {
256 my $rrdfile = $host->{'rrdfile'};
258 foreach my $var (keys(%{$host->{'vars'}})) {
259 $host->{'vars'}->{$var}->{'min'} = 'U' if (!defined($host->{'vars'}->{$var}->{'min'}));
260 $host->{'vars'}->{$var}->{'max'} = 'U' if (!defined($host->{'vars'}->{$var}->{'max'}));
263 if (-e
"${rrdfile}") {
264 print "Reading old ${rrdfile} to preserve data...\n";
266 my $rrdinfo = RRDs
::info
("${rrdfile}");
268 print "Error while getting info: " . RRDs
::error
. "\n";
272 (my $start, my $ostep, my $names, my $data) =
273 RRDs
::fetch
("${rrdfile}",
274 "-s " . (time() - ($rrdinfo->{'rra[0].rows'} * $rrdinfo->{'step'})),
278 print "Error while fetching data: " . RRDs
::error
. "\n";
282 foreach my $field (@
$names) {
283 if (! -e
"${rrdfile}.${field}") {
284 rrdcreate
("${rrdfile}.${field}",
287 (${start
}-${ostep
}));
292 foreach my $line (@
$data) {
293 foreach my $field (@
$names) {
294 my $val = shift (@
$line);
295 $val = 'U' if (!defined($val));
297 RRDs
::update
("${rrdfile}.${field}", "${pos}:${val}");
299 print "Can't insert data: " . RRDs
::error
. "\n";
307 if ((($pos-$start)/$ostep) == $#$data) {
312 rename("${rrdfile}", "${rrdfile}.old") or die "Can't rename old file: $!\n";
315 foreach my $field (@
{$host->{'fields'}}) {
316 if (! -e
"${rrdfile}.${field}") {
317 print "Creating ${rrdfile}.${field}...\n";
318 rrdcreate
("${rrdfile}.${field}",
323 my $rrdinfo = RRDs
::info
("${rrdfile}.${field}");
325 print "Error while getting info: " . RRDs
::error
. "\n";
329 my $limitsChanged = 0;
330 if (defined($rrdinfo->{"ds[${field}].min"})) {
331 if ($rrdinfo->{"ds[${field}].min"} ne $host->{'vars'}->{$field}->{'min'}) {
333 RRDs
::tune
("${rrdfile}.${field}","-i",$field.":".$host->{'vars'}->{$field}->{'min'});
336 if ($host->{'vars'}->{$field}->{'min'} ne 'U') {
338 RRDs
::tune
("${rrdfile}.${field}","-i",$field.":".$host->{'vars'}->{$field}->{'min'});
343 print "Error while setting min: " . RRDs
::error
. "\n";
347 if (defined($rrdinfo->{"ds[${field}].max"})) {
348 if ($rrdinfo->{"ds[${field}].max"} ne $host->{'vars'}->{$field}->{'max'}) {
350 RRDs
::tune
("${rrdfile}.${field}","-a",$field.":".$host->{'vars'}->{$field}->{'max'});
353 if ($host->{'vars'}->{$field}->{'max'} ne 'U') {
355 RRDs
::tune
("${rrdfile}.${field}","-a",$field.":".$host->{'vars'}->{$field}->{'max'});
360 print "Error while setting max: " . RRDs
::error
. "\n";
364 if ($rrdinfo->{'rra[0].rows'} != $keep ||
365 !defined($rrdinfo->{'rra[1].rows'}) || $rrdinfo->{'rra[1].rows'} != $keepHours ||
366 $limitsChanged == 1) {
368 print "Resizing ${rrdfile}.${field} from " . $rrdinfo->{'rra[0].rows'} .
369 " to ${keep} samples.\n";
371 (my $start, my $ostep, my $names, my $data) =
372 RRDs
::fetch
("${rrdfile}.${field}",
373 "-s " . (time() - ($rrdinfo->{'rra[0].rows'} * $rrdinfo->{'step'})),
377 print "Error while fetching data: " . RRDs
::error
. "\n";
381 rrdcreate
("${rrdfile}.${field}.new",
384 (${start
}-${ostep
}));
386 print "Preserving data in file ${rrdfile}.${field} since " . localtime($start) . "\n";
389 foreach my $line (@
$data) {
390 my $vline = "${pos}";
392 foreach my $val (@
$line) {
393 $val = 'U' if (!defined($val));
396 RRDs
::update
("${rrdfile}.${field}.new", $vline) or die "Can't insert data\n";
399 print "Error while updating: " . RRDs
::error
. "\n";
404 if ((($pos-$start)/$ostep) == $#$data) {
409 rename("${rrdfile}.${field}", "${rrdfile}.${field}.old") or die "Can't rename old file: $!\n";
410 rename("${rrdfile}.${field}.new", "${rrdfile}.${field}") or die "Can't rename new file: $!\n";
412 $rrdinfo = RRDs
::info
("${rrdfile}.${field}");
414 print "Error while getting info: " . RRDs
::error
. "\n";
418 if ($rrdinfo->{'rra[0].rows'} != $keep) {
428 #die "fork failed!" if (!defined($child));
430 #exit 0 if ($child != 0);
432 my $first = $UPSGRAPH::regenerateOnStart
;
434 open(HTML
, ">${outdir}/index.html.new");
436 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>';
437 print HTML
'<body bgcolor="#ffffff">';
439 foreach my $host (@
$hosts) {
440 print HTML
"[<a href=\"#".${host
}->{'name'}."\">".${host
}->{'name'}."</a>] ";
444 foreach my $host (@
$hosts) {
446 print HTML
"<a name=\"".${host
}->{'name'}."\"></a>\n";
447 my $vars = $host->{'vars'};
448 my $rrdfile = $host->{'rrdfile'};
449 my $hostname = $host->{'name'};
450 my %multi_values = ();
452 foreach my $var (@
{$host->{'fields'}}) {
453 delete $vars->{$var}->{'value'};
457 if ((!defined($vars->{$var}->{'proto'})) ||
458 ($vars->{$var}->{'proto'} eq '') ||
459 ($vars->{$var}->{'proto'} eq 'snmp')) {
460 $result = fetch_snmp
($host->{'address'}, $host->{'community'}, $vars->{$var}->{'oid'});
461 } elsif ($vars->{$var}->{'proto'} eq 'tcp') {
462 $result = fetch_tcp
($host->{'address'}, $vars->{$var}->{'port'});
463 } elsif ($vars->{$var}->{'proto'} eq 'tcp_multi') {
464 if (defined($multi_values{$vars->{$var}->{'multi_id'}})) {
465 $result = $multi_values{$vars->{$var}->{'multi_id'}}
467 my %values = fetch_tcp_multi
($host->{'address'}, $vars->{$var}->{'port'}, $vars->{$var}->{'multi_delimiter'});
468 @multi_values{keys %values} = values %values;
469 $result = $multi_values{$vars->{$var}->{'multi_id'}};
473 next unless (defined $result);
475 $vars->{$var}->{'value'} = $result;
476 if (defined($vars->{$var}->{'factor'})) {
477 $vars->{$var}->{'value'} *= $vars->{$var}->{'factor'};
481 foreach my $var (@
{$host->{'fields'}}) {
482 if (!(defined($vars->{$var}->{'value'}))) {
483 $vars->{$var}->{'value'} = 'U';
485 rrd_update
("${rrdfile}.${var}", "N:" . $vars->{$var}->{'value'});
488 print "Error while updating: " . $rrd_result . "\n";
491 foreach my $var (@
{$host->{'fields'}}) {
492 my $graphWidth = 365 * 3;
494 if (defined $vars->{$var}->{'graph'}) {
495 $graphConfig = $vars->{$var}->{'graph'};
496 foreach my $subGraph (qw(day week year)) {
497 if (!defined($graphConfig->{$subGraph})) {
498 $graphConfig->{$subGraph} = [ 'avg'];
509 my @graphdef = ('-P', "-t", $hostname." - ".$vars->{$var}->{'name'});
510 push @graphdef, "--lazy" if !$first;
512 push @graphdef, "DEF:${var}-avg=${rrdfile}.${var}:${var}:AVERAGE";
513 push @graphdef, "DEF:${var}-min=${rrdfile}.${var}:${var}:MIN";
514 push @graphdef, "DEF:${var}-max=${rrdfile}.${var}:${var}:MAX";
515 push @graphdef, "LINE1:${var}-avg#FF0000";
516 push @graphdef, "VDEF:cur=${var}-avg,LAST";
517 push @graphdef, 'GPRINT:cur:Current\\: <span foreground="#FF0000">%.2lf</span>\\r';
520 $mtime=(stat("${outdir}/${hostname}.${var}.png.work"))[9];
522 (my $averages, my $width, my $height) =
523 rrd_graph
("${outdir}/${hostname}.${var}.png.work",
524 "-w", "720", @graphdef);
531 print "Error while graphing: " . $rrd_result . "\n";
533 my $newmtime=(stat("${outdir}/${hostname}.${var}.png.work"))[9];
534 if ((!defined($mtime)) || ($newmtime != $mtime)) {
535 copy
("${outdir}/${hostname}.${var}.png.work", "${outdir}/${hostname}.${var}.png.new");
536 rename("${outdir}/${hostname}.${var}.png.new", "${outdir}/${hostname}.${var}.png");
540 print HTML
"<a href=\"${hostname}.${var}.html\"><img src=\"${hostname}.${var}.png\" width=\"${width}\" height=\"${height}\" border=\"0\"></a><br>\n";
542 open (HTML2
, ">${outdir}/${hostname}.${var}.html.new");
543 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>';
544 print HTML2
'<body bgcolor="#ffffff">';
546 push @graphdef, "VDEF:min=${var}-min,MINIMUM";
547 push @graphdef, "GPRINT:min:Minimum\\: %.2lf";
549 push @graphdef, "VDEF:avg=${var}-avg,AVERAGE";
550 push @graphdef, "GPRINT:avg:Average\\: %.2lf";
552 push @graphdef, "VDEF:max=${var}-max,MAXIMUM";
553 push @graphdef, "GPRINT:max:Maximum\\: %.2lf";
555 push @graphdef, "VDEF:cur=${var}-avg,LAST";
556 push @graphdef, "GPRINT:cur:Current\\: %.2lf";
558 my @dayGraphDef = @graphdef;
559 push @dayGraphDef, @
{cfgToGraphDef
($graphConfig->{'day'}, ${var
}, 24)};
561 $mtime=(stat("${outdir}/${hostname}.${var}.long.png.work"))[9];
562 ($averages, $width, $height) =
563 rrd_graph
("${outdir}/${hostname}.${var}.long.png.work",
564 "-w", $graphWidth, @dayGraphDef);
567 print "Error while graphing: " . $rrd_result . "\n";
569 my $newmtime=(stat("${outdir}/${hostname}.${var}.long.png.work"))[9];
570 if ((!defined($mtime)) || ($newmtime != $mtime)) {
571 copy
("${outdir}/${hostname}.${var}.long.png.work", "${outdir}/${hostname}.${var}.long.png.new");
572 rename("${outdir}/${hostname}.${var}.long.png.new", "${outdir}/${hostname}.${var}.long.png");
576 print HTML2
"<img src=\"${hostname}.${var}.long.png\" width=\"${width}\" height=\"${height}\"><br>\n";
578 $mtime=(stat("${outdir}/${hostname}.${var}.week.png.work"))[9];
580 my @weekGraphDef = @graphdef;
581 push @weekGraphDef, @
{cfgToGraphDef
($graphConfig->{'week'}, ${var
}, 24)};
583 ($averages, $width, $height) =
584 rrd_graph
("${outdir}/${hostname}.${var}.week.png.work",
585 "-w", "$graphWidth", "-e", "now", "-s", "00:00-8d", @weekGraphDef);
588 print "Error while graphing: " . $rrd_result . "\n";
590 my $newmtime=(stat("${outdir}/${hostname}.${var}.week.png.work"))[9];
591 if ((!defined($mtime)) || ($newmtime != $mtime)) {
592 copy
("${outdir}/${hostname}.${var}.week.png.work", "${outdir}/${hostname}.${var}.week.png.new");
593 rename("${outdir}/${hostname}.${var}.week.png.new", "${outdir}/${hostname}.${var}.week.png");
597 print HTML2
"<img src=\"${hostname}.${var}.week.png\" width=\"${width}\" height=\"${height}\"><br>\n";
599 $mtime=(stat("${outdir}/${hostname}.${var}.year.png.work"))[9];
601 my @yearGraphDef = @graphdef;
602 push @yearGraphDef, @
{cfgToGraphDef
($graphConfig->{'year'}, ${var
}, 3)};
604 ($averages, $width, $height) =
605 rrd_graph
("${outdir}/${hostname}.${var}.year.png.work",
606 "-w", "$graphWidth", "-e", "00:00", "-s", "end-365d", @yearGraphDef);
609 print "Error while graphing: " . $rrd_result . "\n";
611 my $newmtime=(stat("${outdir}/${hostname}.${var}.year.png.work"))[9];
612 if ((!defined($mtime)) || ($newmtime != $mtime)) {
613 copy
("${outdir}/${hostname}.${var}.year.png.work", "${outdir}/${hostname}.${var}.year.png.new");
614 rename("${outdir}/${hostname}.${var}.year.png.new", "${outdir}/${hostname}.${var}.year.png");
618 print HTML2
"<img src=\"${hostname}.${var}.year.png\" width=\"${width}\" height=\"${height}\"><br>\n";
620 print HTML2
"</body></html>\n";
622 rename("${outdir}/${hostname}.${var}.html.new", "${outdir}/${hostname}.${var}.html");
626 print HTML
"</body></html>\n";
627 print HTML
"<br>Generated on: " . localtime(time());
628 print HTML
' by <a href="http://git.zerfleddert.de/cgi-bin/gitweb.cgi/upsgraph">upsgraph</a>.';
632 rename("${outdir}/index.html.new", "${outdir}/index.html");