Skip to content

Commit

Permalink
Merge pull request #7 from NETWAYS/style_and_linter_fixes
Browse files Browse the repository at this point in the history
Style and Linter fixes
  • Loading branch information
RincewindsHat authored Oct 13, 2022
2 parents 5271352 + 911aa72 commit ef520c7
Showing 1 changed file with 23 additions and 27 deletions.
50 changes: 23 additions & 27 deletions check_vmware_nsxt.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,20 @@
import sys
import argparse
import logging
import requests
import datetime
from requests.auth import HTTPBasicAuth
import ssl
from urllib.parse import urljoin
import urllib3
import requests
from requests.auth import HTTPBasicAuth


VERSION = '0.1.2'

OK = 0
WARNING = 1
OK = 0
WARNING = 1
CRITICAL = 2
UNKNOWN = 3
UNKNOWN = 3

STATES = {
OK: "OK",
Expand All @@ -63,7 +65,6 @@ def fix_tls_cert_store():
See https://github.com/psf/requests/issues/2966
Inspired by https://github.com/psf/requests/issues/2966#issuecomment-614323746
"""
import ssl

try:
system_ca_store = ssl.get_default_verify_paths().cafile
Expand All @@ -79,8 +80,6 @@ class CriticalException(Exception):
Provide an exception that will cause the check to exit critically with an error
"""

pass


class Client:
"""
Expand All @@ -103,8 +102,7 @@ def __init__(self, api, username, password, logger=None, verify=True, max_age=5)

self.logger = logger


def request(self, url, method='GET', **kwargs):
def request(self, url, method='GET'):
"""
Basic JSON request handling
Expand All @@ -118,8 +116,8 @@ def request(self, url, method='GET', **kwargs):

try:
response = requests.request(method, request_url, auth=HTTPBasicAuth(self.username, self.password), verify=self.verify)
except requests.exceptions.RequestException as e:
raise CriticalException(e)
except requests.exceptions.RequestException as req_exc:
raise CriticalException(req_exc)

if response.status_code != 200:
raise CriticalException('Request to %s was not successful: %s' % (request_url, response.status_code))
Expand All @@ -129,24 +127,21 @@ def request(self, url, method='GET', **kwargs):
except Exception as e:
raise CriticalException('Could not decode API JSON: ' + str(e))


def get_cluster_status(self):
"""
GET and build ClusterStatus
"""
return ClusterStatus(self.request('cluster/status'))


def get_alarms(self):
"""
GET and build Alarms
"""
status = "OPEN"
#status = "RESOLVED" # for testing
# status = "RESOLVED" # for testing
result = self.request('alarms?page_size=100&status=%s&sort_ascending=false' % status)
return Alarms(result['results'])


def get_capacity_usage(self):
"""
GET and build CapacityUsage
Expand All @@ -155,14 +150,17 @@ def get_capacity_usage(self):


class CheckResult:
"""
CheckResult class, stores output, perfdata and state
"""
def __init__(self):
self.state = -1
self.summary = []
self.output = []
self.perfdata = []

def build_output(self):
raise NotImplemented("build_output not implemented in %s" % type(self))
raise NotImplementedError("build_output not implemented in %s" % type(self))

def get_output(self):
if len(self.summary) == 0:
Expand All @@ -184,7 +182,7 @@ def get_output(self):
return "[%s] " % state + output

def build_status(self):
raise NotImplemented("build_status not implemented in %s" % type(self))
raise NotImplementedError("build_status not implemented in %s" % type(self))

def get_status(self):
if self.state < 0:
Expand Down Expand Up @@ -266,12 +264,11 @@ def build_output(self):
for state in states:
self.summary.append("%d %s" % (states[state], state.lower()))


def build_status(self):
states = []

for alarm in self.data:
state = WARNING if alarm['severity'] in ['MEDIUM', 'LOW'] else CRITICAL # CRITICAL, HIGH
state = WARNING if alarm['severity'] in ['MEDIUM', 'LOW'] else CRITICAL # CRITICAL, HIGH
states.append(state)

if len(states) > 0:
Expand All @@ -295,7 +292,7 @@ def build_output(self):
states = {}

for usage in self.data['capacity_usage']:
severity = usage['severity'] # INFO, WARNING, CRITICAL, ERROR
severity = usage['severity'] # INFO, WARNING, CRITICAL, ERROR

if severity in states:
states[severity] += 1
Expand All @@ -321,7 +318,7 @@ def build_output(self):
label = usage['usage_type'].lower()
self.perfdata.append("%s=%g%%;%d;%d;0;100" % (label, usage['current_usage_percentage'], usage['min_threshold_percentage'], usage['max_threshold_percentage']))
# Maybe we need count at some point...
#self.perfdata.append("%s_count=%d;;;0;%d" % (label, usage['current_usage_count'], usage['max_supported_count']))
# self.perfdata.append("%s_count=%d;;;0;%d" % (label, usage['current_usage_count'], usage['max_supported_count']))

for state in states:
self.summary.append("%d %s" % (states[state], state.lower()))
Expand All @@ -342,7 +339,7 @@ def build_status(self):
self.summary.append("last update older than %s minutes" % (self.max_age))

for usage in self.data['capacity_usage']:
severity = usage['severity'] # INFO, WARNING, CRITICAL, ERROR
severity = usage['severity'] # INFO, WARNING, CRITICAL, ERROR

if severity == "INFO":
state = OK
Expand Down Expand Up @@ -413,7 +410,6 @@ def main():

args = parse_args()
if args.insecure:
import urllib3
urllib3.disable_warnings()

if args.version:
Expand All @@ -428,9 +424,9 @@ def main():
return client.get_alarms().print_and_return()
elif args.mode == 'capacity-usage':
return client.get_capacity_usage().print_and_return()
else:
print("[UNKNOWN] unknown mode %s" % args.mode)
return UNKNOWN

print("[UNKNOWN] unknown mode %s" % args.mode)
return UNKNOWN


if __package__ == '__main__' or __package__ is None:
Expand Down

0 comments on commit ef520c7

Please sign in to comment.