Merge 1723992b30 into 41f83bdb06
				
					
				
			This commit is contained in:
		
						commit
						882ac69902
					
				
					 6 changed files with 194 additions and 275 deletions
				
			
		
							
								
								
									
										12
									
								
								README.md
									
									
									
									
									
								
							
							
						
						
									
										12
									
								
								README.md
									
									
									
									
									
								
							| 
						 | 
					@ -3,7 +3,6 @@ Sending Icinga 2 notifications via Telegram!
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Special thanks to contributing [steadfasterX](https://github.com/steadfasterX) and [Tom](https://github.com/Thomas-Gelf) :-)
 | 
					Special thanks to contributing [steadfasterX](https://github.com/steadfasterX) and [Tom](https://github.com/Thomas-Gelf) :-)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					 | 
				
			||||||
## Preparing
 | 
					## Preparing
 | 
				
			||||||
### Create your bot
 | 
					### Create your bot
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -44,7 +43,8 @@ If you do not want to notify a group i.e. just **a direct user notification**:
 | 
				
			||||||
Sending a test notification – replace the token, botname and chat id obviously.
 | 
					Sending a test notification – replace the token, botname and chat id obviously.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
```
 | 
					```
 | 
				
			||||||
sudo -u nagios ./service-by-telegram.sh -4 127.0.0.1 \
 | 
					sudo -u nagios ./alert-by-telegram.sh -4 127.0.0.1 \
 | 
				
			||||||
 | 
					-a service \
 | 
				
			||||||
-l myhostname \
 | 
					-l myhostname \
 | 
				
			||||||
-o testingTGnotifiy \
 | 
					-o testingTGnotifiy \
 | 
				
			||||||
-p <myBotname> \
 | 
					-p <myBotname> \
 | 
				
			||||||
| 
						 | 
					@ -130,13 +130,14 @@ object User "telegram_bot" {
 | 
				
			||||||
```ini
 | 
					```ini
 | 
				
			||||||
object NotificationCommand "Notify Host By Telegram" {
 | 
					object NotificationCommand "Notify Host By Telegram" {
 | 
				
			||||||
    import "plugin-notification-command"
 | 
					    import "plugin-notification-command"
 | 
				
			||||||
    command = [ "/etc/icinga2/scripts/host-by-telegram.sh" ]
 | 
					    command = [ "/etc/icinga2/scripts/alert-by-telegram.sh" ]
 | 
				
			||||||
    arguments += {
 | 
					    arguments += {
 | 
				
			||||||
        "-4" = {
 | 
					        "-4" = {
 | 
				
			||||||
            required = true
 | 
					            required = true
 | 
				
			||||||
            value = "$address$"
 | 
					            value = "$address$"
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        "-6" = "$address6$"
 | 
					        "-6" = "$address6$"
 | 
				
			||||||
 | 
					        "-a" = "host"
 | 
				
			||||||
        "-b" = "$notification.author$"
 | 
					        "-b" = "$notification.author$"
 | 
				
			||||||
        "-c" = "$notification.comment$"
 | 
					        "-c" = "$notification.comment$"
 | 
				
			||||||
        "-d" = {
 | 
					        "-d" = {
 | 
				
			||||||
| 
						 | 
					@ -185,13 +186,14 @@ object NotificationCommand "Notify Host By Telegram" {
 | 
				
			||||||
```ini
 | 
					```ini
 | 
				
			||||||
object NotificationCommand "Notify Service By Telegram" {
 | 
					object NotificationCommand "Notify Service By Telegram" {
 | 
				
			||||||
    import "plugin-notification-command"
 | 
					    import "plugin-notification-command"
 | 
				
			||||||
    command = [ "/etc/icinga2/scripts/service-by-telegram.sh" ]
 | 
					    command = [ "/etc/icinga2/scripts/alert-by-telegram.sh" ]
 | 
				
			||||||
    arguments += {
 | 
					    arguments += {
 | 
				
			||||||
        "-4" = {
 | 
					        "-4" = {
 | 
				
			||||||
            required = true
 | 
					            required = true
 | 
				
			||||||
            value = "$address$"
 | 
					            value = "$address$"
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        "-6" = "$address6$"
 | 
					        "-6" = "$address6$"
 | 
				
			||||||
 | 
					        "-a" = "service"
 | 
				
			||||||
        "-b" = "$notification.author$"
 | 
					        "-b" = "$notification.author$"
 | 
				
			||||||
        "-c" = "$notification.comment$"
 | 
					        "-c" = "$notification.comment$"
 | 
				
			||||||
        "-d" = {
 | 
					        "-d" = {
 | 
				
			||||||
| 
						 | 
					@ -283,7 +285,6 @@ template Notification "Template: Service Notifications via Telegram" {
 | 
				
			||||||
```
 | 
					```
 | 
				
			||||||
</details>
 | 
					</details>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					 | 
				
			||||||
#### Example notification apply rules
 | 
					#### Example notification apply rules
 | 
				
			||||||
<details>
 | 
					<details>
 | 
				
			||||||
   <summary>Apply rule for host notifications</summary>
 | 
					   <summary>Apply rule for host notifications</summary>
 | 
				
			||||||
| 
						 | 
					@ -315,7 +316,6 @@ apply Notification "Service Alerts via @telegram_bot" to Service {
 | 
				
			||||||
```
 | 
					```
 | 
				
			||||||
</details>
 | 
					</details>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					 | 
				
			||||||
#### Example Director screenshot
 | 
					#### Example Director screenshot
 | 
				
			||||||
<details>
 | 
					<details>
 | 
				
			||||||
   <summary>Notifications by Telegram via Icinga Director</summary>
 | 
					   <summary>Notifications by Telegram via Icinga Director</summary>
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										169
									
								
								alert-by-telegram.sh
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										169
									
								
								alert-by-telegram.sh
									
									
									
									
									
										Executable file
									
								
							| 
						 | 
					@ -0,0 +1,169 @@
 | 
				
			||||||
 | 
					#!/bin/bash
 | 
				
			||||||
 | 
					set -euo pipefail
 | 
				
			||||||
 | 
					# /etc/icinga2/scripts/service-by-telegram.sh
 | 
				
			||||||
 | 
					# Marianne M. Spiller <github@spiller.me>
 | 
				
			||||||
 | 
					# Last updated 2020-06-11
 | 
				
			||||||
 | 
					# Last tests used icinga2-2.11.2-1.buster
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					PROG="$(basename $0)"
 | 
				
			||||||
 | 
					HOSTNAME="$(hostname)"
 | 
				
			||||||
 | 
					TRANSPORT="curl"
 | 
				
			||||||
 | 
					unset DEBUG
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					if [[ -z "$(command -v $TRANSPORT)" ]]; then
 | 
				
			||||||
 | 
						echo "$TRANSPORT not in \$PATH. Consider installing it."
 | 
				
			||||||
 | 
						exit 1
 | 
				
			||||||
 | 
					fi
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Usage() {
 | 
				
			||||||
 | 
					cat << EOF
 | 
				
			||||||
 | 
					alert-by-telegram notification script for Icinga 2 by spillerm <github@spiller.me>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					The following are mandatory:
 | 
				
			||||||
 | 
					  -a ALERTTYPE (host or service)
 | 
				
			||||||
 | 
					  -d LONGDATETIME (\$icinga.long_date_time$)
 | 
				
			||||||
 | 
					  -e SERVICENAME (\$service.name$ Only if ALERTTYPE is service) # TODO, currently unused
 | 
				
			||||||
 | 
					  -l HOSTALIAS (\$host.name$)
 | 
				
			||||||
 | 
					  -n HOSTDISPLAYNAME (\$host.display_name$)
 | 
				
			||||||
 | 
					  -o SERVICEOUTPUT (\$service.output$ or \$host.output$)
 | 
				
			||||||
 | 
					  -q TELEGRAM_CHATID (\$telegram_chatid$)
 | 
				
			||||||
 | 
					  -r TELEGRAM_BOTTOKEN (\$telegram_bottoken$)
 | 
				
			||||||
 | 
					  -s SERVICESTATE (\$service.state$ or \$host.state$)
 | 
				
			||||||
 | 
					  -t NOTIFICATIONTYPE (\$notification.type$)
 | 
				
			||||||
 | 
					  -u SERVICEDISPLAYNAME (\$service.display_name$)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					And these are optional:
 | 
				
			||||||
 | 
					  -4 HOSTADDRESS (\$address$)
 | 
				
			||||||
 | 
					  -6 HOSTADDRESS6 (\$address6$)
 | 
				
			||||||
 | 
					  -b NOTIFICATIONAUTHORNAME (\$notification.author$)
 | 
				
			||||||
 | 
					  -c NOTIFICATIONCOMMENT (\$notification.comment$)
 | 
				
			||||||
 | 
					  -i HAS_ICINGAWEB2 (\$icingaweb2url$, Default: unset)
 | 
				
			||||||
 | 
					  -v (\$notification_logtosyslog$, Default: false)
 | 
				
			||||||
 | 
					  -p TELEGRAM_BOT (\$telegram_bot$)
 | 
				
			||||||
 | 
					  -D DEBUG enable debug output - meant for CLI debug only
 | 
				
			||||||
 | 
					EOF
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					while getopts 4:6:a:b:c:d:e:f:hi:l:n:o:p:q:r:s:t:u:v:D opt; do
 | 
				
			||||||
 | 
						case "$opt" in
 | 
				
			||||||
 | 
							4) HOSTADDRESS=$OPTARG ;;
 | 
				
			||||||
 | 
							6) HOSTADDRESS6=$OPTARG ;;
 | 
				
			||||||
 | 
							a) ALERTTYPE=$OPTARG ;;
 | 
				
			||||||
 | 
							b) NOTIFICATIONAUTHORNAME=$OPTARG ;;
 | 
				
			||||||
 | 
							c) NOTIFICATIONCOMMENT=$OPTARG ;;
 | 
				
			||||||
 | 
							d) LONGDATETIME=$OPTARG ;;
 | 
				
			||||||
 | 
							e) SERVICENAME=$OPTARG ;;
 | 
				
			||||||
 | 
							h) Usage; exit 0;;
 | 
				
			||||||
 | 
							i) HAS_ICINGAWEB2=$OPTARG ;;
 | 
				
			||||||
 | 
							l) HOSTALIAS=$OPTARG ;;
 | 
				
			||||||
 | 
							n) HOSTDISPLAYNAME=$OPTARG ;;
 | 
				
			||||||
 | 
							o) SERVICEOUTPUT=$OPTARG ;;
 | 
				
			||||||
 | 
							p) TELEGRAM_BOT=$OPTARG ;;
 | 
				
			||||||
 | 
							q) TELEGRAM_CHATID=$OPTARG ;;
 | 
				
			||||||
 | 
							r) TELEGRAM_BOTTOKEN=$OPTARG ;;
 | 
				
			||||||
 | 
							s) SERVICESTATE=$OPTARG ;;
 | 
				
			||||||
 | 
							t) NOTIFICATIONTYPE=$OPTARG ;;
 | 
				
			||||||
 | 
							u) SERVICEDISPLAYNAME=$OPTARG ;;
 | 
				
			||||||
 | 
							v) VERBOSE=$OPTARG ;;
 | 
				
			||||||
 | 
							D) DEBUG=1; echo -e "\n**********************************************\nWARNING: DEBUG MODE, DEACTIVATE ASAP\n**********************************************\n" ;;
 | 
				
			||||||
 | 
							\?) echo "ERROR: Invalid option -$OPTARG" >&2
 | 
				
			||||||
 | 
								Usage; exit 1;;
 | 
				
			||||||
 | 
							:) echo "Missing option argument for -$OPTARG" >&2
 | 
				
			||||||
 | 
								Usage; exit 1;;
 | 
				
			||||||
 | 
							*) echo "Unimplemented option: -$OPTARG" >&2
 | 
				
			||||||
 | 
								Usage; exit 1;;
 | 
				
			||||||
 | 
						esac
 | 
				
			||||||
 | 
					done
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					if [[ ${ALERTTYPE-} != "host" ]] && [[ ${ALERTTYPE-} != "service" ]]; then
 | 
				
			||||||
 | 
						Usage
 | 
				
			||||||
 | 
						echo ""
 | 
				
			||||||
 | 
						echo "ALERTTYPE needs to be either 'host' or 'service'!"
 | 
				
			||||||
 | 
						exit 1
 | 
				
			||||||
 | 
					fi
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					if [[ $ALERTTYPE == "host" ]]; then
 | 
				
			||||||
 | 
						echo ""
 | 
				
			||||||
 | 
					else
 | 
				
			||||||
 | 
						if [[ -z ${SERVICENAME-} ]] || [[ -z ${SERVICEDISPLAYNAME-} ]]; then
 | 
				
			||||||
 | 
							Usage
 | 
				
			||||||
 | 
							exit 1
 | 
				
			||||||
 | 
						fi
 | 
				
			||||||
 | 
					fi
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					if [[ -z ${LONGDATETIME-} ]]      || [[ -z ${HOSTALIAS-} ]]       || [[ -z ${HOSTDISPLAYNAME-} ]] \
 | 
				
			||||||
 | 
						|| [[ -z ${SERVICEOUTPUT-} ]]   || [[ -z ${TELEGRAM_CHATID-} ]] || [[ -z ${TELEGRAM_BOTTOKEN-} ]] \
 | 
				
			||||||
 | 
						|| [[ -z ${SERVICESTATE-} ]]    || [[ -z ${NOTIFICATIONTYPE-} ]]; then
 | 
				
			||||||
 | 
						Usage
 | 
				
			||||||
 | 
						exit 1
 | 
				
			||||||
 | 
					fi
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					## Build the message's subject
 | 
				
			||||||
 | 
					if [[ $ALERTTYPE == "host" ]]; then
 | 
				
			||||||
 | 
						SUBJECT="[$NOTIFICATIONTYPE] Host $HOSTDISPLAYNAME is $SERVICESTATE!"
 | 
				
			||||||
 | 
					else
 | 
				
			||||||
 | 
						SUBJECT="[$NOTIFICATIONTYPE] $SERVICEDISPLAYNAME on $HOSTDISPLAYNAME is $SERVICESTATE!"
 | 
				
			||||||
 | 
					fi
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					## Build the message itself
 | 
				
			||||||
 | 
					if [[ $ALERTTYPE == "host" ]]; then
 | 
				
			||||||
 | 
						NOTIFICATION_MESSAGE=$(cat << EOF
 | 
				
			||||||
 | 
					<u>[$SERVICESTATE] $HOSTDISPLAYNAME ($HOSTALIAS) - at $LONGDATETIME</u>
 | 
				
			||||||
 | 
					EOF
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					else
 | 
				
			||||||
 | 
						NOTIFICATION_MESSAGE=$(cat << EOF
 | 
				
			||||||
 | 
					<u>[$SERVICESTATE] $SERVICEDISPLAYNAME - at $LONGDATETIME</u>
 | 
				
			||||||
 | 
					<b>Host:</b> <code>$HOSTALIAS</code>
 | 
				
			||||||
 | 
					EOF
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					fi
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					if [[ -n "${HOSTADDRESS-}" ]]; then
 | 
				
			||||||
 | 
						NOTIFICATION_MESSAGE="$NOTIFICATION_MESSAGE
 | 
				
			||||||
 | 
					<b>IPv4:</b> <code>$HOSTADDRESS</code>"
 | 
				
			||||||
 | 
					fi
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					if [[ -n "${HOSTADDRESS6-}" ]]; then
 | 
				
			||||||
 | 
						NOTIFICATION_MESSAGE="$NOTIFICATION_MESSAGE
 | 
				
			||||||
 | 
					<b>IPv6:</b> <code>$HOSTADDRESS6</code>"
 | 
				
			||||||
 | 
					fi
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					NOTIFICATION_MESSAGE="$NOTIFICATION_MESSAGE
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					<b>Output:</b> <code>$SERVICEOUTPUT</code>"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					## Are there any comments? Put them into the message!
 | 
				
			||||||
 | 
					if [[ -n "${NOTIFICATIONCOMMENT-}" ]] ; then
 | 
				
			||||||
 | 
						NOTIFICATION_MESSAGE="$NOTIFICATION_MESSAGE
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					<b>Comment by $NOTIFICATIONAUTHORNAME:</b> <code>$NOTIFICATIONCOMMENT</code>"
 | 
				
			||||||
 | 
					fi
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					## Are we using Icinga Web 2? Put the URL into the message!
 | 
				
			||||||
 | 
					if [[ -n "${HAS_ICINGAWEB2-}" ]] ; then
 | 
				
			||||||
 | 
						NOTIFICATION_MESSAGE="$NOTIFICATION_MESSAGE
 | 
				
			||||||
 | 
					<b>Get live status:</b> <code>$HAS_ICINGAWEB2/monitoring/host/show?host=$HOSTALIAS</code>"
 | 
				
			||||||
 | 
					fi
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					## Are we verbose? Then put a message to syslog...
 | 
				
			||||||
 | 
					if [[ "${VERBOSE-}" == "true" ]] ; then
 | 
				
			||||||
 | 
						logger "$PROG sends $SUBJECT => Telegram Channel $TELEGRAM_BOT"
 | 
				
			||||||
 | 
					fi
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					## debug output or not?
 | 
				
			||||||
 | 
					if [[ -z ${DEBUG-} ]]; then
 | 
				
			||||||
 | 
						CURLARGS="--silent --output /dev/null"
 | 
				
			||||||
 | 
					else
 | 
				
			||||||
 | 
						CURLARGS=-v
 | 
				
			||||||
 | 
						set -x
 | 
				
			||||||
 | 
						echo -e "DEBUG MODE!"
 | 
				
			||||||
 | 
					fi
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					## And finally, send the message
 | 
				
			||||||
 | 
					/usr/bin/curl $CURLARGS \
 | 
				
			||||||
 | 
						--data-urlencode "chat_id=${TELEGRAM_CHATID}" \
 | 
				
			||||||
 | 
						--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"
 | 
				
			||||||
| 
						 | 
					@ -9,6 +9,9 @@
 | 
				
			||||||
                    "required": true,
 | 
					                    "required": true,
 | 
				
			||||||
                    "value": "$address$"
 | 
					                    "value": "$address$"
 | 
				
			||||||
                },
 | 
					                },
 | 
				
			||||||
 | 
					                "-a": {
 | 
				
			||||||
 | 
					                    "value": "service"
 | 
				
			||||||
 | 
					                },
 | 
				
			||||||
                "-b": {
 | 
					                "-b": {
 | 
				
			||||||
                    "value": "$notification.author$"
 | 
					                    "value": "$notification.author$"
 | 
				
			||||||
                },
 | 
					                },
 | 
				
			||||||
