From af249dfc7328361610ee5e7b4f240f89c04fbee5 Mon Sep 17 00:00:00 2001 From: Bram Daams Date: Fri, 10 Apr 2020 15:34:21 +0200 Subject: [PATCH 1/4] default log level ERROR --- .gitignore | 5 ++++- README.md | 10 ++++++++-- sch/sch.py | 33 ++++++++++++++++++++++++++------- 3 files changed, 38 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index 43091aa..2ddb653 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/README.md b/README.md index 379180a..21d97fe 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 @@ -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/sch/sch.py b/sch/sch.py index 49f8001..70aeb0e 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, -- GitLab From db27e1163b46d3629e341f676e4a5d3407fce14f Mon Sep 17 00:00:00 2001 From: Bram Daams Date: Fri, 10 Apr 2020 15:35:35 +0200 Subject: [PATCH 2/4] =?UTF-8?q?Bump=20version:=200.6.0=20=E2=86=92=200.6.1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- sch/__init__.py | 2 +- setup.cfg | 2 +- setup.py | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 21d97fe..105d087 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/sch/__init__.py b/sch/__init__.py index f0b08f7..81ab4e9 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/setup.cfg b/setup.cfg index 2471bc7..af9daea 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 b83bf96..7a34ef9 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, ) -- GitLab From 2983f19073220788eaa4313d3c17ae7b22f6d72a Mon Sep 17 00:00:00 2001 From: Bram Daams Date: Fri, 10 Apr 2020 15:37:14 +0200 Subject: [PATCH 3/4] changelog --- HISTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/HISTORY.md b/HISTORY.md index 6f5a37b..434584f 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 -- GitLab From b49d68893cca046949e9a521e21080f4ac058d05 Mon Sep 17 00:00:00 2001 From: Bram Daams Date: Fri, 10 Apr 2020 15:39:18 +0200 Subject: [PATCH 4/4] loglevel in example configuration --- doc/sch.conf.example | 3 +++ 1 file changed, 3 insertions(+) diff --git a/doc/sch.conf.example b/doc/sch.conf.example index d04afe6..da34052 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 -- GitLab