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