]>
git.zerfleddert.de Git - upsgraph/blob - upsgraph.pl
2783fcf75e86ba259cd0ddad1a3469f1a4eeb0f5
3 #Due to memory leak in Debian squeeze (Bug #545519)
7 if ((@ARGV != 1) && (@ARGV != 2)) {
8 print STDERR
"Syntax: ${0} configfile [uid]\n";
18 $UPSGRAPH::outdir
= "";
20 $UPSGRAPH::keep
= (370*24*60*60)/$UPSGRAPH::step
;
21 $UPSGRAPH::hosts
= ();
23 do $ARGV[0] or die "can't read config: $!";
25 my $outdir = $UPSGRAPH::outdir
;
26 my $step = $UPSGRAPH::step
;
27 my $keep = $UPSGRAPH::keep
;
28 my $hosts = $UPSGRAPH::hosts
;
35 $rrd_result = RRDs
::error
;
37 $rrd_result = system("rrdtool", "update", @args);
46 @rrd_out = RRDs
::graph
(@args);
47 $rrd_result = RRDs
::error
;
51 open(RRDFD
, '-|', 'rrdtool', 'graph', @args);
58 if ($rrd_result == 0) {
60 push @rrd_out, split(/x/, $rrd_stdout);
73 my @cmd = ("${newrrd}", "--step=${step}");
75 if (defined($start)) {
76 push @cmd, "--start=${start}";
79 push @cmd, "DS:${field}:GAUGE:600:" .
80 $vars->{$field}->{'min'} . ":" .
81 $vars->{$field}->{'max'} . " ";
83 push @cmd, "RRA:AVERAGE:0.5:1:${keep}";
87 print "Error while creating: " . RRDs
::error
. "\n";
94 my $community = shift;
97 (my $session, my $error) = Net
::SNMP
->session(Hostname
=> $address,
98 Community
=> $community);
101 print STDERR
"session error: $error";
105 $session->translate(0);
107 my $result = $session->get_request($oid);
111 return undef if (!defined($result));
120 my $sock = IO
::Socket
::INET
->new(PeerAddr
=> $address,
125 return undef if (!$sock);
127 chomp(my $value = <$sock>);
142 print STDERR
"Running as root, please provide UID as 2th argument!\n";
146 print "Running as root, switching to ".$ARGV[1]."\n";
150 foreach my $host (@
$hosts) {
151 my $rrdfile = $host->{'rrdfile'};
153 foreach my $var (keys(%{$host->{'vars'}})) {
154 $host->{'vars'}->{$var}->{'min'} = 'U' if (!defined($host->{'vars'}->{$var}->{'min'}));
155 $host->{'vars'}->{$var}->{'max'} = 'U' if (!defined($host->{'vars'}->{$var}->{'max'}));
158 if (-e
"${rrdfile}") {
159 print "Reading old ${rrdfile} to preserve data...\n";
161 my $rrdinfo = RRDs
::info
("${rrdfile}");
163 print "Error while getting info: " . RRDs
::error
. "\n";
167 (my $start, my $ostep, my $names, my $data) =
168 RRDs
::fetch
("${rrdfile}",
169 "-s " . (time() - ($rrdinfo->{'rra[0].rows'} * $rrdinfo->{'step'})),
173 print "Error while fetching data: " . RRDs
::error
. "\n";
177 foreach my $field (@
$names) {
178 if (! -e
"${rrdfile}.${field}") {
179 rrdcreate
("${rrdfile}.${field}",
182 (${start
}-${ostep
}));
187 foreach my $line (@
$data) {
188 foreach my $field (@
$names) {
189 my $val = shift (@
$line);
190 $val = 'U' if (!defined($val));
192 RRDs
::update
("${rrdfile}.${field}", "${pos}:${val}");
194 print "Can't insert data: " . RRDs
::error
. "\n";
202 if ((($pos-$start)/$ostep) == $#$data) {
207 rename("${rrdfile}", "${rrdfile}.old") or die "Can't rename old file: $!\n";
210 foreach my $field (@
{$host->{'fields'}}) {
211 if (! -e
"${rrdfile}.${field}") {
212 print "Creating ${rrdfile}.${field}...\n";
213 rrdcreate
("${rrdfile}.${field}",
218 my $rrdinfo = RRDs
::info
("${rrdfile}.${field}");
220 print "Error while getting info: " . RRDs
::error
. "\n";
224 if (defined($rrdinfo->{"ds[${field}].min"})) {
225 if ($rrdinfo->{"ds[${field}].min"} ne $host->{'vars'}->{$field}->{'min'}) {
226 RRDs
::tune
("${rrdfile}.${field}","-i",$field.":".$host->{'vars'}->{$field}->{'min'});
229 if ($host->{'vars'}->{$field}->{'min'} ne 'U') {
230 RRDs
::tune
("${rrdfile}.${field}","-i",$field.":".$host->{'vars'}->{$field}->{'min'});
235 print "Error while setting min: " . RRDs
::error
. "\n";
239 if (defined($rrdinfo->{"ds[${field}].max"})) {
240 if ($rrdinfo->{"ds[${field}].max"} ne $host->{'vars'}->{$field}->{'max'}) {
241 RRDs
::tune
("${rrdfile}.${field}","-a",$field.":".$host->{'vars'}->{$field}->{'max'});
244 if ($host->{'vars'}->{$field}->{'max'} ne 'U') {
245 RRDs
::tune
("${rrdfile}.${field}","-a",$field.":".$host->{'vars'}->{$field}->{'max'});
250 print "Error while setting max: " . RRDs
::error
. "\n";
254 if ($rrdinfo->{'rra[0].rows'} != $keep) {
255 print "Resizing ${rrdfile}.${field} from " . $rrdinfo->{'rra[0].rows'} .
256 " to ${keep} samples.\n";
258 (my $start, my $ostep, my $names, my $data) =
259 RRDs
::fetch
("${rrdfile}.${field}",
260 "-s " . (time() - ($rrdinfo->{'rra[0].rows'} * $rrdinfo->{'step'})),
264 print "Error while fetching data: " . RRDs
::error
. "\n";
268 rrdcreate
("${rrdfile}.${field}.new",
271 (${start
}-${ostep
}));
273 print "Preserving data since " . localtime($start) . "\n";
276 foreach my $line (@
$data) {
277 my $vline = "${pos}";
279 foreach my $val (@
$line) {
280 $val = 'U' if (!defined($val));
283 RRDs
::update
("${rrdfile}.${field}.new", $vline) or die "Can't insert data\n";
286 print "Error while updating: " . RRDs
::error
. "\n";
291 if ((($pos-$start)/$ostep) == $#$data) {
296 rename("${rrdfile}.${field}", "${rrdfile}.${field}.old") or die "Can't rename old file: $!\n";
297 rename("${rrdfile}.${field}.new", "${rrdfile}.${field}") or die "Can't rename new file: $!\n";
299 $rrdinfo = RRDs
::info
("${rrdfile}.${field}");
301 print "Error while getting info: " . RRDs
::error
. "\n";
305 if ($rrdinfo->{'rra[0].rows'} != $keep) {
315 die "fork failed!" if (!defined($child));
317 exit 0 if ($child != 0);
320 open(HTML
, ">${outdir}/index.html.new");
322 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>';
323 print HTML
'<body bgcolor="#ffffff">';
325 foreach my $host (@
$hosts) {
326 print HTML
"[<a href=\"#".${host
}->{'name'}."\">".${host
}->{'name'}."</a>] ";
330 foreach my $host (@
$hosts) {
332 print HTML
"<a name=\"".${host
}->{'name'}."\"></a>\n";
333 my $vars = $host->{'vars'};
334 my $rrdfile = $host->{'rrdfile'};
335 my $hostname = $host->{'name'};
337 foreach my $var (@
{$host->{'fields'}}) {
338 delete $vars->{$var}->{'value'};
342 if ((!defined($vars->{$var}->{'proto'})) ||
343 ($vars->{$var}->{'proto'} eq '') ||
344 ($vars->{$var}->{'proto'} eq 'snmp')) {
345 $result = fetch_snmp
($host->{'address'}, $host->{'community'}, $vars->{$var}->{'oid'});
346 } elsif ($vars->{$var}->{'proto'} eq 'tcp') {
347 $result = fetch_tcp
($host->{'address'}, $vars->{$var}->{'port'});
350 next unless (defined $result);
352 $vars->{$var}->{'value'} = $result;
353 if (defined($vars->{$var}->{'factor'})) {
354 $vars->{$var}->{'value'} *= $vars->{$var}->{'factor'};
358 foreach my $var (@
{$host->{'fields'}}) {
359 if (!(defined($vars->{$var}->{'value'}))) {
360 $vars->{$var}->{'value'} = 'U';
362 rrd_update
("${rrdfile}.${var}", "N:" . $vars->{$var}->{'value'});
365 print "Error while updating: " . $rrd_result . "\n";
368 foreach my $var (@
{$host->{'fields'}}) {
369 my @graphdef = ('-P', "--lazy", "-t", $hostname." - ".$vars->{$var}->{'name'}, "DEF:${var}=${rrdfile}.${var}:${var}:AVERAGE", "LINE1:${var}#FF0000");
371 push @graphdef, "VDEF:cur=${var},LAST";
372 push @graphdef, 'GPRINT:cur:Current\\: <span foreground="#FF0000">%.2lf</span>\\r';
375 $mtime=(stat("${outdir}/${hostname}.${var}.png.work"))[9];
377 (my $averages, my $width, my $height) =
378 rrd_graph
("${outdir}/${hostname}.${var}.png.work",
379 "-w", "720", @graphdef);
385 print "Error while graphing: " . $rrd_result . "\n";
387 my $newmtime=(stat("${outdir}/${hostname}.${var}.png.work"))[9];
388 if ((!defined($mtime)) || ($newmtime != $mtime)) {
389 copy
("${outdir}/${hostname}.${var}.png.work", "${outdir}/${hostname}.${var}.png.new");
390 rename("${outdir}/${hostname}.${var}.png.new", "${outdir}/${hostname}.${var}.png");
394 print HTML
"<a href=\"${hostname}.${var}.html\"><img src=\"${hostname}.${var}.png\" width=\"${width}\" height=\"${height}\" border=\"0\"></a><br>\n";
396 open (HTML2
, ">${outdir}/${hostname}.${var}.html.new");
397 print HTML2
'<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>' . $vars->{$var}->{'name'} . '</title></head>';
398 print HTML2
'<body bgcolor="#ffffff">';
400 push @graphdef, "VDEF:min=${var},MINIMUM";
401 push @graphdef, "GPRINT:min:Minimum\\: %.2lf";
403 push @graphdef, "VDEF:avg=${var},AVERAGE";
404 push @graphdef, "GPRINT:avg:Average\\: %.2lf";
406 push @graphdef, "VDEF:max=${var},MAXIMUM";
407 push @graphdef, "GPRINT:max:Maximum\\: %.2lf";
409 push @graphdef, "VDEF:cur=${var},LAST";
410 push @graphdef, "GPRINT:cur:Current\\: %.2lf";
412 $mtime=(stat("${outdir}/${hostname}.${var}.long.png.work"))[9];
413 ($averages, $width, $height) =
414 rrd_graph
("${outdir}/${hostname}.${var}.long.png.work",
415 "-w", "1008", @graphdef);
418 print "Error while graphing: " . $rrd_result . "\n";
420 my $newmtime=(stat("${outdir}/${hostname}.${var}.long.png.work"))[9];
421 if ((!defined($mtime)) || ($newmtime != $mtime)) {
422 copy
("${outdir}/${hostname}.${var}.long.png.work", "${outdir}/${hostname}.${var}.long.png.new");
423 rename("${outdir}/${hostname}.${var}.long.png.new", "${outdir}/${hostname}.${var}.long.png");
427 print HTML2
"<img src=\"${hostname}.${var}.long.png\" width=\"${width}\" height=\"${height}\"><br>";
429 $mtime=(stat("${outdir}/${hostname}.${var}.week.png.work"))[9];
430 ($averages, $width, $height) =
431 rrd_graph
("${outdir}/${hostname}.${var}.week.png.work",
432 "-w", "1008", "-e", "now", "-s", "end-1w", @graphdef);
435 print "Error while graphing: " . $rrd_result . "\n";
437 my $newmtime=(stat("${outdir}/${hostname}.${var}.week.png.work"))[9];
438 if ((!defined($mtime)) || ($newmtime != $mtime)) {
439 copy
("${outdir}/${hostname}.${var}.week.png.work", "${outdir}/${hostname}.${var}.week.png.new");
440 rename("${outdir}/${hostname}.${var}.week.png.new", "${outdir}/${hostname}.${var}.week.png");
444 print HTML2
"<img src=\"${hostname}.${var}.week.png\" width=\"${width}\" height=\"${height}\"><br>";
446 $mtime=(stat("${outdir}/${hostname}.${var}.year.png.work"))[9];
447 ($averages, $width, $height) =
448 rrd_graph
("${outdir}/${hostname}.${var}.year.png.work",
449 "-w", "1008", "-e", "now", "-s", "end-1y", @graphdef);
452 print "Error while graphing: " . $rrd_result . "\n";
454 my $newmtime=(stat("${outdir}/${hostname}.${var}.year.png.work"))[9];
455 if ((!defined($mtime)) || ($newmtime != $mtime)) {
456 copy
("${outdir}/${hostname}.${var}.year.png.work", "${outdir}/${hostname}.${var}.year.png.new");
457 rename("${outdir}/${hostname}.${var}.year.png.new", "${outdir}/${hostname}.${var}.year.png");
461 print HTML2
"<img src=\"${hostname}.${var}.year.png\" width=\"${width}\" height=\"${height}\"><br>";
463 print HTML2
"</body></html>\n";
465 rename("${outdir}/${hostname}.${var}.html.new", "${outdir}/${hostname}.${var}.html");
469 print HTML
"</body></html>\n";
470 print HTML
"<br>Generated on: " . localtime(time());
471 print HTML
' by <a href="http://git.zerfleddert.de/cgi-bin/gitweb.cgi/upsgraph">upsgraph</a>.';
475 rename("${outdir}/index.html.new", "${outdir}/index.html");