forked from isaac-galvan/nagios-plugins-statuspage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check-statuspage-component.py
132 lines (106 loc) · 3.57 KB
/
check-statuspage-component.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
#!/usr/bin/env python3
#
# Author: Isaac J. Galvan
# Date: 2018-12-04
#
# https://github.com/
import argparse
import datetime
import json
import requests
from json.decoder import JSONDecodeError
# Nagios return codes
OK = 0
WARNING = 1
CRITICAL = 2
UNKNOWN = 3
# Statuspage.io statuses
OPERATIONAL = 'operational'
DEGRADED_PERFORMANCE = 'degraded_performance'
PARTIAL_OUTAGE = 'partial_outage'
MAJOR_OUTAGE = 'major_outage'
UNDER_MAINTENANCE = 'under_maintenance'
class CheckResult():
def __init__(self):
self._exitcode = UNKNOWN
self._message = ''
self._longmessage = ''
def set_code(self, code):
self._exitcode = code
def set_message(self, message):
self._message = message
def set_longmessage(self, longmessage):
self._longmessage = longmessage
def send(self):
# print the status description message
print(self._message)
# print the multi-line long message
if len(self._longmessage) > 0:
print(self._longmessage)
# exit process
raise SystemExit(self._exitcode)
class ComponentsList():
def __init__(self, page_id):
url = 'https://{0}.statuspage.io/api/v2/components.json'
self._url = url.format(page_id)
def load(self):
# request the json from statuspage
r = requests.get(self._url)
components_json = r.text
self._data = json.loads(components_json)
def get_component(self, id):
component_list = self._data.get('components')
# print(component_list)
for c in component_list:
if c.get('id') == id:
if c.get('group_id'):
parent = self.get_component(c.get('group_id'))
parent_name = parent.get('name')
full_name = '{0} - {1}'.format(parent_name, c.get('name'))
c['name'] = full_name
return c
def main(args):
#create the result
result = CheckResult()
# load the components json
page_id = args.get('page_id')
components = ComponentsList(page_id)
try:
components.load()
except JSONDecodeError:
message = 'UNKNOWN: page {0} not found'.format(page_id)
result.set_message(message)
result.set_code(UNKNOWN)
result.send()
# find the component
component_id = args.get('component_id')
component = components.get_component(component_id)
# return unknown if component not found
if component is None:
message = 'UNKNOWN: component {0} not found'.format(component_id)
result.set_message(message)
result.set_code(UNKNOWN)
result.send()
# perform check logic
status = component.get('status')
name = component.get('name')
if status == OPERATIONAL:
message = 'OK: {0} is {1}'.format(name, status)
result.set_message(message)
result.set_code(OK)
elif status == DEGRADED_PERFORMANCE:
message = 'WARNING: {0} is {1}'.format(name, status)
result.set_message(message)
result.set_code(WARNING)
elif status == PARTIAL_OUTAGE or status == MAJOR_OUTAGE:
message = 'CRITICAL: {0} is {1}'.format(name, status)
result.set_message(message)
result.set_code(CRITICAL)
result.send()
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='statuspage.io nagios check')
parser.add_argument('page_id', help='statuspage.io page id')
parser.add_argument('component_id', help='component id')
# args = {}
args = parser.parse_args()
main(vars(args))