-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcheck_apcaccess.py
276 lines (205 loc) · 9.62 KB
/
check_apcaccess.py
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# check_apcaccess.py - a script for checking a APC UPS
# using the apcaccess utility
#
# 2016 By Christian Stankowic
# <info at stankowic hyphen development dot net>
# https://github.com/stdevel
#
# Enhanced and error corrections by Chris Johnston 2017
# Tested on BX1300G & RS 1500G but should work on most APC UPS
# <HireChrisJohnston at g mail>
#
#> Added detection of TONBATT
#> Corrected misc errors
#> Enchanced charting & messaging
from optparse import OptionParser, OptionGroup
import os
import subprocess
import logging
import re
#set logger
LOGGER = logging.getLogger("check_apcaccess")
#global variables
ups_info={}
state=0
def check_value(val, desc, warn, crit, reverse=False):
#compares value to thresholds and sets codes
LOGGER.debug("Comparing '{0}' ({1}) to warning/critical thresholds {2}/{3} (reverse: {4})".format(val, desc, warn, crit, reverse))
snip=""
if reverse == False:
if val > crit:
#critical
snip="{0} *Critical* ({1})".format(desc, val)
set_code(2)
elif val > warn:
#warning
snip="{0} -Warning ({1})".format(desc, val)
set_code(1)
else: snip="{0}: {1}".format(desc, val)
else:
if val < crit:
#critical
snip="{0} *Critical* ({1})".format(desc, val)
set_code(2)
elif val < warn:
#warning
snip="{0} -Warning ({1})".format(desc, val)
set_code(1)
else: snip="{0}: {1}".format(desc, val)
return snip
def set_code(int):
#set result code
global state
if int > state: state = int
def get_return_str():
#get return string
if state == 3: return "UNKNOWN"
elif state == 2: return "CRITICAL"
elif state == 1: return "WARNING"
else: return "OK"
def get_value(key, isFloat=False):
#get value from apcaccess information
if isFloat:
temp = re.findall(r'[-+]?[0-9]*\.?[0-9]*', ups_info[key])
return float(temp[0])
else: return ups_info[key]
def calc_consumption():
#calculate power consumption
load = get_value('LOADPCT', True)
out = get_value('NOMPOWER', True)
power_cons = int(out*(load/100))
LOGGER.debug("MATH says, based on the information provided, it is assumed that the power consumption might be ~{0} watts".format(power_cons))
return power_cons
def check_ups():
#check UPS
global state
#get _all_ the values
starttime = get_value('STARTTIME')
status = get_value('STATUS')
battv = get_value('BATTV', True)
LOGGER.debug("BattV: {0}".format(battv))
load = get_value('LOADPCT', True)
batt = get_value('BCHARGE', True)
xfers = get_value('NUMXFERS')
tot_onbat = get_value('CUMONBATT')
on_bat = get_value('TONBATT')
linev = get_value('LINEV')
if options.time_warn and options.time_crit: time = get_value('TIMELEFT', True)
power_cons = calc_consumption()
#Check if line level is high
curr_line_level = get_value('LINEV', True)
if options.line_level > curr_line_level:
snip_line_level = " Line Level low {2} {0} for {1}".format(status,on_bat,linev)
set_code(1)
else: snip_line_level = status
#check Batt V
snip_battv = check_value(battv, "Voltage", options.battv_warn, options.battv_crit, True) +'v'
#check load
snip_load = check_value(load, "Load", options.load_warn, options.load_crit)+ '%'
#check battery charge
snip_batt = check_value(batt, "Charge", options.bat_warn, options.bat_crit, True) +'%'
#check battery time (optional)
if options.time_warn or options.time_crit:
snip_time = check_value(time, "Time Left", options.time_warn, options.time_crit, True) + 'min'
else: snip_time=""
#check power consumption (optional)
if options.consum_warn or options.consum_crit:
snip_consum = check_value(power_cons, "Power consumption", options.consum_warn, options.consum_crit) +'w'
else: snip_consum=""
# get detail
snip_detail ="(Total On Battery: " + tot_onbat + " / #Xfers: " + xfers + " since "+starttime+")"
#get performance data
if options.show_perfdata:
#initialize perfdata
perfdata=" |"
#power consumption
if options.consum_warn and options.consum_crit: perfdata = "{0} 'consumption'={1};{2};{3};;".format(perfdata, power_cons, float(options.consum_warn), float(options.consum_crit))
else: perfdata = "{0} 'Consumption'={1}w;;;".format(perfdata, power_cons)
#voltage
perfdata = "{0} 'Voltage'={1}v;{2};{3};{4};{5}".format(perfdata, battv, float(options.battv_warn), float(options.battv_crit), 11.0, 27.3)
#load
perfdata = "{0} 'Load'={1}%;{2};{3};{4};{5}".format(perfdata, load, float(options.load_warn), float(options.load_crit), 0.0, 100.0)
#battery charge
perfdata = "{0} 'Battery_Charge'={1}%;{2};{3};{4};{5}".format(perfdata, batt, float(options.bat_warn), float(options.bat_crit), 0.0, 100.0)
#battery time left only if user specified the warning and critical values
if options.time_warn or options.time_crit:
perfdata = "{0} 'Battery_Time_Left'={1};{2};{3};;".format(perfdata, time, float(options.time_warn), float(options.time_crit))
else: perfdata=""
#return result
snips = [x for x in [snip_line_level,snip_battv, snip_batt, snip_load,snip_consum,snip_time,snip_detail ] if x != ""]
print "{0}: {1}{2}".format(get_return_str(), str(", ".join(snips)), perfdata)
exit(state)
def run_cmd(cmd=""):
#run the command, it's tricky!
output = subprocess.Popen("LANG=C {0}".format(cmd), shell=True, stdout=subprocess.PIPE).stdout.read()
LOGGER.debug("Output of '{0}' => '{1}".format(cmd, output))
return output
def get_apcaccess_data():
#get output of apcaccess
global ups_info
raw_data = run_cmd("apcaccess -h" + options.host)
raw_data = raw_data.splitlines()
for line in raw_data:
#parse lines to key/value dict
key=line[:line.find(":")].strip()
value=line[line.find(":")+1:].strip()
LOGGER.debug("Found key '{0}' with value '{1}'".format(key, value))
ups_info[key]=value
if __name__ == "__main__":
#define description, version and load parser
desc='''%prog is used to check a APC UPS using the apcaccess utility.
https://github.com/HireChrisJohnston/nagios-apcupsd'''
parser = OptionParser(description=desc,version="%prog version 1.0.0")
gen_opts = OptionGroup(parser, "Generic options")
mon_opts = OptionGroup(parser, "Monitoring options")
thres_opts = OptionGroup(parser, "Threshold options")
parser.add_option_group(gen_opts)
parser.add_option_group(mon_opts)
parser.add_option_group(thres_opts)
#-d / --debug
gen_opts.add_option("-d", "--debug", dest="debug", default=False, action="store_true", help="enable debugging outputs")
#-P / --enable-perfdata
mon_opts.add_option("-P", "--enable-perfdata", dest="show_perfdata", default=False, action="store_true", help="enables performance data (default: no)")
#-w / --battv-warning
thres_opts.add_option("-w", "--battv-warning", dest="battv_warn", default=24, type=float, metavar="VOLTS", action="store", help="Defines battery voltage warning threshold (default: 24)")
#-W / --battv-critical
thres_opts.add_option("-W", "--battv-critical", dest="battv_crit", default=23.3, type=float, metavar="VOLTS", action="store", help="Defines battery voltage critical threshold (default: 23.3)")
#-c / --temp-critical
#thres_opts.add_option("-c", "--temp-critical", dest="temp_crit", default=55, type=float, metavar="TEMP", action="store", help="Defines temprature critical threshold(defalt: 55)")
#-l / --load-warning
thres_opts.add_option("-l", "--load-warning", dest="load_warn", default=50, type=int, metavar="PERCENT", action="store", help="Defines load warning threshold in percent (default: 50%)")
#-L / --load-critical
thres_opts.add_option("-L", "--load-critical", dest="load_crit", default=80, type=int, metavar="PERCENT", action="store", help="Defines load critical threshold in percent (default: 80%)")
#-b / --battery-warning
thres_opts.add_option("-b", "--battery-warning", dest="bat_warn", default=30, type=int, metavar="PERCENT", action="store", help="Defines battery load warning threshold in percent (default: 30%)")
#-B / --battery-critical
thres_opts.add_option("-B", "--battery-critical", dest="bat_crit", default=15, type=int, metavar="PERCENT", action="store", help="Defines battery load critical threshold in percent (default: 15%)")
#-t / --time-warning
thres_opts.add_option("-t", "--time-warning", dest="time_warn", type=int, metavar="TIME", action="store", help="Defines battery time left warning threshold in minutes (default: empty). If defined you must also define time-critical")
#-T / --time-critical
thres_opts.add_option("-T", "--time-critical", dest="time_crit", type=int, metavar="TIME", action="store", help="Defines battery time left critical threshold in minutes (default: empty). If defined you must also define time-warning")
#-u / --consumption-warning
thres_opts.add_option("-u", "--consumption-warning", dest="consum_warn", type=int, metavar="WATTS", action="store", help="Defines power consumption warning threshold in watts (default: empty)")
#-U / --consumption-critical
thres_opts.add_option("-U", "--consumption-critical", dest="consum_crit", type=int, metavar="WATTS", action="store", help="Defines power consumption critical threshold in watts (default: empty)")
#-H / --host
gen_opts.add_option("-H", "--host", dest="host", type="string", action="store", default="127.0.0.1", help="host of appcupsd")
#-X / --line-level
gen_opts.add_option("-X", "--line-level", dest="line_level", type="int", action="store", default="110", help="Volts of power outlet to detect no power if less than the line level")
#parse arguments
(options, args) = parser.parse_args()
#set logger level
if options.debug:
logging.basicConfig(level=logging.DEBUG)
LOGGER.setLevel(logging.DEBUG)
else:
logging.basicConfig()
LOGGER.setLevel(logging.INFO)
#debug outputs
LOGGER.debug("OPTIONS: {0}".format(options))
#get information
get_apcaccess_data()
#check UPS
check_ups()