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