Skip to content

Commit e39f6b9

Browse files
committed
update
0 parents  commit e39f6b9

File tree

5 files changed

+147
-0
lines changed

5 files changed

+147
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
config.xml

LICENSE

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
BSD 3-Clause License
2+
3+
Copyright (c) 2020, Benedikt Filip
4+
All rights reserved.
5+
6+
Redistribution and use in source and binary forms, with or without
7+
modification, are permitted provided that the following conditions are met:
8+
9+
1. Redistributions of source code must retain the above copyright notice, this
10+
list of conditions and the following disclaimer.
11+
12+
2. Redistributions in binary form must reproduce the above copyright notice,
13+
this list of conditions and the following disclaimer in the documentation
14+
and/or other materials provided with the distribution.
15+
16+
3. Neither the name of the copyright holder nor the names of its
17+
contributors may be used to endorse or promote products derived from
18+
this software without specific prior written permission.
19+
20+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

README.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Check_MK Telegram notification
2+
Telegram has long been one of my real-time communication media. It is obvious to output monitoring messages for server and network components as alarm messages. There are several scripts for this on the internet, but most of them are written in Python, many of them have problems with Python3 and its libraries. Instead of spending hours and hours with Python, I decided to use a scripting language I know and write a Linux Bash script for it.
3+
4+
The following Script is for Check_MK, I have used it exclusively with the RAW version 1.6.0_p18.
5+
6+
## INSTALLATION
7+
Change to your Check_MK site user
8+
```
9+
su - mysite
10+
```
11+
12+
Change to the notification directory
13+
```
14+
cd ~/local/share/check_mk/notifications/
15+
```
16+
17+
Download the Telegram notify script from Git repository
18+
19+
```
20+
wget https://github.com/filipnet/telegram/blob/main/check_mk_telegram-notify.sh
21+
wget https://github.com/filipnet/telegram/blob/main/config.xml.sample
22+
```
23+
or
24+
```
25+
git clone https://github.com/filipnet/telegram.git
26+
cd telegram
27+
cp check_mk_telegram-notify.sh /opt/omd/sites/mysite/local/share/check_mk/notifications/
28+
cp config.xml.sample /opt/omd/sites/mysite/local/share/check_mk/notifications/
29+
```
30+
31+
Adjusting the config.xml
32+
```
33+
cd /opt/omd/sites/mysite/local/share/check_mk/notifications/
34+
cp config.xml.sample config.xml
35+
```
36+
37+
Inside your API Token and Chat/Group-ID
38+
```
39+
<telegram_api_token>TELEGRAM_API_TOKEN_WITHOUT_BOT_PREFIX</telegram_api_token>
40+
<telegram_chat_id>TELEGRAM_GROUP_OR_CHAT-ID</telegram_chat_id>
41+
```
42+
43+
Give the script execution permissions
44+
```
45+
chmod +x check_mk_telegram-notify.sh
46+
```
47+
48+
## CHECK_MK CONFIGURATION
49+
Now you can create your own alarm rules in Check_MK.
50+
51+
```WATO → Notifications → New Rule → Notification Method → Push Notification (using Telegram)```
52+
53+
No further settings are required for this.
54+
55+
## LICENSE
56+
telegram and all individual scripts are under the BSD 3-Clause license unless explicitly noted otherwise. Please refer to the LICENSE

