-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_apc-ink-volt
executable file
·259 lines (209 loc) · 6.83 KB
/
check_apc-ink-volt
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
#! /usr/local/bin/perl
#
# $Id$
# $HeadURL$
#
# nagios: -epn
#
#################################################
# Product: ATS - Automatic Transfer Switch #
# Manufacturer: APC #
# Model: AP7723 #
# #
# Retrive incoming voltage, source A and B. #
# #
#################################################
use strict;
use lib "/local/nagios/lib";
use SU_Nagios qw ( exit_clean get_snmp_community );
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 incoming voltage
my $OID_BASE = "1.3.6.1.4.1.318.1.1.8.5.3.3.1.3.";
# SNMP options
my $version = "1";
my $timeout = 2;
# The top secret community name (password)...
#
my $community = get_snmp_community ('snmp');
# Thresholds
my $critical_high_volt = -1;
my $warning_high_volt = -1;
my $critical_low_volt = -1;
my $warning_low_volt = -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 ATS
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_volt = $warning[0];
}
if (!($warning[1] eq "")) {
$warning_high_volt = $warning[1];
}
# If we also have the maximum volt value, make sure it is higher
# than the minimum
if (($warning_high_volt != -1) &&
($warning_high_volt < $warning_low_volt)) {
exit_clean ('CRITICAL', "Warning volt maximum is lower than the minimum!\n");
}
}
# Grab the critical thresholds
if (defined($opt_c)) {
my @critical = split(/\./, $opt_c);
if (!($critical[0] eq "")) {
$critical_low_volt = $critical[0];
}
if (!($critical[1] eq "")) {
$critical_high_volt = $critical[1];
}
# If we also have the maximum volt value, make sure it is higher
# than the minimum
if (($critical_high_volt != -1) &&
($critical_high_volt < $critical_low_volt)) {
exit_clean ('CRITICAL', "Critical volt maximum is lower than the minimum!\n");
}
}
# If we have both high voltage thresholds, make sure the critical is higher than
# the warning
if (($warning_high_volt != -1) &&
($critical_high_volt != -1) &&
($warning_high_volt > $critical_high_volt)) {
exit_clean ('CRITICAL', "The high critical volt is lower than warning volt!\n");
}
# If we have both low voltage thresholds, make sure the critical is lower than
# the warning
if (($warning_low_volt != -1) &&
($critical_low_volt != -1) &&
($warning_low_volt < $critical_low_volt)) {
exit_clean ('CRITICAL', "The low critical volt is higher than warning volt!\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 {
my $phase;
my $OID;
my $snmp_ret_value;
my $AB;
$returnstr = '';
foreach (1..$num_phases) {
$phase = $_;
$OID = "$OID_BASE" . "$phase" . ".1.1";
# Decide which $phase corespond to which Source Name.
if ($phase == 1) {
$AB = "A";
} else {
$AB = "B";
}
$snmp_ret_value = grab_snmp_value($OID);
if ($snmp_ret_value) {
# Have we broken a threshold?
if ((($critical_high_volt != -1) &&
($snmp_ret_value > $critical_high_volt)) ||
(($critical_low_volt != -1) &&
($snmp_ret_value < $critical_low_volt))) {
status(2); # Critical
$returnstr .= ($returnstr?", ":''). "Critical ink volt:";
} elsif ((($warning_high_volt != -1) &&
($snmp_ret_value > $warning_high_volt)) ||
(($warning_low_volt != -1) &&
($snmp_ret_value < $warning_low_volt))) {
status(1); # Warning
$returnstr .= ($returnstr?", ":''). "Warning ink volt:";
}
$returnstr .= ($returnstr?", ":''). "Ink volt Source $AB: $snmp_ret_value";
} else {
status(2); # CRITICAL
$returnstr .= ($returnstr?", ":''). "Warning no data for Source $AB";
}
}
}
# Usage information
sub usage {
print << "USAGE";
$0
Check a APC's ATS\' incomming voltage via SNMP
Usage: $0 -H <hostname> -v <version> -f <phases> [-C <community>] [-w <volt min>,<volt max>] [-c <volt min>,<volt max>]
Options:
-H Hostname or IP address of the APC ATS
-v SNMP version supported by the UPS (default is $version)
-f The number of power phases attached to the ATS !!! IMPORTANT !!!
-C SNMP read community (default is public)
-w Warning level min och max for inkomming voltage
-c Critical level min och max for inkomming voltage
For example: lower volt 210 and max 250 ="210.250"
USAGE
exit_clean ('CRITICAL');
}