]>
git.zerfleddert.de Git - upsgraph/blob - upsgraph.pl
812de11d19898a8ba02b5ca83734d4f720f77981
3 if ((@ARGV != 1) && (@ARGV != 2)) {
4 print STDERR
"Syntax: ${0} configfile [uid]\n";
12 $UPSGRAPH::outdir
= "";
14 $UPSGRAPH::keep
= (370*24*60*60)/$UPSGRAPH::step
;
15 $UPSGRAPH::hosts
= ();
17 do $ARGV[0] or die "can't read config: $!";
19 my $outdir = $UPSGRAPH::outdir
;
20 my $step = $UPSGRAPH::step
;
21 my $keep = $UPSGRAPH::keep
;
22 my $hosts = $UPSGRAPH::hosts
;
30 my @cmd = ("${newrrd}", "--step=${step}");
32 if (defined($start)) {
33 push @cmd, "--start=${start}";
36 push @cmd, "DS:${field}:GAUGE:600:" .
37 $vars->{$field}->{'min'} . ":" .
38 $vars->{$field}->{'max'} . " ";
40 push @cmd, "RRA:AVERAGE:0.5:1:${keep}";
44 print "Error while creating: " . RRDs
::error
. "\n";
51 my $community = shift;
54 (my $session, my $error) = Net
::SNMP
->session(Hostname
=> $address,
55 Community
=> $community);
57 die "session error: $error" unless ($session);
59 $session->translate(0);
61 my $val = $session->get_request($oid);
70 print STDERR
"Running as root, please provide UID as 2th argument!\n";
74 print "Running as root, switching to ".$ARGV[1]."\n";
78 foreach my $host (@
$hosts) {
79 my $rrdfile = $host->{'rrdfile'};
81 if (-e
"${rrdfile}") {
82 print "Reading old ${rrdfile} to preserve data...\n";
84 my $rrdinfo = RRDs
::info
("${rrdfile}");
86 print "Error while getting info: " . RRDs
::error
. "\n";
90 (my $start, my $ostep, my $names, my $data) =
91 RRDs
::fetch
("${rrdfile}",
92 "-s " . (time() - ($rrdinfo->{'rra[0].rows'} * $rrdinfo->{'step'})),
96 print "Error while fetching data: " . RRDs
::error
. "\n";
100 foreach my $field (@
$names) {
101 if (! -e
"${rrdfile}.${field}") {
102 rrdcreate
("${rrdfile}.${field}",
105 (${start
}-${ostep
}));
110 foreach my $line (@
$data) {
111 foreach my $field (@
$names) {
112 my $val = shift (@
$line);
113 $val = 'U' if (!defined($val));
115 RRDs
::update
("${rrdfile}.${field}", "${pos}:${val}");
117 print "Can't insert data: " . RRDs
::error
. "\n";
125 if ((($pos-$start)/$ostep) == $#$data) {
130 rename("${rrdfile}", "${rrdfile}.old") or die "Can't rename old file: $!\n";
133 foreach my $field (@
{$host->{'fields'}}) {
134 if (! -e
"${rrdfile}.${field}") {
135 print "Creating ${rrdfile}.${field}...\n";
136 rrdcreate
("${rrdfile}.${field}",
141 my $rrdinfo = RRDs
::info
("${rrdfile}.${field}");
143 print "Error while getting info: " . RRDs
::error
. "\n";
147 if ($rrdinfo->{'rra[0].rows'} != $keep) {
148 print "Resizing ${rrdfile}.${field} from " . $rrdinfo->{'rra[0].rows'} .
149 " to ${keep} samples.\n";
151 (my $start, my $ostep, my $names, my $data) =
152 RRDs
::fetch
("${rrdfile}.${field}",
153 "-s " . (time() - ($rrdinfo->{'rra[0].rows'} * $rrdinfo->{'step'})),
157 print "Error while fetching data: " . RRDs
::error
. "\n";
161 rrdcreate
("${rrdfile}.${field}.new",
164 (${start
}-${ostep
}));
166 print "Preserving data since " . localtime($start) . "\n";
169 foreach my $line (@
$data) {
170 my $vline = "${pos}";
172 foreach my $val (@
$line) {
173 $val = 'U' if (!defined($val));
176 RRDs
::update
("${rrdfile}.${field}.new", $vline) or die "Can't insert data\n";
179 print "Error while updating: " . RRDs
::error
. "\n";
184 if ((($pos-$start)/$ostep) == $#$data) {
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";
192 $rrdinfo = RRDs
::info
("${rrdfile}.${field}");
194 print "Error while getting info: " . RRDs
::error
. "\n";
198 if ($rrdinfo->{'rra[0].rows'} != $keep) {
208 die "fork failed!" if (!defined($child));
210 exit 0 if ($child != 0);
213 open(HTML
, ">${outdir}/index.html.new");
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">';
218 foreach my $host (@
$hosts) {
219 my $vars = $host->{'vars'};
220 my $rrdfile = $host->{'rrdfile'};
221 my $hostname = $host->{'name'};
223 foreach my $var (@
{$host->{'fields'}}) {
224 delete $vars->{$var}->{'value'};
226 my $result = fetch_snmp
($host->{'address'}, $host->{'community'}, $vars->{$var}->{'oid'});
227 next unless (defined $result);
229 $vars->{$var}->{'value'} = $result->{$vars->{$var}->{'oid'}};
230 if (defined($vars->{$var}->{'factor'})) {
231 $vars->{$var}->{'value'} *= $vars->{$var}->{'factor'};
235 foreach my $var (@
{$host->{'fields'}}) {
236 if (!(defined($vars->{$var}->{'value'}))) {
237 $vars->{$var}->{'value'} = 'U';
239 RRDs
::update
("${rrdfile}.${var}", "N:" . $vars->{$var}->{'value'});
242 print "Error while updating: " . RRDs
::error
. "\n";
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);
252 print "Error while graphing: " . RRDs
::error
. "\n";
254 rename("${outdir}/${hostname}.${var}.png.new", "${outdir}/${hostname}.${var}.png");
257 print HTML
"<a href=\"${hostname}.${var}.html\"><img src=\"${hostname}.${var}.png\" width=\"${width}\" height=\"${height}\" border=\"0\"></a>";
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">';
264 push @graphdef, "VDEF:min=${var},MINIMUM";
265 push @graphdef, "GPRINT:min:Minimum\\: %.2lf";
267 push @graphdef, "VDEF:avg=${var},AVERAGE";
268 push @graphdef, "GPRINT:avg:Average\\: %.2lf";
270 push @graphdef, "VDEF:max=${var},MAXIMUM";
271 push @graphdef, "GPRINT:max:Maximum\\: %.2lf";
273 push @graphdef, "VDEF:cur=${var},LAST";
274 push @graphdef, "GPRINT:cur:Current\\: %.2lf";
276 ($averages, $width, $height) =
277 RRDs
::graph
("${outdir}/${hostname}.${var}.long.png.new",
278 "-w", "1008", @graphdef);
281 print "Error while graphing: " . RRDs
::error
. "\n";
283 rename("${outdir}/${hostname}.${var}.long.png.new", "${outdir}/${hostname}.${var}.long.png");
286 print HTML2
"<img src=\"${hostname}.${var}.long.png\" width=\"${width}\" height=\"${height}\"><br>";
288 ($averages, $width, $height) =
289 RRDs
::graph
("${outdir}/${hostname}.${var}.week.png.new",
290 "-w", "1008", "-e", "now", "-s", "end-1w", @graphdef);
293 print "Error while graphing: " . RRDs
::error
. "\n";
295 rename("${outdir}/${hostname}.${var}.week.png.new", "${outdir}/${hostname}.${var}.week.png");
298 print HTML2
"<img src=\"${hostname}.${var}.week.png\" width=\"${width}\" height=\"${height}\"><br>";
300 ($averages, $width, $height) =
301 RRDs
::graph
("${outdir}/${hostname}.${var}.year.png.new",
302 "-w", "1008", "-e", "now", "-s", "end-1y", @graphdef);
305 print "Error while graphing: " . RRDs
::error
. "\n";
307 rename("${outdir}/${hostname}.${var}.year.png.new", "${outdir}/${hostname}.${var}.year.png");
310 print HTML2
"<img src=\"${hostname}.${var}.year.png\" width=\"${width}\" height=\"${height}\"><br>";
312 print HTML2
"</body></html>\n";
314 rename("${outdir}/${hostname}.${var}.html.new", "${outdir}/${hostname}.${var}.html");
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>.';
324 rename("${outdir}/index.html.new", "${outdir}/index.html");