]>
git.zerfleddert.de Git - upsgraph/blob - upsgraph.pl
3 if ((@ARGV != 1) && (@ARGV != 2)) {
4 print STDERR
"Syntax: ${0} configfile [uid]\n";
13 $UPSGRAPH::rrdfile
= "";
14 $UPSGRAPH::outdir
= "";
15 $UPSGRAPH::community
= "public";
17 $UPSGRAPH::keep
= (370*24*60*60)/$UPSGRAPH::step
;
18 @UPSGRAPH::fields
= ();
21 do $ARGV[0] or die "can't read config: $!";
23 my $host = $UPSGRAPH::host
;
24 my $rrdfile = $UPSGRAPH::rrdfile
;
25 my $outdir = $UPSGRAPH::outdir
;
26 my $community = $UPSGRAPH::community
;
27 my $step = $UPSGRAPH::step
;
28 my $keep = $UPSGRAPH::keep
;
29 my @fields = @UPSGRAPH::fields
;
30 my $vars = $UPSGRAPH::vars
;
36 my @cmd = ("${rrdfile}", "--step=${step}");
38 if (defined($start)) {
39 push @cmd, "--start=${start}";
42 foreach my $var (@fields) {
43 push @cmd, "DS:${var}:GAUGE:600:" .
44 $vars->{$var}->{'min'} . ":" .
45 $vars->{$var}->{'max'} . " ";
47 push @cmd, "RRA:AVERAGE:0.5:1:${keep}";
51 print "Error while creating: " . RRDs
::error
. "\n";
58 print STDERR
"Running as root, please provide UID as 2th argument!\n";
62 print "Running as root, switching to ".$ARGV[1]."\n";
66 if (! -e
"${rrdfile}") {
67 print "Creating ${rrdfile}...\n";
68 rrdcreate
("${rrdfile}");
71 my $rrdinfo = RRDs
::info
("${rrdfile}");
73 print "Error while getting info: " . RRDs
::error
. "\n";
77 if ($rrdinfo->{'rra[0].rows'} != $keep) {
78 print "Resizing ${rrdfile} from " . $rrdinfo->{'rra[0].rows'} .
79 " to ${keep} samples.\n";
81 (my $start, my $ostep, my $names, my $data) =
82 RRDs
::fetch
("${rrdfile}",
83 "-s " . (time() - ($rrdinfo->{'rra[0].rows'} * $rrdinfo->{'step'})),
87 print "Error while fetching data: " . RRDs
::error
. "\n";
91 rrdcreate
("${rrdfile}.new", (${start
}-${ostep
}));
93 print "Preserving data since " . localtime($start) . "\n";
96 foreach my $line (@
$data) {
99 foreach my $val (@
$line) {
100 $val = 'U' if (!defined($val));
103 RRDs
::update
("${rrdfile}.new", $vline) or die "Can't insert data\n";
106 print "Error while updating: " . RRDs
::error
. "\n";
111 if ((($pos-$start)/$ostep) == $#$data) {
116 rename("${rrdfile}", "${rrdfile}.old") or die "Can't rename old file: $!\n";
117 rename("${rrdfile}.new", "${rrdfile}") or die "Can't rename new file: $!\n";
119 $rrdinfo = RRDs
::info
("${rrdfile}");
121 print "Error while getting info: " . RRDs
::error
. "\n";
125 if ($rrdinfo->{'rra[0].rows'} != $keep) {
133 die "fork failed!" if (!defined($child));
135 exit 0 if ($child != 0);
138 ($session,$error) = Net
::SNMP
->session(Hostname
=> $host,
139 Community
=> $community);
141 die "session error: $error" unless ($session);
143 $session->translate(0);
145 foreach my $var (@fields) {
146 delete $vars->{$var}->{'value'};
148 my $result = $session->get_request($vars->{$var}->{'oid'});
149 next unless (defined $result);
151 $vars->{$var}->{'value'} = $result->{$vars->{$var}->{'oid'}};
152 if (defined($vars->{$var}->{'factor'})) {
153 $vars->{$var}->{'value'} *= $vars->{$var}->{'factor'};
160 foreach my $var (@fields) {
161 if (!(defined($vars->{$var}->{'value'}))) {
162 $vars->{$var}->{'value'} = 'U';
164 $vline .= ":" . $vars->{$var}->{'value'};
166 RRDs
::update
("${rrdfile}", $vline);
168 print "Error while updating: " . RRDs
::error
. "\n";
171 open(HTML
, ">${outdir}/index.html.new");
173 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>';
174 print HTML
'<body bgcolor="#ffffff">';
176 foreach my $var (@fields) {
177 my @graphdef = ("-t", $vars->{$var}->{'name'}, "DEF:${var}=${rrdfile}:${var}:AVERAGE", "LINE1:${var}#FF0000");
178 (my $averages, my $width, my $height) =
179 RRDs
::graph
("${outdir}/${var}.png.new",
180 "-w", "720", @graphdef);
183 print "Error while graphing: " . RRDs
::error
. "\n";
185 rename("${outdir}/${var}.png.new", "${outdir}/${var}.png");
188 print HTML
"<a href=\"${var}.html\"><img src=\"${var}.png\" width=\"${width}\" height=\"${height}\" border=\"0\"></a>";
190 open (HTML2
, ">${outdir}/${var}.html.new");
191 print HTML2
"<html><head><title>" . $vars->{$var}->{'name'} . "</title></head>";
192 print HTML2
'<body bgcolor="#ffffff">';
194 ($averages, $width, $height) =
195 RRDs
::graph
("${outdir}/${var}.long.png.new",
196 "-w", "1008", @graphdef);
199 print "Error while graphing: " . RRDs
::error
. "\n";
201 rename("${outdir}/${var}.long.png.new", "${outdir}/${var}.long.png");
204 print HTML2
"<img src=\"${var}.long.png\" width=\"${width}\" height=\"${height}\"><br>";
206 ($averages, $width, $height) =
207 RRDs
::graph
("${outdir}/${var}.week.png.new",
208 "-w", "1008", "-e", "now", "-s", "end-1w", @graphdef);
211 print "Error while graphing: " . RRDs
::error
. "\n";
213 rename("${outdir}/${var}.week.png.new", "${outdir}/${var}.week.png");
216 print HTML2
"<img src=\"${var}.week.png\" width=\"${width}\" height=\"${height}\"><br>";
218 ($averages, $width, $height) =
219 RRDs
::graph
("${outdir}/${var}.year.png.new",
220 "-w", "1008", "-e", "now", "-s", "end-1y", @graphdef);
223 print "Error while graphing: " . RRDs
::error
. "\n";
225 rename("${outdir}/${var}.year.png.new", "${outdir}/${var}.year.png");
228 print HTML2
"<img src=\"${var}.year.png\" width=\"${width}\" height=\"${height}\"><br>";
230 print HTML2
"</body></html>\n";
232 rename("${outdir}/${var}.html.new", "${outdir}/${var}.html");
235 print HTML
"</body></html>\n";
236 print HTML
"<br>Generated on: " . localtime(time());
237 print HTML
' by <a href="http://git.zerfleddert.de/cgi-bin/gitweb.cgi/upsgraph">upsgraph</a>.';
241 rename("${outdir}/index.html.new", "${outdir}/index.html");