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