diff --git a/checkvsphere/cli.py b/checkvsphere/cli.py index 16e1bed..19b372a 100644 --- a/checkvsphere/cli.py +++ b/checkvsphere/cli.py @@ -69,6 +69,7 @@ def run(): for cmd in sorted(cmds): print(f" {cmd}") print() + sys.exit(3) def main(): @@ -106,11 +107,11 @@ def main(): traceback.print_exc(file=sys.stdout) sys.exit(3) except ConnectionRefusedError: - print("UNKNOWN - Connection refused") + print("CRITICAL - Connection refused") raise SystemExit(2) except vim.fault.VimFault as e: if hasattr(e, 'msg'): - print(f"ERROR - {e.msg}") + print(f"UNKNOWN - {e.msg}") else: # in case there is no msg attribute # According to the docs there is @@ -118,7 +119,7 @@ def main(): # but there is a msg attribute (which is not in the docs) # i don't know if it is set always # so fall back to the normal string representation - print(f"ERROR - {e}") + print(f"UNKNOWN - {e}") if int(os.environ.get("VSPHERE_DEBUG", "0")) > 0: traceback.print_exc(file=sys.stdout) raise SystemExit(3) diff --git a/checkvsphere/tools/cli.py b/checkvsphere/tools/cli.py index 2e3e1f9..99c82d7 100644 --- a/checkvsphere/tools/cli.py +++ b/checkvsphere/tools/cli.py @@ -36,6 +36,13 @@ def __call__(self, parser, namespace, values, option_string=None): setattr(namespace, self.dest, values) +class MonitoringArgumentParser(argparse.ArgumentParser): + def exit(self, status=0, message=None): + if status in (0, 2): + status = 3 + super().exit(status, message) + + class Parser: """ Samples specific argument parser. @@ -56,7 +63,7 @@ def __init__(self): One for the standard arguments and one for sample specific arguments. The standard group cannot be extended. """ - self._parser = argparse.ArgumentParser(description='Arguments for talking to vCenter') + self._parser = MonitoringArgumentParser(description='Arguments for talking to vCenter') self._standard_args_group = self._parser.add_argument_group('standard arguments') self._specific_args_group = self._parser.add_argument_group('sample-specific arguments') @@ -99,7 +106,7 @@ def __init__(self): self._standard_args_group.add_argument('--sessionfile', required=False, action='store', - help='Path to a file where the sessioncookie' + help='Path to a file where the session cookie ' 'will be stored for later reuse. (EXPERIMENTAL)') self._standard_args_group.add_argument('--match-method', diff --git a/checkvsphere/vcmd/about.py b/checkvsphere/vcmd/about.py index 366fab3..1f0b381 100644 --- a/checkvsphere/vcmd/about.py +++ b/checkvsphere/vcmd/about.py @@ -25,59 +25,57 @@ import logging import os from monplugin import Status -from pyVmomi import vim from ..tools import cli, service_instance def run(): - try: - parser = cli.Parser() - parser.add_optional_arguments({ - 'name_or_flags': ['--skip-permission'], - 'options': { - 'action': 'store_true', - 'default': False, - 'help': 'skips the System.View permission check', - } - }) + parser = cli.Parser() + parser.add_optional_arguments({ + 'name_or_flags': ['--skip-permission'], + 'options': { + 'action': 'store_true', + 'default': False, + 'help': 'skips the System.View permission check', + } + }) - args = parser.get_args() - si = service_instance.connect(args) - about = si.content.about - status = Status.OK - clock = True - if not args.skip_permission: - try: - clock = si.serverClock - except Exception: - logging.debug("no server clock", exc_info=1) - status = Status.CRITICAL - clock = None - if args.sessionfile: - try: - logging.debug(f"deleting {args.sessionfile}") - os.unlink(args.sessionfile) - except Exception: - logging.debug(f"unlink {args.sessionfile} failed", exc_info=1) + args = parser.get_args() + si = service_instance.connect(args) + about = si.content.about + status = Status.OK + clock = True + if not args.skip_permission: + try: + clock = si.serverClock + except Exception: + logging.debug("no server clock", exc_info=1) + status = Status.CRITICAL + clock = None + if args.sessionfile: + try: + logging.debug(f"deleting {args.sessionfile}") + os.unlink(args.sessionfile) + except Exception: + logging.debug(f"unlink {args.sessionfile} failed", exc_info=1) - out = ( - f'{status.name}: ' - f'{ "No System.View permission, " if not clock else "" }' - f'{ about.fullName }, ' - f'api: { about.apiType }/{ about.apiVersion }, ' - f'product: { about.licenseProductName } { about.licenseProductVersion }' - ) - print(out) - raise SystemExit(status.value) - except vim.fault.VimFault as e: - if hasattr(e, 'msg'): - print(f"ERROR: {e.msg}") - else: - print(f"ERROR: {e}") - raise SystemExit(2) - except Exception as e: - print(f"ERROR: {e}") - raise SystemExit(2) + out = ( + f'{status.name}: ' + f'{ "No System.View permission, " if not clock else "" }' + f'{ about.fullName }, ' + f'api: { about.apiType }/{ about.apiVersion }, ' + f'product: { about.licenseProductName } { about.licenseProductVersion }' + ) + print(out) + raise SystemExit(status.value) if __name__ == "__main__": - run() + try: + run() + except SystemExit as e: + if not isinstance(e.code, int) or e.code > 3 or e.code < 0: + print("UNKNOWN EXIT CODE") + raise SystemExit(Status.UNKNOWN) + raise + except Exception as e: + print("UNKNOWN - " + str(e)) + raise SystemExit(Status.UNKNOWN)