-
Notifications
You must be signed in to change notification settings - Fork 0
/
check.py
58 lines (51 loc) · 2.04 KB
/
check.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
#!/usr/bin/python
"""
Nagios check for Casa C10G PSM.
"""
import sys, collections, argparse, requests
def getopts():
parser = argparse.ArgumentParser()
parser.add_argument("url", help="C10G monitoring web-service URL", type=str)
args = parser.parse_args()
return args
def output(code, message):
state = {0: 'OK',
1: 'WARNING',
2: 'CRITICAL',
3: 'UNKNOWN'}
try:
print '%s: %s' % (state[code], message)
sys.exit(code)
except Exception:
output(3, 'Internal check error')
def check():
opts = getopts()
try:
response = requests.get(opts.url)
data = response.json()
except requests.exceptions.ConnectionError as e:
output(2, 'Monitoring URL is not reachable ({0})'.format(e.request.url))
except requests.exceptions.RequestException as e:
output(3, e)
except ValueError as e:
output(2, 'Monitoring data could not be read as json ({0}...)'.format(response.text[:50]))
except Exception as e:
output(2, 'Unknown check error ({0}: {1})'.format(e.__class__, e.message))
if all([alarms['OK'] for module, alarms in data['alarms'].viewitems()]):
output (0, 'All green')
else:
active_alarms = collections.defaultdict(list)
for module, alarm in [(module, alarm)
for module, alarms in data['alarms'].viewitems()
for alarm, active in alarms.viewitems()
if active and alarm != 'OK']:
active_alarms[module].append(alarm)
if data['human']['Fault']:
active_alarms['Chassis'].append('Fault indicator active')
string = ('Alarm on modules: {0}\n'.format(', '.join(active_alarms.viewkeys())) +
'\n'.join(['Module {0}: {1}'.format(module, ', '.join(alarms))
for module, alarms in active_alarms.viewitems()]))
code = 1 if len(active_alarms.viewkeys()) <= 2 else 2
output(code, string)
if __name__ == '__main__':
check()