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