]> git.zerfleddert.de Git - upsgraph/blame - upsgraph.pl
workaround for memory-leak in debian squeeze librrds-perl (Bug #545519)
[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
6c1071ee
MG
140if ($> == 0) {
141 if (@ARGV != 2) {
142 print STDERR "Running as root, please provide UID as 2th argument!\n";
143 exit(1);
e5da3876
MG
144 }
145
6c1071ee
MG
146 print "Running as root, switching to ".$ARGV[1]."\n";
147 $< = $> = $ARGV[1];
21377f43 148}
e5da3876 149
6c1071ee
MG
150foreach my $host (@$hosts) {
151 my $rrdfile = $host->{'rrdfile'};
21377f43 152
46ead44b
MG
153 foreach my $var (keys(%{$host->{'vars'}})) {
154 $host->{'vars'}->{$var}->{'min'} = 'U' if (!defined($host->{'vars'}->{$var}->{'min'}));
155 $host->{'vars'}->{$var}->{'max'} = 'U' if (!defined($host->{'vars'}->{$var}->{'max'}));
156 }
157
6c1071ee
MG
158 if (-e "${rrdfile}") {
159 print "Reading old ${rrdfile} to preserve data...\n";
e5da3876 160
6c1071ee
MG
161 my $rrdinfo = RRDs::info("${rrdfile}");
162 if (RRDs::error) {
163 print "Error while getting info: " . RRDs::error . "\n";
164 exit 1;
165 }
21377f43
MG
166
167 (my $start, my $ostep, my $names, my $data) =
6c1071ee 168 RRDs::fetch("${rrdfile}",
21377f43
MG
169 "-s " . (time() - ($rrdinfo->{'rra[0].rows'} * $rrdinfo->{'step'})),
170 "AVERAGE");
171
172 if (RRDs::error) {
173 print "Error while fetching data: " . RRDs::error . "\n";
174 exit 1;
175 }
176
6c1071ee
MG
177 foreach my $field (@$names) {
178 if (! -e "${rrdfile}.${field}") {
179 rrdcreate("${rrdfile}.${field}",
180 "${field}",
181 $host->{'vars'},
182 (${start}-${ostep}));
183 }
184 }
21377f43
MG
185
186 my $pos = $start;
187 foreach my $line (@$data) {
6c1071ee
MG
188 foreach my $field (@$names) {
189 my $val = shift (@$line);
21377f43 190 $val = 'U' if (!defined($val));
21377f43 191
6c1071ee
MG
192 RRDs::update("${rrdfile}.${field}", "${pos}:${val}");
193 if (RRDs::error) {
194 print "Can't insert data: " . RRDs::error . "\n";
195 exit 1;
196 }
197
21377f43 198 }
6c1071ee 199
21377f43
MG
200 $pos += $ostep;
201
202 if ((($pos-$start)/$ostep) == $#$data) {
203 last;
204 }
205 }
206
6c1071ee
MG
207 rename("${rrdfile}", "${rrdfile}.old") or die "Can't rename old file: $!\n";
208 }
21377f43 209
6c1071ee
MG
210 foreach my $field (@{$host->{'fields'}}) {
211 if (! -e "${rrdfile}.${field}") {
212 print "Creating ${rrdfile}.${field}...\n";
213 rrdcreate("${rrdfile}.${field}",
214 "${field}",
215 $host->{'vars'});
216 }
217
218 my $rrdinfo = RRDs::info("${rrdfile}.${field}");
21377f43
MG
219 if (RRDs::error) {
220 print "Error while getting info: " . RRDs::error . "\n";
221 exit 1;
222 }
223
178e4778
MG
224 if (defined($rrdinfo->{"ds[${field}].min"})) {
225 if ($rrdinfo->{"ds[${field}].min"} ne $host->{'vars'}->{$field}->{'min'}) {
226 RRDs::tune("${rrdfile}.${field}","-i",$field.":".$host->{'vars'}->{$field}->{'min'});
227 }
228 } else {
229 if ($host->{'vars'}->{$field}->{'min'} ne 'U') {
230 RRDs::tune("${rrdfile}.${field}","-i",$field.":".$host->{'vars'}->{$field}->{'min'});
231 }
232 }
233
234 if (RRDs::error) {
235 print "Error while setting min: " . RRDs::error . "\n";
236 exit 1;
237 }
238
239 if (defined($rrdinfo->{"ds[${field}].max"})) {
240 if ($rrdinfo->{"ds[${field}].max"} ne $host->{'vars'}->{$field}->{'max'}) {
178e4778
MG
241 RRDs::tune("${rrdfile}.${field}","-a",$field.":".$host->{'vars'}->{$field}->{'max'});
242 }
243 } else {
244 if ($host->{'vars'}->{$field}->{'max'} ne 'U') {
245 RRDs::tune("${rrdfile}.${field}","-a",$field.":".$host->{'vars'}->{$field}->{'max'});
246 }
247 }
248
249 if (RRDs::error) {
250 print "Error while setting max: " . RRDs::error . "\n";
251 exit 1;
252 }
253
21377f43 254 if ($rrdinfo->{'rra[0].rows'} != $keep) {
6c1071ee
MG
255 print "Resizing ${rrdfile}.${field} from " . $rrdinfo->{'rra[0].rows'} .
256 " to ${keep} samples.\n";
e18c93b3 257
6c1071ee
MG
258 (my $start, my $ostep, my $names, my $data) =
259 RRDs::fetch("${rrdfile}.${field}",
260 "-s " . (time() - ($rrdinfo->{'rra[0].rows'} * $rrdinfo->{'step'})),
261 "AVERAGE");
6aa53fc5 262
6c1071ee
MG
263 if (RRDs::error) {
264 print "Error while fetching data: " . RRDs::error . "\n";
265 exit 1;
266 }
6aa53fc5 267
6c1071ee
MG
268 rrdcreate("${rrdfile}.${field}.new",
269 "${field}",
270 $host->{'vars'},
271 (${start}-${ostep}));
6aa53fc5 272
6c1071ee 273 print "Preserving data since " . localtime($start) . "\n";
e18c93b3 274
6c1071ee
MG
275 my $pos = $start;
276 foreach my $line (@$data) {
277 my $vline = "${pos}";
e18c93b3 278
6c1071ee
MG
279 foreach my $val (@$line) {
280 $val = 'U' if (!defined($val));
281 $vline .= ":${val}";
282 }
283 RRDs::update("${rrdfile}.${field}.new", $vline) or die "Can't insert data\n";
e18c93b3 284
6c1071ee
MG
285 if (RRDs::error) {
286 print "Error while updating: " . RRDs::error . "\n";
287 exit 1;
288 }
289 $pos += $ostep;
e18c93b3 290
6c1071ee
MG
291 if ((($pos-$start)/$ostep) == $#$data) {
292 last;
293 }
294 }
e18c93b3 295
6c1071ee
MG
296 rename("${rrdfile}.${field}", "${rrdfile}.${field}.old") or die "Can't rename old file: $!\n";
297 rename("${rrdfile}.${field}.new", "${rrdfile}.${field}") or die "Can't rename new file: $!\n";
e18c93b3 298
6c1071ee
MG
299 $rrdinfo = RRDs::info("${rrdfile}.${field}");
300 if (RRDs::error) {
301 print "Error while getting info: " . RRDs::error . "\n";
302 exit 1;
303 }
e18c93b3 304
6c1071ee
MG
305 if ($rrdinfo->{'rra[0].rows'} != $keep) {
306 print "Failed!\n";
307 exit 1;
308 }
e18c93b3 309 }
e18c93b3 310 }
6c1071ee
MG
311}
312
313my $child = fork();
e18c93b3 314
6c1071ee
MG
315die "fork failed!" if (!defined($child));
316
317exit 0 if ($child != 0);
318
319while(1) {
e18c93b3
MG
320 open(HTML, ">${outdir}/index.html.new");
321
2c148a8e 322 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
323 print HTML '<body bgcolor="#ffffff">';
324
6c1071ee 325 foreach my $host (@$hosts) {
2c148a8e
MG
326 print HTML "[<a href=\"#".${host}->{'name'}."\">".${host}->{'name'}."</a>]&nbsp;";
327 }
328 print HTML "<br>\n";
329
330 foreach my $host (@$hosts) {
331 print HTML "<br>\n";
332 print HTML "<a name=\"".${host}->{'name'}."\"></a>\n";
6c1071ee
MG
333 my $vars = $host->{'vars'};
334 my $rrdfile = $host->{'rrdfile'};
335 my $hostname = $host->{'name'};
336
337 foreach my $var (@{$host->{'fields'}}) {
338 delete $vars->{$var}->{'value'};
e5da3876 339
0e572cdc
MG
340 my $result;
341
342 if ((!defined($vars->{$var}->{'proto'})) ||
343 ($vars->{$var}->{'proto'} eq '') ||
344 ($vars->{$var}->{'proto'} eq 'snmp')) {
345 $result = fetch_snmp($host->{'address'}, $host->{'community'}, $vars->{$var}->{'oid'});
346 } elsif ($vars->{$var}->{'proto'} eq 'tcp') {
347 $result = fetch_tcp($host->{'address'}, $vars->{$var}->{'port'});
348 }
349
6c1071ee
MG
350 next unless (defined $result);
351
0e572cdc 352 $vars->{$var}->{'value'} = $result;
6c1071ee
MG
353 if (defined($vars->{$var}->{'factor'})) {
354 $vars->{$var}->{'value'} *= $vars->{$var}->{'factor'};
355 }
356 }
357
358 foreach my $var (@{$host->{'fields'}}) {
359 if (!(defined($vars->{$var}->{'value'}))) {
360 $vars->{$var}->{'value'} = 'U';
361 }
f49c04f7 362 rrd_update("${rrdfile}.${var}", "N:" . $vars->{$var}->{'value'});
6c1071ee 363 }
f49c04f7
MG
364 if ($rrd_result) {
365 print "Error while updating: " . $rrd_result . "\n";
e5da3876
MG
366 }
367
6c1071ee 368 foreach my $var (@{$host->{'fields'}}) {
de10ff59 369 my @graphdef = ('-P', "--lazy", "-t", $hostname." - ".$vars->{$var}->{'name'}, "DEF:${var}=${rrdfile}.${var}:${var}:AVERAGE", "LINE1:${var}#FF0000");
0e74e3d8
MG
370
371 push @graphdef, "VDEF:cur=${var},LAST";
de10ff59 372 push @graphdef, 'GPRINT:cur:Current\\: <span foreground="#FF0000">%.2lf</span>\\r';
52259377
MG
373
374 my $mtime;
375 $mtime=(stat("${outdir}/${hostname}.${var}.png.work"))[9];
376
6c1071ee 377 (my $averages, my $width, my $height) =
f49c04f7 378 rrd_graph("${outdir}/${hostname}.${var}.png.work",
6c1071ee 379 "-w", "720", @graphdef);
e5da3876 380
0e74e3d8
MG
381 pop @graphdef;
382 pop @graphdef;
383
f49c04f7
MG
384 if ($rrd_result) {
385 print "Error while graphing: " . $rrd_result . "\n";
6c1071ee 386 } else {
52259377 387 my $newmtime=(stat("${outdir}/${hostname}.${var}.png.work"))[9];
f004592a 388 if ((!defined($mtime)) || ($newmtime != $mtime)) {
52259377
MG
389 copy("${outdir}/${hostname}.${var}.png.work", "${outdir}/${hostname}.${var}.png.new");
390 rename("${outdir}/${hostname}.${var}.png.new", "${outdir}/${hostname}.${var}.png");
391 }
6c1071ee 392 }
e5da3876 393
2c148a8e 394 print HTML "<a href=\"${hostname}.${var}.html\"><img src=\"${hostname}.${var}.png\" width=\"${width}\" height=\"${height}\" border=\"0\"></a><br>\n";
58c56c83 395
6c1071ee 396 open (HTML2, ">${outdir}/${hostname}.${var}.html.new");
0e74e3d8 397 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 398 print HTML2 '<body bgcolor="#ffffff">';
58c56c83 399
6c1071ee
MG
400 push @graphdef, "VDEF:min=${var},MINIMUM";
401 push @graphdef, "GPRINT:min:Minimum\\: %.2lf";
58c56c83 402
6c1071ee
MG
403 push @graphdef, "VDEF:avg=${var},AVERAGE";
404 push @graphdef, "GPRINT:avg:Average\\: %.2lf";
58c56c83 405
6c1071ee
MG
406 push @graphdef, "VDEF:max=${var},MAXIMUM";
407 push @graphdef, "GPRINT:max:Maximum\\: %.2lf";
e5da3876 408
6c1071ee
MG
409 push @graphdef, "VDEF:cur=${var},LAST";
410 push @graphdef, "GPRINT:cur:Current\\: %.2lf";
e5da3876 411
52259377 412 $mtime=(stat("${outdir}/${hostname}.${var}.long.png.work"))[9];
6c1071ee 413 ($averages, $width, $height) =
f49c04f7 414 rrd_graph("${outdir}/${hostname}.${var}.long.png.work",
6c1071ee 415 "-w", "1008", @graphdef);
e5da3876 416
f49c04f7
MG
417 if ($rrd_result) {
418 print "Error while graphing: " . $rrd_result . "\n";
6c1071ee 419 } else {
52259377 420 my $newmtime=(stat("${outdir}/${hostname}.${var}.long.png.work"))[9];
f004592a 421 if ((!defined($mtime)) || ($newmtime != $mtime)) {
52259377
MG
422 copy("${outdir}/${hostname}.${var}.long.png.work", "${outdir}/${hostname}.${var}.long.png.new");
423 rename("${outdir}/${hostname}.${var}.long.png.new", "${outdir}/${hostname}.${var}.long.png");
424 }
6c1071ee 425 }
e5da3876 426
6c1071ee
MG
427 print HTML2 "<img src=\"${hostname}.${var}.long.png\" width=\"${width}\" height=\"${height}\"><br>";
428
52259377 429 $mtime=(stat("${outdir}/${hostname}.${var}.week.png.work"))[9];
6c1071ee 430 ($averages, $width, $height) =
f49c04f7 431 rrd_graph("${outdir}/${hostname}.${var}.week.png.work",
6c1071ee
MG
432 "-w", "1008", "-e", "now", "-s", "end-1w", @graphdef);
433
f49c04f7
MG
434 if ($rrd_result) {
435 print "Error while graphing: " . $rrd_result . "\n";
6c1071ee 436 } else {
52259377 437 my $newmtime=(stat("${outdir}/${hostname}.${var}.week.png.work"))[9];
f004592a 438 if ((!defined($mtime)) || ($newmtime != $mtime)) {
52259377
MG
439 copy("${outdir}/${hostname}.${var}.week.png.work", "${outdir}/${hostname}.${var}.week.png.new");
440 rename("${outdir}/${hostname}.${var}.week.png.new", "${outdir}/${hostname}.${var}.week.png");
441 }
6c1071ee 442 }
e5da3876 443
6c1071ee 444 print HTML2 "<img src=\"${hostname}.${var}.week.png\" width=\"${width}\" height=\"${height}\"><br>";
e5da3876 445
52259377 446 $mtime=(stat("${outdir}/${hostname}.${var}.year.png.work"))[9];
6c1071ee 447 ($averages, $width, $height) =
f49c04f7 448 rrd_graph("${outdir}/${hostname}.${var}.year.png.work",
6c1071ee 449 "-w", "1008", "-e", "now", "-s", "end-1y", @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}.year.png.work"))[9];
f004592a 455 if ((!defined($mtime)) || ($newmtime != $mtime)) {
52259377
MG
456 copy("${outdir}/${hostname}.${var}.year.png.work", "${outdir}/${hostname}.${var}.year.png.new");
457 rename("${outdir}/${hostname}.${var}.year.png.new", "${outdir}/${hostname}.${var}.year.png");
458 }
6c1071ee 459 }
e18c93b3 460
6c1071ee 461 print HTML2 "<img src=\"${hostname}.${var}.year.png\" width=\"${width}\" height=\"${height}\"><br>";
e18c93b3 462
6c1071ee
MG
463 print HTML2 "</body></html>\n";
464 close(HTML2);
465 rename("${outdir}/${hostname}.${var}.html.new", "${outdir}/${hostname}.${var}.html");
466 }
e18c93b3
MG
467 }
468
469 print HTML "</body></html>\n";
470 print HTML "<br>Generated on: " . localtime(time());
863e62dc 471 print HTML ' by <a href="http://git.zerfleddert.de/cgi-bin/gitweb.cgi/upsgraph">upsgraph</a>.';
e18c93b3
MG
472
473 close(HTML);
474
475 rename("${outdir}/index.html.new", "${outdir}/index.html");
476
477 sleep(${step}/2);
478}
Impressum, Datenschutz