]>
git.zerfleddert.de Git - upsgraph/blob - upsgraph.pl
9887d0c4ab2215e9b20eaecd4abf0cc23de55292
3 if ((@ARGV != 1) && (@ARGV != 2)) {
4 print STDERR
"Syntax: ${0} configfile [uid]\n";
14 $UPSGRAPH::outdir
= "";
16 $UPSGRAPH::keep
= (370*24*60*60)/$UPSGRAPH::step
;
17 $UPSGRAPH::hosts
= ();
19 do $ARGV[0] or die "can't read config: $!";
21 my $outdir = $UPSGRAPH::outdir
;
22 my $step = $UPSGRAPH::step
;
23 my $keep = $UPSGRAPH::keep
;
24 my $hosts = $UPSGRAPH::hosts
;
32 my @cmd = ("${newrrd}", "--step=${step}");
34 if (defined($start)) {
35 push @cmd, "--start=${start}";
38 push @cmd, "DS:${field}:GAUGE:600:" .
39 $vars->{$field}->{'min'} . ":" .
40 $vars->{$field}->{'max'} . " ";
42 push @cmd, "RRA:AVERAGE:0.5:1:${keep}";
46 print "Error while creating: " . RRDs
::error
. "\n";
53 my $community = shift;
56 (my $session, my $error) = Net
::SNMP
->session(Hostname
=> $address,
57 Community
=> $community);
60 print STDERR
"session error: $error";
64 $session->translate(0);
66 my $result = $session->get_request($oid);
70 return undef if (!defined($result));
79 my $sock = IO
::Socket
::INET
->new(PeerAddr
=> $address,
84 return undef if (!$sock);
86 chomp(my $value = <$sock>);
101 print STDERR
"Running as root, please provide UID as 2th argument!\n";
105 print "Running as root, switching to ".$ARGV[1]."\n";
109 foreach my $host (@
$hosts) {
110 my $rrdfile = $host->{'rrdfile'};
112 foreach my $var (keys(%{$host->{'vars'}})) {
113 $host->{'vars'}->{$var}->{'min'} = 'U' if (!defined($host->{'vars'}->{$var}->{'min'}));
114 $host->{'vars'}->{$var}->{'max'} = 'U' if (!defined($host->{'vars'}->{$var}->{'max'}));
117 if (-e
"${rrdfile}") {
118 print "Reading old ${rrdfile} to preserve data...\n";
120 my $rrdinfo = RRDs
::info
("${rrdfile}");
122 print "Error while getting info: " . RRDs
::error
. "\n";
126 (my $start, my $ostep, my $names, my $data) =
127 RRDs
::fetch
("${rrdfile}",
128 "-s " . (time() - ($rrdinfo->{'rra[0].rows'} * $rrdinfo->{'step'})),
132 print "Error while fetching data: " . RRDs
::error
. "\n";
136 foreach my $field (@
$names) {
137 if (! -e
"${rrdfile}.${field}") {
138 rrdcreate
("${rrdfile}.${field}",
141 (${start
}-${ostep
}));
146 foreach my $line (@
$data) {
147 foreach my $field (@
$names) {
148 my $val = shift (@
$line);
149 $val = 'U' if (!defined($val));
151 RRDs
::update
("${rrdfile}.${field}", "${pos}:${val}");
153 print "Can't insert data: " . RRDs
::error
. "\n";
161 if ((($pos-$start)/$ostep) == $#$data) {
166 rename("${rrdfile}", "${rrdfile}.old") or die "Can't rename old file: $!\n";
169 foreach my $field (@
{$host->{'fields'}}) {
170 if (! -e
"${rrdfile}.${field}") {
171 print "Creating ${rrdfile}.${field}...\n";
172 rrdcreate
("${rrdfile}.${field}",
177 my $rrdinfo = RRDs
::info
("${rrdfile}.${field}");
179 print "Error while getting info: " . RRDs
::error
. "\n";
183 if (defined($rrdinfo->{"ds[${field}].min"})) {
184 if ($rrdinfo->{"ds[${field}].min"} ne $host->{'vars'}->{$field}->{'min'}) {
185 RRDs
::tune
("${rrdfile}.${field}","-i",$field.":".$host->{'vars'}->{$field}->{'min'});
188 if ($host->{'vars'}->{$field}->{'min'} ne 'U') {
189 RRDs
::tune
("${rrdfile}.${field}","-i",$field.":".$host->{'vars'}->{$field}->{'min'});
194 print "Error while setting min: " . RRDs
::error
. "\n";
198 if (defined($rrdinfo->{"ds[${field}].max"})) {
199 if ($rrdinfo->{"ds[${field}].max"} ne $host->{'vars'}->{$field}->{'max'}) {
200 RRDs
::tune
("${rrdfile}.${field}","-a",$field.":".$host->{'vars'}->{$field}->{'max'});
203 if ($host->{'vars'}->{$field}->{'max'} ne 'U') {
204 RRDs
::tune
("${rrdfile}.${field}","-a",$field.":".$host->{'vars'}->{$field}->{'max'});
209 print "Error while setting max: " . RRDs
::error
. "\n";
213 if ($rrdinfo->{'rra[0].rows'} != $keep) {
214 print "Resizing ${rrdfile}.${field} from " . $rrdinfo->{'rra[0].rows'} .
215 " to ${keep} samples.\n";
217 (my $start, my $ostep, my $names, my $data) =
218 RRDs
::fetch
("${rrdfile}.${field}",
219 "-s " . (time() - ($rrdinfo->{'rra[0].rows'} * $rrdinfo->{'step'})),
223 print "Error while fetching data: " . RRDs
::error
. "\n";
227 rrdcreate
("${rrdfile}.${field}.new",
230 (${start
}-${ostep
}));
232 print "Preserving data since " . localtime($start) . "\n";
235 foreach my $line (@
$data) {
236 my $vline = "${pos}";
238 foreach my $val (@
$line) {
239 $val = 'U' if (!defined($val));
242 RRDs
::update
("${rrdfile}.${field}.new", $vline) or die "Can't insert data\n";
245 print "Error while updating: " . RRDs
::error
. "\n";
250 if ((($pos-$start)/$ostep) == $#$data) {
255 rename("${rrdfile}.${field}", "${rrdfile}.${field}.old") or die "Can't rename old file: $!\n";
256 rename("${rrdfile}.${field}.new", "${rrdfile}.${field}") or die "Can't rename new file: $!\n";
258 $rrdinfo = RRDs
::info
("${rrdfile}.${field}");
260 print "Error while getting info: " . RRDs
::error
. "\n";
264 if ($rrdinfo->{'rra[0].rows'} != $keep) {
274 die "fork failed!" if (!defined($child));
276 exit 0 if ($child != 0);
279 open(HTML
, ">${outdir}/index.html.new");
281 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>';
282 print HTML
'<body bgcolor="#ffffff">';
284 foreach my $host (@
$hosts) {
285 print HTML
"[<a href=\"#".${host
}->{'name'}."\">".${host
}->{'name'}."</a>] ";
289 foreach my $host (@
$hosts) {
291 print HTML
"<a name=\"".${host
}->{'name'}."\"></a>\n";
292 my $vars = $host->{'vars'};
293 my $rrdfile = $host->{'rrdfile'};
294 my $hostname = $host->{'name'};
296 foreach my $var (@
{$host->{'fields'}}) {
297 delete $vars->{$var}->{'value'};
301 if ((!defined($vars->{$var}->{'proto'})) ||
302 ($vars->{$var}->{'proto'} eq '') ||
303 ($vars->{$var}->{'proto'} eq 'snmp')) {
304 $result = fetch_snmp
($host->{'address'}, $host->{'community'}, $vars->{$var}->{'oid'});
305 } elsif ($vars->{$var}->{'proto'} eq 'tcp') {
306 $result = fetch_tcp
($host->{'address'}, $vars->{$var}->{'port'});
309 next unless (defined $result);
311 $vars->{$var}->{'value'} = $result;
312 if (defined($vars->{$var}->{'factor'})) {
313 $vars->{$var}->{'value'} *= $vars->{$var}->{'factor'};
317 foreach my $var (@
{$host->{'fields'}}) {
318 if (!(defined($vars->{$var}->{'value'}))) {
319 $vars->{$var}->{'value'} = 'U';
321 RRDs
::update
("${rrdfile}.${var}", "N:" . $vars->{$var}->{'value'});
324 print "Error while updating: " . RRDs
::error
. "\n";
327 foreach my $var (@
{$host->{'fields'}}) {
328 my @graphdef = ('-P', "--lazy", "-t", $hostname." - ".$vars->{$var}->{'name'}, "DEF:${var}=${rrdfile}.${var}:${var}:AVERAGE", "LINE1:${var}#FF0000");
330 push @graphdef, "VDEF:cur=${var},LAST";
331 push @graphdef, 'GPRINT:cur:Current\\: <span foreground="#FF0000">%.2lf</span>\\r';
334 $mtime=(stat("${outdir}/${hostname}.${var}.png.work"))[9];
336 (my $averages, my $width, my $height) =
337 RRDs
::graph
("${outdir}/${hostname}.${var}.png.work",
338 "-w", "720", @graphdef);
344 print "Error while graphing: " . RRDs
::error
. "\n";
346 my $newmtime=(stat("${outdir}/${hostname}.${var}.png.work"))[9];
347 if ((!defined($mtime)) || ($newmtime != $mtime)) {
348 copy
("${outdir}/${hostname}.${var}.png.work", "${outdir}/${hostname}.${var}.png.new");
349 rename("${outdir}/${hostname}.${var}.png.new", "${outdir}/${hostname}.${var}.png");
353 print HTML
"<a href=\"${hostname}.${var}.html\"><img src=\"${hostname}.${var}.png\" width=\"${width}\" height=\"${height}\" border=\"0\"></a><br>\n";
355 open (HTML2
, ">${outdir}/${hostname}.${var}.html.new");
356 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>';
357 print HTML2
'<body bgcolor="#ffffff">';
359 push @graphdef, "VDEF:min=${var},MINIMUM";
360 push @graphdef, "GPRINT:min:Minimum\\: %.2lf";
362 push @graphdef, "VDEF:avg=${var},AVERAGE";
363 push @graphdef, "GPRINT:avg:Average\\: %.2lf";
365 push @graphdef, "VDEF:max=${var},MAXIMUM";
366 push @graphdef, "GPRINT:max:Maximum\\: %.2lf";
368 push @graphdef, "VDEF:cur=${var},LAST";
369 push @graphdef, "GPRINT:cur:Current\\: %.2lf";
371 $mtime=(stat("${outdir}/${hostname}.${var}.long.png.work"))[9];
372 ($averages, $width, $height) =
373 RRDs
::graph
("${outdir}/${hostname}.${var}.long.png.work",
374 "-w", "1008", @graphdef);
377 print "Error while graphing: " . RRDs
::error
. "\n";
379 my $newmtime=(stat("${outdir}/${hostname}.${var}.long.png.work"))[9];
380 if ((!defined($mtime)) || ($newmtime != $mtime)) {
381 copy
("${outdir}/${hostname}.${var}.long.png.work", "${outdir}/${hostname}.${var}.long.png.new");
382 rename("${outdir}/${hostname}.${var}.long.png.new", "${outdir}/${hostname}.${var}.long.png");
386 print HTML2
"<img src=\"${hostname}.${var}.long.png\" width=\"${width}\" height=\"${height}\"><br>";
388 $mtime=(stat("${outdir}/${hostname}.${var}.week.png.work"))[9];
389 ($averages, $width, $height) =
390 RRDs
::graph
("${outdir}/${hostname}.${var}.week.png.work",
391 "-w", "1008", "-e", "now", "-s", "end-1w", @graphdef);
394 print "Error while graphing: " . RRDs
::error
. "\n";
396 my $newmtime=(stat("${outdir}/${hostname}.${var}.week.png.work"))[9];
397 if ((!defined($mtime)) || ($newmtime != $mtime)) {
398 copy
("${outdir}/${hostname}.${var}.week.png.work", "${outdir}/${hostname}.${var}.week.png.new");
399 rename("${outdir}/${hostname}.${var}.week.png.new", "${outdir}/${hostname}.${var}.week.png");
403 print HTML2
"<img src=\"${hostname}.${var}.week.png\" width=\"${width}\" height=\"${height}\"><br>";
405 $mtime=(stat("${outdir}/${hostname}.${var}.year.png.work"))[9];
406 ($averages, $width, $height) =
407 RRDs
::graph
("${outdir}/${hostname}.${var}.year.png.work",
408 "-w", "1008", "-e", "now", "-s", "end-1y", @graphdef);
411 print "Error while graphing: " . RRDs
::error
. "\n";
413 my $newmtime=(stat("${outdir}/${hostname}.${var}.year.png.work"))[9];
414 if ((!defined($mtime)) || ($newmtime != $mtime)) {
415 copy
("${outdir}/${hostname}.${var}.year.png.work", "${outdir}/${hostname}.${var}.year.png.new");
416 rename("${outdir}/${hostname}.${var}.year.png.new", "${outdir}/${hostname}.${var}.year.png");
420 print HTML2
"<img src=\"${hostname}.${var}.year.png\" width=\"${width}\" height=\"${height}\"><br>";
422 print HTML2
"</body></html>\n";
424 rename("${outdir}/${hostname}.${var}.html.new", "${outdir}/${hostname}.${var}.html");
428 print HTML
"</body></html>\n";
429 print HTML
"<br>Generated on: " . localtime(time());
430 print HTML
' by <a href="http://git.zerfleddert.de/cgi-bin/gitweb.cgi/upsgraph">upsgraph</a>.';
434 rename("${outdir}/index.html.new", "${outdir}/index.html");