]>
git.zerfleddert.de Git - upsgraph/blob - upsgraph.pl
91ae5adb99c055bf04f5e0b8e0ca4a876240e8a4
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 if (-e
"${rrdfile}") {
111 print "Reading old ${rrdfile} to preserve data...\n";
113 my $rrdinfo = RRDs
::info
("${rrdfile}");
115 print "Error while getting info: " . RRDs
::error
. "\n";
119 (my $start, my $ostep, my $names, my $data) =
120 RRDs
::fetch
("${rrdfile}",
121 "-s " . (time() - ($rrdinfo->{'rra[0].rows'} * $rrdinfo->{'step'})),
125 print "Error while fetching data: " . RRDs
::error
. "\n";
129 foreach my $field (@
$names) {
130 if (! -e
"${rrdfile}.${field}") {
131 rrdcreate
("${rrdfile}.${field}",
134 (${start
}-${ostep
}));
139 foreach my $line (@
$data) {
140 foreach my $field (@
$names) {
141 my $val = shift (@
$line);
142 $val = 'U' if (!defined($val));
144 RRDs
::update
("${rrdfile}.${field}", "${pos}:${val}");
146 print "Can't insert data: " . RRDs
::error
. "\n";
154 if ((($pos-$start)/$ostep) == $#$data) {
159 rename("${rrdfile}", "${rrdfile}.old") or die "Can't rename old file: $!\n";
162 foreach my $field (@
{$host->{'fields'}}) {
163 if (! -e
"${rrdfile}.${field}") {
164 print "Creating ${rrdfile}.${field}...\n";
165 rrdcreate
("${rrdfile}.${field}",
170 my $rrdinfo = RRDs
::info
("${rrdfile}.${field}");
172 print "Error while getting info: " . RRDs
::error
. "\n";
176 if (defined($rrdinfo->{"ds[${field}].min"})) {
177 if ($rrdinfo->{"ds[${field}].min"} ne $host->{'vars'}->{$field}->{'min'}) {
178 RRDs
::tune
("${rrdfile}.${field}","-i",$field.":".$host->{'vars'}->{$field}->{'min'});
181 if ($host->{'vars'}->{$field}->{'min'} ne 'U') {
182 RRDs
::tune
("${rrdfile}.${field}","-i",$field.":".$host->{'vars'}->{$field}->{'min'});
187 print "Error while setting min: " . RRDs
::error
. "\n";
191 if (defined($rrdinfo->{"ds[${field}].max"})) {
192 if ($rrdinfo->{"ds[${field}].max"} ne $host->{'vars'}->{$field}->{'max'}) {
193 print "Max differs\n";
194 RRDs
::tune
("${rrdfile}.${field}","-a",$field.":".$host->{'vars'}->{$field}->{'max'});
197 if ($host->{'vars'}->{$field}->{'max'} ne 'U') {
198 RRDs
::tune
("${rrdfile}.${field}","-a",$field.":".$host->{'vars'}->{$field}->{'max'});
203 print "Error while setting max: " . RRDs
::error
. "\n";
207 if ($rrdinfo->{'rra[0].rows'} != $keep) {
208 print "Resizing ${rrdfile}.${field} from " . $rrdinfo->{'rra[0].rows'} .
209 " to ${keep} samples.\n";
211 (my $start, my $ostep, my $names, my $data) =
212 RRDs
::fetch
("${rrdfile}.${field}",
213 "-s " . (time() - ($rrdinfo->{'rra[0].rows'} * $rrdinfo->{'step'})),
217 print "Error while fetching data: " . RRDs
::error
. "\n";
221 rrdcreate
("${rrdfile}.${field}.new",
224 (${start
}-${ostep
}));
226 print "Preserving data since " . localtime($start) . "\n";
229 foreach my $line (@
$data) {
230 my $vline = "${pos}";
232 foreach my $val (@
$line) {
233 $val = 'U' if (!defined($val));
236 RRDs
::update
("${rrdfile}.${field}.new", $vline) or die "Can't insert data\n";
239 print "Error while updating: " . RRDs
::error
. "\n";
244 if ((($pos-$start)/$ostep) == $#$data) {
249 rename("${rrdfile}.${field}", "${rrdfile}.${field}.old") or die "Can't rename old file: $!\n";
250 rename("${rrdfile}.${field}.new", "${rrdfile}.${field}") or die "Can't rename new file: $!\n";
252 $rrdinfo = RRDs
::info
("${rrdfile}.${field}");
254 print "Error while getting info: " . RRDs
::error
. "\n";
258 if ($rrdinfo->{'rra[0].rows'} != $keep) {
268 die "fork failed!" if (!defined($child));
270 exit 0 if ($child != 0);
273 open(HTML
, ">${outdir}/index.html.new");
275 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>';
276 print HTML
'<body bgcolor="#ffffff">';
278 foreach my $host (@
$hosts) {
279 print HTML
"[<a href=\"#".${host
}->{'name'}."\">".${host
}->{'name'}."</a>] ";
283 foreach my $host (@
$hosts) {
285 print HTML
"<a name=\"".${host
}->{'name'}."\"></a>\n";
286 my $vars = $host->{'vars'};
287 my $rrdfile = $host->{'rrdfile'};
288 my $hostname = $host->{'name'};
290 foreach my $var (@
{$host->{'fields'}}) {
291 delete $vars->{$var}->{'value'};
295 if ((!defined($vars->{$var}->{'proto'})) ||
296 ($vars->{$var}->{'proto'} eq '') ||
297 ($vars->{$var}->{'proto'} eq 'snmp')) {
298 $result = fetch_snmp
($host->{'address'}, $host->{'community'}, $vars->{$var}->{'oid'});
299 } elsif ($vars->{$var}->{'proto'} eq 'tcp') {
300 $result = fetch_tcp
($host->{'address'}, $vars->{$var}->{'port'});
303 next unless (defined $result);
305 $vars->{$var}->{'value'} = $result;
306 if (defined($vars->{$var}->{'factor'})) {
307 $vars->{$var}->{'value'} *= $vars->{$var}->{'factor'};
311 foreach my $var (@
{$host->{'fields'}}) {
312 if (!(defined($vars->{$var}->{'value'}))) {
313 $vars->{$var}->{'value'} = 'U';
315 RRDs
::update
("${rrdfile}.${var}", "N:" . $vars->{$var}->{'value'});
318 print "Error while updating: " . RRDs
::error
. "\n";
321 foreach my $var (@
{$host->{'fields'}}) {
322 my @graphdef = ("-t", $hostname." - ".$vars->{$var}->{'name'}, "DEF:${var}=${rrdfile}.${var}:${var}:AVERAGE", "LINE1:${var}#FF0000");
323 (my $averages, my $width, my $height) =
324 RRDs
::graph
("${outdir}/${hostname}.${var}.png.new",
325 "-w", "720", @graphdef);
328 print "Error while graphing: " . RRDs
::error
. "\n";
330 rename("${outdir}/${hostname}.${var}.png.new", "${outdir}/${hostname}.${var}.png");
333 print HTML
"<a href=\"${hostname}.${var}.html\"><img src=\"${hostname}.${var}.png\" width=\"${width}\" height=\"${height}\" border=\"0\"></a><br>\n";
335 open (HTML2
, ">${outdir}/${hostname}.${var}.html.new");
336 print HTML2
"<html><head><title>" . $vars->{$var}->{'name'} . "</title></head>";
337 print HTML2
'<body bgcolor="#ffffff">';
340 push @graphdef, "VDEF:min=${var},MINIMUM";
341 push @graphdef, "GPRINT:min:Minimum\\: %.2lf";
343 push @graphdef, "VDEF:avg=${var},AVERAGE";
344 push @graphdef, "GPRINT:avg:Average\\: %.2lf";
346 push @graphdef, "VDEF:max=${var},MAXIMUM";
347 push @graphdef, "GPRINT:max:Maximum\\: %.2lf";
349 push @graphdef, "VDEF:cur=${var},LAST";
350 push @graphdef, "GPRINT:cur:Current\\: %.2lf";
352 ($averages, $width, $height) =
353 RRDs
::graph
("${outdir}/${hostname}.${var}.long.png.new",
354 "-w", "1008", @graphdef);
357 print "Error while graphing: " . RRDs
::error
. "\n";
359 rename("${outdir}/${hostname}.${var}.long.png.new", "${outdir}/${hostname}.${var}.long.png");
362 print HTML2
"<img src=\"${hostname}.${var}.long.png\" width=\"${width}\" height=\"${height}\"><br>";
364 ($averages, $width, $height) =
365 RRDs
::graph
("${outdir}/${hostname}.${var}.week.png.new",
366 "-w", "1008", "-e", "now", "-s", "end-1w", @graphdef);
369 print "Error while graphing: " . RRDs
::error
. "\n";
371 rename("${outdir}/${hostname}.${var}.week.png.new", "${outdir}/${hostname}.${var}.week.png");
374 print HTML2
"<img src=\"${hostname}.${var}.week.png\" width=\"${width}\" height=\"${height}\"><br>";
376 ($averages, $width, $height) =
377 RRDs
::graph
("${outdir}/${hostname}.${var}.year.png.new",
378 "-w", "1008", "-e", "now", "-s", "end-1y", @graphdef);
381 print "Error while graphing: " . RRDs
::error
. "\n";
383 rename("${outdir}/${hostname}.${var}.year.png.new", "${outdir}/${hostname}.${var}.year.png");
386 print HTML2
"<img src=\"${hostname}.${var}.year.png\" width=\"${width}\" height=\"${height}\"><br>";
388 print HTML2
"</body></html>\n";
390 rename("${outdir}/${hostname}.${var}.html.new", "${outdir}/${hostname}.${var}.html");
394 print HTML
"</body></html>\n";
395 print HTML
"<br>Generated on: " . localtime(time());
396 print HTML
' by <a href="http://git.zerfleddert.de/cgi-bin/gitweb.cgi/upsgraph">upsgraph</a>.';
400 rename("${outdir}/index.html.new", "${outdir}/index.html");