-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_socomec-load
executable file
·237 lines (191 loc) · 6.39 KB
/
check_socomec-load
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
#! /usr/local/bin/perl
#
#
#########################################################
# Product: UPS #
# Manufacturer: Socomec #
# Modell: SOCOMEC #
# #
# Retriving battery load in %. #
# #
#########################################################
use strict;
use Net::SNMP;
use Getopt::Std;
use vars qw ($opt_h $opt_H $opt_v $opt_f $opt_C $opt_w $opt_c);
# OID base for load
my $OID_BASE = ".1.3.6.1.2.1.33.1.4.4.1.5.";
# SNMP options
my $version = "2c";
my $timeout = 2;
# The top secret community name (password)...
my $community = get_snmp_community ('snmp');
# Thresholds
my $critical_high_load = -1;
my $warning_high_load = -1;
my $critical_low_load = -1;
my $warning_low_load = -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 = "";
# Number of power phases attached to the UPS
my $num_phases = "0";
# Grab the command line options
getopts("h:H:v:f: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;
}
if (defined($opt_f)) {
# We need to know how many power phases
$num_phases = $opt_f;
} else {
usage();
}
# 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_low_load= $warning[0];
}
if (!($warning[1] eq "")) {
$warning_high_load = $warning[1];
}
}
# Grab the critical thresholds
if (defined($opt_c)) {
my @critical = split(/\./, $opt_c);
if (!($critical[0] eq "")) {
$critical_low_load = $critical[0];
}
if (!($critical[1] eq "")) {
$critical_high_load = $critical[1];
}
# If we also have the maximum load value, make sure it is higher
# than the minimum
if (($critical_high_load != -1) &&
($critical_high_load < $critical_low_load)) {
exit_clean ('CRITICAL', "Critical load maximum is lower than the minimum!\n");
}
}
# If we have both high load thresholds, make sure the critical is higher than
# the warning
if (($warning_high_load != -1) &&
($critical_high_load != -1) &&
($warning_high_load > $critical_high_load)) {
exit_clean ('CRITICAL', "The high critical load is lower than warning load!\n");
}
# If we have both low load thresholds, make sure the critical is lower than
# the warning
if (($warning_low_load != -1) &&
($critical_low_load != -1) &&
($warning_low_load < $critical_low_load)) {
exit_clean ('CRITICAL', "The low critical load is higher than warning load!\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;
}
# Grab lots of interesting info about the device
sub check_device {
# Grab some values
my $phase;
my $OID;
my $snmp_ret_value;
$returnstr = '';
foreach (1..$num_phases) {
$phase = $_;
$OID = "$OID_BASE" . "$phase";
$snmp_ret_value = grab_snmp_value($OID);
# Only perfom checks if $snmp_ret_value is not empty.
# A value of "0" is OK, therefore check for an empty string.
if ($snmp_ret_value ne "") {
# Have we broken a threshold?
if ((($critical_high_load != -1) &&
($snmp_ret_value > $critical_high_load)) ||
(($critical_low_load != -1) &&
($snmp_ret_value < $critical_low_load))) {
status(2); # Critical
$returnstr .= ($returnstr?", ":''). "Critical outgoing load:";
} elsif ((($warning_high_load != -1) &&
($snmp_ret_value > $warning_high_load)) ||
(($warning_low_load != -1) &&
($snmp_ret_value < $warning_low_load))) {
status(1); # Warning
$returnstr .= ($returnstr?", ":''). "Warning outgoing load:";
}
$returnstr .= ($returnstr?", ":''). "Outgoing load: ".sprintf("%.2f", $snmp_ret_value)." %";
} else {
status(2); # CRITICAL
$returnstr .= ($returnstr?", ":''). "no data for phase $phase";
}
}
}
# Usage information
sub usage {
print << "USAGE";
$0
Check a Socomec UPS\' battery load % via SNMP
Usage: $0 -H <hostname> -v <version> -f <phases> [-C <community>] [-w <load min>,<load max>] [-c <load min>,<load max>]
Options:
-v SNMP version supported by the UPS (default is $version)
-H Hostname or IP address of the Socomec UPS
-C SNMP read community (default is public)
-w Warning level min och max for system load
-c Critical level min och max for system load
For example: lower level 5% and max 80% ="5.80"
USAGE
exit_clean ('CRITICAL');
}