]>
git.zerfleddert.de Git - upsgraph/blob - upsgraph.pl
3d35c2a1059304bad881cfa1a5d7c01f070804da
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">';
243 ($averages, $width, $height) =
244 RRDs
::graph
("${outdir}/${var}.long.png.new",
245 "-w", "1008", @graphdef);
248 print "Error while graphing: " . RRDs
::error
. "\n";
250 rename("${outdir}/${var}.long.png.new", "${outdir}/${var}.long.png");
253 print HTML2
"<img src=\"${var}.long.png\" width=\"${width}\" height=\"${height}\"><br>";
255 ($averages, $width, $height) =
256 RRDs
::graph
("${outdir}/${var}.week.png.new",
257 "-w", "1008", "-e", "now", "-s", "end-1w", @graphdef);
260 print "Error while graphing: " . RRDs
::error
. "\n";
262 rename("${outdir}/${var}.week.png.new", "${outdir}/${var}.week.png");
265 print HTML2
"<img src=\"${var}.week.png\" width=\"${width}\" height=\"${height}\"><br>";
267 ($averages, $width, $height) =
268 RRDs
::graph
("${outdir}/${var}.year.png.new",
269 "-w", "1008", "-e", "now", "-s", "end-1y", @graphdef);
272 print "Error while graphing: " . RRDs
::error
. "\n";
274 rename("${outdir}/${var}.year.png.new", "${outdir}/${var}.year.png");
277 print HTML2
"<img src=\"${var}.year.png\" width=\"${width}\" height=\"${height}\"><br>";
279 print HTML2
"</body></html>\n";
281 rename("${outdir}/${var}.html.new", "${outdir}/${var}.html");
284 print HTML
"</body></html>\n";
285 print HTML
"<br>Generated on: " . localtime(time());
286 print HTML
' by <a href="http://git.zerfleddert.de/cgi-bin/gitweb.cgi/upsgraph">upsgraph</a>.';
290 rename("${outdir}/index.html.new", "${outdir}/index.html");