diff options
author | Rahiel Kasim <rahielkasim@gmail.com> | 2017-09-25 15:12:36 +0200 |
---|---|---|
committer | Rahiel Kasim <rahielkasim@gmail.com> | 2017-09-25 15:12:36 +0200 |
commit | c83d2c6bf65c5ae0aca73e256a8ed7144155e3b3 (patch) | |
tree | 713be2a39cfcdb5cf6dda6dcb256427e77e57ca4 | |
parent | c255aa76610dd8467401b6cef146284b89105ca5 (diff) |
option to prepend messages with the hostname
-rw-r--r-- | README.md | 12 | ||||
-rw-r--r-- | supervisor_alert.py | 22 |
2 files changed, 25 insertions, 9 deletions
@@ -57,9 +57,15 @@ telegram-send, to use a different config, or to pass any other options: command=supervisor-alert -c 'telegram-send --config /home/user/bunny.conf' ``` -This configuration will run the event listener as the user `supervisor_alert`. -It is a good practice to isolate services by running them as separate users (and -avoiding running them as root). Add the user with: +Optionally you can show the hostname before each message with the +`--show-hostname` option: +``` shell +command=supervisor-alert --telegram --show-hostname +``` + +The default configuration will run the event listener as the user +`supervisor_alert`. It is a good practice to isolate services by running them as +separate users (and avoiding running them as root). Add the user with: ``` shell sudo adduser supervisor_alert --system --no-create-home ``` diff --git a/supervisor_alert.py b/supervisor_alert.py index 5572c95..30f64dc 100644 --- a/supervisor_alert.py +++ b/supervisor_alert.py @@ -18,22 +18,23 @@ import shlex from functools import partial from os.path import expanduser from pwd import getpwnam +from socket import gethostname from subprocess import CalledProcessError, check_call -import socket from supervisor.childutils import listener, get_headers -__version__ = "0.4" - +__version__ = "0.5" telegram_conf_args = ["--config", "/etc/telegram-send.conf"] + def main(): parser = argparse.ArgumentParser(description="Supervisor event listener to notify on process events.", epilog="Homepage: https://github.com/rahiel/supervisor-alert") parser.add_argument("-c", "--command", help="Specify the command to process the event messages.") parser.add_argument("--telegram", help="Use telegram-send to send event messages.", action="store_true") parser.add_argument("--configure", help="configure %(prog)s", action="store_true") + parser.add_argument("--show-hostname", help="show hostname in messages", action="store_true") parser.add_argument("--version", action="version", version="%(prog)s {}".format(__version__)) args = parser.parse_args() @@ -42,6 +43,8 @@ def main(): s = "PROCESS_STATE_" + hostname = gethostname() + if args.telegram: alert = telegram elif args.command: @@ -58,6 +61,8 @@ def main(): data = get_headers(payload) # keys: from_state, pid, processname process_name = data["processname"] message = process_name + " has entered state " + event_name + if args.show_hostname: + message = hostname + ": " + message alert(message) else: listener.ok() @@ -65,7 +70,6 @@ def main(): def telegram(message): """Send message with telegram-send.""" - message = "%s)\n%s" % (socket.gethostname(), message) # Add hostname info before the message try: check_call(["telegram-send", message] + telegram_conf_args) listener.ok() @@ -78,7 +82,7 @@ def telegram(message): def send(command, message): - "Send message with an arbitrary command." + """Send message with an arbitrary command.""" try: check_call(command + [message]) listener.ok() @@ -91,13 +95,19 @@ def configure(): conf = "/etc/supervisor/conf.d/supervisor_alert.conf" config = """[eventlistener:supervisor_alert] -command=supervisor-alert --telegram +command=supervisor-alert --telegram{} events=PROCESS_STATE_RUNNING,PROCESS_STATE_EXITED,PROCESS_STATE_FATAL autostart=true autorestart=true user=supervisor_alert """ + show_hostname = raw_input("Prepend messages with the hostname? [y/n] ").strip().lower() + if show_hostname == "y": + config = config.format(" --show-hostname") + else: + config = config.format("") + try: with open(conf, "w") as f: f.write(config) |