Update test.conf to include daily average graphs
[upsgraph] / upsgraph.pl
CommitLineData
e18c93b3
MG
1#!/usr/bin/perl -w
2
f49c04f7
MG
3#Due to memory leak in Debian squeeze (Bug #545519)
4my $use_rrds = 0;
5my $rrd_result = 0;
6
1c9fb9df
MG
7if ((@ARGV != 1) && (@ARGV != 2)) {
8 print STDERR "Syntax: ${0} configfile [uid]\n";
e18c93b3
MG
9 exit(1);
10}
11
1c9fb9df 12use Net::SNMP;
0e572cdc 13use IO::Socket::INET;
3679b927 14use IO::Select;
e5da3876 15use RRDs;
e26b065f 16use File::Copy;
e5da3876 17use Data::Dumper;
e18c93b3 18
1c9fb9df 19$UPSGRAPH::outdir = "";
3d7df03f 20$UPSGRAPH::daysCovered = 370;
1c9fb9df 21$UPSGRAPH::step = 60;
3d7df03f
BO
22$UPSGRAPH::stepsPerHour = (60 * 60) / $UPSGRAPH::step;
23$UPSGRAPH::keep = ($UPSGRAPH::daysCovered*24*60*60)/$UPSGRAPH::step;
24$UPSGRAPH::keepHours = ($UPSGRAPH::daysCovered*24*60*60)/$UPSGRAPH::stepsPerHour/$UPSGRAPH::step;
6c1071ee 25$UPSGRAPH::hosts = ();
63a9aad6 26$UPSGRAPH::regenerateOnStart = 1; #when set, regenerate graphs on script startup
e18c93b3 27
1c9fb9df 28do $ARGV[0] or die "can't read config: $!";
e18c93b3 29
1c9fb9df 30my $outdir = $UPSGRAPH::outdir;
1c9fb9df 31my $step = $UPSGRAPH::step;
3d7df03f 32my $stepsPerHour = $UPSGRAPH::stepsPerHour;
e5da3876 33my $keep = $UPSGRAPH::keep;
3d7df03f 34my $keepHours = $UPSGRAPH::keepHours;
6c1071ee 35my $hosts = $UPSGRAPH::hosts;
e18c93b3 36
f49c04f7
MG
37sub rrd_update(@) {
38 my @args = @_;
f3f486e1 39 print "Updating: ". Dumper(\@_);
f49c04f7
MG
40 if ($use_rrds == 1) {
41 RRDs::update(@args);
42 $rrd_result = RRDs::error;
43 } else {
44 $rrd_result = system("rrdtool", "update", @args);
45 }
46}
47
48sub rrd_graph(@) {
49 my @args = @_;
50 my @rrd_out = ();
f3f486e1 51 print "Graphing: " . Dumper(\@_);
f49c04f7
MG
52 if ($use_rrds == 1) {
53 @rrd_out = RRDs::graph(@args);
54 $rrd_result = RRDs::error;
55 } else {
56 my $rrd_stdout;
57
58 open(RRDFD, '-|', 'rrdtool', 'graph', @args);
59 while(<RRDFD>) {
60 chomp;
61 $rrd_stdout = $_;
62 }
63 close(RRDFD);
64 $rrd_result = $?;
65 if ($rrd_result == 0) {
66 push @rrd_out, 0;
67 push @rrd_out, split(/x/, $rrd_stdout);
68 }
69 }
70
71 return @rrd_out;
72}
73
e5da3876 74sub rrdcreate(@) {
21377f43
MG
75 my $newrrd = shift;
76 my $field = shift;
6c1071ee 77 my $vars = shift;
e5da3876
MG
78 my $start = shift;
79
21377f43 80 my @cmd = ("${newrrd}", "--step=${step}");
e5da3876
MG
81
82 if (defined($start)) {
83 push @cmd, "--start=${start}";
84 }
85
21377f43
MG
86 push @cmd, "DS:${field}:GAUGE:600:" .
87 $vars->{$field}->{'min'} . ":" .
f447c2a1 88 $vars->{$field}->{'max'};
21377f43 89
e5da3876 90 push @cmd, "RRA:AVERAGE:0.5:1:${keep}";
3d7df03f
BO
91 push @cmd, "RRA:MIN:0.4:${stepsPerHour}:${keepHours}";
92 push @cmd, "RRA:MAX:0.4:${stepsPerHour}:${keepHours}";
0bd26ed9 93 push @cmd, "RRA:AVERAGE:0.5:${stepsPerHour}:${keepHours}";
e5da3876
MG
94
95 RRDs::create(@cmd);
96 if (RRDs::error) {
97 print "Error while creating: " . RRDs::error . "\n";
f3f486e1 98 print Dumper(\@cmd);
e5da3876
MG
99 exit 1;
100 }
101}
102
6c1071ee
MG
103sub fetch_snmp(@) {
104 my $address = shift;
105 my $community = shift;
106 my $oid = shift;
e5da3876 107
6c1071ee
MG
108 (my $session, my $error) = Net::SNMP->session(Hostname => $address,
109 Community => $community);
e18c93b3 110
0e572cdc
MG
111 if (!$session) {
112 print STDERR "session error: $error";
e0acc35a 113 return undef;
0e572cdc 114 }
e5da3876 115
6c1071ee 116 $session->translate(0);
e5da3876 117
0e572cdc 118 my $result = $session->get_request($oid);
e5da3876 119
6c1071ee 120 $session->close;
21377f43 121
0e572cdc
MG
122 return undef if (!defined($result));
123
124 $result->{$oid};
125}
126
127sub fetch_tcp(@) {
128 my $address = shift;
129 my $port = shift;
130
131 my $sock = IO::Socket::INET->new(PeerAddr => $address,
132 PeerPort => $port,
133 Proto => 'tcp',
134 Timeout => 1);
135
136 return undef if (!$sock);
137
3679b927
MG
138 my $select = IO::Select->new($sock);
139
140 my $value = undef;
141
142 if ($select->can_read(1)) {
143 chomp($value) if (sysread($sock, $value, 4096) > 0);
144 }
0e572cdc
MG
145
146 close($sock);
147
148 if (!$value) {
149 return undef;
150 }
151
152 $value=~ s/\s//g;
153
154 $value;
6c1071ee 155}
e5da3876 156
b57167a1
MG
157sub fetch_tcp_multi(@) {
158 my $address = shift;
159 my $port = shift;
160 my $delimiter = shift;
161 my %values;
162
163 my $sock = IO::Socket::INET->new(PeerAddr => $address,
164 PeerPort => $port,
165 Proto => 'tcp',
166 Timeout => 1);
167
168 return undef if (!$sock);
169
3679b927
MG
170 my $select = IO::Select->new($sock);
171
172 while($select->can_read(1)) {
173 if (sysread($sock, my $buf, 16384) > 0) {
174 $buf=~s/\r//g;
175 foreach my $line (split(/\n/, $buf)) {
176 (my $key, my $value) = split(/${delimiter}/, $line);
177 $value=~ s/\s//g;
178 $values{$key} = $value;
179 }
180 } else {
181 last;
182 }
b57167a1
MG
183 }
184
185 close($sock);
186
187 %values;
188}
189
fd198fcd
BO
190sub dayGraphFunc {
191 my $dataSrc = shift;
192 my $mode = shift;
193 my $color = shift;
194 my $label = shift;
195 my $dataPoints = shift;
196 my @args = ();
197 push @args, "CDEF:prev${mode}1=PREV(${dataSrc})";
198 for (my $i = 1; $i < $dataPoints - 1; ++$i) {
199 my $prev = $i;
200 my $next = $i+1;
201 push @args, "CDEF:prev${mode}${next}=PREV(prev${mode}${prev})";
202 }
203 my $dayCons = '';
204 my $consFunc = '';
0bd26ed9
BO
205 if ($mode ne 'AVG') {
206 for (my $i = 1; $i < $dataPoints; ++$i) {
207 $dayCons .= "prev${mode}${i},";
208 $consFunc .= ",${mode}";
209 }
210 } else {
211 for (my $i = 1; $i < $dataPoints; ++$i) {
212 $dayCons .= "prev${mode}${i},";
213 }
214 $consFunc = ",${dataPoints},${mode}";
fd198fcd
BO
215 }
216 push @args, "CDEF:day${mode}=${dayCons}${dataSrc}${consFunc}";
217 push @args, "CDEF:fillCalDay${mode}0=COUNT,${dataPoints},%,0,EQ,day${mode},UNKN,IF";
218 for (my $i = 1; $i < $dataPoints; ++$i) {
219 my $prev = $i-1;
220 my $next = $i;
221 push @args, "CDEF:fillCalDay${mode}${next}=PREV(fillCalDay${mode}${prev})";
222 }
223 my $fillPoint = '';
224 my $if = '';
225 for (my $i = 0; $i < $dataPoints; ++$i) {
226 $fillPoint .= "COUNT,${dataPoints},%,${i},EQ,fillCalDay${mode}${i},";
227 $if .= ",IF";
228 }
229 push @args, "CDEF:${mode}Curve=${fillPoint}UNKN${if}";
230 my $forwardShift = (24*60*60) * ($dataPoints - 1) / $dataPoints;
231 push @args, "SHIFT:${mode}Curve:-${forwardShift}";
232 push @args, "LINE1:${mode}Curve#${color}:${label}";
233 return \@args;
234}
235
235b08ab
BO
236sub cfgToGraphDef {
237 my $cfg = shift;
238 my $varname = shift;
239 my $dpPerDay= shift;
240 my @graphDef = ();
241 foreach my $subGraph (@$cfg) {
242 if ($subGraph eq 'avg') {
243 push @graphDef, "LINE1:${varname}-avg#FF0000";
244 } elsif ($subGraph eq 'day-min') {
245 push @graphDef, @{dayGraphFunc("${varname}-min", 'MIN', '0000ff', 'Day Minimum Temperature', $dpPerDay)};
246 } elsif ($subGraph eq 'day-max') {
247 push @graphDef, @{dayGraphFunc("${varname}-max", 'MAX', '00ff00', 'Day Maximum Temperature', $dpPerDay)};
0bd26ed9
BO
248 } elsif ($subGraph eq 'day-avg') {
249 push @graphDef, @{dayGraphFunc("${varname}-houravg", 'AVG', 'ff0000', 'Day Average Temperature', $dpPerDay)};
235b08ab
BO
250 }
251 }
252 return \@graphDef;
253}
254
6c1071ee
MG
255if ($> == 0) {
256 if (@ARGV != 2) {
257 print STDERR "Running as root, please provide UID as 2th argument!\n";
258 exit(1);
e5da3876
MG
259 }
260
6c1071ee
MG
261 print "Running as root, switching to ".$ARGV[1]."\n";
262 $< = $> = $ARGV[1];
21377f43 263}
e5da3876 264
6c1071ee
MG
265foreach my $host (@$hosts) {
266 my $rrdfile = $host->{'rrdfile'};
21377f43 267
46ead44b
MG
268 foreach my $var (keys(%{$host->{'vars'}})) {
269 $host->{'vars'}->{$var}->{'min'} = 'U' if (!defined($host->{'vars'}->{$var}->{'min'}));
270 $host->{'vars'}->{$var}->{'max'} = 'U' if (!defined($host->{'vars'}->{$var}->{'max'}));
271 }
272
6c1071ee
MG
273 if (-e "${rrdfile}") {
274 print "Reading old ${rrdfile} to preserve data...\n";
e5da3876 275
6c1071ee
MG
276 my $rrdinfo = RRDs::info("${rrdfile}");
277 if (RRDs::error) {
278 print "Error while getting info: " . RRDs::error . "\n";
279 exit 1;
280 }
21377f43
MG
281
282 (my $start, my $ostep, my $names, my $data) =
6c1071ee 283 RRDs::fetch("${rrdfile}",
21377f43
MG
284 "-s " . (time() - ($rrdinfo->{'rra[0].rows'} * $rrdinfo->{'step'})),
285 "AVERAGE");
286
287 if (RRDs::error) {
288 print "Error while fetching data: " . RRDs::error . "\n";
289 exit 1;
290 }
291
6c1071ee
MG
292 foreach my $field (@$names) {
293 if (! -e "${rrdfile}.${field}") {
294 rrdcreate("${rrdfile}.${field}",
295 "${field}",
296 $host->{'vars'},
297 (${start}-${ostep}));
298 }
299 }
21377f43
MG
300
301 my $pos = $start;
302 foreach my $line (@$data) {
6c1071ee
MG
303 foreach my $field (@$names) {
304 my $val = shift (@$line);
21377f43 305 $val = 'U' if (!defined($val));
21377f43 306
6c1071ee
MG
307 RRDs::update("${rrdfile}.${field}", "${pos}:${val}");
308 if (RRDs::error) {
309 print "Can't insert data: " . RRDs::error . "\n";
310 exit 1;
311 }
312
21377f43 313 }
6c1071ee 314
21377f43
MG
315 $pos += $ostep;
316
317 if ((($pos-$start)/$ostep) == $#$data) {
318 last;
319 }
320 }
321
6c1071ee
MG
322 rename("${rrdfile}", "${rrdfile}.old") or die "Can't rename old file: $!\n";
323 }
21377f43 324
6c1071ee
MG
325 foreach my $field (@{$host->{'fields'}}) {
326 if (! -e "${rrdfile}.${field}") {
327 print "Creating ${rrdfile}.${field}...\n";
328 rrdcreate("${rrdfile}.${field}",
329 "${field}",
330 $host->{'vars'});
331 }
332
333 my $rrdinfo = RRDs::info("${rrdfile}.${field}");
21377f43
MG
334 if (RRDs::error) {
335 print "Error while getting info: " . RRDs::error . "\n";
336 exit 1;
337 }
338
55867f3d 339 my $limitsChanged = 0;
178e4778
MG
340 if (defined($rrdinfo->{"ds[${field}].min"})) {
341 if ($rrdinfo->{"ds[${field}].min"} ne $host->{'vars'}->{$field}->{'min'}) {
55867f3d 342 $limitsChanged = 1;
178e4778
MG
343 RRDs::tune("${rrdfile}.${field}","-i",$field.":".$host->{'vars'}->{$field}->{'min'});
344 }
345 } else {
346 if ($host->{'vars'}->{$field}->{'min'} ne 'U') {
55867f3d 347 $limitsChanged = 1;
178e4778
MG
348 RRDs::tune("${rrdfile}.${field}","-i",$field.":".$host->{'vars'}->{$field}->{'min'});
349 }
350 }
351
352 if (RRDs::error) {
353 print "Error while setting min: " . RRDs::error . "\n";
354 exit 1;
355 }
356
357 if (defined($rrdinfo->{"ds[${field}].max"})) {
358 if ($rrdinfo->{"ds[${field}].max"} ne $host->{'vars'}->{$field}->{'max'}) {
55867f3d 359 $limitsChanged = 1;
178e4778
MG
360 RRDs::tune("${rrdfile}.${field}","-a",$field.":".$host->{'vars'}->{$field}->{'max'});
361 }
362 } else {
363 if ($host->{'vars'}->{$field}->{'max'} ne 'U') {
55867f3d 364 $limitsChanged = 1;
178e4778
MG
365 RRDs::tune("${rrdfile}.${field}","-a",$field.":".$host->{'vars'}->{$field}->{'max'});
366 }
367 }
368
369 if (RRDs::error) {
370 print "Error while setting max: " . RRDs::error . "\n";
371 exit 1;
372 }
373
3d7df03f 374 if ($rrdinfo->{'rra[0].rows'} != $keep ||
0bd26ed9 375 !defined($rrdinfo->{'rra[3].rows'}) || $rrdinfo->{'rra[3].rows'} != $keepHours ||
55867f3d 376 $limitsChanged == 1) {
3d7df03f 377
6c1071ee
MG
378 print "Resizing ${rrdfile}.${field} from " . $rrdinfo->{'rra[0].rows'} .
379 " to ${keep} samples.\n";
e18c93b3 380
6c1071ee
MG
381 (my $start, my $ostep, my $names, my $data) =
382 RRDs::fetch("${rrdfile}.${field}",
383 "-s " . (time() - ($rrdinfo->{'rra[0].rows'} * $rrdinfo->{'step'})),
384 "AVERAGE");
6aa53fc5 385
6c1071ee
MG
386 if (RRDs::error) {
387 print "Error while fetching data: " . RRDs::error . "\n";
388 exit 1;
389 }
6aa53fc5 390
6c1071ee
MG
391 rrdcreate("${rrdfile}.${field}.new",
392 "${field}",
393 $host->{'vars'},
394 (${start}-${ostep}));
6aa53fc5 395
3d7df03f 396 print "Preserving data in file ${rrdfile}.${field} since " . localtime($start) . "\n";
e18c93b3 397
6c1071ee
MG
398 my $pos = $start;
399 foreach my $line (@$data) {
400 my $vline = "${pos}";
e18c93b3 401
6c1071ee
MG
402 foreach my $val (@$line) {
403 $val = 'U' if (!defined($val));
404 $vline .= ":${val}";
405 }
406 RRDs::update("${rrdfile}.${field}.new", $vline) or die "Can't insert data\n";
e18c93b3 407
6c1071ee
MG
408 if (RRDs::error) {
409 print "Error while updating: " . RRDs::error . "\n";
410 exit 1;
411 }
412 $pos += $ostep;
e18c93b3 413
6c1071ee
MG
414 if ((($pos-$start)/$ostep) == $#$data) {
415 last;
416 }
417 }
e18c93b3 418
6c1071ee
MG
419 rename("${rrdfile}.${field}", "${rrdfile}.${field}.old") or die "Can't rename old file: $!\n";
420 rename("${rrdfile}.${field}.new", "${rrdfile}.${field}") or die "Can't rename new file: $!\n";
e18c93b3 421
6c1071ee
MG
422 $rrdinfo = RRDs::info("${rrdfile}.${field}");
423 if (RRDs::error) {
424 print "Error while getting info: " . RRDs::error . "\n";
425 exit 1;
426 }
e18c93b3 427
6c1071ee
MG
428 if ($rrdinfo->{'rra[0].rows'} != $keep) {
429 print "Failed!\n";
430 exit 1;
431 }
e18c93b3 432 }
e18c93b3 433 }
6c1071ee
MG
434}
435
f3f486e1
BO
436#my $child = fork();
437#
438#die "fork failed!" if (!defined($child));
439#
440#exit 0 if ($child != 0);
6c1071ee 441
63a9aad6 442my $first = $UPSGRAPH::regenerateOnStart;
6c1071ee 443while(1) {
e18c93b3
MG
444 open(HTML, ">${outdir}/index.html.new");
445
2c148a8e 446 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>';
e18c93b3
MG
447 print HTML '<body bgcolor="#ffffff">';
448
6c1071ee 449 foreach my $host (@$hosts) {
2c148a8e
MG
450 print HTML "[<a href=\"#".${host}->{'name'}."\">".${host}->{'name'}."</a>]&nbsp;";
451 }
452 print HTML "<br>\n";
453
454 foreach my $host (@$hosts) {
455 print HTML "<br>\n";
456 print HTML "<a name=\"".${host}->{'name'}."\"></a>\n";
6c1071ee
MG
457 my $vars = $host->{'vars'};
458 my $rrdfile = $host->{'rrdfile'};
459 my $hostname = $host->{'name'};
b57167a1 460 my %multi_values = ();
6c1071ee
MG
461
462 foreach my $var (@{$host->{'fields'}}) {
463 delete $vars->{$var}->{'value'};
e5da3876 464
0e572cdc
MG
465 my $result;
466
467 if ((!defined($vars->{$var}->{'proto'})) ||
468 ($vars->{$var}->{'proto'} eq '') ||
469 ($vars->{$var}->{'proto'} eq 'snmp')) {
470 $result = fetch_snmp($host->{'address'}, $host->{'community'}, $vars->{$var}->{'oid'});
471 } elsif ($vars->{$var}->{'proto'} eq 'tcp') {
472 $result = fetch_tcp($host->{'address'}, $vars->{$var}->{'port'});
b57167a1
MG
473 } elsif ($vars->{$var}->{'proto'} eq 'tcp_multi') {
474 if (defined($multi_values{$vars->{$var}->{'multi_id'}})) {
475 $result = $multi_values{$vars->{$var}->{'multi_id'}}
476 } else {
477 my %values = fetch_tcp_multi($host->{'address'}, $vars->{$var}->{'port'}, $vars->{$var}->{'multi_delimiter'});
478 @multi_values{keys %values} = values %values;
479 $result = $multi_values{$vars->{$var}->{'multi_id'}};
480 }
0e572cdc
MG
481 }
482
6c1071ee
MG
483 next unless (defined $result);
484
0e572cdc 485 $vars->{$var}->{'value'} = $result;
6c1071ee
MG
486 if (defined($vars->{$var}->{'factor'})) {
487 $vars->{$var}->{'value'} *= $vars->{$var}->{'factor'};
488 }
489 }
490
491 foreach my $var (@{$host->{'fields'}}) {
492 if (!(defined($vars->{$var}->{'value'}))) {
493 $vars->{$var}->{'value'} = 'U';
494 }
f49c04f7 495 rrd_update("${rrdfile}.${var}", "N:" . $vars->{$var}->{'value'});
6c1071ee 496 }
f49c04f7
MG
497 if ($rrd_result) {
498 print "Error while updating: " . $rrd_result . "\n";
e5da3876
MG
499 }
500
6c1071ee 501 foreach my $var (@{$host->{'fields'}}) {
235b08ab
BO
502 my $graphWidth = 365 * 3;
503 my $graphConfig;
504 if (defined $vars->{$var}->{'graph'}) {
505 $graphConfig = $vars->{$var}->{'graph'};
506 foreach my $subGraph (qw(day week year)) {
507 if (!defined($graphConfig->{$subGraph})) {
508 $graphConfig->{$subGraph} = [ 'avg'];
509 }
510 }
511 } else {
512 $graphConfig = {
513 'day' => [ 'avg' ],
514 'week' => [ 'avg' ],
515 'year' => [ 'avg' ],
516 };
517 }
518
63a9aad6
BO
519 my @graphdef = ('-P', "-t", $hostname." - ".$vars->{$var}->{'name'});
520 push @graphdef, "--lazy" if !$first;
235b08ab
BO
521
522 push @graphdef, "DEF:${var}-avg=${rrdfile}.${var}:${var}:AVERAGE";
523 push @graphdef, "DEF:${var}-min=${rrdfile}.${var}:${var}:MIN";
524 push @graphdef, "DEF:${var}-max=${rrdfile}.${var}:${var}:MAX";
0bd26ed9 525 push @graphdef, "DEF:${var}-houravg=${rrdfile}.${var}:${var}:AVERAGE:step=3600";
235b08ab
BO
526 push @graphdef, "LINE1:${var}-avg#FF0000";
527 push @graphdef, "VDEF:cur=${var}-avg,LAST";
de10ff59 528 push @graphdef, 'GPRINT:cur:Current\\: <span foreground="#FF0000">%.2lf</span>\\r';
52259377
MG
529
530 my $mtime;
531 $mtime=(stat("${outdir}/${hostname}.${var}.png.work"))[9];
532
6c1071ee 533 (my $averages, my $width, my $height) =
f49c04f7 534 rrd_graph("${outdir}/${hostname}.${var}.png.work",
6c1071ee 535 "-w", "720", @graphdef);
e5da3876 536
0e74e3d8
MG
537 pop @graphdef;
538 pop @graphdef;
235b08ab 539 pop @graphdef;
0e74e3d8 540
f49c04f7
MG
541 if ($rrd_result) {
542 print "Error while graphing: " . $rrd_result . "\n";
6c1071ee 543 } else {
52259377 544 my $newmtime=(stat("${outdir}/${hostname}.${var}.png.work"))[9];
f004592a 545 if ((!defined($mtime)) || ($newmtime != $mtime)) {
52259377
MG
546 copy("${outdir}/${hostname}.${var}.png.work", "${outdir}/${hostname}.${var}.png.new");
547 rename("${outdir}/${hostname}.${var}.png.new", "${outdir}/${hostname}.${var}.png");
548 }
6c1071ee 549 }
e5da3876 550
2c148a8e 551 print HTML "<a href=\"${hostname}.${var}.html\"><img src=\"${hostname}.${var}.png\" width=\"${width}\" height=\"${height}\" border=\"0\"></a><br>\n";
58c56c83 552
6c1071ee 553 open (HTML2, ">${outdir}/${hostname}.${var}.html.new");
41cfd723 554 print HTML2 '<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><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>';
6c1071ee 555 print HTML2 '<body bgcolor="#ffffff">';
58c56c83 556
3d7df03f 557 push @graphdef, "VDEF:min=${var}-min,MINIMUM";
6c1071ee 558 push @graphdef, "GPRINT:min:Minimum\\: %.2lf";
58c56c83 559
235b08ab 560 push @graphdef, "VDEF:avg=${var}-avg,AVERAGE";
6c1071ee 561 push @graphdef, "GPRINT:avg:Average\\: %.2lf";
58c56c83 562
3d7df03f 563 push @graphdef, "VDEF:max=${var}-max,MAXIMUM";
6c1071ee 564 push @graphdef, "GPRINT:max:Maximum\\: %.2lf";
e5da3876 565
235b08ab 566 push @graphdef, "VDEF:cur=${var}-avg,LAST";
6c1071ee 567 push @graphdef, "GPRINT:cur:Current\\: %.2lf";
e5da3876 568
235b08ab
BO
569 my @dayGraphDef = @graphdef;
570 push @dayGraphDef, @{cfgToGraphDef($graphConfig->{'day'}, ${var}, 24)};
571
52259377 572 $mtime=(stat("${outdir}/${hostname}.${var}.long.png.work"))[9];
6c1071ee 573 ($averages, $width, $height) =
f49c04f7 574 rrd_graph("${outdir}/${hostname}.${var}.long.png.work",
235b08ab 575 "-w", $graphWidth, @dayGraphDef);
e5da3876 576
f49c04f7
MG
577 if ($rrd_result) {
578 print "Error while graphing: " . $rrd_result . "\n";
6c1071ee 579 } else {
52259377 580 my $newmtime=(stat("${outdir}/${hostname}.${var}.long.png.work"))[9];
f004592a 581 if ((!defined($mtime)) || ($newmtime != $mtime)) {
52259377
MG
582 copy("${outdir}/${hostname}.${var}.long.png.work", "${outdir}/${hostname}.${var}.long.png.new");
583 rename("${outdir}/${hostname}.${var}.long.png.new", "${outdir}/${hostname}.${var}.long.png");
584 }
6c1071ee 585 }
e5da3876 586
1c46ea61 587 print HTML2 "<img src=\"${hostname}.${var}.long.png\" width=\"${width}\" height=\"${height}\"><br>\n";
6c1071ee 588
52259377 589 $mtime=(stat("${outdir}/${hostname}.${var}.week.png.work"))[9];
235b08ab
BO
590
591 my @weekGraphDef = @graphdef;
592 push @weekGraphDef, @{cfgToGraphDef($graphConfig->{'week'}, ${var}, 24)};
593
6c1071ee 594 ($averages, $width, $height) =
f49c04f7 595 rrd_graph("${outdir}/${hostname}.${var}.week.png.work",
235b08ab 596 "-w", "$graphWidth", "-e", "now", "-s", "00:00-8d", @weekGraphDef);
6c1071ee 597
f49c04f7
MG
598 if ($rrd_result) {
599 print "Error while graphing: " . $rrd_result . "\n";
6c1071ee 600 } else {
52259377 601 my $newmtime=(stat("${outdir}/${hostname}.${var}.week.png.work"))[9];
f004592a 602 if ((!defined($mtime)) || ($newmtime != $mtime)) {
52259377
MG
603 copy("${outdir}/${hostname}.${var}.week.png.work", "${outdir}/${hostname}.${var}.week.png.new");
604 rename("${outdir}/${hostname}.${var}.week.png.new", "${outdir}/${hostname}.${var}.week.png");
605 }
6c1071ee 606 }
e5da3876 607
1c46ea61 608 print HTML2 "<img src=\"${hostname}.${var}.week.png\" width=\"${width}\" height=\"${height}\"><br>\n";
e5da3876 609
52259377 610 $mtime=(stat("${outdir}/${hostname}.${var}.year.png.work"))[9];
235b08ab
BO
611
612 my @yearGraphDef = @graphdef;
613 push @yearGraphDef, @{cfgToGraphDef($graphConfig->{'year'}, ${var}, 3)};
614
6c1071ee 615 ($averages, $width, $height) =
f49c04f7 616 rrd_graph("${outdir}/${hostname}.${var}.year.png.work",
235b08ab 617 "-w", "$graphWidth", "-e", "00:00", "-s", "end-365d", @yearGraphDef);
e5da3876 618
f49c04f7
MG
619 if ($rrd_result) {
620 print "Error while graphing: " . $rrd_result . "\n";
6c1071ee 621 } else {
52259377 622 my $newmtime=(stat("${outdir}/${hostname}.${var}.year.png.work"))[9];
f004592a 623 if ((!defined($mtime)) || ($newmtime != $mtime)) {
52259377
MG
624 copy("${outdir}/${hostname}.${var}.year.png.work", "${outdir}/${hostname}.${var}.year.png.new");
625 rename("${outdir}/${hostname}.${var}.year.png.new", "${outdir}/${hostname}.${var}.year.png");
626 }
6c1071ee 627 }
e18c93b3 628
1c46ea61 629 print HTML2 "<img src=\"${hostname}.${var}.year.png\" width=\"${width}\" height=\"${height}\"><br>\n";
e18c93b3 630
6c1071ee
MG
631 print HTML2 "</body></html>\n";
632 close(HTML2);
633 rename("${outdir}/${hostname}.${var}.html.new", "${outdir}/${hostname}.${var}.html");
634 }
e18c93b3
MG
635 }
636
637 print HTML "</body></html>\n";
638 print HTML "<br>Generated on: " . localtime(time());
863e62dc 639 print HTML ' by <a href="http://git.zerfleddert.de/cgi-bin/gitweb.cgi/upsgraph">upsgraph</a>.';
e18c93b3
MG
640
641 close(HTML);
642
643 rename("${outdir}/index.html.new", "${outdir}/index.html");
644
645 sleep(${step}/2);
63a9aad6 646 $first = 0;
e18c93b3 647}
Impressum, Datenschutz