diff --git a/.gitignore b/.gitignore index 43091aa95b63522b73f7b47f3b3ef36356cd0b50..2ddb653c76ae4e2f8772cda89296e8c98dcdd1a2 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,9 @@ __pycache__/ # C extensions *.so +# config file +sch.conf + # Distribution / packaging .Python env/ @@ -102,4 +105,4 @@ ENV/ .mypy_cache/ # IDE settings -.vscode/ \ No newline at end of file +.vscode/ diff --git a/HISTORY.md b/HISTORY.md index 6f5a37b5ce40d1170ed02de81c3cd38346fd213c..434584f4c38b943ffcccb1fc433274709727d0d4 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -27,3 +27,4 @@ ### release notes for 0.6.x - listing of check status sorted by `last_ping` +- added configuration option sch:loglevel diff --git a/README.md b/README.md index 379180af822dd4a93cd151be84675edf296e2149..105d087634a171af57d03504ce040f286fdd68ca 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ A cron shell wrapper for registering and updating cron jobs automatically in [healthchecks](https://healthchecks.io) or your [own hosted copy of Healthchecks](https://github.com/healthchecks/healthchecks). -> warning: this software package should be considered "alpha" +> WARNING: once setup and configured, the code in this package runs as user specified in the cron jobs and is wrapped around the cron job commands. Errors in this package could prevent your cron jobs from being executed. ## Installation @@ -23,7 +23,7 @@ $ which sch `sch --version` should return something like: ``` console -sch, version 0.6.0 +sch, version 0.6.1 ``` ## Command line usage See the `--help` option for usage: @@ -76,10 +76,16 @@ Create a configuration file `/etc/sch.conf` that looks like: healthchecks_api_url = https://hc.example.com/api/v1/ healthchecks_api_key = xxmysecretkeyxx ``` - And fill in the API URL and the key obtained from the Healthchecks project settings block labeled "API Access". +Optionally, specify the log level in the configuration file: +``` ini +[sch] +loglevel = DEBUG +``` +Possible values for loglevel are explained [here](https://docs.python.org/3/library/logging.html#levels). The default log level is `ERROR`. + ## Monitoring cron jobs Just decorate your existing cron tabs by specifying the alternative `sch`: ``` diff --git a/doc/sch.conf.example b/doc/sch.conf.example index d04afe61b2d3ae672dbb49199ca9557341d24477..da340523e3339ad478b56a5d359275ab64ce41c2 100644 --- a/doc/sch.conf.example +++ b/doc/sch.conf.example @@ -1,3 +1,6 @@ +[sch] +loglevel = ERROR + [hc] healthchecks_api_url = https://hc.example.com/api/v1/ healthchecks_api_key = xxmysecretkeyxx diff --git a/sch/__init__.py b/sch/__init__.py index f0b08f797e4e94c926b1c6234d8ee750046b53a0..81ab4e9be74794c88cd74a4fd3a382d8690c15d7 100644 --- a/sch/__init__.py +++ b/sch/__init__.py @@ -2,4 +2,4 @@ __author__ = """Bram Daams""" __email__ = 'b.daams@science.ru.nl' -__version__ = '0.6.0' +__version__ = '0.6.1' diff --git a/sch/sch.py b/sch/sch.py index 49f8001aef4bcb7eccbaad4a225d16f322cc15e8..70aeb0e3ddcbe5128a2ab3be166ed800154b4694 100644 --- a/sch/sch.py +++ b/sch/sch.py @@ -22,6 +22,23 @@ import tzlocal from crontabs import CronTabs from . import __version__ + +def get_config(): + """ + try loading the configuration file + """ + try: + my_config = configparser.ConfigParser() + my_config.read(['sch.conf', '/etc/sch.conf']) + except configparser.Error: + logging.error( + 'Could not find/read/parse config' + 'file sch.conf or /etc/sch.conf' + ) + my_config = None + return my_config + + HANDLER = logging.handlers.SysLogHandler('/dev/log') FORMATTER = logging.Formatter( '{name}/%(module)s.%(funcName)s:' @@ -29,8 +46,13 @@ FORMATTER = logging.Formatter( ) HANDLER.setFormatter(FORMATTER) ROOT = logging.getLogger() -# log level DEBUG -ROOT.setLevel(logging.DEBUG) +CONFIG = get_config() +try: + LEVEL = CONFIG.get('sch', 'loglevel') + ROOT.setLevel(LEVEL) +except configparser.Error: + ROOT.setLevel(logging.ERROR) + ROOT.addHandler(HANDLER) @@ -81,10 +103,8 @@ def get_hc_api(): try loading Healthchecks API url and key and return an instance of Healthchecks or None if it failed """ + config = get_config() 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') @@ -97,7 +117,6 @@ def get_hc_api(): except configparser.Error: logging.error( 'Could not find/read/parse config' - 'file sch.conf or /etc/sch.conf' ) healthchecks = None @@ -195,7 +214,7 @@ def shell(command): start_time = time.time() # execute command - logging.debug( + logging.info( "Executing shell commmand: %s (job.id=%s)", command, job.id, diff --git a/setup.cfg b/setup.cfg index 2471bc7f2acb9d14373364e3f76ae4e2cea12db6..af9daeaf2bb5e39025435087dfc0937cd4e7a16d 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,5 +1,5 @@ [bumpversion] -current_version = 0.6.0 +current_version = 0.6.1 commit = True tag = True diff --git a/setup.py b/setup.py index b83bf96276489cdef28f01d0153d916b647eea6e..7a34ef9dc70706d54c1a7dca2db783e96f9ef8fe 100644 --- a/setup.py +++ b/setup.py @@ -58,6 +58,6 @@ setup( test_suite='tests', tests_require=TEST_REQUIREMENTS, url='https://gitlab.science.ru.nl/bram/sch', - version='0.6.0', + version='0.6.1', zip_safe=False, )