Commit | Line | Data |
---|---|---|
e18c93b3 MG |
1 | #!/usr/bin/perl -w |
2 | ||
1c9fb9df MG |
3 | if ((@ARGV != 1) && (@ARGV != 2)) { |
4 | print STDERR "Syntax: ${0} configfile [uid]\n"; | |
e18c93b3 MG |
5 | exit(1); |
6 | } | |
7 | ||
1c9fb9df | 8 | use Net::SNMP; |
0e572cdc | 9 | use IO::Socket::INET; |
e5da3876 | 10 | use RRDs; |
e26b065f | 11 | use File::Copy; |
e5da3876 | 12 | use Data::Dumper; |
e18c93b3 | 13 | |
1c9fb9df | 14 | $UPSGRAPH::outdir = ""; |
1c9fb9df | 15 | $UPSGRAPH::step = 60; |
e5da3876 | 16 | $UPSGRAPH::keep = (370*24*60*60)/$UPSGRAPH::step; |
6c1071ee | 17 | $UPSGRAPH::hosts = (); |
e18c93b3 | 18 | |
1c9fb9df | 19 | do $ARGV[0] or die "can't read config: $!"; |
e18c93b3 | 20 | |
1c9fb9df | 21 | my $outdir = $UPSGRAPH::outdir; |
1c9fb9df | 22 | my $step = $UPSGRAPH::step; |
e5da3876 | 23 | my $keep = $UPSGRAPH::keep; |
6c1071ee | 24 | my $hosts = $UPSGRAPH::hosts; |
e18c93b3 | 25 | |
e5da3876 | 26 | sub rrdcreate(@) { |
21377f43 MG |
27 | my $newrrd = shift; |
28 | my $field = shift; | |
6c1071ee | 29 | my $vars = shift; |
e5da3876 MG |
30 | my $start = shift; |
31 | ||
21377f43 | 32 | my @cmd = ("${newrrd}", "--step=${step}"); |
e5da3876 MG |
33 | |
34 | if (defined($start)) { | |
35 | push @cmd, "--start=${start}"; | |
36 | } | |
37 | ||
21377f43 MG |
38 | push @cmd, "DS:${field}:GAUGE:600:" . |
39 | $vars->{$field}->{'min'} . ":" . | |
40 | $vars->{$field}->{'max'} . " "; | |
41 | ||
e5da3876 MG |
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 | ||
6c1071ee MG |
51 | sub fetch_snmp(@) { |
52 | my $address = shift; | |
53 | my $community = shift; | |
54 | my $oid = shift; | |
e5da3876 | 55 | |
6c1071ee MG |
56 | (my $session, my $error) = Net::SNMP->session(Hostname => $address, |
57 | Community => $community); | |
e18c93b3 | 58 | |
0e572cdc MG |
59 | if (!$session) { |
60 | print STDERR "session error: $error"; | |
e0acc35a | 61 | return undef; |
0e572cdc | 62 | } |
e5da3876 | 63 | |
6c1071ee | 64 | $session->translate(0); |
e5da3876 | 65 | |
0e572cdc | 66 | my $result = $session->get_request($oid); |
e5da3876 | 67 | |
6c1071ee | 68 | $session->close; |
21377f43 | 69 | |
0e572cdc MG |
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; | |
6c1071ee | 97 | } |
e5da3876 | 98 | |
6c1071ee MG |
99 | if ($> == 0) { |
100 | if (@ARGV != 2) { | |
101 | print STDERR "Running as root, please provide UID as 2th argument!\n"; | |
102 | exit(1); | |
e5da3876 MG |
103 | } |
104 | ||
6c1071ee MG |
105 | print "Running as root, switching to ".$ARGV[1]."\n"; |
106 | $< = $> = $ARGV[1]; | |
21377f43 | 107 | } |
e5da3876 | 108 | |
6c1071ee MG |
109 | foreach my $host (@$hosts) { |
110 | my $rrdfile = $host->{'rrdfile'}; | |
21377f43 | 111 | |
46ead44b MG |
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 | ||
6c1071ee MG |
117 | if (-e "${rrdfile}") { |
118 | print "Reading old ${rrdfile} to preserve data...\n"; | |
e5da3876 | 119 | |
6c1071ee MG |
120 | my $rrdinfo = RRDs::info("${rrdfile}"); |
121 | if (RRDs::error) { | |
122 | print "Error while getting info: " . RRDs::error . "\n"; | |
123 | exit 1; | |
124 | } | |
21377f43 MG |
125 | |
126 | (my $start, my $ostep, my $names, my $data) = | |
6c1071ee | 127 | RRDs::fetch("${rrdfile}", |
21377f43 MG |
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 | ||
6c1071ee MG |
136 | foreach my $field (@$names) { |
137 | if (! -e "${rrdfile}.${field}") { | |
138 | rrdcreate("${rrdfile}.${field}", | |
139 | "${field}", | |
140 | $host->{'vars'}, | |
141 | (${start}-${ostep})); | |
142 | } | |
143 | } | |
21377f43 MG |
144 | |
145 | my $pos = $start; | |
146 | foreach my $line (@$data) { | |
6c1071ee MG |
147 | foreach my $field (@$names) { |
148 | my $val = shift (@$line); | |
21377f43 | 149 | $val = 'U' if (!defined($val)); |
21377f43 | 150 | |
6c1071ee MG |
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 | ||
21377f43 | 157 | } |
6c1071ee | 158 | |
21377f43 MG |
159 | $pos += $ostep; |
160 | ||
161 | if ((($pos-$start)/$ostep) == $#$data) { | |
162 | last; | |
163 | } | |
164 | } | |
165 | ||
6c1071ee MG |
166 | rename("${rrdfile}", "${rrdfile}.old") or die "Can't rename old file: $!\n"; |
167 | } | |
21377f43 | 168 | |
6c1071ee MG |
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}"); | |
21377f43 MG |
178 | if (RRDs::error) { |
179 | print "Error while getting info: " . RRDs::error . "\n"; | |
180 | exit 1; | |
181 | } | |
182 | ||
178e4778 MG |
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'}) { | |
178e4778 MG |
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 | ||
21377f43 | 213 | if ($rrdinfo->{'rra[0].rows'} != $keep) { |
6c1071ee MG |
214 | print "Resizing ${rrdfile}.${field} from " . $rrdinfo->{'rra[0].rows'} . |
215 | " to ${keep} samples.\n"; | |
e18c93b3 | 216 | |
6c1071ee MG |
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"); | |
6aa53fc5 | 221 | |
6c1071ee MG |
222 | if (RRDs::error) { |
223 | print "Error while fetching data: " . RRDs::error . "\n"; | |
224 | exit 1; | |
225 | } | |
6aa53fc5 | 226 | |
6c1071ee MG |
227 | rrdcreate("${rrdfile}.${field}.new", |
228 | "${field}", | |
229 | $host->{'vars'}, | |
230 | (${start}-${ostep})); | |
6aa53fc5 | 231 | |
6c1071ee | 232 | print "Preserving data since " . localtime($start) . "\n"; |
e18c93b3 | 233 | |
6c1071ee MG |
234 | my $pos = $start; |
235 | foreach my $line (@$data) { | |
236 | my $vline = "${pos}"; | |
e18c93b3 | 237 | |
6c1071ee MG |
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"; | |
e18c93b3 | 243 | |
6c1071ee MG |
244 | if (RRDs::error) { |
245 | print "Error while updating: " . RRDs::error . "\n"; | |
246 | exit 1; | |
247 | } | |
248 | $pos += $ostep; | |
e18c93b3 | 249 | |
6c1071ee MG |
250 | if ((($pos-$start)/$ostep) == $#$data) { |
251 | last; | |
252 | } | |
253 | } | |
e18c93b3 | 254 | |
6c1071ee MG |
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"; | |
e18c93b3 | 257 | |
6c1071ee MG |
258 | $rrdinfo = RRDs::info("${rrdfile}.${field}"); |
259 | if (RRDs::error) { | |
260 | print "Error while getting info: " . RRDs::error . "\n"; | |
261 | exit 1; | |
262 | } | |
e18c93b3 | 263 | |
6c1071ee MG |
264 | if ($rrdinfo->{'rra[0].rows'} != $keep) { |
265 | print "Failed!\n"; | |
266 | exit 1; | |
267 | } | |
e18c93b3 | 268 | } |
e18c93b3 | 269 | } |
6c1071ee MG |
270 | } |
271 | ||
272 | my $child = fork(); | |
e18c93b3 | 273 | |
6c1071ee MG |
274 | die "fork failed!" if (!defined($child)); |
275 | ||
276 | exit 0 if ($child != 0); | |
277 | ||
278 | while(1) { | |
e18c93b3 MG |
279 | open(HTML, ">${outdir}/index.html.new"); |
280 | ||
2c148a8e | 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>'; |
e18c93b3 MG |
282 | print HTML '<body bgcolor="#ffffff">'; |
283 | ||
6c1071ee | 284 | foreach my $host (@$hosts) { |
2c148a8e MG |
285 | print HTML "[<a href=\"#".${host}->{'name'}."\">".${host}->{'name'}."</a>] "; |
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"; | |
6c1071ee MG |
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'}; | |
e5da3876 | 298 | |
0e572cdc MG |
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 | ||
6c1071ee MG |
309 | next unless (defined $result); |
310 | ||
0e572cdc | 311 | $vars->{$var}->{'value'} = $result; |
6c1071ee MG |
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 | } | |
e5da3876 | 323 | if (RRDs::error) { |
6c1071ee | 324 | print "Error while updating: " . RRDs::error . "\n"; |
e5da3876 MG |
325 | } |
326 | ||
6c1071ee | 327 | foreach my $var (@{$host->{'fields'}}) { |
de10ff59 | 328 | my @graphdef = ('-P', "--lazy", "-t", $hostname." - ".$vars->{$var}->{'name'}, "DEF:${var}=${rrdfile}.${var}:${var}:AVERAGE", "LINE1:${var}#FF0000"); |
0e74e3d8 MG |
329 | |
330 | push @graphdef, "VDEF:cur=${var},LAST"; | |
de10ff59 | 331 | push @graphdef, 'GPRINT:cur:Current\\: <span foreground="#FF0000">%.2lf</span>\\r'; |
52259377 MG |
332 | |
333 | my $mtime; | |
334 | $mtime=(stat("${outdir}/${hostname}.${var}.png.work"))[9]; | |
335 | ||
6c1071ee | 336 | (my $averages, my $width, my $height) = |
75dbe166 | 337 | RRDs::graph("${outdir}/${hostname}.${var}.png.work", |
6c1071ee | 338 | "-w", "720", @graphdef); |
e5da3876 | 339 | |
0e74e3d8 MG |
340 | pop @graphdef; |
341 | pop @graphdef; | |
342 | ||
6c1071ee MG |
343 | if (RRDs::error) { |
344 | print "Error while graphing: " . RRDs::error . "\n"; | |
345 | } else { | |
52259377 | 346 | my $newmtime=(stat("${outdir}/${hostname}.${var}.png.work"))[9]; |
f004592a | 347 | if ((!defined($mtime)) || ($newmtime != $mtime)) { |
52259377 MG |
348 | copy("${outdir}/${hostname}.${var}.png.work", "${outdir}/${hostname}.${var}.png.new"); |
349 | rename("${outdir}/${hostname}.${var}.png.new", "${outdir}/${hostname}.${var}.png"); | |
350 | } | |
6c1071ee | 351 | } |
e5da3876 | 352 | |
2c148a8e | 353 | print HTML "<a href=\"${hostname}.${var}.html\"><img src=\"${hostname}.${var}.png\" width=\"${width}\" height=\"${height}\" border=\"0\"></a><br>\n"; |
58c56c83 | 354 | |
6c1071ee | 355 | open (HTML2, ">${outdir}/${hostname}.${var}.html.new"); |
0e74e3d8 | 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>'; |
6c1071ee | 357 | print HTML2 '<body bgcolor="#ffffff">'; |
58c56c83 | 358 | |
6c1071ee MG |
359 | push @graphdef, "VDEF:min=${var},MINIMUM"; |
360 | push @graphdef, "GPRINT:min:Minimum\\: %.2lf"; | |
58c56c83 | 361 | |
6c1071ee MG |
362 | push @graphdef, "VDEF:avg=${var},AVERAGE"; |
363 | push @graphdef, "GPRINT:avg:Average\\: %.2lf"; | |
58c56c83 | 364 | |
6c1071ee MG |
365 | push @graphdef, "VDEF:max=${var},MAXIMUM"; |
366 | push @graphdef, "GPRINT:max:Maximum\\: %.2lf"; | |
e5da3876 | 367 | |
6c1071ee MG |
368 | push @graphdef, "VDEF:cur=${var},LAST"; |
369 | push @graphdef, "GPRINT:cur:Current\\: %.2lf"; | |
e5da3876 | 370 | |
52259377 | 371 | $mtime=(stat("${outdir}/${hostname}.${var}.long.png.work"))[9]; |
6c1071ee | 372 | ($averages, $width, $height) = |
75dbe166 | 373 | RRDs::graph("${outdir}/${hostname}.${var}.long.png.work", |
6c1071ee | 374 | "-w", "1008", @graphdef); |
e5da3876 | 375 | |
6c1071ee MG |
376 | if (RRDs::error) { |
377 | print "Error while graphing: " . RRDs::error . "\n"; | |
378 | } else { | |
52259377 | 379 | my $newmtime=(stat("${outdir}/${hostname}.${var}.long.png.work"))[9]; |
f004592a | 380 | if ((!defined($mtime)) || ($newmtime != $mtime)) { |
52259377 MG |
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 | } | |
6c1071ee | 384 | } |
e5da3876 | 385 | |
6c1071ee MG |
386 | print HTML2 "<img src=\"${hostname}.${var}.long.png\" width=\"${width}\" height=\"${height}\"><br>"; |
387 | ||
52259377 | 388 | $mtime=(stat("${outdir}/${hostname}.${var}.week.png.work"))[9]; |
6c1071ee | 389 | ($averages, $width, $height) = |
75dbe166 | 390 | RRDs::graph("${outdir}/${hostname}.${var}.week.png.work", |
6c1071ee MG |
391 | "-w", "1008", "-e", "now", "-s", "end-1w", @graphdef); |
392 | ||
393 | if (RRDs::error) { | |
394 | print "Error while graphing: " . RRDs::error . "\n"; | |
395 | } else { | |
52259377 | 396 | my $newmtime=(stat("${outdir}/${hostname}.${var}.week.png.work"))[9]; |
f004592a | 397 | if ((!defined($mtime)) || ($newmtime != $mtime)) { |
52259377 MG |
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 | } | |
6c1071ee | 401 | } |
e5da3876 | 402 | |
6c1071ee | 403 | print HTML2 "<img src=\"${hostname}.${var}.week.png\" width=\"${width}\" height=\"${height}\"><br>"; |
e5da3876 | 404 | |
52259377 | 405 | $mtime=(stat("${outdir}/${hostname}.${var}.year.png.work"))[9]; |
6c1071ee | 406 | ($averages, $width, $height) = |
75dbe166 | 407 | RRDs::graph("${outdir}/${hostname}.${var}.year.png.work", |
6c1071ee | 408 | "-w", "1008", "-e", "now", "-s", "end-1y", @graphdef); |
e5da3876 | 409 | |
6c1071ee MG |
410 | if (RRDs::error) { |
411 | print "Error while graphing: " . RRDs::error . "\n"; | |
412 | } else { | |
52259377 | 413 | my $newmtime=(stat("${outdir}/${hostname}.${var}.year.png.work"))[9]; |
f004592a | 414 | if ((!defined($mtime)) || ($newmtime != $mtime)) { |
52259377 MG |
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 | } | |
6c1071ee | 418 | } |
e18c93b3 | 419 | |
6c1071ee | 420 | print HTML2 "<img src=\"${hostname}.${var}.year.png\" width=\"${width}\" height=\"${height}\"><br>"; |
e18c93b3 | 421 | |
6c1071ee MG |
422 | print HTML2 "</body></html>\n"; |
423 | close(HTML2); | |
424 | rename("${outdir}/${hostname}.${var}.html.new", "${outdir}/${hostname}.${var}.html"); | |
425 | } | |
e18c93b3 MG |
426 | } |
427 | ||
428 | print HTML "</body></html>\n"; | |
429 | print HTML "<br>Generated on: " . localtime(time()); | |
863e62dc | 430 | print HTML ' by <a href="http://git.zerfleddert.de/cgi-bin/gitweb.cgi/upsgraph">upsgraph</a>.'; |
e18c93b3 MG |
431 | |
432 | close(HTML); | |
433 | ||
434 | rename("${outdir}/index.html.new", "${outdir}/index.html"); | |
435 | ||
436 | sleep(${step}/2); | |
437 | } |