| 
						 | 
					@ -86,6 +89,9 @@
 | 
				
			||||||
                    "required": true,
 | 
					                    "required": true,
 | 
				
			||||||
                    "value": "$address$"
 | 
					                    "value": "$address$"
 | 
				
			||||||
                },
 | 
					                },
 | 
				
			||||||
 | 
					                "-a": {
 | 
				
			||||||
 | 
					                    "value": "host"
 | 
				
			||||||
 | 
					                },
 | 
				
			||||||
                "-b": {
 | 
					                "-b": {
 | 
				
			||||||
                    "value": "$notification.author$"
 | 
					                    "value": "$notification.author$"
 | 
				
			||||||
                },
 | 
					                },
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -9,6 +9,9 @@
 | 
				
			||||||
                    "required": true,
 | 
					                    "required": true,
 | 
				
			||||||
                    "value": "$address$"
 | 
					                    "value": "$address$"
 | 
				
			||||||
                },
 | 
					                },
 | 
				
			||||||
 | 
					                "-a": {
 | 
				
			||||||
 | 
					                    "value": "service"
 | 
				
			||||||
 | 
					                },
 | 
				
			||||||
                "-b": {
 | 
					                "-b": {
 | 
				
			||||||
                    "value": "$notification.author$"
 | 
					                    "value": "$notification.author$"
 | 
				
			||||||
                },
 | 
					                },
 | 
				
			||||||
| 
						 | 
					@ -86,6 +89,9 @@
 | 
				
			||||||
                    "required": true,
 | 
					                    "required": true,
 | 
				
			||||||
                    "value": "$address$"
 | 
					                    "value": "$address$"
 | 
				
			||||||
                },
 | 
					                },
 | 
				
			||||||
 | 
					                "-a": {
 | 
				
			||||||
 | 
					                    "value": "host"
 | 
				
			||||||
 | 
					                },
 | 
				
			||||||
                "-b": {
 | 
					                "-b": {
 | 
				
			||||||
                    "value": "$notification.author$"
 | 
					                    "value": "$notification.author$"
 | 
				
			||||||
                },
 | 
					                },
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,130 +0,0 @@
 | 
				
			||||||
#!/usr/bin/env bash
 | 
					 | 
				
			||||||
## /etc/icinga2/scripts/host-by-telegram.sh / 20170330
 | 
					 | 
				
			||||||
## Marianne M. Spiller <github@spiller.me>
 | 
					 | 
				
			||||||
## Last updated 20190820
 | 
					 | 
				
			||||||
## Last tests used 2.11.2-1.buster
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
PROG="`basename $0`"
 | 
					 | 
				
			||||||
HOSTNAME="`hostname`"
 | 
					 | 
				
			||||||
TRANSPORT="curl"
 | 
					 | 
				
			||||||
unset DEBUG
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
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 <github@spiller.me>
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
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_CHATID (\$telegram_chatid$)
 | 
					 | 
				
			||||||
  -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)
 | 
					 | 
				
			||||||
  -D DEBUG enable debug output - meant for CLI debug only
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
EOF
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
exit 1;
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
while getopts 4:6::b:c:d:hi:l:n:o:p:q:r:s:t:u:v:D 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_CHATID=$OPTARG ;;
 | 
					 | 
				
			||||||
    r) TELEGRAM_BOTTOKEN=$OPTARG ;;
 | 
					 | 
				
			||||||
    s) HOSTSTATE=$OPTARG ;;
 | 
					 | 
				
			||||||
    t) NOTIFICATIONTYPE=$OPTARG ;;
 | 
					 | 
				
			||||||
    v) VERBOSE=$OPTARG ;;
 | 
					 | 
				
			||||||
    D) DEBUG=1; echo -e "\n**********************************************\nWARNING: DEBUG MODE, DEACTIVATE ASAP\n**********************************************\n" ;;
 | 
					 | 
				
			||||||
   \?) 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
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
## debug output or not
 | 
					 | 
				
			||||||
if [ -z $DEBUG ];then
 | 
					 | 
				
			||||||
    CURLARGS="--silent --output /dev/null"
 | 
					 | 
				
			||||||
else
 | 
					 | 
				
			||||||
    CURLARGS=-v
 | 
					 | 
				
			||||||
    set -x
 | 
					 | 
				
			||||||
    echo -e "DEBUG MODE!"
 | 
					 | 
				
			||||||
fi
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
## And finally, send the message
 | 
					 | 
				
			||||||
/usr/bin/curl $CURLARGS \
 | 
					 | 
				
			||||||
    --data-urlencode "chat_id=${TELEGRAM_CHATID}" \
 | 
					 | 
				
			||||||
    --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"
 | 
					 | 
				
			||||||
set +x
 | 
					 | 
				
			||||||
| 
						 | 
					@ -1,132 +0,0 @@
 | 
				
			||||||
#!/usr/bin/env bash
 | 
					 | 
				
			||||||
## /etc/icinga2/scripts/service-by-telegram.sh / 20170330
 | 
					 | 
				
			||||||
## Marianne M. Spiller <github@spiller.me>
 | 
					 | 
				
			||||||
## Last updated 20190820
 | 
					 | 
				
			||||||
## Last tests used icinga2-2.11.2-1.buster
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
PROG="`basename $0`"
 | 
					 | 
				
			||||||
HOSTNAME="`hostname`"
 | 
					 | 
				
			||||||
TRANSPORT="curl"
 | 
					 | 
				
			||||||
