]>
Commit | Line | Data |
---|---|---|
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 | if (-e "${rrdfile}") { | |
111 | print "Reading old ${rrdfile} to preserve data...\n"; | |
112 | ||
113 | my $rrdinfo = RRDs::info("${rrdfile}"); | |
114 | if (RRDs::error) { | |
115 | print "Error while getting info: " . RRDs::error . "\n"; | |
116 | exit 1; | |
117 | } | |
118 | ||
119 | (my $start, my $ostep, my $names, my $data) = | |
120 | RRDs::fetch("${rrdfile}", | |
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 | ||
129 | foreach my $field (@$names) { | |
130 | if (! -e "${rrdfile}.${field}") { | |
131 | rrdcreate("${rrdfile}.${field}", | |
132 | "${field}", | |
133 | $host->{'vars'}, | |
134 | (${start}-${ostep})); | |
135 | } | |
136 | } | |
137 | ||
138 | my $pos = $start; | |
139 | foreach my $line (@$data) { | |
140 | foreach my $field (@$names) { | |
141 | my $val = shift (@$line); | |
142 | $val = 'U' if (!defined($val)); | |
143 | ||
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 | ||
150 | } | |
151 | ||
152 | $pos += $ostep; | |
153 | ||
154 | if ((($pos-$start)/$ostep) == $#$data) { | |
155 | last; | |
156 | } | |
157 | } | |
158 | ||
159 | rename("${rrdfile}", "${rrdfile}.old") or die "Can't rename old file: $!\n"; | |
160 | } | |
161 | ||
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}"); | |
171 | if (RRDs::error) { | |
172 | print "Error while getting info: " . RRDs::error . "\n"; | |
173 | exit 1; | |
174 | } | |
175 | ||
176 | if ($rrdinfo->{'rra[0].rows'} != $keep) { | |
177 | print "Resizing ${rrdfile}.${field} from " . $rrdinfo->{'rra[0].rows'} . | |
178 | " to ${keep} samples.\n"; | |
179 | ||
180 | (my $start, my $ostep, my $names, my $data) = | |
181 | RRDs::fetch("${rrdfile}.${field}", | |
182 | "-s " . (time() - ($rrdinfo->{'rra[0].rows'} * $rrdinfo->{'step'})), | |
183 | "AVERAGE"); | |
184 | ||
185 | if (RRDs::error) { | |
186 | print "Error while fetching data: " . RRDs::error . "\n"; | |
187 | exit 1; | |
188 | } | |
189 | ||
190 | rrdcreate("${rrdfile}.${field}.new", | |
191 | "${field}", | |
192 | $host->{'vars'}, | |
193 | (${start}-${ostep})); | |
194 | ||
195 | print "Preserving data since " . localtime($start) . "\n"; | |
196 | ||
197 | my $pos = $start; | |
198 | foreach my $line (@$data) { | |
199 | my $vline = "${pos}"; | |
200 | ||
201 | foreach my $val (@$line) { | |
202 | $val = 'U' if (!defined($val)); | |
203 | $vline .= ":${val}"; | |
204 | } | |
205 | RRDs::update("${rrdfile}.${field}.new", $vline) or die "Can't insert data\n"; | |
206 | ||
207 | if (RRDs::error) { | |
208 | print "Error while updating: " . RRDs::error . "\n"; | |
209 | exit 1; | |
210 | } | |
211 | $pos += $ostep; | |
212 | ||
213 | if ((($pos-$start)/$ostep) == $#$data) { | |
214 | last; | |
215 | } | |
216 | } | |
217 | ||
218 | rename("${rrdfile}.${field}", "${rrdfile}.${field}.old") or die "Can't rename old file: $!\n"; | |
219 | rename("${rrdfile}.${field}.new", "${rrdfile}.${field}") or die "Can't rename new file: $!\n"; | |
220 | ||
221 | $rrdinfo = RRDs::info("${rrdfile}.${field}"); | |
222 | if (RRDs::error) { | |
223 | print "Error while getting info: " . RRDs::error . "\n"; | |
224 | exit 1; | |
225 | } | |
226 | ||
227 | if ($rrdinfo->{'rra[0].rows'} != $keep) { | |
228 | print "Failed!\n"; | |
229 | exit 1; | |
230 | } | |
231 | } | |
232 | } | |
233 | } | |
234 | ||
235 | my $child = fork(); | |
236 | ||
237 | die "fork failed!" if (!defined($child)); | |
238 | ||
239 | exit 0 if ($child != 0); | |
240 | ||
241 | while(1) { | |
242 | open(HTML, ">${outdir}/index.html.new"); | |
243 | ||
244 | 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>'; | |
245 | print HTML '<body bgcolor="#ffffff">'; | |
246 | ||
247 | foreach my $host (@$hosts) { | |
248 | print HTML "[<a href=\"#".${host}->{'name'}."\">".${host}->{'name'}."</a>] "; | |
249 | } | |
250 | print HTML "<br>\n"; | |
251 | ||
252 | foreach my $host (@$hosts) { | |
253 | print HTML "<br>\n"; | |
254 | print HTML "<a name=\"".${host}->{'name'}."\"></a>\n"; | |
255 | my $vars = $host->{'vars'}; | |
256 | my $rrdfile = $host->{'rrdfile'}; | |
257 | my $hostname = $host->{'name'}; | |
258 | ||
259 | foreach my $var (@{$host->{'fields'}}) { | |
260 | delete $vars->{$var}->{'value'}; | |
261 | ||
262 | my $result; | |
263 | ||
264 | if ((!defined($vars->{$var}->{'proto'})) || | |
265 | ($vars->{$var}->{'proto'} eq '') || | |
266 | ($vars->{$var}->{'proto'} eq 'snmp')) { | |
267 | $result = fetch_snmp($host->{'address'}, $host->{'community'}, $vars->{$var}->{'oid'}); | |
268 | } elsif ($vars->{$var}->{'proto'} eq 'tcp') { | |
269 | $result = fetch_tcp($host->{'address'}, $vars->{$var}->{'port'}); | |
270 | } | |
271 | ||
272 | next unless (defined $result); | |
273 | ||
274 | $vars->{$var}->{'value'} = $result; | |
275 | if (defined($vars->{$var}->{'factor'})) { | |
276 | $vars->{$var}->{'value'} *= $vars->{$var}->{'factor'}; | |
277 | } | |
278 | } | |
279 | ||
280 | foreach my $var (@{$host->{'fields'}}) { | |
281 | if (!(defined($vars->{$var}->{'value'}))) { | |
282 | $vars->{$var}->{'value'} = 'U'; | |
283 | } | |
284 | RRDs::update("${rrdfile}.${var}", "N:" . $vars->{$var}->{'value'}); | |
285 | } | |
286 | if (RRDs::error) { | |
287 | print "Error while updating: " . RRDs::error . "\n"; | |
288 | } | |
289 | ||
290 | foreach my $var (@{$host->{'fields'}}) { | |
291 | my @graphdef = ("-t", $hostname." - ".$vars->{$var}->{'name'}, "DEF:${var}=${rrdfile}.${var}:${var}:AVERAGE", "LINE1:${var}#FF0000"); | |
292 | (my $averages, my $width, my $height) = | |
293 | RRDs::graph("${outdir}/${hostname}.${var}.png.new", | |
294 | "-w", "720", @graphdef); | |
295 | ||
296 | if (RRDs::error) { | |
297 | print "Error while graphing: " . RRDs::error . "\n"; | |
298 | } else { | |
299 | rename("${outdir}/${hostname}.${var}.png.new", "${outdir}/${hostname}.${var}.png"); | |
300 | } | |
301 | ||
302 | print HTML "<a href=\"${hostname}.${var}.html\"><img src=\"${hostname}.${var}.png\" width=\"${width}\" height=\"${height}\" border=\"0\"></a><br>\n"; | |
303 | ||
304 | open (HTML2, ">${outdir}/${hostname}.${var}.html.new"); | |
305 | print HTML2 "<html><head><title>" . $vars->{$var}->{'name'} . "</title></head>"; | |
306 | print HTML2 '<body bgcolor="#ffffff">'; | |
307 | ||
308 | ||
309 | push @graphdef, "VDEF:min=${var},MINIMUM"; | |
310 | push @graphdef, "GPRINT:min:Minimum\\: %.2lf"; | |
311 | ||
312 | push @graphdef, "VDEF:avg=${var},AVERAGE"; | |
313 | push @graphdef, "GPRINT:avg:Average\\: %.2lf"; | |
314 | ||
315 | push @graphdef, "VDEF:max=${var},MAXIMUM"; | |
316 | push @graphdef, "GPRINT:max:Maximum\\: %.2lf"; | |
317 | ||
318 | push @graphdef, "VDEF:cur=${var},LAST"; | |
319 | push @graphdef, "GPRINT:cur:Current\\: %.2lf"; | |
320 | ||
321 | ($averages, $width, $height) = | |
322 | RRDs::graph("${outdir}/${hostname}.${var}.long.png.new", | |
323 | "-w", "1008", @graphdef); | |
324 | ||
325 | if (RRDs::error) { | |
326 | print "Error while graphing: " . RRDs::error . "\n"; | |
327 | } else { | |
328 | rename("${outdir}/${hostname}.${var}.long.png.new", "${outdir}/${hostname}.${var}.long.png"); | |
329 | } | |
330 | ||
331 | print HTML2 "<img src=\"${hostname}.${var}.long.png\" width=\"${width}\" height=\"${height}\"><br>"; | |
332 | ||
333 | ($averages, $width, $height) = | |
334 | RRDs::graph("${outdir}/${hostname}.${var}.week.png.new", | |
335 | "-w", "1008", "-e", "now", "-s", "end-1w", @graphdef); | |
336 | ||
337 | if (RRDs::error) { | |
338 | print "Error while graphing: " . RRDs::error . "\n"; | |
339 | } else { | |
340 | rename("${outdir}/${hostname}.${var}.week.png.new", "${outdir}/${hostname}.${var}.week.png"); | |
341 | } | |
342 | ||
343 | print HTML2 "<img src=\"${hostname}.${var}.week.png\" width=\"${width}\" height=\"${height}\"><br>"; | |
344 | ||
345 | ($averages, $width, $height) = | |
346 | RRDs::graph("${outdir}/${hostname}.${var}.year.png.new", | |
347 | "-w", "1008", "-e", "now", "-s", "end-1y", @graphdef); | |
348 | ||
349 | if (RRDs::error) { | |
350 | print "Error while graphing: " . RRDs::error . "\n"; | |
351 | } else { | |
352 | rename("${outdir}/${hostname}.${var}.year.png.new", "${outdir}/${hostname}.${var}.year.png"); | |
353 | } | |
354 | ||
355 | print HTML2 "<img src=\"${hostname}.${var}.year.png\" width=\"${width}\" height=\"${height}\"><br>"; | |
356 | ||
357 | print HTML2 "</body></html>\n"; | |
358 | close(HTML2); | |
359 | rename("${outdir}/${hostname}.${var}.html.new", "${outdir}/${hostname}.${var}.html"); | |
360 | } | |
361 | } | |
362 | ||
363 | print HTML "</body></html>\n"; | |
364 | print HTML "<br>Generated on: " . localtime(time()); | |
365 | print HTML ' by <a href="http://git.zerfleddert.de/cgi-bin/gitweb.cgi/upsgraph">upsgraph</a>.'; | |
366 | ||
367 | close(HTML); | |
368 | ||
369 | rename("${outdir}/index.html.new", "${outdir}/index.html"); | |
370 | ||
371 | sleep(${step}/2); | |
372 | } |