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