]> git.zerfleddert.de Git - upsgraph/blob - upsgraph.pl
bef10a5460a9d302b8eb974c630b7e1ffec577ec
[upsgraph] / upsgraph.pl
1 #!/usr/bin/perl -w
2
3 if ((@ARGV != 3) && (@ARGV != 4)) {
4 print STDERR "Syntax: ${0} host /path/to/file.rrd /output/directory/ [uid]\n";
5 exit(1);
6 }
7
8 my $host=$ARGV[0];
9 my $rrdfile=$ARGV[1];
10 my $outdir=$ARGV[2];
11 my $community="public";
12 my $step=60;
13
14 my @fields = ( 'inputV', 'outputV', 'inputHZ', 'outputHZ', 'battT', 'battC', 'load', 'ambT', 'ambH', 'timeR', 'timeO' );
15
16 my $vars = {
17 'inputV' => {
18 'name' => 'Input Voltage',
19 'oidtext' => 'PowerNet-MIB::upsAdvInputLineVoltage.0',
20 'oid' => '1.3.6.1.4.1.318.1.1.1.3.2.1.0',
21 'min' => '180',
22 'max' => '280',
23 },
24 'outputV' => {
25 'name' => 'Output Voltage',
26 'oidtext' => 'PowerNet-MIB::upsAdvOutputVoltage.0',
27 'oid' => '1.3.6.1.4.1.318.1.1.1.4.2.1.0',
28 'min' => '180',
29 'max' => '280',
30 },
31 'inputHZ' => {
32 'name' => 'Input Frequency',
33 'oidtext' => 'PowerNet-MIB::upsAdvInputFrequency.0',
34 'oid' => '1.3.6.1.4.1.318.1.1.1.3.2.4.0',
35 'min' => '40',
36 'max' => '60',
37 },
38 'outputHZ' => {
39 'name' => 'Output Frequency',
40 'oidtext' => 'PowerNet-MIB::upsAdvOutputFrequency.0',
41 'oid' => '1.3.6.1.4.1.318.1.1.1.4.2.2.0',
42 'min' => '40',
43 'max' => '60',
44 },
45 'battT' => {
46 'name' => 'Battery Temperature',
47 'oidtext' => 'PowerNet-MIB::upsAdvBatteryTemperature.0',
48 'oid' => '1.3.6.1.4.1.318.1.1.1.2.2.2.0',
49 'min' => '0',
50 'max' => '100',
51 },
52 'battC' => {
53 'name' => 'Battery Capacity',
54 'oidtext' => 'PowerNet-MIB::upsAdvBatteryCapacity.0',
55 'oid' => '1.3.6.1.4.1.318.1.1.1.2.2.1.0',
56 'min' => '0',
57 'max' => '110',
58 },
59 'load' => {
60 'name' => 'UPS Load',
61 'oidtext' => 'PowerNet-MIB::upsAdvOutputLoad.0',
62 'oid' => '1.3.6.1.4.1.318.1.1.1.4.2.3.0',
63 'min' => '0',
64 'max' => '110',
65 },
66 'ambT' => {
67 'name' => 'Ambient Temperature',
68 'oidtext' => 'PowerNet-MIB::mUpsEnvironAmbientTemperature.0',
69 'oid' => '1.3.6.1.4.1.318.1.1.2.1.1.0',
70 'min' => '0',
71 'max' => '60',
72 },
73 'ambH' => {
74 'name' => 'Ambient Humidity',
75 'oidtext' => 'PowerNet-MIB::mUpsEnvironRelativeHumidity.0',
76 'oid' => '1.3.6.1.4.1.318.1.1.2.1.2.0',
77 'min' => '0',
78 'max' => '100',
79 },
80 'timeR' => {
81 'name' => 'Time Remaining',
82 'oidtext' => 'PowerNet-MIB::upsAdvBatteryRunTimeRemaining.0',
83 'oid' => '1.3.6.1.4.1.318.1.1.1.2.2.3.0',
84 'factor' => 1/6000,
85 'min' => '0',
86 'max' => '360',
87 },
88 'timeO' => {
89 'name' => 'Time On Battery',
90 'oidtext' => 'PowerNet-MIB::upsBasicBatteryTimeOnBattery.0',
91 'oid' => '1.3.6.1.4.1.318.1.1.1.2.1.2.0',
92 'factor' => 1/6000,
93 'min' => '0',
94 'max' => '360',
95 },
96 };
97
98 use Net::SNMP;
99
100 if ($> == 0) {
101 if (@ARGV != 4) {
102 print STDERR "Running as root, please provide UID as 4th argument!\n";
103 exit(1);
104 }
105
106 print "Running as root, switching to ".$ARGV[3]."\n";
107 $< = $> = $ARGV[3];
108 }
109
110 if (! -e "${rrdfile}") {
111 my $cmd = "rrdtool create \"${rrdfile}\" ";
112 foreach my $var (@fields) {
113 $cmd .= "DS:${var}:GAUGE:600:" .
114 $vars->{$var}->{'min'} . ":" .
115 $vars->{$var}->{'max'} . " ";
116 }
117 $cmd .= "RRA:AVERAGE:0.5:1:10080 --step ${step}";
118
119 print "Creating ${rrdfile}...\n";
120 print `${cmd}`;
121 }
122
123 my $child = fork();
124
125 die "fork failed!" if (!defined($child));
126
127 exit 0 if ($child != 0);
128
129 while(1) {
130 ($session,$error) = Net::SNMP->session(Hostname => $host,
131 Community => $community);
132
133 die "session error: $error" unless ($session);
134
135 $session->translate(0);
136
137 foreach my $var (@fields) {
138 delete $vars->{$var}->{'value'};
139
140 my $result = $session->get_request($vars->{$var}->{'oid'});
141 next unless (defined $result);
142
143 $vars->{$var}->{'value'} = $result->{$vars->{$var}->{'oid'}};
144 if (defined($vars->{$var}->{'factor'})) {
145 $vars->{$var}->{'value'} *= $vars->{$var}->{'factor'};
146 }
147 }
148
149 $session->close;
150
151 my $cmd = "rrdtool update \"${rrdfile}\" \"N";
152 foreach my $var (@fields) {
153 if (!(defined($vars->{$var}->{'value'}))) {
154 $vars->{$var}->{'value'} = 'U';
155 }
156 $cmd .= ":" . $vars->{$var}->{'value'};
157 }
158 $cmd .= "\"";
159 print `${cmd}`;
160
161 open(HTML, ">${outdir}/index.html.new");
162
163 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>';
164 print HTML '<body bgcolor="#ffffff">';
165
166 foreach my $var (@fields) {
167 my $graphdef = "-t \"" . $vars->{$var}->{'name'} . "\" \"DEF:${var}=${rrdfile}:${var}:AVERAGE\" \"LINE1:${var}#FF0000\"";
168 my $cmd = "rrdtool graph \"${outdir}/${var}.png.new\" -w 720 ${graphdef}";
169 my $size = `$cmd`;
170 rename("${outdir}/${var}.png.new", "${outdir}/${var}.png");
171 (my $width, my $height) = split(/x/,$size);
172
173 my $cmd2 = "rrdtool graph \"${outdir}/${var}.long.png.new\" -w 1008 -e now -s end-1w ${graphdef}";
174 my $size2 = `$cmd2`;
175 rename("${outdir}/${var}.long.png.new", "${outdir}/${var}.long.png");
176
177 print HTML "<a href=\"${var}.long.png\"><img src=\"${var}.png\" width=\"${width}\" height=\"${height}\" border=\"0\"></a>";
178 }
179
180 print HTML "</body></html>\n";
181 print HTML "<br>Generated on: " . localtime(time());
182 print HTML ' by <a href="http://git.zerfleddert.de/cgi-bin/gitweb.cgi/upsgraph">upsgraph</a>.';
183
184 close(HTML);
185
186 rename("${outdir}/index.html.new", "${outdir}/index.html");
187
188 sleep(${step}/2);
189 }
Impressum, Datenschutz