4141from urllib .parse import urljoin
4242
4343
44- VERSION = '0.1.1 '
44+ VERSION = '0.1.2 '
4545
4646OK = 0
4747WARNING = 1
@@ -89,13 +89,14 @@ class Client:
8989
9090 API_PREFIX = '/api/v1/'
9191
92- def __init__ (self , api , username , password , logger = None , verify = True ):
92+ def __init__ (self , api , username , password , logger = None , verify = True , max_age = 5 ):
9393 # TODO: parse and validate url?
9494
9595 self .api = api
9696 self .username = username
9797 self .password = password
9898 self .verify = verify
99+ self .max_age = max_age
99100
100101 if logger is None :
101102 logger = logging .getLogger ()
@@ -150,7 +151,7 @@ def get_capacity_usage(self):
150151 """
151152 GET and build CapacityUsage
152153 """
153- return CapacityUsage (self .request ('capacity/usage' ))
154+ return CapacityUsage (self .request ('capacity/usage' ), self . max_age )
154155
155156
156157class CheckResult :
@@ -285,9 +286,10 @@ class CapacityUsage(CheckResult):
285286 https://vdc-download.vmware.com/vmwb-repository/dcr-public/787988e9-6348-4b2a-8617-e6d672c690ee/a187360c-77d5-4c0c-92a8-8e07aa161a27/api_includes/method_GetProtonCapacityUsage.html
286287 """
287288
288- def __init__ (self , data ):
289+ def __init__ (self , data , max_age ):
289290 super ().__init__ ()
290291 self .data = data
292+ self .max_age = max_age
291293
292294 def build_output (self ):
293295 states = {}
@@ -335,9 +337,9 @@ def build_status(self):
335337 now = datetime .datetime .now ()
336338 last_updated = build_datetime (self .data ['meta_info' ]['last_updated_timestamp' ])
337339
338- if (now - last_updated ).total_seconds () / 60 > 5 :
340+ if (now - last_updated ).total_seconds () / 60 > self . max_age :
339341 states .append (WARNING )
340- self .summary .append ("last update older than 5 minutes" )
342+ self .summary .append ("last update older than %s minutes" % ( self . max_age ) )
341343
342344 for usage in self .data ['capacity_usage' ]:
343345 severity = usage ['severity' ] # INFO, WARNING, CRITICAL, ERROR
@@ -397,6 +399,8 @@ def parse_args():
397399
398400 args .add_argument ('--mode' , '-m' , help = 'Check mode' , required = True )
399401
402+ args .add_argument ('--max-age' , '-M' , help = 'Max age in minutes for capacity usage updates. Defaults to 5' , default = 5 , required = False )
403+
400404 args .add_argument ('--version' , '-V' , help = 'Print version' , action = 'store_true' )
401405
402406 args .add_argument ('--insecure' , help = 'Do not verify TLS certificate. Be careful with this option, please' , action = 'store_true' , required = False )
@@ -416,7 +420,7 @@ def main():
416420 print ("check_vmware_nsxt version %s" % VERSION )
417421 return 0
418422
419- client = Client (args .api , args .username , args .password , verify = (not args .insecure ))
423+ client = Client (args .api , args .username , args .password , verify = (not args .insecure ), max_age = int ( args . max_age ) )
420424
421425 if args .mode == 'cluster-status' :
422426 return client .get_cluster_status ().print_and_return ()
0 commit comments