]>
git.zerfleddert.de Git - upsgraph/blob - upsgraph.pl
10f622b7f643dd04ec4c6f458ace8d6069457c5d
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>);
140 sub fetch_tcp_multi
(@
) {
143 my $delimiter = shift;
146 my $sock = IO
::Socket
::INET
->new(PeerAddr
=> $address,
151 return undef if (!$sock);
155 (my $key, my $value) = split(/${delimiter}/, $_);
157 $values{$key} = $value;
167 print STDERR
"Running as root, please provide UID as 2th argument!\n";
171 print "Running as root, switching to ".$ARGV[1]."\n";
175 foreach my $host (@
$hosts) {
176 my $rrdfile = $host->{'rrdfile'};
178 foreach my $var (keys(%{$host->{'vars'}})) {
179 $host->{'vars'}->{$var}->{'min'} = 'U' if (!defined($host->{'vars'}->{$var}->{'min'}));
180 $host->{'vars'}->{$var}->{'max'} = 'U' if (!defined($host->{'vars'}->{$var}->{'max'}));
183 if (-e
"${rrdfile}") {
184 print "Reading old ${rrdfile} to preserve data...\n";
186 my $rrdinfo = RRDs
::info
("${rrdfile}");
188 print "Error while getting info: " . RRDs
::error
. "\n";
192 (my $start, my $ostep, my $names, my $data) =
193 RRDs
::fetch
("${rrdfile}",
194 "-s " . (time() - ($rrdinfo->{'rra[0].rows'} * $rrdinfo->{'step'})),
198 print "Error while fetching data: " . RRDs
::error
. "\n";
202 foreach my $field (@
$names) {
203 if (! -e
"${rrdfile}.${field}") {
204 rrdcreate
("${rrdfile}.${field}",
207 (${start
}-${ostep
}));
212 foreach my $line (@
$data) {
213 foreach my $field (@
$names) {
214 my $val = shift (@
$line);
215 $val = 'U' if (!defined($val));
217 RRDs
::update
("${rrdfile}.${field}", "${pos}:${val}");
219 print "Can't insert data: " . RRDs
::error
. "\n";
227 if ((($pos-$start)/$ostep) == $#$data) {
232 rename("${rrdfile}", "${rrdfile}.old") or die "Can't rename old file: $!\n";
235 foreach my $field (@
{$host->{'fields'}}) {
236 if (! -e
"${rrdfile}.${field}") {
237 print "Creating ${rrdfile}.${field}...\n";
238 rrdcreate
("${rrdfile}.${field}",
243 my $rrdinfo = RRDs
::info
("${rrdfile}.${field}");
245 print "Error while getting info: " . RRDs
::error
. "\n";
249 if (defined($rrdinfo->{"ds[${field}].min"})) {
250 if ($rrdinfo->{"ds[${field}].min"} ne $host->{'vars'}->{$field}->{'min'}) {
251 RRDs
::tune
("${rrdfile}.${field}","-i",$field.":".$host->{'vars'}->{$field}->{'min'});
254 if ($host->{'vars'}->{$field}->{'min'} ne 'U') {
255 RRDs
::tune
("${rrdfile}.${field}","-i",$field.":".$host->{'vars'}->{$field}->{'min'});
260 print "Error while setting min: " . RRDs
::error
. "\n";
264 if (defined($rrdinfo->{"ds[${field}].max"})) {
265 if ($rrdinfo->{"ds[${field}].max"} ne $host->{'vars'}->{$field}->{'max'}) {
266 RRDs
::tune
("${rrdfile}.${field}","-a",$field.":".$host->{'vars'}->{$field}->{'max'});
269 if ($host->{'vars'}->{$field}->{'max'} ne 'U') {
270 RRDs
::tune
("${rrdfile}.${field}","-a",$field.":".$host->{'vars'}->{$field}->{'max'});
275 print "Error while setting max: " . RRDs
::error
. "\n";
279 if ($rrdinfo->{'rra[0].rows'} != $keep) {
280 print "Resizing ${rrdfile}.${field} from " . $rrdinfo->{'rra[0].rows'} .
281 " to ${keep} samples.\n";
283 (my $start, my $ostep, my $names, my $data) =
284 RRDs
::fetch
("${rrdfile}.${field}",
285 "-s " . (time() - ($rrdinfo->{'rra[0].rows'} * $rrdinfo->{'step'})),
289 print "Error while fetching data: " . RRDs
::error
. "\n";
293 rrdcreate
("${rrdfile}.${field}.new",
296 (${start
}-${ostep
}));
298 print "Preserving data since " . localtime($start) . "\n";
301 foreach my $line (@
$data) {
302 my $vline = "${pos}";
304 foreach my $val (@
$line) {
305 $val = 'U' if (!defined($val));
308 RRDs
::update
("${rrdfile}.${field}.new", $vline) or die "Can't insert data\n";
311 print "Error while updating: " . RRDs
::error
. "\n";
316 if ((($pos-$start)/$ostep) == $#$data) {
321 rename("${rrdfile}.${field}", "${rrdfile}.${field}.old") or die "Can't rename old file: $!\n";
322 rename("${rrdfile}.${field}.new", "${rrdfile}.${field}") or die "Can't rename new file: $!\n";
324 $rrdinfo = RRDs
::info
("${rrdfile}.${field}");
326 print "Error while getting info: " . RRDs
::error
. "\n";
330 if ($rrdinfo->{'rra[0].rows'} != $keep) {
340 die "fork failed!" if (!defined($child));
342 exit 0 if ($child != 0);
345 open(HTML
, ">${outdir}/index.html.new");
347 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>';
348 print HTML
'<body bgcolor="#ffffff">';
350 foreach my $host (@
$hosts) {
351 print HTML
"[<a href=\"#".${host
}->{'name'}."\">".${host
}->{'name'}."</a>] ";
355 foreach my $host (@
$hosts) {
357 print HTML
"<a name=\"".${host
}->{'name'}."\"></a>\n";
358 my $vars = $host->{'vars'};
359 my $rrdfile = $host->{'rrdfile'};
360 my $hostname = $host->{'name'};
361 my %multi_values = ();
363 foreach my $var (@
{$host->{'fields'}}) {
364 delete $vars->{$var}->{'value'};
368 if ((!defined($vars->{$var}->{'proto'})) ||
369 ($vars->{$var}->{'proto'} eq '') ||
370 ($vars->{$var}->{'proto'} eq 'snmp')) {
371 $result = fetch_snmp
($host->{'address'}, $host->{'community'}, $vars->{$var}->{'oid'});
372 } elsif ($vars->{$var}->{'proto'} eq 'tcp') {
373 $result = fetch_tcp
($host->{'address'}, $vars->{$var}->{'port'});
374 } elsif ($vars->{$var}->{'proto'} eq 'tcp_multi') {
375 if (defined($multi_values{$vars->{$var}->{'multi_id'}})) {
376 $result = $multi_values{$vars->{$var}->{'multi_id'}}
378 my %values = fetch_tcp_multi
($host->{'address'}, $vars->{$var}->{'port'}, $vars->{$var}->{'multi_delimiter'});
379 @multi_values{keys %values} = values %values;
380 $result = $multi_values{$vars->{$var}->{'multi_id'}};
384 next unless (defined $result);
386 $vars->{$var}->{'value'} = $result;
387 if (defined($vars->{$var}->{'factor'})) {
388 $vars->{$var}->{'value'} *= $vars->{$var}->{'factor'};
392 foreach my $var (@
{$host->{'fields'}}) {
393 if (!(defined($vars->{$var}->{'value'}))) {
394 $vars->{$var}->{'value'} = 'U';
396 rrd_update
("${rrdfile}.${var}", "N:" . $vars->{$var}->{'value'});
399 print "Error while updating: " . $rrd_result . "\n";
402 foreach my $var (@
{$host->{'fields'}}) {
403 my @graphdef = ('-P', "--lazy", "-t", $hostname." - ".$vars->{$var}->{'name'}, "DEF:${var}=${rrdfile}.${var}:${var}:AVERAGE", "LINE1:${var}#FF0000");
405 push @graphdef, "VDEF:cur=${var},LAST";
406 push @graphdef, 'GPRINT:cur:Current\\: <span foreground="#FF0000">%.2lf</span>\\r';
409 $mtime=(stat("${outdir}/${hostname}.${var}.png.work"))[9];
411 (my $averages, my $width, my $height) =
412 rrd_graph
("${outdir}/${hostname}.${var}.png.work",
413 "-w", "720", @graphdef);
419 print "Error while graphing: " . $rrd_result . "\n";
421 my $newmtime=(stat("${outdir}/${hostname}.${var}.png.work"))[9];
422 if ((!defined($mtime)) || ($newmtime != $mtime)) {
423 copy
("${outdir}/${hostname}.${var}.png.work", "${outdir}/${hostname}.${var}.png.new");
424 rename("${outdir}/${hostname}.${var}.png.new", "${outdir}/${hostname}.${var}.png");
428 print HTML
"<a href=\"${hostname}.${var}.html\"><img src=\"${hostname}.${var}.png\" width=\"${width}\" height=\"${height}\" border=\"0\"></a><br>\n";
430 open (HTML2
, ">${outdir}/${hostname}.${var}.html.new");
431 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>';
432 print HTML2
'<body bgcolor="#ffffff">';
434 push @graphdef, "VDEF:min=${var},MINIMUM";
435 push @graphdef, "GPRINT:min:Minimum\\: %.2lf";
437 push @graphdef, "VDEF:avg=${var},AVERAGE";
438 push @graphdef, "GPRINT:avg:Average\\: %.2lf";
440 push @graphdef, "VDEF:max=${var},MAXIMUM";
441 push @graphdef, "GPRINT:max:Maximum\\: %.2lf";
443 push @graphdef, "VDEF:cur=${var},LAST";
444 push @graphdef, "GPRINT:cur:Current\\: %.2lf";
446 $mtime=(stat("${outdir}/${hostname}.${var}.long.png.work"))[9];
447 ($averages, $width, $height) =
448 rrd_graph
("${outdir}/${hostname}.${var}.long.png.work",
449 "-w", "1008", @graphdef);
452 print "Error while graphing: " . $rrd_result . "\n";
454 my $newmtime=(stat("${outdir}/${hostname}.${var}.long.png.work"))[9];
455 if ((!defined($mtime)) || ($newmtime != $mtime)) {
456 copy
("${outdir}/${hostname}.${var}.long.png.work", "${outdir}/${hostname}.${var}.long.png.new");
457 rename("${outdir}/${hostname}.${var}.long.png.new", "${outdir}/${hostname}.${var}.long.png");
461 print HTML2
"<img src=\"${hostname}.${var}.long.png\" width=\"${width}\" height=\"${height}\"><br>";
463 $mtime=(stat("${outdir}/${hostname}.${var}.week.png.work"))[9];
464 ($averages, $width, $height) =
465 rrd_graph
("${outdir}/${hostname}.${var}.week.png.work",
466 "-w", "1008", "-e", "now", "-s", "end-1w", @graphdef);
469 print "Error while graphing: " . $rrd_result . "\n";
471 my $newmtime=(stat("${outdir}/${hostname}.${var}.week.png.work"))[9];
472 if ((!defined($mtime)) || ($newmtime != $mtime)) {
473 copy
("${outdir}/${hostname}.${var}.week.png.work", "${outdir}/${hostname}.${var}.week.png.new");
474 rename("${outdir}/${hostname}.${var}.week.png.new", "${outdir}/${hostname}.${var}.week.png");
478 print HTML2
"<img src=\"${hostname}.${var}.week.png\" width=\"${width}\" height=\"${height}\"><br>";
480 $mtime=(stat("${outdir}/${hostname}.${var}.year.png.work"))[9];
481 ($averages, $width, $height) =
482 rrd_graph
("${outdir}/${hostname}.${var}.year.png.work",
483 "-w", "1008", "-e", "now", "-s", "end-1y", @graphdef);
486 print "Error while graphing: " . $rrd_result . "\n";
488 my $newmtime=(stat("${outdir}/${hostname}.${var}.year.png.work"))[9];
489 if ((!defined($mtime)) || ($newmtime != $mtime)) {
490 copy
("${outdir}/${hostname}.${var}.year.png.work", "${outdir}/${hostname}.${var}.year.png.new");
491 rename("${outdir}/${hostname}.${var}.year.png.new", "${outdir}/${hostname}.${var}.year.png");
495 print HTML2
"<img src=\"${hostname}.${var}.year.png\" width=\"${width}\" height=\"${height}\"><br>";
497 print HTML2
"</body></html>\n";
499 rename("${outdir}/${hostname}.${var}.html.new", "${outdir}/${hostname}.${var}.html");
503 print HTML
"</body></html>\n";
504 print HTML
"<br>Generated on: " . localtime(time());
505 print HTML
' by <a href="http://git.zerfleddert.de/cgi-bin/gitweb.cgi/upsgraph">upsgraph</a>.';
509 rename("${outdir}/index.html.new", "${outdir}/index.html");