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