support for multiple values in one tcp connection and example config
[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 RRDs;
15 use File::Copy;
16 use Data::Dumper;
17
18 $UPSGRAPH::outdir = "";
19 $UPSGRAPH::step = 60;
20 $UPSGRAPH::keep = (370*24*60*60)/$UPSGRAPH::step;
21 $UPSGRAPH::hosts = ();
22
23 do $ARGV[0] or die "can't read config: $!";
24
25 my $outdir = $UPSGRAPH::outdir;
26 my $step = $UPSGRAPH::step;
27 my $keep = $UPSGRAPH::keep;
28 my $hosts = $UPSGRAPH::hosts;
29
30 sub 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
41 sub 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
67 sub rrdcreate(@) {
68 my $newrrd = shift;
69 my $field = shift;
70 my $vars = shift;
71 my $start = shift;
72
73 my @cmd = ("${newrrd}", "--step=${step}");
74
75 if (defined($start)) {
76 push @cmd, "--start=${start}";
77 }
78
79 push @cmd, "DS:${field}:GAUGE:600:" .
80 $vars->{$field}->{'min'} . ":" .
81 $vars->{$field}->{'max'} . " ";
82
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
92 sub fetch_snmp(@) {
93 my $address = shift;
94 my $community = shift;
95 my $oid = shift;
96
97 (my $session, my $error) = Net::SNMP->session(Hostname => $address,
98 Community => $community);
99
100 if (!$session) {
101 print STDERR "session error: $error";
102 return undef;
103 }
104
105 $session->translate(0);
106
107 my $result = $session->get_request($oid);
108
109 $session->close;
110
111 return undef if (!defined($result));
112
113 $result->{$oid};
114 }
115
116 sub 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;
138 }
139
140 sub 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
165 if ($> == 0) {
166 if (@ARGV != 2) {
167 print STDERR "Running as root, please provide UID as 2th argument!\n";
168 exit(1);
169 }
170
171 print "Running as root, switching to ".$ARGV[1]."\n";
172 $< = $> = $ARGV[1];
173 }
174
175 foreach my $host (@$hosts) {
176 my $rrdfile = $host->{'rrdfile'};
177
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
183 if (-e "${rrdfile}") {
184 print "Reading old ${rrdfile} to preserve data...\n";
185
186 my $rrdinfo = RRDs::info("${rrdfile}");
187 if (RRDs::error) {
188 print "Error while getting info: " . RRDs::error . "\n";
189 exit 1;
190 }
191
192 (my $start, my $ostep, my $names, my $data) =
193 RRDs::fetch("${rrdfile}",
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
202 foreach my $field (@$names) {
203 if (! -e "${rrdfile}.${field}") {
204 rrdcreate("${rrdfile}.${field}",
205 "${field}",
206 $host->{'vars'},
207 (${start}-${ostep}));
208 }
209 }
210
211 my $pos = $start;
212 foreach my $line (@$data) {
213 foreach my $field (@$names) {
214 my $val = shift (@$line);
215 $val = 'U' if (!defined($val));
216
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
223 }
224
225 $pos += $ostep;
226
227 if ((($pos-$start)/$ostep) == $#$data) {
228 last;
229 }
230 }
231
232 rename("${rrdfile}", "${rrdfile}.old") or die "Can't rename old file: $!\n";
233 }
234
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}");
244 if (RRDs::error) {
245 print "Error while getting info: " . RRDs::error . "\n";
246 exit 1;
247 }
248
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'}) {
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
279 if ($rrdinfo->{'rra[0].rows'} != $keep) {
280 print "Resizing ${rrdfile}.${field} from " . $rrdinfo->{'rra[0].rows'} .
281 " to ${keep} samples.\n";
282
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");
287
288 if (RRDs::error) {
289 print "Error while fetching data: " . RRDs::error . "\n";
290 exit 1;
291 }
292
293 rrdcreate("${rrdfile}.${field}.new",
294 "${field}",
295 $host->{'vars'},
296 (${start}-${ostep}));
297
298 print "Preserving data since " . localtime($start) . "\n";
299
300 my $pos = $start;
301 foreach my $line (@$data) {
302 my $vline = "${pos}";
303
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";
309
310 if (RRDs::error) {
311 print "Error while updating: " . RRDs::error . "\n";
312 exit 1;
313 }
314 $pos += $ostep;
315
316 if ((($pos-$start)/$ostep) == $#$data) {
317 last;
318 }
319 }
320
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";
323
324 $rrdinfo = RRDs::info("${rrdfile}.${field}");
325 if (RRDs::error) {
326 print "Error while getting info: " . RRDs::error . "\n";
327 exit 1;
328 }
329
330 if ($rrdinfo->{'rra[0].rows'} != $keep) {
331 print "Failed!\n";
332 exit 1;
333 }
334 }
335 }
336 }
337
338 my $child = fork();
339
340 die "fork failed!" if (!defined($child));
341
342 exit 0 if ($child != 0);
343
344 while(1) {
345 open(HTML, ">${outdir}/index.html.new");
346
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>';
348 print HTML '<body bgcolor="#ffffff">';
349
350 foreach my $host (@$hosts) {
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";
358 my $vars = $host->{'vars'};
359 my $rrdfile = $host->{'rrdfile'};
360 my $hostname = $host->{'name'};
361 my %multi_values = ();
362
363 foreach my $var (@{$host->{'fields'}}) {
364 delete $vars->{$var}->{'value'};
365
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'});
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 }
382 }
383
384 next unless (defined $result);
385
386 $vars->{$var}->{'value'} = $result;
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 }
396 rrd_update("${rrdfile}.${var}", "N:" . $vars->{$var}->{'value'});
397 }
398 if ($rrd_result) {
399 print "Error while updating: " . $rrd_result . "\n";
400 }
401
402 foreach my $var (@{$host->{'fields'}}) {
403 my @graphdef = ('-P', "--lazy", "-t", $hostname." - ".$vars->{$var}->{'name'}, "DEF:${var}=${rrdfile}.${var}:${var}:AVERAGE", "LINE1:${var}#FF0000");
404
405 push @graphdef, "VDEF:cur=${var},LAST";
406 push @graphdef, 'GPRINT:cur:Current\\: <span foreground="#FF0000">%.2lf</span>\\r';
407
408 my $mtime;
409 $mtime=(stat("${outdir}/${hostname}.${var}.png.work"))[9];
410
411 (my $averages, my $width, my $height) =
412 rrd_graph("${outdir}/${hostname}.${var}.png.work",
413 "-w", "720", @graphdef);
414
415 pop @graphdef;
416 pop @graphdef;
417
418 if ($rrd_result) {
419 print "Error while graphing: " . $rrd_result . "\n";
420 } else {
421 my $newmtime=(stat("${outdir}/${hostname}.${var}.png.work"))[9];
422 if ((!defined($mtime)) || ($newmtime != $mtime)) {
423 copy("${outdir}/${hostname}.${var}.png.work", "${outdir}/${hostname}.${var}.png.new");
424 rename("${outdir}/${hostname}.${var}.png.new", "${outdir}/${hostname}.${var}.png");
425 }
426 }
427
428 print HTML "<a href=\"${hostname}.${var}.html\"><img src=\"${hostname}.${var}.png\" width=\"${width}\" height=\"${height}\" border=\"0\"></a><br>\n";
429
430 open (HTML2, ">${outdir}/${hostname}.${var}.html.new");
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>';
432 print HTML2 '<body bgcolor="#ffffff">';
433
434 push @graphdef, "VDEF:min=${var},MINIMUM";
435 push @graphdef, "GPRINT:min:Minimum\\: %.2lf";
436
437 push @graphdef, "VDEF:avg=${var},AVERAGE";
438 push @graphdef, "GPRINT:avg:Average\\: %.2lf";
439
440 push @graphdef, "VDEF:max=${var},MAXIMUM";
441 push @graphdef, "GPRINT:max:Maximum\\: %.2lf";
442
443 push @graphdef, "VDEF:cur=${var},LAST";
444 push @graphdef, "GPRINT:cur:Current\\: %.2lf";
445
446 $mtime=(stat("${outdir}/${hostname}.${var}.long.png.work"))[9];
447 ($averages, $width, $height) =
448 rrd_graph("${outdir}/${hostname}.${var}.long.png.work",
449 "-w", "1008", @graphdef);
450
451 if ($rrd_result) {
452 print "Error while graphing: " . $rrd_result . "\n";
453 } else {
454 my $newmtime=(stat("${outdir}/${hostname}.${var}.long.png.work"))[9];
455 if ((!defined($mtime)) || ($newmtime != $mtime)) {
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 }
459 }
460
461 print HTML2 "<img src=\"${hostname}.${var}.long.png\" width=\"${width}\" height=\"${height}\"><br>";
462
463 $mtime=(stat("${outdir}/${hostname}.${var}.week.png.work"))[9];
464 ($averages, $width, $height) =
465 rrd_graph("${outdir}/${hostname}.${var}.week.png.work",
466 "-w", "1008", "-e", "now", "-s", "end-1w", @graphdef);
467
468 if ($rrd_result) {
469 print "Error while graphing: " . $rrd_result . "\n";
470 } else {
471 my $newmtime=(stat("${outdir}/${hostname}.${var}.week.png.work"))[9];
472 if ((!defined($mtime)) || ($newmtime != $mtime)) {
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 }
476 }
477
478 print HTML2 "<img src=\"${hostname}.${var}.week.png\" width=\"${width}\" height=\"${height}\"><br>";
479
480 $mtime=(stat("${outdir}/${hostname}.${var}.year.png.work"))[9];
481 ($averages, $width, $height) =
482 rrd_graph("${outdir}/${hostname}.${var}.year.png.work",
483 "-w", "1008", "-e", "now", "-s", "end-1y", @graphdef);
484
485 if ($rrd_result) {
486 print "Error while graphing: " . $rrd_result . "\n";
487 } else {
488 my $newmtime=(stat("${outdir}/${hostname}.${var}.year.png.work"))[9];
489 if ((!defined($mtime)) || ($newmtime != $mtime)) {
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 }
493 }
494
495 print HTML2 "<img src=\"${hostname}.${var}.year.png\" width=\"${width}\" height=\"${height}\"><br>";
496
497 print HTML2 "</body></html>\n";
498 close(HTML2);
499 rename("${outdir}/${hostname}.${var}.html.new", "${outdir}/${hostname}.${var}.html");
500 }
501 }
502
503 print HTML "</body></html>\n";
504 print HTML "<br>Generated on: " . localtime(time());
505 print HTML ' by <a href="http://git.zerfleddert.de/cgi-bin/gitweb.cgi/upsgraph">upsgraph</a>.';
506
507 close(HTML);
508
509 rename("${outdir}/index.html.new", "${outdir}/index.html");
510
511 sleep(${step}/2);
512 }
Impressum, Datenschutz