check_mk_telegram-notify.sh

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/bin/bash
2+
# Push Notification (using Telegram)
3+
#
4+
# Script Name : check_mk_telegram-notify.sh
5+
# Description : Send Check_MK notifications by Telegram
6+
# Author : https://github.com/filipnet/telegram
7+
# License : BSD 3-Clause "New" or "Revised" License
8+
# ======================================================================================
9+
10+
CONFIGFILE="${BASH_SOURCE%/*}/config.xml"
11+
# Telegram API Token
12+
# Find telegram bot named "@botfarther", type /mybots, select your bot and select "API Token" to see your current token
13+
TOKEN=$(xmllint --xpath 'string(/config/telegram_api_token)' $CONFIGFILE)
14+
# Telegram Chat-ID or Group-ID
15+
# Open "https://api.telegram.org/bot<YOUR_TOKEN>/getUpdates" inside your Browser and send a HELLO to your bot, refresh side
16+
CHAT_ID=$(xmllint --xpath 'string(/config/telegram_chat_id)' $CONFIGFILE)
17+
18+
# Write Check_MK output to a temporary file, delete depricated macros and create variable OUTPUT
19+
env | grep NOTIFY_ | grep -v "This macro is deprecated" | sort > $OMD_ROOT/tmp/telegram.out
20+
OUTPUT=$OMD_ROOT/tmp/telegram.out
21+
22+
# Read OUTPUT variable and create some more variables for every text-part you want to use afterwards
23+
HOSTNAME=$(grep NOTIFY_HOSTNAME $OUTPUT | cut -d'=' -f2)
24+
HOSTALIAS=$(grep NOTIFY_HOSTALIAS $OUTPUT | cut -d'=' -f2)
25+
WHAT=$(grep NOTIFY_WHAT $OUTPUT | cut -d'=' -f2)
26+
NOTIFICATIONTYPE=$(grep NOTIFY_NOTIFICATIONTYPE $OUTPUT | cut -d'=' -f2)
27+
SERVICEDESC=$(grep NOTIFY_SERVICEDESC $OUTPUT | cut -d'=' -f2)
28+
SERVICEOUTPUT=$(grep NOTIFY_SERVICEOUTPUT $OUTPUT | cut -d'=' -f2)
29+
HOSTOUTPUT=$(grep NOTIFY_HOSTOUTPUT $OUTPUT | cut -d'=' -f2)
30+
PREVIOUSHOSTHARDSHORTSTATE=$(grep NOTIFY_PREVIOUSHOSTHARDSHORTSTATE $OUTPUT | cut -d'=' -f2)
31+
HOSTSHORTSTATE=$(grep NOTIFY_HOSTSHORTSTATE $OUTPUT | cut -d'=' -f2)
32+
PREVIOUSSERVICEHARDSHORTSTATE=$(grep NOTIFY_PREVIOUSSERVICEHARDSHORTSTATE $OUTPUT | cut -d'=' -f2)
33+
SERVICESHORTSTATE=$(grep NOTIFY_SERVICESHORTSTATE $OUTPUT | cut -d'=' -f2)
34+
SHORTDATETIME=$(grep NOTIFY_SHORTDATETIME $OUTPUT | cut -d'=' -f2)
35+
HOST_ADDRESS_4=$(grep NOTIFY_HOST_ADDRESS_4 $OUTPUT | cut -d'=' -f2)
36+
HOST_ADDRESS_6=$(grep NOTIFY_HOST_ADDRESS_6 $OUTPUT | cut -d'=' -f2)
37+
38+
# Create a MESSAGE variable to send to your Telegram bot
39+
MESSAGE="$HOSTNAME ($HOSTALIAS)%0A"
40+
MESSAGE+="$WHAT $NOTIFICATIONTYPE%0A%0A"
41+
if [ $WHAT == "SERVICE" ]; then
42+
MESSAGE+="$SERVICEDESC%0A"
43+
MESSAGE+="State changed from $PREVIOUSSERVICEHARDSHORTSTATE to $SERVICESHORTSTATE%0A"
44+
MESSAGE+="$SERVICEOUTPUT%0A"
45+
else
46+
MESSAGE+="State changed from $PREVIOUSHOSTHARDSHORTSTATE to $HOSTSHORTSTATE%0A"
47+
MESSAGE+="$HOSTOUTPUT%0A"
48+
fi
49+
MESSAGE+="%0AIPv4: $HOST_ADDRESS_4 %0AIPv6: $HOST_ADDRESS_6%0A"
50+
MESSAGE+="$SHORTDATETIME"
51+
52+
# Send message to Telegram bot
53+
curl -s -X POST "https://api.telegram.org/bot$TOKEN/sendMessage" -d chat_id=$CHAT_ID -d text="$MESSAGE" >> /dev/null
54+
55+
# End of script
56+
exit 0

config.xml.sample

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0"?>
2+
<config>
3+
<telegram_api_token>TELEGRAM_API_TOKEN_WITHOUT_BOT_PREFIX</telegram_api_token>
4+
<telegram_chat_id>TELEGRAM_GROUP_OR_CHAT-ID</telegram_chat_id>
5+
</config>

0 commit comments

Comments
 (0)