This file is indexed.

/usr/share/check_mk/notifications/pushover is in check-mk-server 1.2.8p16-1ubuntu0.1.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/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 urllib2, urllib, json, os, re

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):
    state = context[context["WHAT"]+"STATE"]

    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):
    post_data = urllib.urlencode([
        ("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)),
        ("html",      1),
    ])

    result = urllib2.urlopen(api_url, post_data)

    # FIXME: add error handling
    response_txt = result.read()
    response = json.loads(response_txt)

    if response["status"] == 1:
        return True
    else:
        print "Failed to send notification. Response: %s" % response
        return False
    #status_code = result.getcode()
    #if status_code != 200:
    #    print "Status Code: %d" % status_code
    #    print "Response: %s" %  result.read()


main()