aboutsummaryrefslogtreecommitdiff
path: root/contrib/check_apc_ups.pl
blob: dd979f5212f265ef32d154e4fb7f2169f0e75a66 (plain)
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
#! /usr/bin/perl -wT
#
# Check_apc_ups - Check APC UPS status via SNMP
# Shamelessly copied from check_breeze.pl
#
# To do:
# - Send SNMP queries directly, instead of forking `snmpget`.
# - Make the status less verbose.  Maybe we can send an "onLine, time
#   remaining: hh:mm:ss" if all is well, and a list of specific problems
#   if something is broken. 

BEGIN {
	if ($0 =~ m/^(.*?)[\/\\]([^\/\\]+)$/) {
		$runtimedir = $1;
		$PROGNAME = $2;
	}
}

use strict;
use Getopt::Long;
use vars qw($opt_V $opt_h $opt_H $opt_T $opt_t $opt_R $opt_r 
  $opt_L $opt_l $PROGNAME);
use lib $main::runtimedir;
use utils qw(%ERRORS &print_revision &support &usage);

sub print_help ();
sub print_usage ();
sub get_snmp_int_val ($);
sub escalate_exitval ($);

$ENV{'PATH'}='';
$ENV{'BASH_ENV'}=''; 
$ENV{'ENV'}='';

Getopt::Long::Configure('bundling');
GetOptions
	("V"   => \$opt_V, "version"		=> \$opt_V,
	 "h"   => \$opt_h, "help"		=> \$opt_h,
	 "T=s" => \$opt_T, "temp-critical"	=> \$opt_T,
	 "t=s" => \$opt_t, "temp-warning"	=> \$opt_t,
	 "R=s" => \$opt_R, "runtime-critical"	=> \$opt_R,
	 "r=s" => \$opt_r, "runtime-warning"	=> \$opt_r,
	 "L=s" => \$opt_L, "load-critical"	=> \$opt_L,
	 "l=s" => \$opt_l, "load-warning"	=> \$opt_l,
	 "H=s" => \$opt_H, "hostname=s"		=> \$opt_H);

if ($opt_V) {
	print_revision($PROGNAME,'$Revision$');
	exit $ERRORS{'OK'};
}

if ($opt_h) {print_help(); exit $ERRORS{'OK'};}

($opt_H) || ($opt_H = shift) || usage("Host name/address not specified\n");
my $host = $1 if ($opt_H =~ /([-.A-Za-z0-9]+)/);
($host) || usage("Invalid host: $opt_H\n");

# Defaults

$opt_R *= 60 * 100 if (defined $opt_R);	# Convert minutes to secs/100
$opt_r *= 60 * 100 if (defined $opt_R);

my $tempcrit    = $opt_T || 60;
my $tempwarn    = $opt_t || 40;
my $runtimecrit = $opt_R || 30 * 60 * 100;     # Secs / 100
my $runtimewarn = $opt_r || 60 * 60 * 100;
my $loadcrit    = $opt_L || 85;
my $loadwarn    = $opt_l || 50;

if ($tempcrit !~ /\d+/) { usage ("Invalid critical temperature threshold.\n"); }
if ($tempwarn !~ /\d+/) { usage ("Invalid critical temperature threshold.\n"); }

if ($runtimecrit !~ /\d+/) {
  usage ("Invalid critical run time threshold.\n");
}
if ($runtimewarn !~ /\d+/) {
  usage ("Invalid warning run time threshold.\n");
}

if ($loadcrit !~ /\d+/ || $loadcrit < 0 || $loadcrit > 100) {
  usage ("Invalid critical load threshold.\n");
}
if ($loadwarn !~ /\d+/ || $loadwarn < 0 || $loadwarn > 100) {
  usage ("Invalid warning load threshold.\n");
}


