]> git.zerfleddert.de Git - upsgraph/blob - upsgraph.pl
1e335acd1aa613b9a151140a4cf965b04a967203
[upsgraph] / upsgraph.pl
1 #!/usr/bin/perl -w
2
3 #Due to memory leak in Debian squeeze (Bug #545519)
4 my $use_rrds = 0;
5 my $rrd_result = 0;
6
7 if ((@ARGV != 1) && (@ARGV != 2)) {
8 print STDERR "Syntax: ${0} configfile [uid]\n";
9 exit(1);
10 }
11
12 use Net::SNMP;
13 use IO::Socket::INET;
14 use IO::Select;
15 use RRDs;
16 use File::Copy;
17 use Data::Dumper;
18
19 $UPSGRAPH::outdir = "";
20 $UPSGRAPH::daysCovered = 370;
21 $UPSGRAPH::step = 60;
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;
25 $UPSGRAPH::hosts = ();
26
27 do $ARGV[0] or die "can't read config: $!";
28
29 my $outdir = $UPSGRAPH::outdir;
30 my $step = $UPSGRAPH::step;
31 my $stepsPerHour = $UPSGRAPH::stepsPerHour;
32 my $keep = $UPSGRAPH::keep;
33 my $keepHours = $UPSGRAPH::keepHours;
34 my $hosts = $UPSGRAPH::hosts;
35
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
73 sub rrdcreate(@) {
74 my $newrrd = shift;
75 my $field = shift;
76 my $vars = shift;
77 my $start = shift;
78
79 my @cmd = ("${newrrd}", "--step=${step}");
80
81 if (defined($start)) {
82 push @cmd, "--start=${start}";
83 }
84
85 push @cmd, "DS:${field}:GAUGE:600:" .
86 $vars->{$field}->{'min'} . ":" .
87 $vars->{$field}->{'max'};
88
89 push @cmd, "RRA:AVERAGE:0.5:1:${keep}";
90 push @cmd, "RRA:MIN:0.4:${stepsPerHour}:${keepHours}";
91 push @cmd, "RRA:MAX:0.4:${stepsPerHour}:${keepHours}";
92
93 RRDs::create(@cmd);
94 if (RRDs::error) {
95 print "Error while creating: " . RRDs::error . "\n";
96 exit 1;
97 }
98 }
99
100 sub fetch_snmp(@) {
101 my $address = shift;
102 my $community = shift;
103 my $oid = shift;
104
105 (my $session, my $error) = Net::SNMP->session(Hostname => $address,
106 Community => $community);
107
108 if (!$session) {
109 print STDERR "session error: $error";
110 return undef;
111 }
112
113 $session->translate(0);
114
115 my $result = $session->get_request($oid);
116
117 $session->close;
118
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
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 }
142
143 close($sock);
144
145 if (!$value) {
146 return undef;
147 }
148
149 $value=~ s/\s//g;
150
151 $value;
152 }
153
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
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 }
180 }
181
182 close($sock);
183
184 %values;
185 }
186
187 if ($> == 0) {
188 if (@ARGV != 2) {
189 print STDERR "Running as root, please provide UID as 2th argument!\n";
190 exit(1);
191 }
192
193 print "Running as root, switching to ".$ARGV[1]."\n";
194 $< = $> = $ARGV[1];
195 }
196
197 foreach my $host (@$hosts) {
198 my $rrdfile = $host->{'rrdfile'};
199
200 foreach my $var (keys(%{$host->{'vars'}})) {
201 $host->{'vars'}->{$var}->{'min'} = 'U' if (!defined($host->{'vars'}->{$var}->{'min'}));
202 $host->{'vars'}->{$var}->{'max'} = 'U' if (!defined($host->{'vars'}->{$var}->{'max'}));
203 }
204
205 if (-e "${rrdfile}") {
206 print "Reading old ${rrdfile} to preserve data...\n";
207
208 my $rrdinfo = RRDs::info("${rrdfile}");
209 if (RRDs::error) {
210 print "Error while getting info: " . RRDs::error . "\n";
211 exit 1;
212 }
213
214 (my $start, my $ostep, my $names, my $data) =
215 RRDs::fetch("${rrdfile}",
216 "-s " . (time() - ($rrdinfo->{'rra[0].rows'} * $rrdinfo->{'step'})),
217 "AVERAGE");
218
219 if (RRDs::error) {
220 print "Error while fetching data: " . RRDs::error . "\n";
221 exit 1;
222 }
223
224 foreach my $field (@$names) {
225 if (! -e "${rrdfile}.${field}") {
226 rrdcreate("${rrdfile}.${field}",
227 "${field}",
228 $host->{'vars'},
229 (${start}-${ostep}));
230 }
231 }
232
233 my $pos = $start;
234 foreach my $line (@$data) {
235 foreach my $field (@$names) {
236 my $val = shift (@$line);
237 $val = 'U' if (!defined($val));
238
239 RRDs::update("${rrdfile}.${field}", "${pos}:${val}");
240 if (RRDs::error) {
241 print "Can't insert data: " . RRDs::error . "\n";
242 exit 1;
243 }
244
245 }
246
247 $pos += $ostep;
248
249 if ((($pos-$start)/$ostep) == $#$data) {
250 last;
251 }
252 }
253
254 rename("${rrdfile}", "${rrdfile}.old") or die "Can't rename old file: $!\n";
255 }
256
257 foreach my $field (@{$host->{'fields'}}) {
258 if (! -e "${rrdfile}.${field}") {
259 print "Creating ${rrdfile}.${field}...\n";
260 rrdcreate("${rrdfile}.${field}",
261 "${field}",
262 $host->{'vars'});
263 }
264
265 my $rrdinfo = RRDs::info("${rrdfile}.${field}");
266 if (RRDs::error) {
267 print "Error while getting info: " . RRDs::error . "\n";
268 exit 1;
269 }
270
271 if (defined($rrdinfo->{"ds[${field}].min"})) {
272 if ($rrdinfo->{"ds[${field}].min"} ne $host->{'vars'}->{$field}->{'min'}) {
273 RRDs::tune("${rrdfile}.${field}","-i",$field.":".$host->{'vars'}->{$field}->{'min'});
274 }
275 } else {
276 if ($host->{'vars'}->{$field}->{'min'} ne 'U') {
277 RRDs::tune("${rrdfile}.${field}","-i",$field.":".$host->{'vars'}->{$field}->{'min'});
278 }
279 }
280
281 if (RRDs::error) {
282 print "Error while setting min: " . RRDs::error . "\n";
283 exit 1;
284 }
285
286 if (defined($rrdinfo->{"ds[${field}].max"})) {
287 if ($rrdinfo->{"ds[${field}].max"} ne $host->{'vars'}->{$field}->{'max'}) {
288 RRDs::tune("${rrdfile}.${field}","-a",$field.":".$host->{'vars'}->{$field}->{'max'});
289 }
290 } else {
291 if ($host->{'vars'}->{$field}->{'max'} ne 'U') {
292 RRDs::tune("${rrdfile}.${field}","-a",$field.":".$host->{'vars'}->{$field}->{'max'});
293 }
294 }
295
296 if (RRDs::error) {
297 print "Error while setting max: " . RRDs::error . "\n";
298 exit 1;
299 }
300
301 if ($rrdinfo->{'rra[0].rows'} != $keep ||
302 !defined($rrdinfo->{'rra[1].rows'}) || $rrdinfo->{'rra[1].rows'} != $keepHours) {
303
304 print "Resizing ${rrdfile}.${field} from " . $rrdinfo->{'rra[0].rows'} .
305 " to ${keep} samples.\n";
306
307 (my $start, my $ostep, my $names, my $data) =
308 RRDs::fetch("${rrdfile}.${field}",
309 "-s " . (time() - ($rrdinfo->{'rra[0].rows'} * $rrdinfo->{'step'})),
310 "AVERAGE");
311
312 if (RRDs::error) {
313 print "Error while fetching data: " . RRDs::error . "\n";
314 exit 1;
315 }
316
317 rrdcreate("${rrdfile}.${field}.new",
318 "${field}",
319 $host->{'vars'},
320 (${start}-${ostep}));
321
322 print "Preserving data in file ${rrdfile}.${field} since " . localtime($start) . "\n";
323
324 my $pos = $start;
325 foreach my $line (@$data) {
326 my $vline = "${pos}";
327
328 foreach my $val (@$line) {
329 $val = 'U' if (!defined($val));
330 $vline .= ":${val}";
331 }
332 RRDs::update("${rrdfile}.${field}.new", $vline) or die "Can't insert data\n";
333
334 if (RRDs::error) {
335 print "Error while updating: " . RRDs::error . "\n";
336 exit 1;
337 }
338 $pos += $ostep;
339
340 if ((($pos-$start)/$ostep) == $#$data) {
341 last;
342 }
343 }
344
345 rename("${rrdfile}.${field}", "${rrdfile}.${field}.old") or die "Can't rename old file: $!\n";
346 rename("${rrdfile}.${field}.new", "${rrdfile}.${field}") or die "Can't rename new file: $!\n";
347
348 $rrdinfo = RRDs::info("${rrdfile}.${field}");
349 if (RRDs::error) {
350 print "Error while getting info: " . RRDs::error . "\n";
351 exit 1;
352 }
353
354 if ($rrdinfo->{'rra[0].rows'} != $keep) {
355 print "Failed!\n";
356 exit 1;
357 }
358 }
359 }
360 }
361
362 my $child = fork();
363
364 die "fork failed!" if (!defined($child));
365
366 exit 0 if ($child != 0);
367
368 while(1) {
369 open(HTML, ">${outdir}/index.html.new");
370
371 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>';
372 print HTML '<body bgcolor="#ffffff">';
373
374 foreach my $host (@$hosts) {
375 print HTML "[<a href=\"#".${host}->{'name'}."\">".${host}->{'name'}."</a>]&nbsp;";
376 }
377 print HTML "<br>\n";
378
379 foreach my $host (@$hosts) {
380 print HTML "<br>\n";
381 print HTML "<a name=\"".${host}->{'name'}."\"></a>\n";
382 my $vars = $host->{'vars'};
383 my $rrdfile = $host->{'rrdfile'};
384 my $hostname = $host->{'name'};
385 my %multi_values = ();
386
387 foreach my $var (@{$host->{'fields'}}) {
388 delete $vars->{$var}->{'value'};
389
390 my $result;
391
392 if ((!defined($vars->{$var}->{'proto'})) ||
393 ($vars->{$var}->{'proto'} eq '') ||
394 ($vars->{$var}->{'proto'} eq 'snmp')) {
395 $result = fetch_snmp($host->{'address'}, $host->{'community'}, $vars->{$var}->{'oid'});
396 } elsif ($vars->{$var}->{'proto'} eq 'tcp') {
397 $result = fetch_tcp($host->{'address'}, $vars->{$var}->{'port'});
398 } elsif ($vars->{$var}->{'proto'} eq 'tcp_multi') {
399 if (defined($multi_values{$vars->{$var}->{'multi_id'}})) {
400 $result = $multi_values{$vars->{$var}->{'multi_id'}}
401 } else {
402 my %values = fetch_tcp_multi($host->{'address'}, $vars->{$var}->{'port'}, $vars->{$var}->{'multi_delimiter'});
403 @multi_values{keys %values} = values %values;
404 $result = $multi_values{$vars->{$var}->{'multi_id'}};
405 }
406 }
407
408 next unless (defined $result);
409
410 $vars->{$var}->{'value'} = $result;
411 if (defined($vars->{$var}->{'factor'})) {
412 $vars->{$var}->{'value'} *= $vars->{$var}->{'factor'};
413 }
414 }
415
416 foreach my $var (@{$host->{'fields'}}) {
417 if (!(defined($vars->{$var}->{'value'}))) {
418 $vars->{$var}->{'value'} = 'U';
419 }
420 rrd_update("${rrdfile}.${var}", "N:" . $vars->{$var}->{'value'});
421 }
422 if ($rrd_result) {
423 print "Error while updating: " . $rrd_result . "\n";
424 }
425
426 foreach my $var (@{$host->{'fields'}}) {
427 my @graphdef = ('-P', "--lazy", "-t", $hostname." - ".$vars->{$var}->{'name'}, "DEF:${var}=${rrdfile}.${var}:${var}:AVERAGE", "LINE1:${var}#FF0000");
428
429 push @graphdef, "DEF:${var}-min=${rrdfile}.${var}:${var}:MIN", "LINE1:${var}-min#0000FF";
430 push @graphdef, "DEF:${var}-max=${rrdfile}.${var}:${var}:MAX", "LINE1:${var}-max#00FF00";
431 push @graphdef, "VDEF:cur=${var},LAST";
432 push @graphdef, 'GPRINT:cur:Current\\: <span foreground="#FF0000">%.2lf</span>\\r';
433
434 my $mtime;
435 $mtime=(stat("${outdir}/${hostname}.${var}.png.work"))[9];
436
437 (my $averages, my $width, my $height) =
438 rrd_graph("${outdir}/${hostname}.${var}.png.work",
439 "-w", "720", @graphdef);
440
441 pop @graphdef;
442 pop @graphdef;
443
444 if ($rrd_result) {
445 print "Error while graphing: " . $rrd_result . "\n";
446 } else {
447 my $newmtime=(stat("${outdir}/${hostname}.${var}.png.work"))[9];
448 if ((!defined($mtime)) || ($newmtime != $mtime)) {
449 copy("${outdir}/${hostname}.${var}.png.work", "${outdir}/${hostname}.${var}.png.new");
450 rename("${outdir}/${hostname}.${var}.png.new", "${outdir}/${hostname}.${var}.png");
451 }
452 }
453
454 print HTML "<a href=\"${hostname}.${var}.html\"><img src=\"${hostname}.${var}.png\" width=\"${width}\" height=\"${height}\" border=\"0\"></a><br>\n";
455
456 open (HTML2, ">${outdir}/${hostname}.${var}.html.new");
457 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>';
458 print HTML2 '<body bgcolor="#ffffff">';
459
460 push @graphdef, "VDEF:min=${var}-min,MINIMUM";
461 push @graphdef, "GPRINT:min:Minimum\\: %.2lf";
462
463 push @graphdef, "VDEF:avg=${var},AVERAGE";
464 push @graphdef, "GPRINT:avg:Average\\: %.2lf";
465
466 push @graphdef, "VDEF:max=${var}-max,MAXIMUM";
467 push @graphdef, "GPRINT:max:Maximum\\: %.2lf";
468
469 push @graphdef, "VDEF:cur=${var},LAST";
470 push @graphdef, "GPRINT:cur:Current\\: %.2lf";
471
472 $mtime=(stat("${outdir}/${hostname}.${var}.long.png.work"))[9];
473 ($averages, $width, $height) =
474 rrd_graph("${outdir}/${hostname}.${var}.long.png.work",
475 "-w", "1008", @graphdef);
476
477 if ($rrd_result) {
478 print "Error while graphing: " . $rrd_result . "\n";
479 } else {
480 my $newmtime=(stat("${outdir}/${hostname}.${var}.long.png.work"))[9];
481 if ((!defined($mtime)) || ($newmtime != $mtime)) {
482 copy("${outdir}/${hostname}.${var}.long.png.work", "${outdir}/${hostname}.${var}.long.png.new");
483 rename("${outdir}/${hostname}.${var}.long.png.new", "${outdir}/${hostname}.${var}.long.png");
484 }
485 }
486
487 print HTML2 "<img src=\"${hostname}.${var}.long.png\" width=\"${width}\" height=\"${height}\"><br>\n";
488
489 $mtime=(stat("${outdir}/${hostname}.${var}.week.png.work"))[9];
490 ($averages, $width, $height) =
491 rrd_graph("${outdir}/${hostname}.${var}.week.png.work",
492 "-w", "1008", "-e", "now", "-s", "end-1w", @graphdef);
493
494 if ($rrd_result) {
495 print "Error while graphing: " . $rrd_result . "\n";
496 } else {
497 my $newmtime=(stat("${outdir}/${hostname}.${var}.week.png.work"))[9];
498 if ((!defined($mtime)) || ($newmtime != $mtime)) {
499 copy("${outdir}/${hostname}.${var}.week.png.work", "${outdir}/${hostname}.${var}.week.png.new");
500 rename("${outdir}/${hostname}.${var}.week.png.new", "${outdir}/${hostname}.${var}.week.png");
501 }
502 }
503
504 print HTML2 "<img src=\"${hostname}.${var}.week.png\" width=\"${width}\" height=\"${height}\"><br>\n";
505
506 $mtime=(stat("${outdir}/${hostname}.${var}.year.png.work"))[9];
507 ($averages, $width, $height) =
508 rrd_graph("${outdir}/${hostname}.${var}.year.png.work",
509 "-w", "1008", "-e", "now", "-s", "end-1y", @graphdef);
510
511 if ($rrd_result) {
512 print "Error while graphing: " . $rrd_result . "\n";
513 } else {
514 my $newmtime=(stat("${outdir}/${hostname}.${var}.year.png.work"))[9];
515 if ((!defined($mtime)) || ($newmtime != $mtime)) {
516 copy("${outdir}/${hostname}.${var}.year.png.work", "${outdir}/${hostname}.${var}.year.png.new");
517 rename("${outdir}/${hostname}.${var}.year.png.new", "${outdir}/${hostname}.${var}.year.png");
518 }
519 }
520
521 print HTML2 "<img src=\"${hostname}.${var}.year.png\" width=\"${width}\" height=\"${height}\"><br>\n";
522
523 print HTML2 "</body></html>\n";
524 close(HTML2);
525 rename("${outdir}/${hostname}.${var}.html.new", "${outdir}/${hostname}.${var}.html");
526 }
527 }
528
529 print HTML "</body></html>\n";
530 print HTML "<br>Generated on: " . localtime(time());
531 print HTML ' by <a href="http://git.zerfleddert.de/cgi-bin/gitweb.cgi/upsgraph">upsgraph</a>.';
532
533 close(HTML);
534
535 rename("${outdir}/index.html.new", "${outdir}/index.html");
536
537 sleep(${step}/2);
538 }
Impressum, Datenschutz