-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_socomec-input-frequency
executable file
·248 lines (199 loc) · 6.91 KB
/
check_socomec-input-frequency
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
#! /usr/local/bin/perl
#
#
#########################################################
# Product: UPS #
# Manufacturer: Socomec #
# Modell: SOCOMEC #
# #
# Retriving incoming frequency. #
# #
#########################################################
#
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 incoming frequency
my $OID_BASE =".1.3.6.1.2.1.33.1.3.3.1.2.";
# SNMP options
my $version = "2c";
my $timeout = 2;
# The top secret community name (password)...
my $community = get_snmp_community ('snmp');
# The information we returned
my $xups_frequency = 0;
# Thresholds
my $critical_high_frequency = -1;
my $warning_high_frequency = -1;
my $critical_low_frequency = -1;
my $warning_low_frequency = -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 = "public";
# 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_frequency= $warning[0];
}
if (!($warning[1] eq "")) {
$warning_high_frequency = $warning[1];
}
# If we also have the maximum frequency value, make sure it is higher
# than the minimum
if (($warning_high_frequency != -1) &&
($warning_high_frequency < $warning_low_frequency)) {
exit_clean ('CRITICAL', "Warning frequency 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_frequency = $critical[0];
}
if (!($critical[1] eq "")) {
$critical_high_frequency = $critical[1];
}
# If we also have the maximum frequency value, make sure it is higher
# than the minimum
if (($critical_high_frequency != -1) &&
($critical_high_frequency < $critical_low_frequency)) {
exit_clean ('CRITICAL', "Critical frequency maximum is lower than the minimum!\n");
}
}
# If we have both high frequency thresholds, make sure the critical is higher than
# the warning
if (($warning_high_frequency != -1) &&
($critical_high_frequency != -1) &&
($warning_high_frequency > $critical_high_frequency)) {
exit_clean ('CRITICAL', "The high critical frequency is lower than warning frequency!\n");
}
# If we have both low frequency thresholds, make sure the critical is lower than
# the warning
if (($warning_low_frequency != -1) &&
($critical_low_frequency != -1) &&
($warning_low_frequency < $critical_low_frequency)) {
exit_clean ('CRITICAL', "The low critical frequency is higher than warning frequency!\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 $fas;
my $OID;
my $snmp_ret_value;
$returnstr = '';
foreach (1..$num_phases) {
$fas = $_;
$OID = "$OID_BASE" . "$fas";
$snmp_ret_value = grab_snmp_value($OID);
if ($snmp_ret_value) {
# Have we broken a threshold?
if ((($critical_high_frequency != -1) &&
($snmp_ret_value > $critical_high_frequency)) ||
(($critical_low_frequency != -1) &&
($snmp_ret_value < $critical_low_frequency))) {
status(2); # Critical
$returnstr .= ($returnstr?", ":''). "Critical input frequency:";
} elsif ((($warning_high_frequency != -1) &&
($snmp_ret_value > $warning_high_frequency)) ||
(($warning_low_frequency != -1) &&
($snmp_ret_value < $warning_low_frequency))) {
status(1); # Warning
$returnstr .= ($returnstr?", ":''). "Warning input frequency:";
}
$returnstr .= ($returnstr?", ":''). "Input frequency: ".sprintf("%.2f", $snmp_ret_value/10)." Hz";
} else {
status(2); # CRITICAL
$returnstr .= ($returnstr?", ":''). "no data for fas $fas";
}
}
}
# Usage information
sub usage {
print << "USAGE";
$0
Check a Socomec UPS\' outgoing frequency via SNMP
Usage: $0 -H <hostname> -v <version> -f <phases> [-C <community>] [-w <freq min>.<freq max>] [-c <freq min>.<freq max>]
Options:
-H Hostname or IP address of the Socomec UPS
-v SNMP version supported by the UPS (default is $version)
-f The number of power phases attached to the UPS. !!! IMPORTANT !!!
-C SNMP read community (default is public)
-w Warning level min och max for outgoing frequency
-c Critical level min och max for outgoing frequency
For example: lower freq 49.5 and max 50.5 ="495.505"
USAGE
exit_clean ('CRITICAL');
}