Checkmk telegram: Unterschied zwischen den Versionen

Aus xinux wiki
Wechseln zu: Navigation, Suche
(Die Seite wurde neu angelegt: „=Telegram Notifications= *cat /omd/sites/<site-name>/local/share/check_mk/notifications/telegram <pre> #!/usr/bin/python # Telegram # Copyright Mathias Kettne…“)
(Telegram Notifications)
Zeile 1: Zeile 1:
=Telegram Notifications=
=Telegram Notifications Plugin=
*cat /omd/sites/<site-name>/local/share/check_mk/notifications/telegram
*cat /omd/sites/<site-name>/local/share/check_mk/notifications/telegram
Zeile 116: Zeile 116:
=CheckMK User config=
*Users -> edit desired User
*Users -> edit desired User
*Add telegram chatid in the Pager address
*Add telegram chatid in the Pager address

Version vom 27. Mai 2020, 09:39 Uhr

Telegram Notifications Plugin

  • cat /omd/sites/<site-name>/local/share/check_mk/notifications/telegram
# Telegram

# Copyright Mathias Kettner  2013
#           Stefan Gehn      2016

# 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-
# ails.  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.

# Telegram notification based on asciimail notification from
# check_mk 1.2.6p16.

import os, re, sys, urllib, urllib2

telegram_bot_token = '<bot-api-token>'
telegram_chatid = str(os.getenv('NOTIFY_CONTACTPAGER'))

tmpl_host_text = """*Check_MK: $HOSTNAME$ - $EVENT_TXT$*
Host:     $HOSTNAME$
Alias:    $HOSTALIAS$
Event:    $EVENT_TXT$
Output:   $HOSTOUTPUT$


tmpl_service_text = """*Check_MK: $HOSTNAME$/$SERVICEDESC$ $EVENT_TXT$*
Host:     $HOSTNAME$
Alias:    $HOSTALIAS$
Event:    $EVENT_TXT$


def substitute_context(template, context):
    # First replace all known variables
    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 construct_message_text(context):
    notification_type = context["NOTIFICATIONTYPE"]
    if notification_type in [ "PROBLEM", "RECOVERY" ]:
    elif notification_type.startswith("FLAP"):
        if "START" in notification_type:
            txt_info = "Started Flapping"
            txt_info = "Stopped Flapping ($@SHORTSTATE$)"
    elif notification_type.startswith("DOWNTIME"):
        what = notification_type[8:].title()
        txt_info = "Downtime " + what + " ($@SHORTSTATE$)"
    elif notification_type == "ACKNOWLEDGEMENT":
        txt_info = "Acknowledged ($@SHORTSTATE$)"
    elif notification_type == "CUSTOM":
        txt_info = "Custom Notification ($@SHORTSTATE$)"
        txt_info = notification_type # Should neven happen

    txt_info = substitute_context(txt_info.replace("@", context["WHAT"]), context)

    context["EVENT_TXT"] = txt_info

    if context['WHAT'] == 'HOST':
        tmpl_text = tmpl_host_text
        tmpl_text = tmpl_service_text

    return substitute_context(tmpl_text, context)

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

def send_telegram_message(token, chat_id, text):
    url = '' % (token)
    data = urllib.urlencode({'chat_id':chat_id, 'text':text, 'parse_mode':'Markdown'})
    #print("sending telegram message, url '%s', chat id '%s', text '%s'" % (url, chat_id, text))
        urllib2.urlopen(url, data).read()
    except urllib2.URLError, e:
        sys.stdout.write('Cannot send Telegram message: HTTP-Error %s %s\n' % (e.code, e))

def main():
    context = fetch_notification_context()
    if not telegram_chatid: # e.g. empty field in user database
        sys.stdout.write("Cannot send Telegram message: Empty destination chat id")
    text = construct_message_text(context)
    send_telegram_message(telegram_bot_token, telegram_chatid, text)


CheckMK User config

  • Users -> edit desired User
  • Add telegram chatid in the Pager address

Cmk user telegram.png