forked from filipnet/checkmk-telegram-notify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_mk_telegram-notify.sh
executable file
·82 lines (76 loc) · 3.17 KB
/
check_mk_telegram-notify.sh
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
#!/bin/bash
# Push Notification (using Telegram)
#
# Script Name : check_mk_telegram-notify.sh
# Description : Send Check_MK notifications by Telegram
# Author : https://github.com/filipnet/checkmk-telegram-notify
# License : BSD 3-Clause "New" or "Revised" License
# ======================================================================================
# Telegram API Token
# Find telegram bot named "@botfarther", type /mybots, select your bot and select "API Token" to see your current token
if [ -z ${NOTIFY_PARAMETER_1} ]; then
echo "No Telegram token ID provided. Exiting" >&2
exit 2
else
TOKEN="${NOTIFY_PARAMETER_1}"
fi
# Telegram Chat-ID or Group-ID
# Open "https://api.telegram.org/bot<YOUR_TOKEN>/getUpdates" inside your Browser and send a HELLO to your bot, refresh side
if [ -z ${NOTIFY_PARAMETER_2} ]; then
echo "No Telegram Chat-ID or Group-ID provided. Exiting" >&2
exit 2
else
CHAT_ID="${NOTIFY_PARAMETER_2}"
fi
# Privacy settings to anonymize/masking IP addresses
if [ ${NOTIFY_PARAMETER_3} = "privacy" ]; then
# IPv4 IP addresses
if [ ${NOTIFY_HOST_ADDRESS_4} ]; then
slice="${NOTIFY_HOST_ADDRESS_4}"
count=1
while [ "$count" -le 4 ]
do
declare sec"$count"="${slice%%.*}"
slice="${slice#*.}"
count=$((count+1))
done
# Adjust the output to your privacy needs here (Details in the readme.md)
NOTIFY_HOST_ADDRESS_4="${sec1}.${sec2}.2.${sec4}"
fi
# IPv6 IP addresses
if [ ${NOTIFY_HOST_ADDRESS_6} ]; then
slice="${NOTIFY_HOST_ADDRESS_6}"
count=1
while [ "$count" -le 8 ]
do
declare sec"$count"="${slice%%:*}"
slice="${slice#*:}"
count=$((count+1))
done
# Adjust the output to your privacy needs here (Details in the readme.md)
NOTIFY_HOST_ADDRESS_6="${sec1}:${sec2}:${sec3}:${sec4}:ffff:ffff:ffff:${sec8}"
fi
else
echo "Invalid privacy parameter, check your Check_MK settings." >&2
fi
# Create a MESSAGE variable to send to your Telegram bot
MESSAGE="${NOTIFY_HOSTNAME} (${NOTIFY_HOSTALIAS})%0A"
MESSAGE+="${NOTIFY_WHAT} ${NOTIFY_NOTIFICATIONTYPE}%0A%0A"
if [[ ${NOTIFY_WHAT} == "SERVICE" ]]; then
MESSAGE+="${NOTIFY_SERVICEDESC}%0A"
MESSAGE+="State changed from ${NOTIFY_PREVIOUSSERVICEHARDSHORTSTATE} to ${NOTIFY_SERVICESHORTSTATE}%0A"
MESSAGE+="${NOTIFY_SERVICEOUTPUT}%0A"
else
MESSAGE+="State changed from ${NOTIFY_PREVIOUSHOSTHARDSHORTSTATE} to ${NOTIFY_HOSTSHORTSTATE}%0A"
MESSAGE+="${NOTIFY_HOSTOUTPUT}%0A"
fi
MESSAGE+="%0AIPv4: ${NOTIFY_HOST_ADDRESS_4} %0AIPv6: ${NOTIFY_HOST_ADDRESS_6}%0A"
MESSAGE+="${NOTIFY_SHORTDATETIME}"
# Send message to Telegram bot
curl -S -X POST "https://api.telegram.org/bot${TOKEN}/sendMessage" -d chat_id="${CHAT_ID}" -d text="${MESSAGE}"
if [ $? -ne 0 ]; then
echo "Not able to send Telegram message" >&2
exit 2
else
exit 0
fi