]>
git.zerfleddert.de Git - upsgraph/blob - upsgraph.pl
9ed59ba5f5b81b9a0e646356d612477ddb075eca
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
;
37 my @cmd = ("${newrrd}", "--step=${step}");
39 if (defined($start)) {
40 push @cmd, "--start=${start}";
43 push @cmd, "DS:${field}:GAUGE:600:" .
44 $vars->{$field}->{'min'} . ":" .
45 $vars->{$field}->{'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 "Reading old ${rrdfile} to preserve data...\n";
69 my $rrdinfo = RRDs
::info
("${rrdfile}");
71 print "Error while getting info: " . RRDs
::error
. "\n";
75 (my $start, my $ostep, my $names, my $data) =
76 RRDs
::fetch
("${rrdfile}",
77 "-s " . (time() - ($rrdinfo->{'rra[0].rows'} * $rrdinfo->{'step'})),
81 print "Error while fetching data: " . RRDs
::error
. "\n";
85 foreach my $field (@
$names) {
86 if (! -e
"${rrdfile}.${field}") {
87 rrdcreate
("${rrdfile}.${field}","${field}",(${start
}-${ostep
}));
92 foreach my $line (@
$data) {
93 foreach my $field (@
$names) {
94 my $val = shift (@
$line);
95 next if (!defined($val));
97 RRDs
::update
("${rrdfile}.${field}", "${pos}:${val}");
99 print "Can't insert data: " . RRDs
::error
. "\n";
107 if ((($pos-$start)/$ostep) == $#$data) {
112 rename("${rrdfile}", "${rrdfile}.old") or die "Can't rename old file: $!\n";
115 foreach my $field (@fields) {
116 if (! -e
"${rrdfile}.${field}") {
117 print "Creating ${rrdfile}.${field}...\n";
118 rrdcreate
("${rrdfile}.${field}","${field}");
121 my $rrdinfo = RRDs
::info
("${rrdfile}.${field}");
123 print "Error while getting info: " . RRDs
::error
. "\n";
127 if ($rrdinfo->{'rra[0].rows'} != $keep) {
128 print "Resizing ${rrdfile}.${field} from " . $rrdinfo->{'rra[0].rows'} .
129 " to ${keep} samples.\n";
131 (my $start, my $ostep, my $names, my $data) =
132 RRDs
::fetch
("${rrdfile}.${field}",
133 "-s " . (time() - ($rrdinfo->{'rra[0].rows'} * $rrdinfo->{'step'})),
137 print "Error while fetching data: " . RRDs
::error
. "\n";
141 rrdcreate
("${rrdfile}.${field}.new", "${field}", (${start
}-${ostep
}));
143 print "Preserving data since " . localtime($start) . "\n";
146 foreach my $line (@
$data) {
147 my $vline = "${pos}";
149 foreach my $val (@
$line) {
150 $val = 'U' if (!defined($val));
153 RRDs
::update
("${rrdfile}.${field}.new", $vline) or die "Can't insert data\n";
156 print "Error while updating: " . RRDs
::error
. "\n";
161 if ((($pos-$start)/$ostep) == $#$data) {
166 rename("${rrdfile}.${field}", "${rrdfile}.${field}.old") or die "Can't rename old file: $!\n";
167 rename("${rrdfile}.${field}.new", "${rrdfile}.${field}") or die "Can't rename new file: $!\n";
169 $rrdinfo = RRDs
::info
("${rrdfile}.${field}");
171 print "Error while getting info: " . RRDs
::error
. "\n";
175 if ($rrdinfo->{'rra[0].rows'} != $keep) {
184 die "fork failed!" if (!defined($child));
186 exit 0 if ($child != 0);
189 ($session,$error) = Net
::SNMP
->session(Hostname
=> $host,
190 Community
=> $community);
192 die "session error: $error" unless ($session);
194 $session->translate(0);
196 foreach my $var (@fields) {
197 delete $vars->{$var}->{'value'};
199 my $result = $session->get_request($vars->{$var}->{'oid'});
200 next unless (defined $result);
202 $vars->{$var}->{'value'} = $result->{$vars->{$var}->{'oid'}};
203 if (defined($vars->{$var}->{'factor'})) {
204 $vars->{$var}->{'value'} *= $vars->{$var}->{'factor'};
210 foreach my $var (@fields) {
211 if (!(defined($vars->{$var}->{'value'}))) {
212 $vars->{$var}->{'value'} = 'U';
214 RRDs
::update
("${rrdfile}.${var}", "N:" . $vars->{$var}->{'value'});
217 print "Error while updating: " . RRDs
::error
. "\n";
220 open(HTML
, ">${outdir}/index.html.new");
222 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>';
223 print HTML
'<body bgcolor="#ffffff">';
225 foreach my $var (@fields) {
226 my @graphdef = ("-t", $vars->{$var}->{'name'}, "DEF:${var}=${rrdfile}.${var}:${var}:AVERAGE", "LINE1:${var}#FF0000");
227 (my $averages, my $width, my $height) =
228 RRDs
::graph
("${outdir}/${var}.png.new",
229 "-w", "720", @graphdef);
232 print "Error while graphing: " . RRDs
::error
. "\n";
234 rename("${outdir}/${var}.png.new", "${outdir}/${var}.png");
237 print HTML
"<a href=\"${var}.html\"><img src=\"${var}.png\" width=\"${width}\" height=\"${height}\" border=\"0\"></a>";
239 open (HTML2
, ">${outdir}/${var}.html.new");
240 print HTML2
"<html><head><title>" . $vars->{$var}->{'name'} . "</title></head>";
241 print HTML2
'<body bgcolor="#ffffff">';
244 push @graphdef, "VDEF:min=${var},MINIMUM";
245 push @graphdef, "GPRINT:min:Minimum\\: %.2lf";
247 push @graphdef, "VDEF:avg=${var},AVERAGE";
248 push @graphdef, "GPRINT:avg:Average\\: %.2lf";
250 push @graphdef, "VDEF:max=${var},MAXIMUM";
251 push @graphdef, "GPRINT:max:Maximum\\: %.2lf";
253 push @graphdef, "VDEF:cur=${var},LAST";
254 push @graphdef, "GPRINT:cur:Current\\: %.2lf";
256 ($averages, $width, $height) =
257 RRDs
::graph
("${outdir}/${var}.long.png.new",
258 "-w", "1008", @graphdef);
261 print "Error while graphing: " . RRDs
::error
. "\n";
263 rename("${outdir}/${var}.long.png.new", "${outdir}/${var}.long.png");
266 print HTML2
"<img src=\"${var}.long.png\" width=\"${width}\" height=\"${height}\"><br>";
268 ($averages, $width, $height) =
269 RRDs
::graph
("${outdir}/${var}.week.png.new",
270 "-w", "1008", "-e", "now", "-s", "end-1w", @graphdef);
273 print "Error while graphing: " . RRDs
::error
. "\n";
275 rename("${outdir}/${var}.week.png.new", "${outdir}/${var}.week.png");
278 print HTML2
"<img src=\"${var}.week.png\" width=\"${width}\" height=\"${height}\"><br>";
280 ($averages, $width, $height) =
281 RRDs
::graph
("${outdir}/${var}.year.png.new",
282 "-w", "1008", "-e", "now", "-s", "end-1y", @graphdef);
285 print "Error while graphing: " . RRDs
::error
. "\n";
287 rename("${outdir}/${var}.year.png.new", "${outdir}/${var}.year.png");
290 print HTML2
"<img src=\"${var}.year.png\" width=\"${width}\" height=\"${height}\"><br>";
292 print HTML2
"</body></html>\n";
294 rename("${outdir}/${var}.html.new", "${outdir}/${var}.html");
297 print HTML
"</body></html>\n";
298 print HTML
"<br>Generated on: " . localtime(time());
299 print HTML
' by <a href="http://git.zerfleddert.de/cgi-bin/gitweb.cgi/upsgraph">upsgraph</a>.';
303 rename("${outdir}/index.html.new", "${outdir}/index.html");