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