Module: check_mk
Branch: master
Commit: fd949c6de2accc237c7492746c248087c1c2e0bd
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=fd949c6de2accc…
Author: Simon Betz <si(a)mathias-kettner.de>
Date: Wed Apr 26 12:09:55 2017 +0200
4592 New options for pushover notification plugin: Usage of a proxy, receipt and sound
selection
Change-Id: I77913c4105a8487aa5951ff3355e66d13f3da6e0
---
.werks/4592 | 10 ++++++++
notifications/pushover | 44 +++++++++++++++++++++------------
web/plugins/wato/notifications.py | 52 ++++++++++++++++++++++++++++++++++++---
3 files changed, 87 insertions(+), 19 deletions(-)
diff --git a/.werks/4592 b/.werks/4592
new file mode 100644
index 0000000..8596215
--- /dev/null
+++ b/.werks/4592
@@ -0,0 +1,10 @@
+Title: New options for pushover notification plugin: Usage of a proxy, receipt and sound
selection
+Level: 1
+Component: notifications
+Compatible: compat
+Edition: cre
+Version: 1.5.0i1
+Date: 1493201333
+Class: feature
+
+
diff --git a/notifications/pushover b/notifications/pushover
index a3527e1..1aed40d 100755
--- a/notifications/pushover
+++ b/notifications/pushover
@@ -25,7 +25,7 @@
# to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
# Boston, MA 02110-1301 USA.
-import urllib2, urllib, json, os, re, sys
+import requests, json, os, re, sys
api_url = "https://api.pushover.net/1/messages.json"
@@ -115,33 +115,45 @@ def substitute_context(template, context):
def send_push_notification(api_key, recipient_key, subject, text, context):
- post_data = urllib.urlencode([
+ params = [
("token", api_key),
("user", recipient_key),
("title", subject.encode("utf-8")),
("message", text.encode("utf-8")),
("timestamp", int(context["MICROTIME"])/1000000),
- ("priority", context.get("PARAMETER_PRIORITY", 0)),
- ("expire", context.get("PARAMETER_PRIORITY_EXPIRE", 0)),
- ("retry", context.get("PARAMETER_PRIORITY_RETRY", 0)),
("html", 1),
- ])
+ ]
- result = urllib2.urlopen(api_url, post_data)
+ if context.get("PARAMETER_PRIORITY", 0):
+ params += [
+ ("priority", context["PARAMETER_PRIORITY"]),
+ ("expire", context.get("PARAMETER_PRIORITY_EXPIRE",
0)),
+ ("retry", context.get("PARAMETER_PRIORITY_RETRY",
0)),
+ ]
- # FIXME: add error handling
- response_txt = result.read()
- response = json.loads(response_txt)
+ if context.get("PARAMETER_PRIORITY_RECEIPTS"):
+ params.append( ("receipts",
context["PARAMETER_PRIORITY_RECEIPTS"]) )
- if response["status"] == 1:
+ if context.get("PARAMETER_SOUND", "none") != "none":
+ params.append( ("sound", context["PARAMETER_SOUND"]) )
+
+ s = requests.Session()
+ if context.get("PARAMETER_PROXY_URL"):
+ if context["PARAMETER_PROXY_URL"].startswith('https'):
+ protocol = 'https'
+ else:
+ protocol = 'http'
+ r = s.get(api_url, params=dict(params), proxies={protocol :
context["PARAMETER_PROXY_URL"]})
+
+ else:
+ r = s.get(api_url, params=dict(params))
+
+ r_status = r.status_code
+ if r_status == '200':
return True
else:
- sys.stdout.write("Failed to send notification. Response: %s\n" %
response)
+ sys.stdout.write("Failed to send notification. Status: %s, Response:
%s\n" % (r_status, r.text))
return False
- #status_code = result.getcode()
- #if status_code != 200:
- # sys.stdout.write("Status Code: %d\n") % status_code
- # sys.stdout.write("Response: %s\n") % result.read()
main()
diff --git a/web/plugins/wato/notifications.py b/web/plugins/wato/notifications.py
index e978975..e6106fd 100644
--- a/web/plugins/wato/notifications.py
+++ b/web/plugins/wato/notifications.py
@@ -288,16 +288,18 @@ register_notification_parameters(
# in modules/events.py can't handle complex data structures
def transform_back_pushover_priority(params):
if type(params) == tuple:
- return {"retry" : params[1][0], "expire" : params[1][1]}
+ return {"retry" : params[1][0],
+ "expire" : params[1][1],
+ "receipts" : params[1][2]}
return params
def transform_forth_pushover_priority(params):
if type(params) == dict:
- return ("2", (params["retry"], params["expire"]))
+ return ("2", (params["retry"], params["expire"],
params["receipts"]))
return params
register_notification_parameters("pushover", Dictionary(
- optional_keys = ["url_prefix", "priority"],
+ optional_keys = ["url_prefix", "proxy_url", "priority",
"sound"],
elements = [
("api_key", TextAscii(
title = _("API Key"),
@@ -332,6 +334,12 @@ register_notification_parameters("pushover", Dictionary(
default_value = "http://" + socket.gethostname() + "/"
+ (
config.omd_site() and config.omd_site() + "/" or
"") + "check_mk/",
)),
+ ("proxy_url", TextAscii(
+ title = _("Proxy-URL"),
+ size = 64,
+ regex = "^(http|https)://.*",
+ regex_error = _("The URL must begin with <tt>http</tt> or
<tt>https</tt>."),
+ )),
("priority", Transform(
CascadingDropdown(
title = _("Priority"),
@@ -340,6 +348,14 @@ register_notification_parameters("pushover", Dictionary(
Tuple(elements = [
Age(title = _("Retry time")),
Age(title = _("Expire time")),
+ TextAscii(
+ title = _("Receipt"),
+ help = _("The receipt can be used to periodically
poll receipts API to get "
+ "the status of the notification. "
+ "See <a
href=\"https://pushover.net/api#receipt\" target=\"_blank\">"
+ "Pushover receipts and callbacks</a>
for more information."),
+ size = 40,
+ regex = "[a-zA-Z0-9]{0,30}"),
]),
),
("1", _("High: Push notification alerts bypass quiet
hours")),
@@ -352,5 +368,35 @@ register_notification_parameters("pushover", Dictionary(
forth = transform_forth_pushover_priority,
back = transform_back_pushover_priority,
)),
+ ("sound", DropdownChoice(
+ title = _("Select sound"),
+ help = _("See <a
href=\"https://pushover.net/api#sounds\" target=\"_blank\">"
+ "Pushover sounds</a> for more information and trying
out available sounds."),
+ choices = [
+ ("none", _("None (silent)")),
+ ("alien", _("Alien Alarm (long)")),
+ ("bike", _("Bike")),
+ ("bugle", _("Bugle")),
+ ("cashregister", _("Cash Register")),
+ ("classical", _("Classical")),
+ ("climb", _("Climb (long)")),
+ ("cosmic", _("Cosmic")),
+ ("echo", _("Pushover Echo (long)")),
+ ("falling", _("Falling")),
+ ("gamelan", _("Gamelan")),
+ ("incoming", _("Incoming")),
+ ("intermission", _("Intermission")),
+ ("magic", _("Magic")),
+ ("mechanical", _("Mechanical")),
+ ("persistent", _("Persistent (long)")),
+ ("pianobar", _("Piano Bar")),
+ ("pushover", _("Pushover")),
+ ("siren", _("Siren")),
+ ("spacealarm", _("Space Alarm")),
+ ("tugboat", _("Tug Boat")),
+ ("updown", _("Up Down (long)")),
+ ],
+ default_value = "none"
+ )),
]
))