]> git.zerfleddert.de Git - upsgraph/blob - upsgraph.pl
020569f69534184d9e387eee923fc7660b2b1957
[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
10 $UPSGRAPH::host = "";
11 $UPSGRAPH::rrdfile = "";
12 $UPSGRAPH::outdir = "";
13 $UPSGRAPH::community = "public";
14 $UPSGRAPH::step = 60;
15 @UPSGRAPH::fields = ();
16 $UPSGRAPH::vars = {};
17
18 do $ARGV[0] or die "can't read config: $!";
19
20 my $host = $UPSGRAPH::host;
21 my $rrdfile = $UPSGRAPH::rrdfile;
22 my $outdir = $UPSGRAPH::outdir;
23 my $community = $UPSGRAPH::community;
24 my $step = $UPSGRAPH::step;
25 my @fields = @UPSGRAPH::fields;
26 my $vars = $UPSGRAPH::vars;
27
28 if ($> == 0) {
29 if (@ARGV != 2) {
30 print STDERR "Running as root, please provide UID as 4th argument!\n";
31 exit(1);
32 }
33
34 print "Running as root, switching to ".$ARGV[3]."\n";
35 $< = $> = $ARGV[1];
36 }
37
38 if (! -e "${rrdfile}") {
39 my $cmd = "rrdtool create \"${rrdfile}\" ";
40 foreach my $var (@fields) {
41 $cmd .= "DS:${var}:GAUGE:600:" .
42 $vars->{$var}->{'min'} . ":" .
43 $vars->{$var}->{'max'} . " ";
44 }
45 $cmd .= "RRA:AVERAGE:0.5:1:10080 --step ${step}";
46
47 print "Creating ${rrdfile}...\n";
48 print `${cmd}`;
49 }
50
51 my $child = fork();
52
53 die "fork failed!" if (!defined($child));
54
55 exit 0 if ($child != 0);
56
57 while(1) {
58 ($session,$error) = Net::SNMP->session(Hostname => $host,
59 Community => $community);
60
61 die "session error: $error" unless ($session);
62
63 $session->translate(0);
64
65 foreach my $var (@fields) {
66 delete $vars->{$var}->{'value'};
67
68 my $result = $session->get_request($vars->{$var}->{'oid'});
69 next unless (defined $result);
70
71 $vars->{$var}->{'value'} = $result->{$vars->{$var}->{'oid'}};
72 if (defined($vars->{$var}->{'factor'})) {
73 $vars->{$var}->{'value'} *= $vars->{$var}->{'factor'};
74 }
75 }
76
77 $session->close;
78
79 my $cmd = "rrdtool update \"${rrdfile}\" \"N";
80 foreach my $var (@fields) {
81 if (!(defined($vars->{$var}->{'value'}))) {
82 $vars->{$var}->{'value'} = 'U';
83 }
84 $cmd .= ":" . $vars->{$var}->{'value'};
85 }
86 $cmd .= "\"";
87 print `${cmd}`;
88
89 open(HTML, ">${outdir}/index.html.new");
90
91 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>';
92 print HTML '<body bgcolor="#ffffff">';
93
94 foreach my $var (@fields) {
95 my $graphdef = "-t \"" . $vars->{$var}->{'name'} . "\" \"DEF:${var}=${rrdfile}:${var}:AVERAGE\" \"LINE1:${var}#FF0000\"";
96 my $cmd = "rrdtool graph \"${outdir}/${var}.png.new\" -w 720 ${graphdef}";
97 my $size = `$cmd`;
98 rename("${outdir}/${var}.png.new", "${outdir}/${var}.png");
99 (my $width, my $height) = split(/x/,$size);
100
101 my $cmd2 = "rrdtool graph \"${outdir}/${var}.long.png.new\" -w 1008 -e now -s end-1w ${graphdef}";
102 my $size2 = `$cmd2`;
103 rename("${outdir}/${var}.long.png.new", "${outdir}/${var}.long.png");
104
105 print HTML "<a href=\"${var}.long.png\"><img src=\"${var}.png\" width=\"${width}\" height=\"${height}\" border=\"0\"></a>";
106 }
107
108 print HTML "</body></html>\n";
109 print HTML "<br>Generated on: " . localtime(time());
110 print HTML ' by <a href="http://git.zerfleddert.de/cgi-bin/gitweb.cgi/upsgraph">upsgraph</a>.';
111
112 close(HTML);
113
114 rename("${outdir}/index.html.new", "${outdir}/index.html");
115
116 sleep(${step}/2);
117 }
Impressum, Datenschutz