diff --git a/commands.d/telegram-notify.conf b/commands.d/telegram-notify.conf new file mode 100644 index 0000000..84dadbb --- /dev/null +++ b/commands.d/telegram-notify.conf @@ -0,0 +1,68 @@ +object NotificationCommand "mail-service-notification" { + command = [ "/etc/icinga2/monitoring-tools/telegram-notify.py" ] + + arguments += { + "-4" = "$notification_address$" + "-6" = "$notification_address6$" + "-b" = "$notification_author$" + "-c" = "$notification_comment$" + "-d" = { + required = true + value = "$notification_date$" + } + "-e" = { + required = true + value = "$notification_servicename$" + } + "-f" = { + value = "$notification_from$" + description = "Set from address. Requires GNU mailutils (Debian/Ubuntu) or mailx (RHEL/SUSE)" + } + "-i" = "$notification_icingaweb2url$" + "-l" = { + required = true + value = "$notification_hostname$" + } + "-n" = { + required = true + value = "$notification_hostdisplayname$" + } + "-o" = { + required = true + value = "$notification_serviceoutput$" + } + "-r" = { + required = true + value = "$notification_useremail$" + } + "-s" = { + required = true + value = "$notification_servicestate$" + } + "-t" = { + required = true + value = "$notification_type$" + } + "-u" = { + required = true + value = "$notification_servicedisplayname$" + } + "-v" = "$notification_logtosyslog$" + } + + vars += { + notification_address = "$address$" + notification_address6 = "$address6$" + notification_author = "$notification.author$" + notification_comment = "$notification.comment$" + notification_type = "$notification.type$" + notification_date = "$icinga.long_date_time$" + notification_hostname = "$host.name$" + notification_hostdisplayname = "$host.display_name$" + notification_servicename = "$service.name$" + notification_serviceoutput = "$service.output$" + notification_servicestate = "$service.state$" + notification_useremail = "$user.email$" + notification_servicedisplayname = "$service.display_name$" + } +} diff --git a/telegram-notify.py b/telegram-notify.py new file mode 100755 index 0000000..fc9daed --- /dev/null +++ b/telegram-notify.py @@ -0,0 +1,39 @@ +#!/usr/bin/python3 +import sys +import argparse +import requests + +if __name__ == "__main__": + parser = argparse.ArgumentParser(description='Send Notifications via Telegram HTTP-Gateway', + formatter_class=argparse.ArgumentDefaultsHelpFormatter) + parser.add_argument('-4', required=False, help="Address (v4)") + parser.add_argument('-6', required=False, help="Address (v6)") + parser.add_argument('-b', required=False, help="Author") + parser.add_argument('-c', required=False, help="Comment") + parser.add_argument('-d', required=False, help="Date") + parser.add_argument('-e', required=False, help="Service Name") + parser.add_argument('-f', required=False, help="From") + parser.add_argument('-i', required=False, help="Icingaweb URL") + parser.add_argument('-l', required=False, help="Hostname") + parser.add_argument('-n', required=False, help="Hostdisplay Name") + parser.add_argument('-o', required=False, help="Service Output") + parser.add_argument('-r', required=False, help="User Email") + parser.add_argument('-s', required=False, help="Service State") + parser.add_argument('-t', required=False, help="Type") + parser.add_argument('-u', required=False, help="Service Display Name") + parser.add_argument('-v', required=False, help="Deprecated. Compability only. Has no Effect.") + parser.add_argument('--target-port', default=6000, help="Target port on which Telegram Gateway is running") + args = parser.parse_args() + + # build message # + serviceName = args.e + if args.u: + serviceName = args.u + message = "*{service} {state} on {host}*\n{output}\Icingaweb: {url}".format( + service=serviceName, state=args.s, host=args.l, output=args.o, url=args.i) + + # create and send request # + jsonPayload = { 'message' : message } + headers = { 'Content-Type' : 'application/json'} + url = "http://localhost:{port}/send-all".format(port=args.target_port) + requests.post(url, data=jsonPayload, headers=headers)