]> git.zerfleddert.de Git - upsgraph/blame - upsgraph.pl
Add linebreaks to HTML output
[upsgraph] / upsgraph.pl
CommitLineData
e18c93b3
MG
1#!/usr/bin/perl -w
2
f49c04f7
MG
3#Due to memory leak in Debian squeeze (Bug #545519)
4my $use_rrds = 0;
5my $rrd_result = 0;
6
1c9fb9df
MG
7if ((@ARGV != 1) && (@ARGV != 2)) {
8 print STDERR "Syntax: ${0} configfile [uid]\n";
e18c93b3
MG
9 exit(1);
10}
11
1c9fb9df 12use Net::SNMP;
0e572cdc 13use IO::Socket::INET;
3679b927 14use IO::Select;
e5da3876 15use RRDs;
e26b065f 16use File::Copy;
e5da3876 17use Data::Dumper;
e18c93b3 18
1c9fb9df 19$UPSGRAPH::outdir = "";
1c9fb9df 20$UPSGRAPH::step = 60;
e5da3876 21$UPSGRAPH::keep = (370*24*60*60)/$UPSGRAPH::step;
6c1071ee 22$UPSGRAPH::hosts = ();
e18c93b3 23
1c9fb9df 24do $ARGV[0] or die "can't read config: $!";
e18c93b3 25
1c9fb9df 26my $outdir = $UPSGRAPH::outdir;
1c9fb9df 27my $step = $UPSGRAPH::step;
e5da3876 28my $keep = $UPSGRAPH::keep;
6c1071ee 29my $hosts = $UPSGRAPH::hosts;
e18c93b3 30
f49c04f7
MG
31sub 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
42sub 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
e5da3876 68sub rrdcreate(@) {
21377f43
MG
69 my $newrrd = shift;
70 my $field = shift;
6c1071ee 71 my $vars = shift;
e5da3876
MG
72 my $start = shift;
73
21377f43 74 my @cmd = ("${newrrd}", "--step=${step}");
e5da3876
MG
75
76 if (defined($start)) {
77 push @cmd, "--start=${start}";
78 }
79
21377f43
MG
80 push @cmd, "DS:${field}:GAUGE:600:" .
81 $vars->{$field}->{'min'} . ":" .
82 $vars->{$field}->{'max'} . " ";
83
e5da3876
MG
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
6c1071ee
MG
93sub fetch_snmp(@) {
94 my $address = shift;
95 my $community = shift;
96 my $oid = shift;
e5da3876 97
6c1071ee
MG
98 (my $session, my $error) = Net::SNMP->session(Hostname => $address,
99 Community => $community);
e18c93b3 100
0e572cdc
MG
101 if (!$session) {
102 print STDERR "session error: $error";
e0acc35a 103 return undef;
0e572cdc 104 }
e5da3876 105
6c1071ee 106 $session->translate(0);
e5da3876 107
0e572cdc 108 my $result = $session->get_request($oid);
e5da3876 109
6c1071ee 110 $session->close;
21377f43 111
0e572cdc
MG
112 return undef if (!defined($result));
113
114 $result->{$oid};
115}
116
117sub 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
3679b927
MG
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 }
0e572cdc
MG
135
136 close($sock);
137
138 if (!$value) {
139 return undef;
140 }
141
142 $value=~ s/\s//g;
143
144 $value;
6c1071ee 145}
e5da3876 146
b57167a1
MG
147sub 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
3679b927
MG
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 }
b57167a1
MG
173 }
174
175 close($sock);
176
177 %values;
178}
179
6c1071ee
MG
180if ($> == 0) {
181 if (@ARGV != 2) {
182 print STDERR "Running as root, please provide UID as 2th argument!\n";
183 exit(1);
e5da3876
MG
184 }
185
6c1071ee
MG
186 print "Running as root, switching to ".$ARGV[1]."\n";
187 $< = $> = $ARGV[1];
21377f43 188}
e5da3876 189
6c1071ee
MG
190foreach my $host (@$hosts) {
191 my $rrdfile = $host->{'rrdfile'};
21377f43 192
46ead44b
MG
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
6c1071ee
MG
198 if (-e "${rrdfile}") {
199 print "Reading old ${rrdfile} to preserve data...\n";
e5da3876 200
6c1071ee
MG
201 my $rrdinfo = RRDs::info("${rrdfile}");
202 if (RRDs::error) {
203 print "Error while getting info: " . RRDs::error . "\n";
204 exit 1;
205 }
21377f43
MG
206
207 (my $start, my $ostep, my $names, my $data) =
6c1071ee 208 RRDs::fetch("${rrdfile}",
21377f43
MG
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
6c1071ee
MG
217 foreach my $field (@$names) {
218 if (! -e "${rrdfile}.${field}") {
219 rrdcreate("${rrdfile}.${field}",
220 "${field}",
221 $host->{'vars'},
222 (${start}-${ostep}));
223 }
224 }
21377f43
MG
225
226 my $pos = $start;
227 foreach my $line (@$data) {
6c1071ee
MG
228 foreach my $field (@$names) {
229 my $val = shift (@$line);
21377f43 230 $val = 'U' if (!defined($val));
21377f43 231
6c1071ee
MG
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
21377f43 238 }
6c1071ee 239
21377f43
MG
240 $pos += $ostep;
241
242 if ((($pos-$start)/$ostep) == $#$data) {
243 last;
244 }
245 }
246
6c1071ee
MG
247 rename("${rrdfile}", "${rrdfile}.old") or die "Can't rename old file: $!\n";
248 }
21377f43 249
6c1071ee
MG
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}");
21377f43
MG
259 if (RRDs::error) {
260 print "Error while getting info: " . RRDs::error . "\n";
261 exit 1;
262 }
263
178e4778
MG
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'}) {
178e4778
MG
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
21377f43 294 if ($rrdinfo->{'rra[0].rows'} != $keep) {
6c1071ee
MG
295 print "Resizing ${rrdfile}.${field} from " . $rrdinfo->{'rra[0].rows'} .
296 " to ${keep} samples.\n";
e18c93b3 297
6c1071ee
MG
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");
6aa53fc5 302
6c1071ee
MG
303 if (RRDs::error) {
304 print "Error while fetching data: " . RRDs::error . "\n";
305 exit 1;
306 }
6aa53fc5 307
6c1071ee
MG
308 rrdcreate("${rrdfile}.${field}.new",
309 "${field}",
310 $host->{'vars'},
311 (${start}-${ostep}));
6aa53fc5 312
6c1071ee 313 print "Preserving data since " . localtime($start) . "\n";
e18c93b3 314
6c1071ee
MG
315 my $pos = $start;
316 foreach my $line (@$data) {
317 my $vline = "${pos}";
e18c93b3 318
6c1071ee
MG
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";
e18c93b3 324
6c1071ee
MG
325 if (RRDs::error) {
326 print "Error while updating: " . RRDs::error . "\n";
327 exit 1;
328 }
329 $pos += $ostep;
e18c93b3 330
6c1071ee
MG
331 if ((($pos-$start)/$ostep) == $#$data) {
332 last;
333 }
334 }
e18c93b3 335
6c1071ee
MG
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";
e18c93b3 338
6c1071ee
MG
339 $rrdinfo = RRDs::info("${rrdfile}.${field}");
340 if (RRDs::error) {
341 print "Error while getting info: " . RRDs::error . "\n";
342 exit 1;
343 }
e18c93b3 344
6c1071ee
MG
345 if ($rrdinfo->{'rra[0].rows'} != $keep) {
346 print "Failed!\n";
347 exit 1;
348 }
e18c93b3 349 }
e18c93b3 350 }
6c1071ee
MG
351}
352
353my $child = fork();
e18c93b3 354
6c1071ee
MG
355die "fork failed!" if (!defined($child));
356
357exit 0 if ($child != 0);
358
359while(1) {
e18c93b3
MG
360 open(HTML, ">${outdir}/index.html.new");
361
2c148a8e 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>';
e18c93b3
MG
363 print HTML '<body bgcolor="#ffffff">';
364
6c1071ee 365 foreach my $host (@$hosts) {
2c148a8e
MG
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";
6c1071ee
MG
373 my $vars = $host->{'vars'};
374 my $rrdfile = $host->{'rrdfile'};
375 my $hostname = $host->{'name'};
b57167a1 376 my %multi_values = ();
6c1071ee
MG
377
378 foreach my $var (@{$host->{'fields'}}) {
379 delete $vars->{$var}->{'value'};
e5da3876 380
0e572cdc
MG
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'});
b57167a1
MG
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 }
0e572cdc
MG
397 }
398
6c1071ee
MG
399 next unless (defined $result);
400
0e572cdc 401 $vars->{$var}->{'value'} = $result;
6c1071ee
MG
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 }
f49c04f7 411 rrd_update("${rrdfile}.${var}", "N:" . $vars->{$var}->{'value'});
6c1071ee 412 }
f49c04f7
MG
413 if ($rrd_result) {
414 print "Error while updating: " . $rrd_result . "\n";
e5da3876
MG
415 }
416
6c1071ee 417 foreach my $var (@{$host->{'fields'}}) {
de10ff59 418 my @graphdef = ('-P', "--lazy", "-t", $hostname." - ".$vars->{$var}->{'name'}, "DEF:${var}=${rrdfile}.${var}:${var}:AVERAGE", "LINE1:${var}#FF0000");
0e74e3d8
MG
419
420 push @graphdef, "VDEF:cur=${var},LAST";
de10ff59 421 push @graphdef, 'GPRINT:cur:Current\\: <span foreground="#FF0000">%.2lf</span>\\r';
52259377
MG
422
423 my $mtime;
424 $mtime=(stat("${outdir}/${hostname}.${var}.png.work"))[9];
425
6c1071ee 426 (my $averages, my $width, my $height) =
f49c04f7 427 rrd_graph("${outdir}/${hostname}.${var}.png.work",
6c1071ee 428 "-w", "720", @graphdef);
e5da3876 429
0e74e3d8
MG
430 pop @graphdef;
431 pop @graphdef;
432
f49c04f7
MG
433 if ($rrd_result) {
434 print "Error while graphing: " . $rrd_result . "\n";
6c1071ee 435 } else {
52259377 436 my $newmtime=(stat("${outdir}/${hostname}.${var}.png.work"))[9];
f004592a 437 if ((!defined($mtime)) || ($newmtime != $mtime)) {
52259377
MG
438 copy("${outdir}/${hostname}.${var}.png.work", "${outdir}/${hostname}.${var}.png.new");
439 rename("${outdir}/${hostname}.${var}.png.new", "${outdir}/${hostname}.${var}.png");
440 }
6c1071ee 441 }
e5da3876 442
2c148a8e 443 print HTML "<a href=\"${hostname}.${var}.html\"><img src=\"${hostname}.${var}.png\" width=\"${width}\" height=\"${height}\" border=\"0\"></a><br>\n";
58c56c83 444
6c1071ee 445 open (HTML2, ">${outdir}/${hostname}.${var}.html.new");
41cfd723 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>';
6c1071ee 447 print HTML2 '<body bgcolor="#ffffff">';
58c56c83 448
6c1071ee
MG
449 push @graphdef, "VDEF:min=${var},MINIMUM";
450 push @graphdef, "GPRINT:min:Minimum\\: %.2lf";
58c56c83 451
6c1071ee
MG
452 push @graphdef, "VDEF:avg=${var},AVERAGE";
453 push @graphdef, "GPRINT:avg:Average\\: %.2lf";
58c56c83 454
6c1071ee
MG
455 push @graphdef, "VDEF:max=${var},MAXIMUM";
456 push @graphdef, "GPRINT:max:Maximum\\: %.2lf";
e5da3876 457
6c1071ee
MG
458 push @graphdef, "VDEF:cur=${var},LAST";
459 push @graphdef, "GPRINT:cur:Current\\: %.2lf";
e5da3876 460
52259377 461 $mtime=(stat("${outdir}/${hostname}.${var}.long.png.work"))[9];
6c1071ee 462 ($averages, $width, $height) =
f49c04f7 463 rrd_graph("${outdir}/${hostname}.${var}.long.png.work",
6c1071ee 464 "-w", "1008", @graphdef);
e5da3876 465
f49c04f7
MG
466 if ($rrd_result) {
467 print "Error while graphing: " . $rrd_result . "\n";
6c1071ee 468 } else {
52259377 469 my $newmtime=(stat("${outdir}/${hostname}.${var}.long.png.work"))[9];
f004592a 470 if ((!defined($mtime)) || ($newmtime != $mtime)) {
52259377
MG
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 }
6c1071ee 474 }
e5da3876 475
1c46ea61 476 print HTML2 "<img src=\"${hostname}.${var}.long.png\" width=\"${width}\" height=\"${height}\"><br>\n";
6c1071ee 477
52259377 478 $mtime=(stat("${outdir}/${hostname}.${var}.week.png.work"))[9];
6c1071ee 479 ($averages, $width, $height) =
f49c04f7 480 rrd_graph("${outdir}/${hostname}.${var}.week.png.work",
6c1071ee
MG
481 "-w", "1008", "-e", "now", "-s", "end-1w", @graphdef);
482
f49c04f7
MG
483 if ($rrd_result) {
484 print "Error while graphing: " . $rrd_result . "\n";
6c1071ee 485 } else {
52259377 486 my $newmtime=(stat("${outdir}/${hostname}.${var}.week.png.work"))[9];
f004592a 487 if ((!defined($mtime)) || ($newmtime != $mtime)) {
52259377
MG
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 }
6c1071ee 491 }
e5da3876 492
1c46ea61 493 print HTML2 "<img src=\"${hostname}.${var}.week.png\" width=\"${width}\" height=\"${height}\"><br>\n";
e5da3876 494
52259377 495 $mtime=(stat("${outdir}/${hostname}.${var}.year.png.work"))[9];
6c1071ee 496 ($averages, $width, $height) =
f49c04f7 497 rrd_graph("${outdir}/${hostname}.${var}.year.png.work",
6c1071ee 498 "-w", "1008", "-e", "now", "-s", "end-1y", @graphdef);
e5da3876 499
f49c04f7
MG
500 if ($rrd_result) {
501 print "Error while graphing: " . $rrd_result . "\n";
6c1071ee 502 } else {
52259377 503 my $newmtime=(stat("${outdir}/${hostname}.${var}.year.png.work"))[9];
f004592a 504 if ((!defined($mtime)) || ($newmtime != $mtime)) {
52259377
MG
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 }
6c1071ee 508 }
e18c93b3 509
1c46ea61 510 print HTML2 "<img src=\"${hostname}.${var}.year.png\" width=\"${width}\" height=\"${height}\"><br>\n";
e18c93b3 511
6c1071ee
MG
512 print HTML2 "</body></html>\n";
513 close(HTML2);
514 rename("${outdir}/${hostname}.${var}.html.new", "${outdir}/${hostname}.${var}.html");
515 }
e18c93b3
MG
516 }
517
518 print HTML "</body></html>\n";
519 print HTML "<br>Generated on: " . localtime(time());
863e62dc 520 print HTML ' by <a href="http://git.zerfleddert.de/cgi-bin/gitweb.cgi/upsgraph">upsgraph</a>.';
e18c93b3
MG
521
522 close(HTML);
523
524 rename("${outdir}/index.html.new", "${outdir}/index.html");
525
526 sleep(${step}/2);
527}
Impressum, Datenschutz