Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions checkvsphere/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ def run():
for cmd in sorted(cmds):
print(f" {cmd}")
print()
sys.exit(3)


def main():
Expand Down Expand Up @@ -106,19 +107,19 @@ 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
# faultCause and faultMessage, but they are empty
# 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)
Expand Down
11 changes: 9 additions & 2 deletions checkvsphere/tools/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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')

Expand Down Expand Up @@ -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',
Expand Down
94 changes: 46 additions & 48 deletions checkvsphere/vcmd/about.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)