Skip to content

Commit 5271352

Browse files
Merge pull request #6 from marnovdm/main
Make CapacityUsage last update age threshold configurable. Backward compatible change.
2 parents 506fdab + 517d21f commit 5271352

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ optional arguments:
4343
--password PASSWORD, -p PASSWORD
4444
Password for Basic Auth
4545
--mode MODE, -m MODE Check mode
46+
--max-age MAX_AGE, -M MAX_AGE
47+
Max age in minutes for capacity usage updates. Defaults to 5
48+
--version, -V Print version
4649
--insecure Do not verify TLS certificate. Be careful with this option, please
4750
```
4851

check_vmware_nsxt.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
from urllib.parse import urljoin
4242

4343

44-
VERSION = '0.1.1'
44+
VERSION = '0.1.2'
4545

4646
OK = 0
4747
WARNING = 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

156157
class 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

Comments
 (0)