From ada7efd6a2965d242619256d33003365d5bbfd30 Mon Sep 17 00:00:00 2001 From: Jonathan Michalon Date: Sun, 24 Aug 2014 18:57:35 +0200 Subject: [PATCH] Move main script code into module and create installable executables --- README.md | 13 ++++++++ bin/picomon | 6 ++++ picomon_watchdog.py => bin/picomon-watchdog | 11 +++++-- picomon.py => picomon/__main__.py | 35 ++++++++++++++------- 4 files changed, 51 insertions(+), 14 deletions(-) create mode 100755 bin/picomon rename picomon_watchdog.py => bin/picomon-watchdog (66%) mode change 100644 => 100755 rename picomon.py => picomon/__main__.py (87%) diff --git a/README.md b/README.md index c2968b1..4aca04a 100644 --- a/README.md +++ b/README.md @@ -77,6 +77,19 @@ They are sent on a regular basis (configured with option `emails.report.every`) only if there are some checks in an error state. +Test it! +-------- + +You have several solutions : + + * Install with `distutils`, for example: `setup.py install --user` then run it from your local bin/ directory; + * Run directly with something like `python3 -m picomon` + * Set python module path by hand: `PYTHONPATH=. bin/picomon` + +The default config file is `config.py`, so either copy/link the sample one, write +your own or use the `-s` command line argument. + + License ------- diff --git a/bin/picomon b/bin/picomon new file mode 100755 index 0000000..0f71293 --- /dev/null +++ b/bin/picomon @@ -0,0 +1,6 @@ +#!/usr/bin/env python3 + +import picomon.__main__ + + +picomon.__main__.run() diff --git a/picomon_watchdog.py b/bin/picomon-watchdog old mode 100644 new mode 100755 similarity index 66% rename from picomon_watchdog.py rename to bin/picomon-watchdog index a55cdf9..ab4873f --- a/picomon_watchdog.py +++ b/bin/picomon-watchdog @@ -1,3 +1,4 @@ +#!/usr/bin/env python3 import subprocess import sys import os @@ -7,16 +8,20 @@ from picomon import mails # launch picomon -retcode = subprocess.call(["python3", "picomon.py"] + sys.argv[1:]) +filename = 'picomon' +if os.path.sep in sys.argv[0]: + filename = os.path.join(os.path.dirname(sys.argv[0]), filename) + +retcode = subprocess.call([filename] + sys.argv[1:]) # load config file (unprotected, will trigger exceptions if problems but real # picomon beforehand) configfile = "config.py" if "-c" in sys.argv: - configfile = sys.argv[sys.argv.index("-c")+1] + configfile = sys.argv[sys.argv.index("-c") + 1] if "--config" in sys.argv: - configfile = sys.argv[sys.argv.index("--config")+1] + configfile = sys.argv[sys.argv.index("--config") + 1] sys.path.append(os.path.dirname(configfile)) filename = os.path.basename(configfile) diff --git a/picomon.py b/picomon/__main__.py similarity index 87% rename from picomon.py rename to picomon/__main__.py index 62e3c7e..cd40e20 100644 --- a/picomon.py +++ b/picomon/__main__.py @@ -1,3 +1,13 @@ + +""" +Picomon executable module. + +This module can be executed from a command line with ``$python -m picomon`` or +from a python programme with ``picomon.__main__.run()``. + +""" + + import concurrent.futures import signal import argparse @@ -6,12 +16,12 @@ import importlib import sys import os from time import sleep -from picomon import config -from picomon import mails from datetime import datetime, timedelta +from . import config +from . import mails -def create_report(only_old=False): +def __create_report(only_old=False): has_error = False report = '' report += "\n Checks in error:\n" @@ -33,22 +43,21 @@ def create_report(only_old=False): return (report, has_error) -def usr1_handler(signum, frame): - (report, err) = create_report() +def __usr1_handler(signum, frame): + (report, err) = __create_report() print ("Signal SIGUSR1 caught, printing state of checks.") print (report) -def alarm_handler(signum, frame): - (report, err) = create_report(only_old=True) +def __alarm_handler(signum, frame): + (report, err) = __create_report(only_old=True) if err: report = "Following entries have failed for more than %ss:\n" % \ config.emails.report.every + report mails.send_email_report(report) -if __name__ == '__main__': - +def run(): # Parse command line parser = argparse.ArgumentParser() parser.add_argument("-1", "--one", @@ -81,8 +90,8 @@ if __name__ == '__main__': logging.getLogger().setLevel('DEBUG') # register signal handling - signal.signal(signal.SIGUSR1, usr1_handler) - signal.signal(signal.SIGALRM, alarm_handler) + signal.signal(signal.SIGUSR1, __usr1_handler) + signal.signal(signal.SIGALRM, __alarm_handler) # register report signal interval if config.emails.report.every > 0: @@ -113,3 +122,7 @@ if __name__ == '__main__': executor.submit(check.run) sleep(config.base_tick) mails.quit() + + +if __name__ == '__main__': + run() -- GitLab