3535import sys
3636import argparse
3737import logging
38- import requests
3938import datetime
40- from requests . auth import HTTPBasicAuth
39+ import ssl
4140from urllib .parse import urljoin
41+ import urllib3
42+ import requests
43+ from requests .auth import HTTPBasicAuth
4244
4345
4446VERSION = '0.1.2'
4547
46- OK = 0
47- WARNING = 1
48+ OK = 0
49+ WARNING = 1
4850CRITICAL = 2
49- UNKNOWN = 3
51+ UNKNOWN = 3
5052
5153STATES = {
5254 OK : "OK" ,
@@ -63,7 +65,6 @@ def fix_tls_cert_store():
6365 See https://github.com/psf/requests/issues/2966
6466 Inspired by https://github.com/psf/requests/issues/2966#issuecomment-614323746
6567 """
66- import ssl
6768
6869 try :
6970 system_ca_store = ssl .get_default_verify_paths ().cafile
@@ -79,8 +80,6 @@ class CriticalException(Exception):
7980 Provide an exception that will cause the check to exit critically with an error
8081 """
8182
82- pass
83-
8483
8584class Client :
8685 """
@@ -103,8 +102,7 @@ def __init__(self, api, username, password, logger=None, verify=True, max_age=5)
103102
104103 self .logger = logger
105104
106-
107- def request (self , url , method = 'GET' , ** kwargs ):
105+ def request (self , url , method = 'GET' ):
108106 """
109107 Basic JSON request handling
110108
@@ -118,8 +116,8 @@ def request(self, url, method='GET', **kwargs):
118116
119117 try :
120118 response = requests .request (method , request_url , auth = HTTPBasicAuth (self .username , self .password ), verify = self .verify )
121- except requests .exceptions .RequestException as e :
122- raise CriticalException (e )
119+ except requests .exceptions .RequestException as req_exc :
120+ raise CriticalException (req_exc )
123121
124122 if response .status_code != 200 :
125123 raise CriticalException ('Request to %s was not successful: %s' % (request_url , response .status_code ))
@@ -129,24 +127,21 @@ def request(self, url, method='GET', **kwargs):
129127 except Exception as e :
130128 raise CriticalException ('Could not decode API JSON: ' + str (e ))
131129
132-
133130 def get_cluster_status (self ):
134131 """
135132 GET and build ClusterStatus
136133 """
137134 return ClusterStatus (self .request ('cluster/status' ))
138135
139-
140136 def get_alarms (self ):
141137 """
142138 GET and build Alarms
143139 """
144140 status = "OPEN"
145- #status = "RESOLVED" # for testing
141+ # status = "RESOLVED" # for testing
146142 result = self .request ('alarms?page_size=100&status=%s&sort_ascending=false' % status )
147143 return Alarms (result ['results' ])
148144
149-
150145 def get_capacity_usage (self ):
151146 """
152147 GET and build CapacityUsage
@@ -155,14 +150,17 @@ def get_capacity_usage(self):
155150
156151
157152class CheckResult :
153+ """
154+ CheckResult class, stores output, perfdata and state
155+ """
158156 def __init__ (self ):
159157 self .state = - 1
160158 self .summary = []
161159 self .output = []
162160 self .perfdata = []
163161
164162 def build_output (self ):
165- raise NotImplemented ("build_output not implemented in %s" % type (self ))
163+ raise NotImplementedError ("build_output not implemented in %s" % type (self ))
166164
167165 def get_output (self ):
168166 if len (self .summary ) == 0 :
@@ -184,7 +182,7 @@ def get_output(self):
184182 return "[%s] " % state + output
185183
186184 def build_status (self ):
187- raise NotImplemented ("build_status not implemented in %s" % type (self ))
185+ raise NotImplementedError ("build_status not implemented in %s" % type (self ))
188186
189187 def get_status (self ):
190188 if self .state < 0 :
@@ -266,12 +264,11 @@ def build_output(self):
266264 for state in states :
267265 self .summary .append ("%d %s" % (states [state ], state .lower ()))
268266
269-
270267 def build_status (self ):
271268 states = []
272269
273270 for alarm in self .data :
274- state = WARNING if alarm ['severity' ] in ['MEDIUM' , 'LOW' ] else CRITICAL # CRITICAL, HIGH
271+ state = WARNING if alarm ['severity' ] in ['MEDIUM' , 'LOW' ] else CRITICAL # CRITICAL, HIGH
275272 states .append (state )
276273
277274 if len (states ) > 0 :
@@ -295,7 +292,7 @@ def build_output(self):
295292 states = {}
296293
297294 for usage in self .data ['capacity_usage' ]:
298- severity = usage ['severity' ] # INFO, WARNING, CRITICAL, ERROR
295+ severity = usage ['severity' ] # INFO, WARNING, CRITICAL, ERROR
299296
300297 if severity in states :
301298 states [severity ] += 1
@@ -321,7 +318,7 @@ def build_output(self):
321318 label = usage ['usage_type' ].lower ()
322319 self .perfdata .append ("%s=%g%%;%d;%d;0;100" % (label , usage ['current_usage_percentage' ], usage ['min_threshold_percentage' ], usage ['max_threshold_percentage' ]))
323320 # Maybe we need count at some point...
324- #self.perfdata.append("%s_count=%d;;;0;%d" % (label, usage['current_usage_count'], usage['max_supported_count']))
321+ # self.perfdata.append("%s_count=%d;;;0;%d" % (label, usage['current_usage_count'], usage['max_supported_count']))
325322
326323 for state in states :
327324 self .summary .append ("%d %s" % (states [state ], state .lower ()))
@@ -342,7 +339,7 @@ def build_status(self):
342339 self .summary .append ("last update older than %s minutes" % (self .max_age ))
343340
344341 for usage in self .data ['capacity_usage' ]:
345- severity = usage ['severity' ] # INFO, WARNING, CRITICAL, ERROR
342+ severity = usage ['severity' ] # INFO, WARNING, CRITICAL, ERROR
346343
347344 if severity == "INFO" :
348345 state = OK
@@ -413,7 +410,6 @@ def main():
413410
414411 args = parse_args ()
415412 if args .insecure :
416- import urllib3
417413 urllib3 .disable_warnings ()
418414
419415 if args .version :
@@ -428,9 +424,9 @@ def main():
428424 return client .get_alarms ().print_and_return ()
429425 elif args .mode == 'capacity-usage' :
430426 return client .get_capacity_usage ().print_and_return ()
431- else :
432- print ("[UNKNOWN] unknown mode %s" % args .mode )
433- return UNKNOWN
427+
428+ print ("[UNKNOWN] unknown mode %s" % args .mode )
429+ return UNKNOWN
434430
435431
436432if __package__ == '__main__' or __package__ is None :
0 commit comments