-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_socomec-battery
executable file
·286 lines (236 loc) · 8.42 KB
/
check_socomec-battery
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#! /usr/local/bin/perl
#
#
#########################################################
# Product: UPS #
# Manufacturer: Socomec #
# Modell: SOCOMEC #
# #
# Retriving battery time, status, volt and state #
# #
#########################################################
use strict;
use Net::SNMP;
use Getopt::Std;
use vars qw ($opt_h $opt_H $opt_v $opt_C $opt_w $opt_c);
use integer;
# SNMP options
my $version = "2c";
my $timeout = 2;
# Various SNMP OID prefixes to look at..
# Battery time remaining in minutes:
my $oid_xups_bat_time = ".1.3.6.1.2.1.33.1.2.3.0";
# 1 = unknown, 2 = batteryNormal, 3 = batteryLow, 4 = BatteryDepleted:
my $oid_ups_batt_status = ".1.3.6.1.2.1.33.1.2.1.0";
# Battery voltage level (0.1 V DC):
my $oid_ups_bat_voltage = ".1.3.6.1.2.1.33.1.2.5.0";
# 0 = on utility power, or if greater than 0, the time in seconds on battery power
my $oid_ups_on_battery_time = ".1.3.6.1.2.1.33.1.2.2.0";
# Thresholds
my $critical_timeleft = -1;
my $warning_timeleft = -1;
my $warning_batvolt_min = -1;
my $warning_batvolt_max = -1;
my $critical_batvolt_min = -1;
my $critical_batvolt_max = -1;
# Our return status - we start with 0 (OK) and hope for the best
my $status = 0;
# Our return string with lots of interesting stuff in it
my $returnstr = "";
# The SNMP hostname and community to use for the query
my $hostname = "";
my $community = get_snmp_community ('snmp');
# Grab the command line options
getopts("h:H:v:C:w:c:");
# If we didn't get any options, show some help and quit
if ($opt_h){
usage ();
}
# Get the hostname, if it was given..
if (defined($opt_H)){
$hostname = $opt_H;
} else {
# We really need a hostname
usage();
}
if (defined($opt_v)) {
# Get the snmp version for the UPS or use the default
$version = $opt_v;
}
# Get the SNMP community
if (defined($opt_C)){
$community = $opt_C;
}
# Grab the warning thresholds
if (defined($opt_w)) {
my @warning = split(/\./, $opt_w);
if (!($warning[0] eq "")) {
$warning_timeleft = $warning[0];
}
if (!($warning[1] eq "" )) {
$warning_batvolt_min = $warning[1];
}
if (!($warning[2] eq "" )) {
$warning_batvolt_max = $warning[2];
}
}
# Grab the critical thresholds
if (defined($opt_c)) {
my @critical = split(/\./, $opt_c);
if (!($critical[0] eq "")) {
$critical_timeleft = $critical[0];
}
if (!($critical[1] eq "" )) {
$critical_batvolt_min = $critical[1];
}
if (!($critical[2] eq "" )) {
$critical_batvolt_max = $critical[2];
}
}
# If we received both warning and critical times values, make sure
# the warning values are higher than the critical values
if (($warning_timeleft != -1) &&
($critical_timeleft != -1) &&
($critical_timeleft >= $warning_timeleft)) {
exit_clean ('CRITICAL', "The critical remaining time threshold must be smaller than the warning\n");
}
# Initialise the SNMP session via the Net::SNMP perl module
my ($snmp_session, $snmp_error) = Net::SNMP->session(
-community => $community,
-hostname => $hostname,
-version => $version,
-timeout => $timeout,
);
# Grab interesting details
check_device();
# Shut down the SNMP session
$snmp_session->close();
# Do not use numerical comparision to avoid failures with ePN in Nagios2. Sigh.
$status = 'UNKNOWN' if ("$status" eq '-1');
$status = 'OK' if ("$status" eq '0');
$status = 'WARNING' if ("$status" eq '1');
$status = 'CRITICAL' if ("$status" eq '2');
exit_clean ($status, $returnstr . "\n");
# Change the status level
sub status {
my $newstatus = $_[0];
# If the new status is greater than the old status, change the old status
if ($newstatus > $status) {
$status = $newstatus;
}
}
# Grab a value from snmp
sub grab_snmp_value {
my $this_oid = $_[0];
my $this_value = "";
# Try to grab the OID's value, if it exists
if (defined($snmp_session->get_request($this_oid))) {
foreach ($snmp_session->var_bind_names()) {
$this_value = $snmp_session->var_bind_list()->{$_};
}
}
# Return the value we got..
return $this_value;
}
# Check status on battery
# Grab battery time remaining
# Check battery voltage
sub check_device {
# Grab some values
my $xups_battery_timeleft = grab_snmp_value($oid_xups_bat_time);
my $xups_battery_status = grab_snmp_value ($oid_ups_batt_status);
my $xups_battery_volt = grab_snmp_value ($oid_ups_bat_voltage);
my $xups_on_battery_time = grab_snmp_value ($oid_ups_on_battery_time);
if ($xups_battery_status == 1){ # unknown
status (2); # Critical
$returnstr = "Battery Status is unknown";
} elsif ($xups_battery_status == 2) { # batteryNormal
status (0); # OK
$returnstr = "Battery OK";
} elsif ($xups_battery_status == 3) { # batteryLow
status(2); # Critical
$returnstr = "Battery Status Low";
} elsif ($xups_battery_status == 4) { #batteryDepleted
status(2); # Critcal
$returnstr = "Battery Status Depleted";
} else {
status(2); # Critical
$returnstr = "Battery Status Unavailable";
}
if ($xups_battery_timeleft ne '') {
# Do we know if we are on battery or not?
# $xups_on_battery_time is the number of minutes we have been running
# on battery power, this is zero if we have utility power.
if ($xups_on_battery_time != 0) {
# We're not on utility power.. If we have no warning/critical, return critical anyway
if (($warning_timeleft == -1) &&
($critical_timeleft == -1)) {
status(2); # Critical
}
# Mention how much time we have left..
$returnstr =
"$returnstr, " .
"Inverter Active: Less than " .
int($xups_battery_timeleft) ." minutes remaining";
# Have we broken a threshold here?
if (($critical_timeleft != -1) &&
($xups_battery_timeleft <= $critical_timeleft)) {
status(2); # Critical
$returnstr = "$returnstr (Critical)";
} elsif (($warning_timeleft != -1) &&
($xups_battery_timeleft <= $warning_timeleft)) {
status(1); # Warning
$returnstr = "$returnstr (Warning)";
}
}
} else {
status(2); # Critical
$returnstr .= ($returnstr?", ":''). "no data for battery time left";
}
if ($xups_battery_volt ne '') {
$xups_battery_volt = $xups_battery_volt /= 10;
# Have we broken a threshold?
if ((($critical_batvolt_max != -1) &&
($xups_battery_volt > $critical_batvolt_max)) ||
(($critical_batvolt_min != -1) &&
($xups_battery_volt < $critical_batvolt_min))) {
status(2); # Critical
$returnstr = "$returnstr, Critical:";
} elsif ((($warning_batvolt_max != -1) &&
($xups_battery_volt > $warning_batvolt_max)) ||
(($warning_batvolt_min != -1) &&
($xups_battery_volt < $warning_batvolt_min))) {
status(1); # Warning
$returnstr = "$returnstr, Warning:";
} else {
$returnstr = "$returnstr,";
}
$returnstr =
"$returnstr " .
"Battery voltage: $xups_battery_volt VDC";
} else {
status(1); # Warning
$returnstr .= ($returnstr?", ":''). "Warning no data for battery volt";
}
# Add on some performance monitoring stuff to the return string :)
$returnstr =
"$returnstr" .
"|$xups_battery_timeleft;$xups_battery_status;$xups_battery_volt" .
";";
}
# Usage information
sub usage {
print << "USAGE";
$0
Check a Socomec UPS\' battery via SNMP
Usage: $0 -H <hostname> -v <version> [-C <community>] [-w <s>,<min v>,<max v>] [-c <s>,<min v>,<max v>]
Options:
-H Hostname or IP address of the Socomec UPS
-v SNMP version supported by the UPS (default is $version)
-C SNMP read community (default is public)
-w Warning level remaining battery minutes and voltage min and max
-c Critical level remaining battery minutes and voltage min and max
For example: 5 minutes left and min 400 max 450 voltage= "5.400.450"
USAGE
exit_clean ('CRITICAL');
}