From 90988e3825b97c3932a7c934a88cbfa330e9ab89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Freitas?= Date: Wed, 16 Dec 2015 17:39:46 +0000 Subject: [PATCH] Adds channel option and version output --- README.md | 6 +++--- mattermost.py | 10 +++++++++- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 38c61bd..ec6a0b9 100644 --- a/README.md +++ b/README.md @@ -9,14 +9,14 @@ Assuming you are using Nagios 4, the steps are: define command { command_name notify-service-by-mattermost - command_line /usr/local/nagios/libexec/mattermost.py --url [URL] --hostalias "$HOSTNAME$" --notificationtype "$NOTIFICATIONTYPE$" --servicedesc "$SERVICEDESC$" --servicestate "$SERVICESTATE$" --serviceoutput "$SERVICEOUTPUT$" + command_line /usr/local/nagios/libexec/mattermost.py --url [URL] --channel [CHANNEL] --hostalias "$HOSTNAME$" --notificationtype "$NOTIFICATIONTYPE$" --servicedesc "$SERVICEDESC$" --servicestate "$SERVICESTATE$" --serviceoutput "$SERVICEOUTPUT$" } define command { command_name notify-host-by-mattermost - command_line /usr/local/nagios/libexec/mattermost.py --url [URL] --hostalias "$HOSTNAME$" --notificationtype "$NOTIFICATIONTYPE$" --hoststate "$HOSTSTATE$" --hostoutput "$HOSTOUTPUT$" + command_line /usr/local/nagios/libexec/mattermost.py --url [URL] --channel [CHANNEL] --hostalias "$HOSTNAME$" --notificationtype "$NOTIFICATIONTYPE$" --hoststate "$HOSTSTATE$" --hostoutput "$HOSTOUTPUT$" } - + 3. Create the contact: diff --git a/mattermost.py b/mattermost.py index 786c473..c4d49a5 100755 --- a/mattermost.py +++ b/mattermost.py @@ -24,6 +24,8 @@ import urllib2 import json +VERSION = "0.1.0" + CONFIG = { "icon_url": "https://slack.global.ssl.fastly.net/7bf4/img/services/nagios_128.png", #noqa "username": "Nagios" @@ -34,7 +36,7 @@ def parse(): - parser = argparse.ArgumentParser(description='Send mattermost webhooks') + parser = argparse.ArgumentParser(description='Sends mattermost webhooks') parser.add_argument('--url', help='Integration URL', required=True) parser.add_argument('--hostalias', help='Host Alias', required=True) parser.add_argument('--notificationtype', help='Notification type', @@ -44,6 +46,9 @@ def parse(): parser.add_argument('--servicedesc', help='Service Description') parser.add_argument('--servicestate', help='Service State') parser.add_argument('--serviceoutput', help='Service Output') + parser.add_argument('--channel', help='Channel to notificate') + parser.add_argument('--version', action='version', + version='%(prog)s {version}'.format(version=VERSION)) args = parser.parse_args() return args @@ -63,6 +68,9 @@ def make_data(args, config): "text": encode_special_characters(text) } + if args.channel: + payload["channel"] = args.channel + data = "payload=" + json.dumps(payload) return data