# APC UPS OIDs
# APC MIBs are available at ftp://ftp.apcftp.com/software/pnetmib/mib
my $upsBasicOutputStatus          = ".1.3.6.1.4.1.318.1.1.1.4.1.1.0";
my $upsBasicBatteryStatus         = ".1.3.6.1.4.1.318.1.1.1.2.1.1.0";
my $upsAdvInputLineFailCause      = ".1.3.6.1.4.1.318.1.1.1.3.2.5.0";
my $upsAdvBatteryTemperature      = ".1.3.6.1.4.1.318.1.1.1.2.2.2.0";
my $upsAdvBatteryRunTimeRemaining = ".1.3.6.1.4.1.318.1.1.1.2.2.3.0";
my $upsAdvBatteryReplaceIndicator = ".1.3.6.1.4.1.318.1.1.1.2.2.4.0";
my $upsAdvOutputLoad              = ".1.3.6.1.4.1.318.1.1.1.4.2.3.0";
my $upsAdvTestDiagnosticsResults  = ".1.3.6.1.4.1.318.1.1.1.7.2.3.0";

my @outputStatVals = (
  [ undef, undef ],					# pad 0
  [ undef, undef ],					# pad 1
  [ "onLine",			$ERRORS{'OK'} ],	# 2
  [ "onBattery",		$ERRORS{'WARNING'} ],	# 3
  [ "onSmartBoost",		$ERRORS{'WARNING'} ],	# 4
  [ "timedSleeping",		$ERRORS{'WARNING'} ],	# 5
  [ "softwareBypass",		$ERRORS{'WARNING'} ],	# 6
  [ "off",			$ERRORS{'CRITICAL'} ],	# 7
  [ "rebooting",		$ERRORS{'WARNING'} ],	# 8
  [ "switchedBypass",		$ERRORS{'WARNING'} ],	# 9
  [ "hardwareFailureBypass",	$ERRORS{'CRITICAL'} ],	# 10
  [ "sleepingUntilPowerReturn",	$ERRORS{'CRITICAL'} ],	# 11
  [ "onSmartTrim",		$ERRORS{'WARNING'} ],	# 12
);

my @failCauseVals = (
  undef,
  "noTransfer",
  "highLineVoltage",
  "brownout",
  "blackout",
  "smallMomentarySag",
  "deepMomentarySag",
  "smallMomentarySpike",
  "largeMomentarySpike",
  "selfTest",
  "rateOfVoltageChnage",
);

my @battStatVals = (
  [ undef, undef ],				# pad 0
  [ undef, undef ],				# pad 1
  [ "batteryNormal",	$ERRORS{'OK'} ],	# 2
  [ "batteryLow",	$ERRORS{'CRITICAL'} ],	# 3
);

my @battReplVals = (
  [ undef, undef ],		 			# pad 0
  [ "noBatteryNeedsReplacing",	$ERRORS{'OK'} ],	# 1
  [ "batteryNeedsReplacing",	$ERRORS{'CRITICAL'} ],	# 2
);

my @diagnosticsResultsVals = (
  [ undef, undef ],				# pad 0
  [ "OK",		$ERRORS{'OK'} ],	# 1
  [ "failed",		$ERRORS{'CRITICAL'} ],	# 2
  [ "invalidTest",	$ERRORS{'CRITICAL'} ],	# 3
  [ "testInProgress",	$ERRORS{'OK'} ],	# 4
);

my $exitval     = $ERRORS{'UNKNOWN'};
my $data;
my $onbattery   = 3;

$data = get_snmp_int_val( $upsBasicOutputStatus );

print "Output status: ";
if (defined ($data) && defined ($outputStatVals[$data][0])) {
  print "$outputStatVals[$data][0] | ";
  escalate_exitval($outputStatVals[$data][1]);
} else {
  print "unknown | ";
}

$data = get_snmp_int_val( $upsAdvBatteryRunTimeRemaining );

print "Rem time: ";
if (defined ($data)) {
  my $hrs  = int($data / (60 * 60 * 100)); # Data is hundredths of a second
  my $mins = int($data / (60 * 100)) % 60;
  my $secs = ($data % 100) / 100;
  printf "%d:%02d:%05.2f | ", $hrs, $mins, $secs;
  if ($data <= $runtimecrit) {
    escalate_exitval($ERRORS{'CRITICAL'});
  } elsif ($data <= $runtimewarn) {
    escalate_exitval($ERRORS{'WARNING'});
  } else {
    escalate_exitval($ERRORS{'OK'});
  }
} else {
  print "unknown | ";
}

