]>
git.zerfleddert.de Git - upsgraph/blob - upsgraph.pl
c61b6c0bbad27d56004498dfa452571642fed401
3 if ((@ARGV != 1) && (@ARGV != 2)) {
4 print STDERR
"Syntax: ${0} configfile [uid]\n";
13 $UPSGRAPH::outdir
= "";
15 $UPSGRAPH::keep
= (370*24*60*60)/$UPSGRAPH::step
;
16 $UPSGRAPH::hosts
= ();
18 do $ARGV[0] or die "can't read config: $!";
20 my $outdir = $UPSGRAPH::outdir
;
21 my $step = $UPSGRAPH::step
;
22 my $keep = $UPSGRAPH::keep
;
23 my $hosts = $UPSGRAPH::hosts
;
31 my @cmd = ("${newrrd}", "--step=${step}");
33 if (defined($start)) {
34 push @cmd, "--start=${start}";
37 push @cmd, "DS:${field}:GAUGE:600:" .
38 $vars->{$field}->{'min'} . ":" .
39 $vars->{$field}->{'max'} . " ";
41 push @cmd, "RRA:AVERAGE:0.5:1:${keep}";
45 print "Error while creating: " . RRDs
::error
. "\n";
52 my $community = shift;
55 (my $session, my $error) = Net
::SNMP
->session(Hostname
=> $address,
56 Community
=> $community);
59 print STDERR
"session error: $error";
62 $session->translate(0);
64 my $result = $session->get_request($oid);
68 return undef if (!defined($result));
77 my $sock = IO
::Socket
::INET
->new(PeerAddr
=> $address,
82 return undef if (!$sock);
84 chomp(my $value = <$sock>);
99 print STDERR
"Running as root, please provide UID as 2th argument!\n";
103 print "Running as root, switching to ".$ARGV[1]."\n";
107 foreach my $host (@
$hosts) {
108 my $rrdfile = $host->{'rrdfile'};
110 foreach my $var (keys(%{$host->{'vars'}})) {
111 $host->{'vars'}->{$var}->{'min'} = 'U' if (!defined($host->{'vars'}->{$var}->{'min'}));
112 $host->{'vars'}->{$var}->{'max'} = 'U' if (!defined($host->{'vars'}->{$var}->{'max'}));
115 if (-e
"${rrdfile}") {
116 print "Reading old ${rrdfile} to preserve data...\n";
118 my $rrdinfo = RRDs
::info
("${rrdfile}");
120 print "Error while getting info: " . RRDs
::error
. "\n";
124 (my $start, my $ostep, my $names, my $data) =
125 RRDs
::fetch
("${rrdfile}",
126 "-s " . (time() - ($rrdinfo->{'rra[0].rows'} * $rrdinfo->{'step'})),
130 print "Error while fetching data: " . RRDs
::error
. "\n";
134 foreach my $field (@
$names) {
135 if (! -e
"${rrdfile}.${field}") {
136 rrdcreate
("${rrdfile}.${field}",
139 (${start
}-${ostep
}));
144 foreach my $line (@
$data) {
145 foreach my $field (@
$names) {
146 my $val = shift (@
$line);
147 $val = 'U' if (!defined($val));
149 RRDs
::update
("${rrdfile}.${field}", "${pos}:${val}");
151 print "Can't insert data: " . RRDs
::error
. "\n";
159 if ((($pos-$start)/$ostep) == $#$data) {
164 rename("${rrdfile}", "${rrdfile}.old") or die "Can't rename old file: $!\n";
167 foreach my $field (@
{$host->{'fields'}}) {
168 if (! -e
"${rrdfile}.${field}") {
169 print "Creating ${rrdfile}.${field}...\n";
170 rrdcreate
("${rrdfile}.${field}",
175 my $rrdinfo = RRDs
::info
("${rrdfile}.${field}");
177 print "Error while getting info: " . RRDs
::error
. "\n";
181 if (defined($rrdinfo->{"ds[${field}].min"})) {
182 if ($rrdinfo->{"ds[${field}].min"} ne $host->{'vars'}->{$field}->{'min'}) {
183 RRDs
::tune
("${rrdfile}.${field}","-i",$field.":".$host->{'vars'}->{$field}->{'min'});
186 if ($host->{'vars'}->{$field}->{'min'} ne 'U') {
187 RRDs
::tune
("${rrdfile}.${field}","-i",$field.":".$host->{'vars'}->{$field}->{'min'});
192 print "Error while setting min: " . RRDs
::error
. "\n";
196 if (defined($rrdinfo->{"ds[${field}].max"})) {
197 if ($rrdinfo->{"ds[${field}].max"} ne $host->{'vars'}->{$field}->{'max'}) {
198 RRDs
::tune
("${rrdfile}.${field}","-a",$field.":".$host->{'vars'}->{$field}->{'max'});
201 if ($host->{'vars'}->{$field}->{'max'} ne 'U') {
202 RRDs
::tune
("${rrdfile}.${field}","-a",$field.":".$host->{'vars'}->{$field}->{'max'});
207 print "Error while setting max: " . RRDs
::error
. "\n";
211 if ($rrdinfo->{'rra[0].rows'} != $keep) {
212 print "Resizing ${rrdfile}.${field} from " . $rrdinfo->{'rra[0].rows'} .
213 " to ${keep} samples.\n";
215 (my $start, my $ostep, my $names, my $data) =
216 RRDs
::fetch
("${rrdfile}.${field}",
217 "-s " . (time() - ($rrdinfo->{'rra[0].rows'} * $rrdinfo->{'step'})),
221 print "Error while fetching data: " . RRDs
::error
. "\n";
225 rrdcreate
("${rrdfile}.${field}.new",
228 (${start
}-${ostep
}));
230 print "Preserving data since " . localtime($start) . "\n";
233 foreach my $line (@
$data) {
234 my $vline = "${pos}";
236 foreach my $val (@
$line) {
237 $val = 'U' if (!defined($val));
240 RRDs
::update
("${rrdfile}.${field}.new", $vline) or die "Can't insert data\n";
243 print "Error while updating: " . RRDs
::error
. "\n";
248 if ((($pos-$start)/$ostep) == $#$data) {
253 rename("${rrdfile}.${field}", "${rrdfile}.${field}.old") or die "Can't rename old file: $!\n";
254 rename("${rrdfile}.${field}.new", "${rrdfile}.${field}") or die "Can't rename new file: $!\n";
256 $rrdinfo = RRDs
::info
("${rrdfile}.${field}");
258 print "Error while getting info: " . RRDs
::error
. "\n";
262 if ($rrdinfo->{'rra[0].rows'} != $keep) {
272 die "fork failed!" if (!defined($child));
274 exit 0 if ($child != 0);
277 open(HTML
, ">${outdir}/index.html.new");
279 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>';
280 print HTML
'<body bgcolor="#ffffff">';
282 foreach my $host (@
$hosts) {
283 print HTML
"[<a href=\"#".${host
}->{'name'}."\">".${host
}->{'name'}."</a>] ";
287 foreach my $host (@
$hosts) {
289 print HTML
"<a name=\"".${host
}->{'name'}."\"></a>\n";
290 my $vars = $host->{'vars'};
291 my $rrdfile = $host->{'rrdfile'};
292 my $hostname = $host->{'name'};
294 foreach my $var (@
{$host->{'fields'}}) {
295 delete $vars->{$var}->{'value'};
299 if ((!defined($vars->{$var}->{'proto'})) ||
300 ($vars->{$var}->{'proto'} eq '') ||
301 ($vars->{$var}->{'proto'} eq 'snmp')) {
302 $result = fetch_snmp
($host->{'address'}, $host->{'community'}, $vars->{$var}->{'oid'});
303 } elsif ($vars->{$var}->{'proto'} eq 'tcp') {
304 $result = fetch_tcp
($host->{'address'}, $vars->{$var}->{'port'});
307 next unless (defined $result);
309 $vars->{$var}->{'value'} = $result;
310 if (defined($vars->{$var}->{'factor'})) {
311 $vars->{$var}->{'value'} *= $vars->{$var}->{'factor'};
315 foreach my $var (@
{$host->{'fields'}}) {
316 if (!(defined($vars->{$var}->{'value'}))) {
317 $vars->{$var}->{'value'} = 'U';
319 RRDs
::update
("${rrdfile}.${var}", "N:" . $vars->{$var}->{'value'});
322 print "Error while updating: " . RRDs
::error
. "\n";
325 foreach my $var (@
{$host->{'fields'}}) {
326 my @graphdef = ("-t", $hostname." - ".$vars->{$var}->{'name'}, "DEF:${var}=${rrdfile}.${var}:${var}:AVERAGE", "LINE1:${var}#FF0000");
327 (my $averages, my $width, my $height) =
328 RRDs
::graph
("${outdir}/${hostname}.${var}.png.new",
329 "-w", "720", @graphdef);
332 print "Error while graphing: " . RRDs
::error
. "\n";
334 rename("${outdir}/${hostname}.${var}.png.new", "${outdir}/${hostname}.${var}.png");
337 print HTML
"<a href=\"${hostname}.${var}.html\"><img src=\"${hostname}.${var}.png\" width=\"${width}\" height=\"${height}\" border=\"0\"></a><br>\n";
339 open (HTML2
, ">${outdir}/${hostname}.${var}.html.new");
340 print HTML2
"<html><head><title>" . $vars->{$var}->{'name'} . "</title></head>";
341 print HTML2
'<body bgcolor="#ffffff">';
344 push @graphdef, "VDEF:min=${var},MINIMUM";
345 push @graphdef, "GPRINT:min:Minimum\\: %.2lf";
347 push @graphdef, "VDEF:avg=${var},AVERAGE";
348 push @graphdef, "GPRINT:avg:Average\\: %.2lf";
350 push @graphdef, "VDEF:max=${var},MAXIMUM";
351 push @graphdef, "GPRINT:max:Maximum\\: %.2lf";
353 push @graphdef, "VDEF:cur=${var},LAST";
354 push @graphdef, "GPRINT:cur:Current\\: %.2lf";
356 ($averages, $width, $height) =
357 RRDs
::graph
("${outdir}/${hostname}.${var}.long.png.new",
358 "-w", "1008", @graphdef);
361 print "Error while graphing: " . RRDs
::error
. "\n";
363 rename("${outdir}/${hostname}.${var}.long.png.new", "${outdir}/${hostname}.${var}.long.png");
366 print HTML2
"<img src=\"${hostname}.${var}.long.png\" width=\"${width}\" height=\"${height}\"><br>";
368 ($averages, $width, $height) =
369 RRDs
::graph
("${outdir}/${hostname}.${var}.week.png.new",
370 "-w", "1008", "-e", "now", "-s", "end-1w", @graphdef);
373 print "Error while graphing: " . RRDs
::error
. "\n";
375 rename("${outdir}/${hostname}.${var}.week.png.new", "${outdir}/${hostname}.${var}.week.png");
378 print HTML2
"<img src=\"${hostname}.${var}.week.png\" width=\"${width}\" height=\"${height}\"><br>";
380 ($averages, $width, $height) =
381 RRDs
::graph
("${outdir}/${hostname}.${var}.year.png.new",
382 "-w", "1008", "-e", "now", "-s", "end-1y", @graphdef);
385 print "Error while graphing: " . RRDs
::error
. "\n";
387 rename("${outdir}/${hostname}.${var}.year.png.new", "${outdir}/${hostname}.${var}.year.png");
390 print HTML2
"<img src=\"${hostname}.${var}.year.png\" width=\"${width}\" height=\"${height}\"><br>";
392 print HTML2
"</body></html>\n";
394 rename("${outdir}/${hostname}.${var}.html.new", "${outdir}/${hostname}.${var}.html");
398 print HTML
"</body></html>\n";
399 print HTML
"<br>Generated on: " . localtime(time());
400 print HTML
' by <a href="http://git.zerfleddert.de/cgi-bin/gitweb.cgi/upsgraph">upsgraph</a>.';
404 rename("${outdir}/index.html.new", "${outdir}/index.html");