From 9dcb770ca500bf74dc91567215ca0f30e243977e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Freitas?= Date: Mon, 14 Dec 2015 10:40:19 +0000 Subject: [PATCH] Encode % special character --- mattermost.py | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/mattermost.py b/mattermost.py index b65a20b..786c473 100755 --- a/mattermost.py +++ b/mattermost.py @@ -25,18 +25,20 @@ import json CONFIG = { - "icon_url": "https://slack.global.ssl.fastly.net/7bf4/img/services/nagios_128.png", + "icon_url": "https://slack.global.ssl.fastly.net/7bf4/img/services/nagios_128.png", #noqa "username": "Nagios" } -TEMPLATE_SERVICE = "__{notificationtype}__ {hostalias}/{servicedesc} is {servicestate}\n{serviceoutput}" -TEMPLATE_HOST = "__{notificationtype}__ {hostalias} is {hoststate}\n{hostoutput}" +TEMPLATE_SERVICE = "__{notificationtype}__ {hostalias}/{servicedesc} is {servicestate}\n{serviceoutput}" #noqa +TEMPLATE_HOST = "__{notificationtype}__ {hostalias} is {hoststate}\n{hostoutput}" #noqa + def parse(): parser = argparse.ArgumentParser(description='Send 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', required=True) + parser.add_argument('--notificationtype', help='Notification type', + required=True) parser.add_argument('--hoststate', help='Host State') parser.add_argument('--hostoutput', help='Host Output') parser.add_argument('--servicedesc', help='Service Description') @@ -45,17 +47,26 @@ def parse(): args = parser.parse_args() return args + +def encode_special_characters(text): + text = text.replace("%", "%25") + return text + + def make_data(args, config): template = TEMPLATE_SERVICE if args.servicestate else TEMPLATE_HOST text = template.format(**vars(args)) + payload = { "username": config["username"], "icon_url": config["icon_url"], - "text": text + "text": encode_special_characters(text) } + data = "payload=" + json.dumps(payload) return data + def request(url, data): req = urllib2.Request(url, data) response = urllib2.urlopen(req)