forked from appfirst/nagios-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_ipmi_sensors
378 lines (336 loc) · 8.97 KB
/
check_ipmi_sensors
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#!/usr/bin/perl
#############################################################################
# Copyright (C) 2003-2009 FreeIPMI Core Team
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#############################################################################
#
# check_ipmi_sensors.pl
#
# Original Author:
#
# Albert Chu <chu11 at llnl dot gov>
#
# Modified by AppFirst for use with the AppFirst Collector
#
#
# Description:
#
# This script can be used to monitor IPMI sensors in nagios via
# FreeIPMI's ipmi-sensors.
#
# Options:
#
# -h - specify hostname(s) to remotely access (don't specify for inband)
# -S - specify an alternate ipmi_sensors location
# -s - specify additional ipmi_sensors arguments
# -d - print debug info
# -t - specify sensor types to be reported (default is all)
# -H - output help
#
# Environment Variables:
#
# IPMI_HOSTS - specify hostname(s) to remotely access (don't specify for inband)
# IPMI_SENSORS_PATH - specify an alternate ipmi-ssensors location
# IPMI_SENSORS_ARGS - specify additional ipmi-sensors arguments
#
# Setup Notes:
#
# Specify the remote hosts you wish to access IPMI information from
# via the -h option or IPMI_HOSTS environment variable. If you wish
# only to monitor the local node, do not specify an ipmi host. The
# input to the -h option is passed directly to ipmimonitoring. So you
# may specify anything the ipmimonitoring tool accepts including
# hostranged (e.g. foo[0-127]) or comma separated
# (e.g. foo0,foo1,foo2,foo3) inputs. If you wish to monitor both
# remote and local system, remember to specify one of the hosts as
# "localhost". Most will probably want to monitor just one host (get
# the IPMI status for each individual machine being monitored),
# however more than one host can be analyzed for a collective result.
#
# If stored in a non-default location the -S option or
# IPMI_SENSORS_PATH environment variable must be specified to
# determine the ipmimonitoring location.
#
# In order to specify non-defaults for ipmimonitoring use the -s
# argument or IPMI_SENSORS_ARGS environment variable. Typically,
# this option is necessary for non-default communication information
# or authentication information (e.g. driver path, driver type,
# username, password, etc.). Non-default communication information
# can also be stored in the FreeIPMI configuration file. This is the
# suggested method because passwords and other sensitive information
# could show up in ps(1). If you wish to limit the sensors being
# monitored, you can also specify which record-ids are to be monitored
# (-s option).
#
# Examples for the appfirst polled data config file:
#
#command[sensor_fan]=/usr/share/appfirst/plugins/libexec/check_ipmi_sensors -a -t fan
#command[sensor_temp]=/usr/share/appfirst/plugins/libexec/check_ipmi_sensors -a -t temperature
#command[sensor_voltage]=/usr/share/appfirst/plugins/libexec/check_ipmi_sensors -a -t voltage
#command[sensor_memory]=/usr/share/appfirst/plugins/libexec/check_ipmi_sensors -a -t memory
#command[sensor_pyhsical_security]=/usr/share/appfirst/plugins/libexec/check_ipmi_sensors -a -t physical_security
#command[sensor_processor]=/usr/share/appfirst/plugins/libexec/check_ipmi_sensors -a -t processor
#
# The default session timeout length in ipmi-sensors is 20 seconds.
# We would recommend that IPMI not be monitored more frequently than
# that.
#
# Help:
#
# Report bugs to [email protected] or [email protected].
#
#############################################################################
use strict;
use Getopt::Std;
my $debug = 0;
my $inject_error = 0;
my $IPMI_HOSTS = undef;
my $IPMI_SENSORS_PATH = "/usr/local/sbin/ipmi-sensors";
my $IPMI_SENSORS_ARGS = "";
my $IPMI_SENSORS_OUTPUT;
my @IPMI_SENSORS_OUTPUT_LINES;
my $line;
my $cmd;
my $sensor_type = "";
my $aggregate_state = "";
my $num_output = 0;
my $ok_num = 0;
my $warning_num = 0;
my $critical_num = 0;
my $fatal_error = 0;
sub usage
{
my $prog = $0;
print "Usage: $prog [-h <hostname(s)>] [-S <path>] [-s <sensors arguments>] [-t <sensor_type>] [-d] [-H]\n";
print " -h specify hostname(s) to remotely access\n";
print " -S specify an alternate ipmi-sensors path\n";
print " -s specify additional ipmi-sensors arguments\n";
print " -t specify sensor type\n";
print " -d print debug info\n";
print " -H output help\n";
exit 0;
}
if (!getopts("h:S:s:t:dHx"))
{
usage();
}
if (defined($main::opt_H))
{
usage();
}
if (defined($main::opt_h))
{
$IPMI_HOSTS = $main::opt_h;
}
if (defined($main::opt_S))
{
$IPMI_SENSORS_PATH = $main::opt_S;
}
if (defined($main::opt_s))
{
$IPMI_SENSORS_ARGS = $main::opt_s;
}
if (defined($main::opt_t))
{
$sensor_type = $main::opt_t;
$IPMI_SENSORS_ARGS .= "-t $sensor_type";
}
if (defined($main::opt_d))
{
$debug = 1;
}
if (defined($main::opt_x))
{
$inject_error = 1;
}
if ($ENV{"IPMI_HOSTS"})
{
$IPMI_HOSTS = $ENV{"IPMI_HOSTS"};
}
if ($ENV{"IPMI_SENSORS_PATH"})
{
$IPMI_SENSORS_PATH = $ENV{"IPMI_SENSORS_PATH"};
}
if ($ENV{"IPMI_SENSORS_ARGS"})
{
$IPMI_SENSORS_ARGS = $ENV{"IPMI_SENSORS_ARGS"};
}
if ($debug)
{
print "IPMI_HOSTS=$IPMI_HOSTS\n";
print "IPMI_SENSORS_PATH=$IPMI_SENSORS_PATH\n";
print "IPMI_SENSORS_ARGS=$IPMI_SENSORS_ARGS\n";
}
if (!(-x $IPMI_SENSORS_PATH))
{
print "$IPMI_SENSORS_PATH cannot be executed\n";
exit(1);
}
# note, don't need --ignore-non-interpretable-sensors, legacy output handles it
if ($IPMI_HOSTS)
{
$cmd = "$IPMI_SENSORS_PATH $IPMI_SENSORS_ARGS -h $IPMI_HOSTS --quiet-cache --sdr-cache-recreate --always-prefix --no-header-output --output-sensor-state";
}
else
{
$cmd = "$IPMI_SENSORS_PATH $IPMI_SENSORS_ARGS --quiet-cache --sdr-cache-recreate --always-prefix --no-header-output --output-sensor-state"
}
if ($debug)
{
print "ipmi-sensors command: $cmd\n";
}
$IPMI_SENSORS_OUTPUT = `$cmd`;
if ($? != 0)
{
print "$IPMI_SENSORS_PATH: exited with error\n";
}
if ($IPMI_SENSORS_OUTPUT =~ /Critical/)
{
$aggregate_state = "CRIT";
}
elsif ($IPMI_SENSORS_OUTPUT =~ /Warning/)
{
$aggregate_state = "WARN";
}
else
{
$aggregate_state = "OK";
}
if ($inject_error)
{
$aggregate_state = "CRIT";
$critical_num++;
}
if (defined($main::opt_t))
{
print "$sensor_type $aggregate_state | ";
}
else
{
print "all_sensors $aggregate_state | ";
}
@IPMI_SENSORS_OUTPUT_LINES = split(/\n/, $IPMI_SENSORS_OUTPUT);
foreach $line (@IPMI_SENSORS_OUTPUT_LINES)
{
my $hostname;
my $record_id;
my $id_string;
my $type;
my $state;
my $reading;
my $units;
my $event;
my $value;
my $id_string_state;
my $output_str;
if ($debug)
{
print "Parsing: $line\n";
}
if ($line =~ /(.+)\: (\d+)(\s+)\| (.+)(\s+)\| (.+)(\s+)\| (.+)(\s+)\| (.+)(\s+)\| (.+)(\s+)\| (.+)/)
{
$hostname = $1;
$record_id = $2;
$id_string = $4;
$type = $6;
$state = $8;
$reading = $10;
$units = $12;
$event = $14;
# trim whitespace off end of string
$record_id =~ s/\s+$//;
$id_string =~ s/\s+$//;
$type =~ s/\s+$//;
$state =~ s/\s+$//;
$reading =~ s/\s+$//;
$units =~ s/\s+$//;
}
else
{
print "Line not parsable\n";
$fatal_error++;
next;
}
# make name better, convert spaces and slashes into underscores
$id_string =~ s/ /_/g;
$id_string =~ s/\//_/g;
if ($state eq "N/A")
{
next;
}
if ($state eq "Nominal")
{
$output_str = "OK";
}
elsif ($state eq "Warning")
{
$warning_num++;
$output_str = "WARN";
}
elsif ($state eq "Critical")
{
$critical_num++;
$output_str = "CRIT";
}
else
{
print "State not parsable ($state)\n";
$fatal_error++;
next;
}
if ($num_output)
{
print ";";
}
if ($reading ne "N/A")
{
if ($inject_error)
{
$reading = int($reading)/2
}
print "$id_string=$reading"
}
else
{
if($state eq "Nominal"){ $value="1"}
if($state eq "Warning"){ $value="2"}
if($state eq "Critical"){ $value="3"}
print "$id_string=$value"
}
$num_output++;
}
if ($num_output)
{
print "\n";
}
# Nagios Exit Codes
# 0 = OK
# 1 = WARNING
# 2 = CRITICAL
# 3 = UNKNOWN
if ($fatal_error)
{
exit 3;
}
if ($critical_num)
{
exit 2;
}
if ($warning_num)
{
exit 1;
}
exit 0;