@@ -24,15 +24,8 @@ import lib.url # pylint: disable=C0413
2424from lib .globals import (STATE_CRIT , STATE_OK , # pylint: disable=C0413
2525 STATE_UNKNOWN , STATE_WARN )
2626
27- try :
28- import flatdict # pylint: disable=C0413
29- except ImportError as e :
30- print ('Python module "flatdict" is not installed.' )
31- sys .exit (STATE_UNKNOWN )
32-
33-
3427__author__ = 'Linuxfabrik GmbH, Zurich/Switzerland'
35- __version__ = '2025101701 '
28+ __version__ = '2026033101 '
3629
3730DESCRIPTION = """Statuspal is a status page provider from Germany. This check plugin gets
3831 the summary of a Statuspal status page, checks its status, services,
@@ -110,37 +103,28 @@ def parse_args():
110103 return args
111104
112105
113- def concat_values (mydict , hierarchy ):
114- """Concat the values of the same hierarchy level in a dict.
115-
116- >>> mydict = {
117- 'services:0:name': 'Global',
118- 'services:0:description': 'Lorem ipsum',
119- 'services:0:children:0:name': 'DNS'
120- 'services:0:children:0:id': '4711'
121- 'services:0:children:0:children:1:name': 'Server01'
122- }
123- >>> concat_values(mydict, 'services:0:name')
124- >>> 'Global'
125- >>> concat_values(mydict, 'services:0:children:0:name')
126- >>> 'Global.DNS'
127- >>> concat_values(mydict, 'services:0:children:0:children:1:name')
128- >>> 'Global.DNS.Server01'
106+ def flatten_services (services , parent_name = '' ):
107+ """Recursively flatten the services tree into a list of dicts with
108+ 'name' (dotted hierarchy) and 'current_incident_type'.
109+
110+ >>> services = [{'name': 'Global', 'current_incident_type': None, 'children': [
111+ ... {'name': 'DNS', 'current_incident_type': None, 'children': []}
112+ ... ]}]
113+ >>> flatten_services(services)
114+ [{'name': 'Global', 'current_incident_type': None},
115+ {'name': 'Global.DNS', 'current_incident_type': None}]
129116 """
130- result = ''
131- hierarchy = hierarchy .split (':' )
132- for i , item in enumerate (hierarchy ): # pylint: disable=W0612
133- # testing
134- # * services:name
135- # * services:0:name
136- # * services:0:children:name
137- # * services:0:children:0:name
138- # etc.
139- key = ':' .join (hierarchy [0 :i + 1 ]) + ':' + hierarchy [- 1 ]
140- if key in mydict :
141- result += mydict [key ] + '.'
142- if result .endswith ('.' ):
143- return result [:- 1 ]
117+ result = []
118+ for service in services :
119+ if not service .get ('name' ):
120+ continue
121+ full_name = '{}.{}' .format (parent_name , service ['name' ]) if parent_name else service ['name' ]
122+ result .append ({
123+ 'name' : full_name ,
124+ 'current_incident_type' : service .get ('current_incident_type' ),
125+ })
126+ for child in service .get ('children' ) or []:
127+ result .extend (flatten_services ([child ], full_name ))
144128 return result
145129
146130
@@ -227,21 +211,16 @@ def main():
227211 msg += ' (see {})' .format (result ['incidents' ][0 ]['url' ]) if result ['incidents' ][0 ]['url' ] else '' # pylint: disable=C0301
228212
229213 # services - search for any incidents in services
230- flattened_result = flatdict .FlatterDict (result ['services' ])
231- item = {}
232- for key , value in flattened_result .items ():
233- if key .endswith (':name' ):
234- item ['name' ] = concat_values (flattened_result , key )
235- if key .endswith (':current_incident_type' ):
236- item ['state' ] = statuspalstate2state (value )
237- if item ['state' ] == STATE_WARN :
238- cnt_warn += 1
239- if item ['state' ] == STATE_CRIT :
240- cnt_crit += 1
241- item ['state' ] = lib .base .state2str (item ['state' ], empty_ok = False )
242- if len (item ) == 2 :
243- table_data .append (item )
244- item = {}
214+ for service in flatten_services (result ['services' ]):
215+ service_state = statuspalstate2state (service ['current_incident_type' ])
216+ if service_state == STATE_WARN :
217+ cnt_warn += 1
218+ if service_state == STATE_CRIT :
219+ cnt_crit += 1
220+ table_data .append ({
221+ 'name' : service ['name' ],
222+ 'state' : lib .base .state2str (service_state , empty_ok = False ),
223+ })
245224 if table_data :
246225 msg += '\n \n '
247226 msg += lib .base .get_table (
0 commit comments