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