From acc2216a164a4d829fe1088002309668b9325b5b Mon Sep 17 00:00:00 2001 From: Bram Daams Date: Sat, 4 Apr 2020 00:26:54 +0200 Subject: [PATCH 1/2] added command and filtering options for listing Healthchecks status --- HISTORY.md | 3 +++ sch/cli.py | 27 ++++++++++++++++++----- sch/sch.py | 65 +++++++++++++++++++++++++++++++++++------------------- 3 files changed, 66 insertions(+), 29 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index 09f16dd..7c000dc 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -16,3 +16,6 @@ - setting the user-agent string to 'sch/{version}' when interacting with the Healthchecks API - got rid of outdated development section in the readme + +### release notes for 0.3.0 +- added command and filtering options for listing Healthchecks status diff --git a/sch/cli.py b/sch/cli.py index cb2dd9e..d0b1f83 100644 --- a/sch/cli.py +++ b/sch/cli.py @@ -9,15 +9,30 @@ import click from sch import sch -@click.command() +@click.group(invoke_without_command=True, no_args_is_help=True) @click.version_option() -@click.option('-c', '--command', required=True) -def main(command): +@click.option('-c', '--shell_command', help='command to execute') +def main(shell_command=None): """ - SmartCronHellper - A shell wrapper for Healthchecks monitored cron jobs + sch - A cron shell wrapper for registering and updating cron jobs + automatically in healthchecks """ - sch.shell(command) - return 0 + if shell_command: + sch.shell(shell_command) + + +@main.command('list') +@click.option('-a', '--all', 'host_filter', flag_value='all') +@click.option('-l', '--local', 'host_filter', flag_value='local', + default=True) +@click.option('-s', '--status', 'status_filter', + type=click.Choice(['up', 'down', 'grace', 'pause', 'new'])) +def listchecks(host_filter, status_filter): + """ + list Healthchecks checks + """ + healthchecks = sch.get_hc_api() + healthchecks.print_status(host_filter, status_filter) if __name__ == "__main__": diff --git a/sch/sch.py b/sch/sch.py index 458a154..67f3796 100644 --- a/sch/sch.py +++ b/sch/sch.py @@ -61,9 +61,37 @@ def get_job_id(command): return None +def get_hc_api(): + """ + try loading Healthchecks API url and key + and return an instance of Healthchecks or None if it failed + """ + try: + config = configparser.ConfigParser() + config.read(['sch.conf', '/etc/sch.conf']) + + url = config.get('hc', 'healthchecks_api_url') + key = config.get('hc', 'healthchecks_api_key') + + cred = HealthchecksCredentials( + api_url=url, + api_key=key + ) + + healthchecks = Healthchecks(cred) + except configparser.Error: + logging.error( + 'Could not find/read/parse config' + 'file sch.conf or /etc/sch.conf' + ) + healthchecks = None + + return healthchecks + + def shell(command): """ - sch:run is a cron shell that registers, updates and pings cron jobs in + sch:shell is a cron shell that registers, updates and pings cron jobs in healthchecks.io a cronfile should have the SHELL variable pointing to the sch executable. @@ -89,25 +117,7 @@ def shell(command): except TypeError: logging.error("Could not find matching cron job") - # try loading Healthchecks API url and key - try: - config = configparser.ConfigParser() - config.read(['sch.conf', '/etc/sch.conf']) - - url = config.get('hc', 'healthchecks_api_url') - key = config.get('hc', 'healthchecks_api_key') - - cred = HealthchecksCredentials( - api_url=url, - api_key=key - ) - - health_checks = Healthchecks(cred) - except configparser.Error: - logging.error( - 'Could not find/read/parse config' - 'file sch.conf or /etc/sch.conf' - ) + health_checks = get_hc_api() check = None interfere = False @@ -228,7 +238,11 @@ class Healthchecks: } def get_checks(self, query=''): - """Returns a list of checks from the HC API""" + """ + Returns a list of checks from the HC API + reference: https://healthchecks.io/docs/api/#list-checks + """ + url = "{api_url}checks/{query}".format( api_url=self.cred.api_url, query=query @@ -455,7 +469,7 @@ class Healthchecks: logging.debug("Successfully set grace_time to %s seconds", grace) return True - def print_status(self, status_filter=""): + def print_status(self, host_filter, status_filter): """Show status of monitored cron jobs""" click.secho("{status:<6} {last_ping:<15} {name:<40}".format( status="Status", @@ -468,7 +482,12 @@ class Healthchecks: last_ping="" )) - checks = self.get_checks() + query = '' # host_filter == all + if host_filter == 'local': + tag_for_host = 'host={hostname}'.format(hostname=socket.getfqdn()) + query = "?&tag={tag}".format(tag=quote_plus(tag_for_host)) + + checks = self.get_checks(query) for i in checks: if status_filter and i['status'] != status_filter: -- GitLab From 63d1c1aac4b991e4cbec75df18a0312a70ad8f46 Mon Sep 17 00:00:00 2001 From: Bram Daams Date: Sat, 4 Apr 2020 00:27:02 +0200 Subject: [PATCH 2/2] =?UTF-8?q?Bump=20version:=200.2.4=20=E2=86=92=200.3.0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sch/__init__.py | 2 +- setup.cfg | 2 +- setup.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/sch/__init__.py b/sch/__init__.py index be92844..4689f1b 100644 --- a/sch/__init__.py +++ b/sch/__init__.py @@ -2,4 +2,4 @@ __author__ = """Bram Daams""" __email__ = 'b.daams@science.ru.nl' -__version__ = '0.2.4' +__version__ = '0.3.0' diff --git a/setup.cfg b/setup.cfg index 51abe58..be5b19c 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,5 +1,5 @@ [bumpversion] -current_version = 0.2.4 +current_version = 0.3.0 commit = True tag = True diff --git a/setup.py b/setup.py index e1a7842..e5671f4 100644 --- a/setup.py +++ b/setup.py @@ -59,6 +59,6 @@ setup( test_suite='tests', tests_require=TEST_REQUIREMENTS, url='https://gitlab.science.ru.nl/bram/sch', - version='0.2.4', + version='0.3.0', zip_safe=False, ) -- GitLab