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