diff --git a/README.md b/README.md index 0e2402f..5417e03 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,134 @@ -# icinga2-notification-telegram -Send Icinga 2 notifications via Telegram +### Notification by Telegram +Send Icinga 2 notification via Telegram + +![Telegram Notification](img/Telegram.png) + +Example Notification Object +--------------------------- + +```ini +apply Notification "Host by Telegram" to Host { + command = "Notify Host By Telegram" + interval = 1h + assign where host.vars.notification_type == "Telegram" + users = [ "telegram_unixe" ] + vars.notification_logtosyslog = true + vars.telegram_bot = "" + vars.telegram_botid = "" + vars.telegram_bottoken = "" +} +``` + +Command Definitions +------------------- +```ini +object NotificationCommand "Notify Host By Telegram" { + import "plugin-notification-command" + command = [ "/etc/icinga2/scripts/host-by-telegram.sh" ] + arguments += { + "-4" = { + required = true + value = "$address$" + } + "-6" = "$address6$" + "-b" = "$notification.author$" + "-c" = "$notification.comment$" + "-d" = { + required = true + value = "$icinga.long_date_time$" + } + "-i" = "$icingaweb2url$" + "-l" = { + required = true + value = "$host.name$" + } + "-n" = "$host.display_name$" + "-o" = { + required = true + value = "$host.output$" + } + "-p" = { + required = true + value = "$telegram_bot$" + } + "-q" = { + required = true + value = "$telegram_botid$" + } + "-r" = { + required = true + value = "$telegram_bottoken$" + } + "-s" = { + required = true + value = "$host.state$" + } + "-t" = { + required = true + value = "$notification.type$" + } + "-v" = "$notification_logtosyslog$" + } +} +``` + +```ini +object NotificationCommand "Notify Service By Telegram" { + import "plugin-notification-command" + command = [ "/etc/icinga2/scripts/service-by-telegram.sh" ] + arguments += { + "-4" = { + required = true + value = "$address$" + } + "-6" = "$address6$" + "-b" = "$notification.author$" + "-c" = "$notification.comment$" + "-d" = { + required = true + value = "$icinga.long_date_time$" + } + "-e" = { + required = true + value = "$service.name$" + } + "-i" = "$icingaweb2url$" + "-l" = { + required = true + value = "$host.name$" + } + "-n" = "$host.display_name$" + "-o" = { + required = true + value = "$service.output$" + } + "-p" = { + required = true + value = "$telegram_bot$" + } + "-q" = { + required = true + value = "$telegram_botid$" + } + "-r" = { + required = true + value = "$telegram_bottoken$" + } + "-s" = { + required = true + value = "$service.state$" + } + "-t" = { + required = true + value = "$notification.type$" + } + "-u" = { + required = true + value = "$service.display_name$" + } + "-v" = "$notification_logtosyslog$" + } +} +``` + +![Icinga Director Config](img/Telegram_Notification_in_Icinga_Director.jpg) diff --git a/host-by-telegram.sh b/host-by-telegram.sh new file mode 100755 index 0000000..db46b25 --- /dev/null +++ b/host-by-telegram.sh @@ -0,0 +1,117 @@ +#!/usr/bin/env bash +## /etc/icinga2/scripts/host-by-telegram.sh / 20170330 +## Marianne M. Spiller +## Last updated 20170424 +## Tested icinga2-2.6.3-1 + +PROG="`basename $0`" +HOSTNAME="`hostname`" +TRANSPORT="curl" + +if [ -z "`which $TRANSPORT`" ] ; then + echo "$TRANSPORT not in \$PATH. Consider installing it." + exit 1 +fi + +Usage() { +cat << EOF + +host-by-mail notification script for Icinga 2 by spillerm + +The following are mandatory: + -4 HOSTADDRESS (\$address$) + -6 HOSTADDRESS6 (\$address6\) + -d LONGDATETIME (\$icinga.long_date_time$) + -l HOSTALIAS (\$host.name$) + -n HOSTDISPLAYNAME (\$host.display_name$) + -o HOSTOUTPUT (\$host.output$) + -p TELEGRAM_BOT (\$telegram_bot$) + -q TELEGRAM_BOTID (\$telegram_botid$) + -r TELEGRAM_BOTTOKEN (\$telegram_bottoken$) + -s HOSTSTATE (\$host.state$) + -t NOTIFICATIONTYPE (\$notification.type$) + +And these are optional: + -b NOTIFICATIONAUTHORNAME (\$notification.author$) + -c NOTIFICATIONCOMMENT (\$notification.comment$) + -i HAS_ICINGAWEB2 (\$icingaweb2url$, Default: unset) + -v (\$notification_logtosyslog$, Default: false) + +EOF + +exit 1; +} + +while getopts 4:6::b:c:d:hi:l:n:o:p:q:r:s:t:u:v: opt +do + case "$opt" in + 4) HOSTADDRESS=$OPTARG ;; + 6) HOSTADDRESS6=$OPTARG ;; + b) NOTIFICATIONAUTHORNAME=$OPTARG ;; + c) NOTIFICATIONCOMMENT=$OPTARG ;; + d) LONGDATETIME=$OPTARG ;; + h) Usage ;; + i) HAS_ICINGAWEB2=$OPTARG ;; + l) HOSTALIAS=$OPTARG ;; + n) HOSTDISPLAYNAME=$OPTARG ;; + o) HOSTOUTPUT=$OPTARG ;; + p) TELEGRAM_BOT=$OPTARG ;; + q) TELEGRAM_BOTID=$OPTARG ;; + r) TELEGRAM_BOTTOKEN=$OPTARG ;; + s) HOSTSTATE=$OPTARG ;; + t) NOTIFICATIONTYPE=$OPTARG ;; + v) VERBOSE=$OPTARG ;; + \?) echo "ERROR: Invalid option -$OPTARG" >&2 + Usage ;; + :) echo "Missing option argument for -$OPTARG" >&2 + Usage ;; + *) echo "Unimplemented option: -$OPTARG" >&2 + Usage ;; + esac +done + +## Build the message's subject +SUBJECT="[$NOTIFICATIONTYPE] $SERVICEDISPLAYNAME on $HOSTDISPLAYNAME is $SERVICESTATE!" + +## Build the message itself +NOTIFICATION_MESSAGE=$(cat << EOF +$HOSTDISPLAYNAME ($HOSTALIAS) is $HOSTSTATE! +When? $LONGDATETIME +Info? $HOSTOUTPUT +Host? $HOSTALIAS +IPv4? $HOSTADDRESS +EOF +) + +## Is this host IPv6 capable? +if [ -n "$HOSTADDRESS6" ] ; then + NOTIFICATION_MESSAGE="$NOTIFICATION_MESSAGE +IPv6? $HOSTADDRESS6" +fi +## Are there any comments? Put them into the message! +if [ -n "$NOTIFICATIONCOMMENT" ] ; then + NOTIFICATION_MESSAGE="$NOTIFICATION_MESSAGE +Comment by $NOTIFICATIONAUTHORNAME: + $NOTIFICATIONCOMMENT" +fi +## Are we using Icinga Web 2? Put the URL into the message! +if [ -n "$HAS_ICINGAWEB2" ] ; then + NOTIFICATION_MESSAGE="$NOTIFICATION_MESSAGE +Get live status: + $HAS_ICINGAWEB2/monitoring/host/show?host=$HOSTALIAS" +fi +## Build the message's subject +SUBJECT="[$NOTIFICATIONTYPE] Host $HOSTDISPLAYNAME is $HOSTSTATE!" + +## Are we verbose? Then put a message to syslog... +if [ "$VERBOSE" == "true" ] ; then + logger "$PROG sends $SUBJECT => Telegram Channel $TELEGRAM_BOT" +fi + +## And finally, send the message +/usr/bin/curl --silent --output /dev/null \ +--data-urlencode "chat_id=${TELEGRAM_BOTID}" \ +--data-urlencode "text=${NOTIFICATION_MESSAGE}" \ +--data-urlencode "parse_mode=HTML" \ +--data-urlencode "disable_web_page_preview=true" \ +"https://api.telegram.org/bot${TELEGRAM_BOTTOKEN}/sendMessage" diff --git a/img/Telegram.png b/img/Telegram.png new file mode 100644 index 0000000..28aac92 Binary files /dev/null and b/img/Telegram.png differ diff --git a/img/Telegram_Notification_in_Icinga_Director.jpg b/img/Telegram_Notification_in_Icinga_Director.jpg new file mode 100644 index 0000000..ed6918e Binary files /dev/null and b/img/Telegram_Notification_in_Icinga_Director.jpg differ diff --git a/service-by-telegram.sh b/service-by-telegram.sh new file mode 100755 index 0000000..ebdcdae --- /dev/null +++ b/service-by-telegram.sh @@ -0,0 +1,119 @@ +#!/usr/bin/env bash +## /etc/icinga2/scripts/service-by-telegram.sh / 20170330 +## Marianne M. Spiller +## Last updated 20170424 +## Tested icinga2-2.6.3-1 + +PROG="`basename $0`" +HOSTNAME="`hostname`" +TRANSPORT="curl" + +if [ -z "`which $TRANSPORT`" ] ; then + echo "$TRANSPORT not in \$PATH. Consider installing it." + exit 1 +fi + +Usage() { +cat << EOF + +service-by-telegram notification script for Icinga 2 by spillerm + +The following are mandatory: + -4 HOSTADDRESS (\$address$) + -6 HOSTADDRESS6 (\$address6$) + -d LONGDATETIME (\$icinga.long_date_time$) + -e SERVICENAME (\$service.name$) + -l HOSTALIAS (\$host.name$) + -n HOSTDISPLAYNAME (\$host.display_name$) + -o SERVICEOUTPUT (\$service.output$) + -p TELEGRAM_BOT (\$telegram_bot$) + -q TELEGRAM_BOTID (\$telegram_botid$) + -r TELEGRAM_BOTTOKEN (\$telegram_bottoken$) + -s SERVICESTATE (\$service.state$) + -t NOTIFICATIONTYPE (\$notification.type$) + -u SERVICEDISPLAYNAME (\$service.display_name$) + +And these are optional: + -b NOTIFICATIONAUTHORNAME (\$notification.author$) + -c NOTIFICATIONCOMMENT (\$notification.comment$) + -i HAS_ICINGAWEB2 (\$icingaweb2url$, Default: unset) + -v (\$notification_logtosyslog$, Default: false) + +EOF +exit 1; +} + +while getopts 4:6:b:c:d:e:f:hi:l:n:o:p:q:r:s:t:u:v: opt +do + case "$opt" in + 4) HOSTADDRESS=$OPTARG ;; + 6) HOSTADDRESS6=$OPTARG ;; + b) NOTIFICATIONAUTHORNAME=$OPTARG ;; + c) NOTIFICATIONCOMMENT=$OPTARG ;; + d) LONGDATETIME=$OPTARG ;; + e) SERVICENAME=$OPTARG ;; + h) Usage ;; + i) HAS_ICINGAWEB2=$OPTARG ;; + l) HOSTALIAS=$OPTARG ;; + n) HOSTDISPLAYNAME=$OPTARG ;; + o) SERVICEOUTPUT=$OPTARG ;; + p) TELEGRAM_BOT=$OPTARG ;; + q) TELEGRAM_BOTID=$OPTARG ;; + r) TELEGRAM_BOTTOKEN=$OPTARG ;; + s) SERVICESTATE=$OPTARG ;; + t) NOTIFICATIONTYPE=$OPTARG ;; + u) SERVICEDISPLAYNAME=$OPTARG ;; + v) VERBOSE=$OPTARG ;; + \?) echo "ERROR: Invalid option -$OPTARG" >&2 + Usage ;; + :) echo "Missing option argument for -$OPTARG" >&2 + Usage ;; + *) echo "Unimplemented option: -$OPTARG" >&2 + Usage ;; + esac +done + +## Build the message's subject +SUBJECT="[$NOTIFICATIONTYPE] $SERVICEDISPLAYNAME on $HOSTDISPLAYNAME is $SERVICESTATE!" + +## Build the message itself +NOTIFICATION_MESSAGE=$(cat << EOF +[$SERVICESTATE] $SERVICEDISPLAYNAME is $SERVICESTATE since $LONGDATETIME +Host: $HOSTALIAS (IPv4 $HOSTADDRESS) +More info: $SERVICEOUTPUT +EOF +) + +## Is this host IPv6 capable? +if [ -n "$HOSTADDRESS6" ] ; then + NOTIFICATION_MESSAGE="$NOTIFICATION_MESSAGE +IPv6? $HOSTADDRESS6" +fi + +## Are there any comments? Put them into the message! +if [ -n "$NOTIFICATIONCOMMENT" ] ; then + NOTIFICATION_MESSAGE="$NOTIFICATION_MESSAGE + +Comment by $NOTIFICATIONAUTHORNAME: + $NOTIFICATIONCOMMENT" +fi + +## Are we using Icinga Web 2? Put the URL into the message! +if [ -n "$HAS_ICINGAWEB2" ] ; then + NOTIFICATION_MESSAGE="$NOTIFICATION_MESSAGE +Get live status: + $HAS_ICINGAWEB2/monitoring/host/show?host=$HOSTALIAS" +fi + +## Are we verbose? Then put a message to syslog... +if [ "$VERBOSE" == "true" ] ; then + logger "$PROG sends $SUBJECT => Telegram Channel $TELEGRAM_BOT" +fi + +## And finally, send the message +/usr/bin/curl --silent --output /dev/null \ +--data-urlencode "chat_id=${TELEGRAM_BOTID}" \ +--data-urlencode "text=${NOTIFICATION_MESSAGE}" \ +--data-urlencode "parse_mode=HTML" \ +--data-urlencode "disable_web_page_preview=true" \ +"https://api.telegram.org/bot${TELEGRAM_BOTTOKEN}/sendMessage"