#!/usr/bin/env python
# Push Notifications (using Pushover)

# +------------------------------------------------------------------+
# |             ____ _               _        __  __ _  __           |
# |            / ___| |__   ___  ___| | __   |  \/  | |/ /           |
# |           | |   | '_ \ / _ \/ __| |/ /   | |\/| | ' /            |
# |           | |___| | | |  __/ (__|   <    | |  | | . \            |
# |            \____|_| |_|\___|\___|_|\_\___|_|  |_|_|\_\           |
# |                                                                  |
# | Copyright Mathias Kettner 2015             mk@mathias-kettner.de |
# +------------------------------------------------------------------+
#
# This file is part of Check_MK.
# The official homepage is at http://mathias-kettner.de/check_mk.
#
# check_mk is free software;  you can redistribute it and/or modify it
# under the  terms of the  GNU General Public License  as published by
# the Free Software Foundation in version 2.  check_mk is  distributed
# in the hope that it will be useful, but WITHOUT ANY WARRANTY;  with-
# out even the implied warranty of  MERCHANTABILITY  or  FITNESS FOR A
# PARTICULAR PURPOSE. See the  GNU General Public License for more de-
# tails. You should have  received  a copy of the  GNU  General Public
# License along with GNU Make; see the file  COPYING.  If  not,  write
# to the Free Software Foundation, Inc., 51 Franklin St,  Fifth Floor,
# Boston, MA 02110-1301 USA.

import requests, json, os, re, sys

api_url = "https://api.pushover.net/1/messages.json"

def main():
    context = dict([ (var[7:], value.decode("utf-8"))
                      for (var, value) in os.environ.items()
                      if var.startswith("NOTIFY_")])

    subject = get_subject(context)
    text    = get_text(context)

    api_key       = context["PARAMETER_API_KEY"]
    recipient_key = context["PARAMETER_RECIPIENT_KEY"]

    send_push_notification(api_key, recipient_key, subject, text, context)


def get_url(what, context):
    url_prefix = context.get("PARAMETER_URL_PREFIX")
    if url_prefix:
        base_url = url_prefix.rstrip('/')
        if base_url.endswith("/check_mk"):
            base_url = base_url[:-9]

        if what == "HOST":
            return base_url + context['HOSTURL']
        else:
            return base_url + context['SERVICEURL']


def get_subject(context):
    s = context["HOSTNAME"]

    if context["WHAT"] != "HOST":
        s += "/" + context["SERVICEDESC"]
    s += " "

    notification_type = context["NOTIFICATIONTYPE"]
    if notification_type in [ "PROBLEM", "RECOVERY" ]:
        s += "$PREVIOUS@HARDSHORTSTATE$ %s $@SHORTSTATE$" % unichr(8594)

    elif notification_type.startswith("FLAP"):
        if "START" in notification_type:
            s += "Started Flapping"
        else:
            s += "Stopped Flapping ($@SHORTSTATE$)"

    elif notification_type.startswith("DOWNTIME"):
        what = notification_type[8:].title()
        s += "Downtime " + what + " ($@SHORTSTATE$)"

    elif notification_type == "ACKNOWLEDGEMENT":
        s += "Acknowledged ($@SHORTSTATE$)"

    elif notification_type == "CUSTOM":
        s += "Custom Notification ($@SHORTSTATE$)"

    else:
        s += notification_type

    return substitute_context(s.replace("@", context["WHAT"]), context)


def get_text(context):
    s = ""

    #s += "<font color=\"%s\">" % color
    #s += "</font>"
    s += "$@OUTPUT$"

    if "PARAMETER_URL_PREFIX" in context:
        s += " <i>Link: </i>"
        s += "<a href=\"%s\">Host</a>" % get_url("HOST", context)
        if context["WHAT"] != "HOST":
            s += ", <a href=\"%s\">Service</a>" % get_url("SERVICE", context)

    return substitute_context(s.replace("@", context["WHAT"]), context)


def substitute_context(template, context):
    for varname, value in context.items():
        template = template.replace("$"+varname+"$", value)

    # Remove the rest of the variables and make them empty
    template = re.sub("\$[A-Z_][A-Z_0-9]*\$", "", template)
    return template


def send_push_notification(api_key, recipient_key, subject, text, context):
    params = [
        ("token",     api_key),
        ("user",      recipient_key),
        ("title",     subject.encode("utf-8")),
        ("message",   text.encode("utf-8")),
        ("timestamp", int(context["MICROTIME"])/1000000),
        ("html",      1),
    ]

    if context.get("PARAMETER_PRIORITY") in ["-2", "-1", "0", "1"]:
        params += [("priority", context["PARAMETER_PRIORITY"])]

    elif context.get("PARAMETER_PRIORITY_PRIORITY") == "2":
        params += [
            ("priority", context["PARAMETER_PRIORITY_PRIORITY"]),
            ("expire",   context.get("PARAMETER_PRIORITY_EXPIRE", 0)),
            ("retry",    context.get("PARAMETER_PRIORITY_RETRY", 0)),
        ]
        if context.get("PARAMETER_PRIORITY_RECEIPTS"):
            params.append(("receipts", context["PARAMETER_PRIORITY_RECEIPTS"]))

    if context.get("PARAMETER_SOUND", "none") != "none":
        params.append(("sound", context["PARAMETER_SOUND"]))

    s = requests.Session()
    if context.get("PARAMETER_PROXY"):
        if context["PARAMETER_PROXY"].startswith('https'):
            protocol = 'https'
        else:
            protocol = 'http'
        r = s.post(api_url, params=dict(params), proxies={protocol : context["PARAMETER_PROXY"]})

    else:
        r = s.post(api_url, params=dict(params))

    r_status = r.status_code
    if r_status == '200':
        return True
    else:
        sys.stdout.write("Failed to send notification. Status: %s, Response: %s\n" % (r_status, r.text))
        return False


main()
