From 852ab9cc10a73b099682c5f190757008d69e0efe Mon Sep 17 00:00:00 2001 From: Jonathan Michalon Date: Sat, 23 Aug 2014 16:50:47 +0200 Subject: [PATCH] Prefix messages with a timestamp and support multiple verbosity levels --- README.md | 2 ++ lib/__init__.py | 3 +++ lib/checks.py | 2 ++ lib/mails.py | 3 ++- picomon.py | 10 ++++++++++ 5 files changed, 19 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 4f34385..d6e52d5 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,8 @@ In addition some checks have specific options, see lib/checks.py for examples. In case you want to check lesser important services and configure very long check intervals, you may want to have another interval, global to all checks, for error retries. This can be set with the `error_every` option. +For a full list of all available options, see the lib/__init__.py file. + Current state output -------------------- diff --git a/lib/__init__.py b/lib/__init__.py index 52d271d..1d799dc 100644 --- a/lib/__init__.py +++ b/lib/__init__.py @@ -15,6 +15,9 @@ config.install_attr('base_tick', 60) # How often we retry checks that are in error (-1 disables feature) config.install_attr('error_every', -1) +# Verbosity level (one of CRITICAL, ERROR, WARNING, INFO, DEBUG) +config.install_attr('verb_level', 'INFO') + # Email addresses to send to when an alert is triggered config.install_attr('emails.to', []) # The From: address diff --git a/lib/checks.py b/lib/checks.py index ad07f84..209a671 100644 --- a/lib/checks.py +++ b/lib/checks.py @@ -1,5 +1,6 @@ from .subprocess_compat import TimeoutExpired, Popen, PIPE import re +import logging from . import mails from collections import Iterable from datetime import datetime @@ -61,6 +62,7 @@ class Check(object): config.error_every < 0 else config.error_every) if self.every_count == 0 or immediate: + logging.debug('Running ' + str(self)) self.setup() if not self.check(): self.retry_count = min(self.retry_count + 1, self.retry) diff --git a/lib/mails.py b/lib/mails.py index d08f27f..1951eb2 100644 --- a/lib/mails.py +++ b/lib/mails.py @@ -1,4 +1,5 @@ import smtplib +import logging from email.mime.text import MIMEText from email.utils import make_msgid from collections import defaultdict @@ -55,7 +56,7 @@ class ThreadedSMTP(object): server = smtplib.SMTP(host) server.sendmail(*args, **kwargs) except Exception as e: - print("Couldn't send email: %s" % str(e), file=stderr) + logging.warning("Couldn't send email: %s" % str(e)) finally: if server: # server is None on exception queue.Empty self._queue.task_done() diff --git a/picomon.py b/picomon.py index 553dd66..ff0e57e 100644 --- a/picomon.py +++ b/picomon.py @@ -1,6 +1,7 @@ import concurrent.futures import signal import argparse +import logging from time import sleep import config as user_config from lib import config @@ -60,8 +61,17 @@ if __name__ == '__main__': help="single run with immediate output of " + "check results (test/debug)", action="store_true") + parser.add_argument("-D", "--debug", + help="Set verbosity to DEBUG", + action="store_true") args = parser.parse_args() + # Configure logging + logging.basicConfig(format='%(asctime)s %(levelname)s: %(message)s', + level=config.verb_level) + if args.debug: + logging.getLogger().setLevel('DEBUG') + # do the actual polling with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor: def runner(check): -- GitLab