Skip to content

Commit bd901aa

Browse files
committed
statuspal: Improve code
1 parent 3f4afe9 commit bd901aa

File tree

1 file changed

+24
-46
lines changed

1 file changed

+24
-46
lines changed

check-plugins/statuspal/statuspal

Lines changed: 24 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212
"""
1313

1414
import argparse # pylint: disable=C0413
15-
import flatdict # pylint: disable=C0413
15+
import json # pylint: disable=C0413
1616
import sys # pylint: disable=C0413
17+
import flatdict # pylint: disable=C0413
1718

1819
import lib.args # pylint: disable=C0413
1920
import lib.base # pylint: disable=C0413
@@ -25,7 +26,7 @@ from lib.globals import (STATE_CRIT, STATE_OK, # pylint: disable=C0413
2526
STATE_UNKNOWN, STATE_WARN)
2627

2728
__author__ = 'Linuxfabrik GmbH, Zurich/Switzerland'
28-
__version__ = '2023091401'
29+
__version__ = '2023091402'
2930

3031
DESCRIPTION = """Statuspal is a status page provider from Germany. This check plugin gets
3132
the summary of a Statuspal status page, checks its status, services,
@@ -90,7 +91,7 @@ def concat_values(mydict, hierarchy):
9091
"""
9192
result = ''
9293
hierarchy = hierarchy.split(':')
93-
for i, item in enumerate(hierarchy):
94+
for i, item in enumerate(hierarchy): # pylint: disable=W0612
9495
# testing
9596
# * services:name
9697
# * services:0:name
@@ -106,6 +107,8 @@ def concat_values(mydict, hierarchy):
106107

107108

108109
def statuspalstate2state(sps):
110+
"""Convert Statuspal's incident level to the Nagios world.
111+
"""
109112
if sps is None:
110113
return STATE_OK
111114
if sps == 'minor':
@@ -132,7 +135,6 @@ def main():
132135
result = lib.base.coe(lib.url.fetch_json(args.URL))
133136
else:
134137
# do not call the command, put in test data
135-
import json
136138
stdout, stderr, retc = lib.test.test(args.TEST)
137139
result = json.loads(stdout)
138140

@@ -144,7 +146,6 @@ def main():
144146
# init some vars
145147
msg = ''
146148
state = statuspalstate2state(result['status_page']['current_incident_type'])
147-
perfdata = ''
148149

149150
# analyze data and build the message
150151

@@ -174,7 +175,6 @@ def main():
174175

175176
# services - search for any incidents in services
176177
flattened_result = flatdict.FlatterDict(result['services'])
177-
old_key = ''
178178
table_data = []
179179
tmp = {}
180180
for key, value in flattened_result.items():
@@ -187,18 +187,13 @@ def main():
187187
tmp = {}
188188
if table_data:
189189
msg += '\n\n'
190-
keys = [
191-
'name',
192-
'state',
193-
]
194-
headers = [
195-
'Service',
196-
'State',
197-
]
198-
msg += lib.base.get_table(table_data , keys, header=headers)
190+
msg += lib.base.get_table(
191+
table_data,
192+
['name', 'state'],
193+
header=['Service', 'State'],
194+
)
199195

200196
# maintenance
201-
old_key = ''
202197
table_data = []
203198
for maint in result['maintenances']:
204199
table_data.append({
@@ -207,29 +202,20 @@ def main():
207202
'starts_at': '{}'.format(
208203
lib.time.timestr2datetime(maint['starts_at'], pattern='%Y-%m-%dT%H:%M:%S'),
209204
),
210-
# in case of an ongoing maint, ends_at is "None"
205+
# in case of an ongoing maint, ends_at is "None"
211206
'ends_at': '{}'.format(
212207
lib.time.timestr2datetime(maint['ends_at'], pattern='%Y-%m-%dT%H:%M:%S'),
213208
) if maint.get('ends_at') is not None else 'in progress',
214209
})
215210
if table_data:
216211
msg += '\n'
217-
keys = [
218-
'title',
219-
'type',
220-
'starts_at',
221-
'ends_at',
222-
]
223-
headers = [
224-
'Maintenance',
225-
'Type',
226-
'Start',
227-
'End',
228-
]
229-
msg += lib.base.get_table(table_data , keys, header=headers)
212+
msg += lib.base.get_table(
213+
table_data,
214+
['title', 'type', 'starts_at', 'ends_at'],
215+
header=['Maintenance', 'Type', 'Start', 'End'],
216+
)
230217

231218
# upcoming_maintenances (just fyi)
232-
old_key = ''
233219
table_data = []
234220
for maint in result['upcoming_maintenances']:
235221
table_data.append({
@@ -238,29 +224,21 @@ def main():
238224
'starts_at': '{}'.format(
239225
lib.time.timestr2datetime(maint['starts_at'], pattern='%Y-%m-%dT%H:%M:%S'),
240226
),
241-
# in case of an ongoing maint, ends_at is "None"
227+
# in case of an ongoing maint, ends_at is "None"
242228
'ends_at': '{}'.format(
243229
lib.time.timestr2datetime(maint['ends_at'], pattern='%Y-%m-%dT%H:%M:%S'),
244230
) if maint.get('ends_at') is not None else 'open end',
245231
})
246232
if table_data:
247233
msg += '\n'
248-
keys = [
249-
'title',
250-
'type',
251-
'starts_at',
252-
'ends_at',
253-
]
254-
headers = [
255-
'Upcoming Maintenance',
256-
'Type',
257-
'Start',
258-
'End',
259-
]
260-
msg += lib.base.get_table(table_data , keys, header=headers)
234+
msg += lib.base.get_table(
235+
table_data,
236+
['title', 'type', 'starts_at', 'ends_at'],
237+
header=['Upcoming Maintenance', 'Type', 'Start', 'End'],
238+
)
261239

262240
# over and out
263-
lib.base.oao(msg, state, perfdata, always_ok=args.ALWAYS_OK)
241+
lib.base.oao(msg, state, '', always_ok=args.ALWAYS_OK)
264242

265243

266244
if __name__ == '__main__':

0 commit comments

Comments
 (0)