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