Commit 454d39a9 authored by Bram Daams's avatar Bram Daams

Merge branch '7-listing-status-with-sch' into 'master'

Resolve "listing status with sch"

Closes #7

See merge request !23
parents a578fd19 17906d5f
Pipeline #40933 passed with stage
in 38 seconds
......@@ -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
......@@ -2,4 +2,4 @@
__author__ = """Bram Daams"""
__email__ = 'b.daams@science.ru.nl'
__version__ = '0.2.4'
__version__ = '0.3.0'
......@@ -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__":
......
......@@ -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:
......
[bumpversion]
current_version = 0.2.4
current_version = 0.3.0
commit = True
tag = True
......
......@@ -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,
)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment