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