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