lazy graph update
[upsgraph] / upsgraph.pl
CommitLineData
e18c93b3
MG
1#!/usr/bin/perl -w
2
1c9fb9df
MG
3if ((@ARGV != 1) && (@ARGV != 2)) {
4 print STDERR "Syntax: ${0} configfile [uid]\n";
e18c93b3
MG
5 exit(1);
6}
7
1c9fb9df 8use Net::SNMP;
0e572cdc 9use IO::Socket::INET;
e5da3876
MG
10use RRDs;
11use 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 18do $ARGV[0] or die "can't read config: $!";
e18c93b3 19
1c9fb9df 20my $outdir = $UPSGRAPH::outdir;
1c9fb9df 21my $step = $UPSGRAPH::step;
e5da3876 22my $keep = $UPSGRAPH::keep;
6c1071ee 23my $hosts = $UPSGRAPH::hosts;
e18c93b3 24
e5da3876 25sub 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
50sub 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
73sub 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
97if ($> == 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
107foreach my $host (@$hosts) {
108 my $rrdfile = $host->{'rrdfile'};
21377f43 109
46ead44b
MG
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
6c1071ee
MG
115 if (-e "${rrdfile}") {
116 print "Reading old ${rrdfile} to preserve data...\n";
e5da3876 117
6c1071ee
MG
118 my $rrdinfo = RRDs::info("${rrdfile}");
119 if (RRDs::error) {
120 print "Error while getting info: " . RRDs::error . "\n";
121 exit 1;
122 }
21377f43
MG
123
124 (my $start, my $ostep, my $names, my $data) =
6c1071ee 125 RRDs::fetch("${rrdfile}",
21377f43
MG
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
6c1071ee
MG
134 foreach my $field (@$names) {
135 if (! -e "${rrdfile}.${field}") {
136 rrdcreate("${rrdfile}.${field}",
137 "${field}",
138 $host->{'vars'},
139 (${start}-${ostep}));
140 }
141 }
21377f43
MG
142
143 my $pos = $start;
144 foreach my $line (@$data) {
6c1071ee
MG
145 foreach my $field (@$names) {
146 my $val = shift (@$line);
21377f43 147 $val = 'U' if (!defined($val));
21377f43 148
6c1071ee
MG
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
21377f43 155 }
6c1071ee 156
21377f43
MG
157 $pos += $ostep;
158
159 if ((($pos-$start)/$ostep) == $#$data) {
160 last;
161 }
162 }
163
6c1071ee
MG
164 rename("${rrdfile}", "${rrdfile}.old") or die "Can't rename old file: $!\n";
165 }
21377f43 166
6c1071ee
MG
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}");
21377f43
MG
176 if (RRDs::error) {
177 print "Error while getting info: " . RRDs::error . "\n";
178 exit 1;
179 }
180
178e4778
MG
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'}) {
178e4778
MG
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
21377f43 211 if ($rrdinfo->{'rra[0].rows'} != $keep) {
6c1071ee
MG
212 print "Resizing ${rrdfile}.${field} from " . $rrdinfo->{'rra[0].rows'} .
213 " to ${keep} samples.\n";
e18c93b3 214
6c1071ee
MG
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");
6aa53fc5 219
6c1071ee
MG
220 if (RRDs::error) {
221 print "Error while fetching data: " . RRDs::error . "\n";
222 exit 1;
223 }
6aa53fc5 224
6c1071ee
MG
225 rrdcreate("${rrdfile}.${field}.new",
226 "${field}",
227 $host->{'vars'},
228 (${start}-${ostep}));
6aa53fc5 229
6c1071ee 230 print "Preserving data since " . localtime($start) . "\n";
e18c93b3 231
6c1071ee
MG
232 my $pos = $start;
233 foreach my $line (@$data) {
234 my $vline = "${pos}";
e18c93b3 235
6c1071ee
MG
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";
e18c93b3 241
6c1071ee
MG
242 if (RRDs::error) {
243 print "Error while updating: " . RRDs::error . "\n";
244 exit 1;
245 }
246 $pos += $ostep;
e18c93b3 247
6c1071ee
MG
248 if ((($pos-$start)/$ostep) == $#$data) {
249 last;
250 }
251 }
e18c93b3 252
6c1071ee
MG
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";
e18c93b3 255
6c1071ee
MG
256 $rrdinfo = RRDs::info("${rrdfile}.${field}");
257 if (RRDs::error) {
258 print "Error while getting info: " . RRDs::error . "\n";
259 exit 1;
260 }
e18c93b3 261
6c1071ee
MG
262 if ($rrdinfo->{'rra[0].rows'} != $keep) {
263 print "Failed!\n";
264 exit 1;
265 }
e18c93b3 266 }
e18c93b3 267 }
6c1071ee
MG
268}
269
270my $child = fork();
e18c93b3 271
6c1071ee
MG
272die "fork failed!" if (!defined($child));
273
274exit 0 if ($child != 0);
275
276while(1) {
e18c93b3
MG
277 open(HTML, ">${outdir}/index.html.new");
278
2c148a8e 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>';
e18c93b3
MG
280 print HTML '<body bgcolor="#ffffff">';
281
6c1071ee 282 foreach my $host (@$hosts) {
2c148a8e
MG
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";
6c1071ee
MG
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'};
e5da3876 296
0e572cdc
MG
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
6c1071ee
MG
307 next unless (defined $result);
308
0e572cdc 309 $vars->{$var}->{'value'} = $result;
6c1071ee
MG
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 }
e5da3876 321 if (RRDs::error) {
6c1071ee 322 print "Error while updating: " . RRDs::error . "\n";
e5da3876
MG
323 }
324
6c1071ee 325 foreach my $var (@{$host->{'fields'}}) {
0d418146 326 my @graphdef = ("--lazy", "-t", $hostname." - ".$vars->{$var}->{'name'}, "DEF:${var}=${rrdfile}.${var}:${var}:AVERAGE", "LINE1:${var}#FF0000");
6c1071ee
MG
327 (my $averages, my $width, my $height) =
328 RRDs::graph("${outdir}/${hostname}.${var}.png.new",
329 "-w", "720", @graphdef);
e5da3876 330
6c1071ee
MG
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 }
e5da3876 336
2c148a8e 337 print HTML "<a href=\"${hostname}.${var}.html\"><img src=\"${hostname}.${var}.png\" width=\"${width}\" height=\"${height}\" border=\"0\"></a><br>\n";
58c56c83 338
6c1071ee
MG
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">';
58c56c83 342
58c56c83 343
6c1071ee
MG
344 push @graphdef, "VDEF:min=${var},MINIMUM";
345 push @graphdef, "GPRINT:min:Minimum\\: %.2lf";
58c56c83 346
6c1071ee
MG
347 push @graphdef, "VDEF:avg=${var},AVERAGE";
348 push @graphdef, "GPRINT:avg:Average\\: %.2lf";
58c56c83 349
6c1071ee
MG
350 push @graphdef, "VDEF:max=${var},MAXIMUM";
351 push @graphdef, "GPRINT:max:Maximum\\: %.2lf";
e5da3876 352
6c1071ee
MG
353 push @graphdef, "VDEF:cur=${var},LAST";
354 push @graphdef, "GPRINT:cur:Current\\: %.2lf";
e5da3876 355
6c1071ee
MG
356 ($averages, $width, $height) =
357 RRDs::graph("${outdir}/${hostname}.${var}.long.png.new",
358 "-w", "1008", @graphdef);
e5da3876 359
6c1071ee
MG
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 }
e5da3876 365
6c1071ee
MG
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 }
e5da3876 377
6c1071ee 378 print HTML2 "<img src=\"${hostname}.${var}.week.png\" width=\"${width}\" height=\"${height}\"><br>";
e5da3876 379
6c1071ee
MG
380 ($averages, $width, $height) =
381 RRDs::graph("${outdir}/${hostname}.${var}.year.png.new",
382 "-w", "1008", "-e", "now", "-s", "end-1y", @graphdef);
e5da3876 383
6c1071ee
MG
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 }
e18c93b3 389
6c1071ee 390 print HTML2 "<img src=\"${hostname}.${var}.year.png\" width=\"${width}\" height=\"${height}\"><br>";
e18c93b3 391
6c1071ee
MG
392 print HTML2 "</body></html>\n";
393 close(HTML2);
394 rename("${outdir}/${hostname}.${var}.html.new", "${outdir}/${hostname}.${var}.html");
395 }
e18c93b3
MG
396 }
397
398 print HTML "</body></html>\n";
399 print HTML "<br>Generated on: " . localtime(time());
863e62dc 400 print HTML ' by <a href="http://git.zerfleddert.de/cgi-bin/gitweb.cgi/upsgraph">upsgraph</a>.';
e18c93b3
MG
401
402 close(HTML);
403
404 rename("${outdir}/index.html.new", "${outdir}/index.html");
405
406 sleep(${step}/2);
407}
Impressum, Datenschutz