$data = get_snmp_int_val( $upsBasicBatteryStatus );

print "Battery status: ";
if (defined ($data) && defined ($battStatVals[$data][0])) {
  my $failcause = "unknown";
  my $fc = get_snmp_int_val( $upsAdvInputLineFailCause );
  if ($data == $onbattery) {
    if (defined ($failCauseVals[$fc])) { $failcause = $failCauseVals[$fc]; }
    print "$battStatVals[$data][0] ($failcause) | ";
  } else {
    print "$battStatVals[$data][0] | ";
  }
  escalate_exitval($battStatVals[$data][1]);
} else {
  print "unknown | ";
}

$data = get_snmp_int_val( $upsAdvBatteryTemperature );

print "Battery temp(C): ";
if (defined ($data)) {
  print "$data | ";
  if ($data >= $tempcrit) {
    escalate_exitval($ERRORS{'CRITICAL'});
  } elsif ($data >= $tempwarn) {
    escalate_exitval($ERRORS{'WARNING'});
  } else {
    escalate_exitval($ERRORS{'OK'});
  }
} else {
  print "unknown | ";
}

$data = get_snmp_int_val( $upsAdvBatteryReplaceIndicator );

print "Battery repl: ";
if (defined ($data) && defined ($battReplVals[$data][0])) {
  print "$battReplVals[$data][0] | ";
  escalate_exitval($battReplVals[$data][1]);
} else {
  print "unknown | ";
}

$data = get_snmp_int_val( $upsAdvOutputLoad );

print "Output load (%): ";
if (defined ($data)) {
  print "$data | ";
  if ($data >= $loadcrit) {
    escalate_exitval($ERRORS{'CRITICAL'});
  } elsif ($data >= $loadwarn) {
    escalate_exitval($ERRORS{'WARNING'});
  } else {
    escalate_exitval($ERRORS{'OK'});
  }
} else {
  print "unknown | ";
}

$data = get_snmp_int_val( $upsAdvTestDiagnosticsResults );

print "Diag result: ";
if (defined ($data) && defined ($diagnosticsResultsVals[$data][0])) {
  print "$diagnosticsResultsVals[$data][0]\n";
  escalate_exitval($diagnosticsResultsVals[$data][1]);
} else {
  print "unknown\n";
}


exit $exitval;


sub print_usage () {
	print "Usage: $PROGNAME -H <host> -T temp -t temp -R minutes -r minutes\n";
	print "  -L percent -l percent\n";
}

sub print_help () {
	print_revision($PROGNAME,'$Revision$');
	print "Copyright (c) 2001 Gerald Combs/Jeffrey Blank/Karl DeBisschop

This plugin reports the status of an APC UPS equipped with an SNMP management
module.

";
	print_usage();
	print "
-H, --hostname=HOST
   Name or IP address of host to check
-T --temp-critical
   Battery degrees C above which a CRITICAL status will result (default: 60)
-t --temp-warning
   Battery degrees C above which a WARNING status will result (default: 40)
-R --runtime-critical
   Minutes remaining below which a CRITICAL status will result (default: 30)
-r --runtime-warning
   Minutes remaining below which a WARNING status will result (default: 60)
-L --load-critical
   Output load pct above which a CRITICAL status will result (default: 85
-l --load-warning
   Output load pct above which a WARNING status will result (default: 50

";
	support();
}

sub get_snmp_int_val ($) {
  my $val=0;
  my $oid = shift(@_);

  $val = `/usr/bin/snmpget $host public $oid 2> /dev/null`;
  my @test = split(/ /,$val,3);

  return undef unless (defined ($test[2]));

  if ($test[2] =~ /\(\d+\)/) {	# Later versions of UCD SNMP
    ($val) = ($test[2] =~ /\((\d+)\)/);
  } elsif ($test[2] =~ /: \d+/) {
    ($val) = ($test[2] =~ /: (\d+)/);
  } else {
    $val = $test[2];
  }

  return $val;
}

sub escalate_exitval ($) {
  my $newval = shift(@_);

  if ($newval > $exitval) { $exitval = $newval; }
}