-
Notifications
You must be signed in to change notification settings - Fork 187
/
ups-apcups
executable file
·138 lines (111 loc) · 4.02 KB
/
ups-apcups
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/env perl
# Author: Zane C. Bowers-Hadley <vvelox@vvelox.net>
# https://docs.librenms.org/#Extensions/Applications/#ups-apcups
# See the above for additional information not documented in the POD below.
=head1 DESCRIPTION
This is a SNMP extend for apcupsd for use with LibreNMS.
For more information, see L<https://docs.librenms.org/#Extensions/Applications/#ups-apcups>.
=head1 SWITCHES
=head2 -p
Pretty print the JSON.
=head1 SNMPD SETUP EXAMPLES
Below is a basic example of setting it up snmpd.conf for NetSNMP.
extend ups-apcups /etc/snmp/ups-apcups
Now if for example apcaccess is not in the PATH enviromental variables that snmpd is running
with, you may need to do something like below.
extend /usr/bin/env PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin /etc/snmp/ups-apcups
=cut
#Copyright (c) 2018, Zane C. Bowers-Hadley
#All rights reserved.
#
#Redistribution and use in source and binary forms, with or without modification,
#are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
#THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
#ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
#WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
#IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
#INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
#BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
#DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
#LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
#THE POSSIBILITY OF SUCH DAMAGE.
use strict;
use warnings;
use Getopt::Std;
use JSON;
# should be no reason to change this
# better to use env to make sure it is in your path when you run this
my $apcaccess='apcaccess';
$Getopt::Std::STANDARD_HELP_VERSION = 1;
sub main::VERSION_MESSAGE {
print "ups-apcups SNMP extend 0.0.0\n";
};
sub main::HELP_MESSAGE {
print "\n";
}
#gets the options
my %opts=();
getopts('p', \%opts);
#holds what will be returned
my %data;
my %toReturn;
$toReturn{version}=1;
# get the current status from apcupsd
my $apcaccess_output=`$apcaccess`;
$toReturn{error}=$?;
# check for bad exit codes
if ( $? == -1) {
$toReturn{errorString}='failed to run apcaccess';
} elsif ($? & 127) {
$toReturn{errorString}= sprintf "apcaccess died with signal %d, %s coredump\n",
($? & 127), ($? & 128) ? 'with' : 'without';
} else {
$toReturn{error}=$? >> 8;
$toReturn{errorString}="apcaccess exited with ".$toReturn{error};
}
# if no bad exit codes, we can process $apcaccess_output
if ( $toReturn{error} == 0 ) {
# holds the found data for the apcupsd status
my %status;
# pulls apart the output
my @lines=split(/\n/, $apcaccess_output);
foreach my $line ( @lines ) {
my ( $var, $val )=split(/\:\ */, $line, 2);
if (
defined( $var ) && defined( $val )
) {
$var=~s/\ .*//;
$val=~s/\ .*//;
$status{$var}=$val;
}
}
#pull the desired variables from the output
$data{charge}=$status{BCHARGE};
$data{time_remaining}=$status{TIMELEFT};
$data{battery_nominal}=$status{NOMBATTV};
$data{battery_voltage}=$status{BATTV};
$data{input_voltage}=$status{LINEV};
$data{nominal_voltage}=$status{NOMINV};
$data{load}=$status{LOADPCT};
}
# add the data to be return to the return hah
$toReturn{data}=\%data;
# convert $toReturn to JSON and pretty print if asked to
my $j=JSON->new;
$j->canonical(1);
if ( $opts{p} ) {
$j->pretty(1);
}
print $j->encode( \%toReturn );
if (! $opts{p} ) {
print "\n";
}
exit 0;