unset DEBUG
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
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 <github@spiller.me>
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
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_CHATID (\$telegram_chatid$)
 | 
					 | 
				
			||||||
  -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)
 | 
					 | 
				
			||||||
  -D DEBUG enable debug output - meant for CLI debug only
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
EOF
 | 
					 | 
				
			||||||
exit 1;
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
while getopts 4:6:b:c:d:e:f:hi:l:n:o:p:q:r:s:t:u:v:D 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_CHATID=$OPTARG ;;
 | 
					 | 
				
			||||||
    r) TELEGRAM_BOTTOKEN=$OPTARG ;;
 | 
					 | 
				
			||||||
    s) SERVICESTATE=$OPTARG ;;
 | 
					 | 
				
			||||||
    t) NOTIFICATIONTYPE=$OPTARG ;;
 | 
					 | 
				
			||||||
    u) SERVICEDISPLAYNAME=$OPTARG ;;
 | 
					 | 
				
			||||||
    v) VERBOSE=$OPTARG ;;
 | 
					 | 
				
			||||||
    D) DEBUG=1; echo -e "\n**********************************************\nWARNING: DEBUG MODE, DEACTIVATE ASAP\n**********************************************\n" ;;
 | 
					 | 
				
			||||||
   \?) 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
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
## debug output or not?
 | 
					 | 
				
			||||||
if [ -z $DEBUG ];then
 | 
					 | 
				
			||||||
    CURLARGS="--silent --output /dev/null"
 | 
					 | 
				
			||||||
else
 | 
					 | 
				
			||||||
    CURLARGS=-v
 | 
					 | 
				
			||||||
    set -x
 | 
					 | 
				
			||||||
    echo -e "DEBUG MODE!"
 | 
					 | 
				
			||||||
fi
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
## And finally, send the message
 | 
					 | 
				
			||||||
/usr/bin/curl $CURLARGS \
 | 
					 | 
				
			||||||
    --data-urlencode "chat_id=${TELEGRAM_CHATID}" \
 | 
					 | 
				
			||||||
    --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"
 | 
					 | 
				
			||||||
set +x
 | 
					 | 
				
			||||||
		Loading…
	
		Reference in a new issue