-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnagios-notify-by-telegram.sh
129 lines (110 loc) · 3.51 KB
/
nagios-notify-by-telegram.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/bin/bash
#This script helps integrate Nagios instances
#with telegrams chat and have special logic inside.
sendFunc()
{
"$tgBinPath" `
`--rsa-key "$tgKeyPath" `
`--wait-dialog-list `
`--exec "$tgSendCmd $contactName $messageText" `
`--disable-link-preview `
`--logname "$mesLogFile" `
`>> $mesLogFile
}
#Path setup
tgSendCmd="msg"
tgDir="/usr/local/bin"
tgBinPath=""$tgDir"/telegram-cli"
tgKeyPath=""$tgDir"/tg-server.pub"
countersDir="/home/telegramd/services/"
logDir="/var/log/telegram"
#dont forget to setup log rotation
mesLogFile=""$logDir"/telegram.log"
#Parse arguments
contactName="$1"
messageText="$2"
serviceName="$3"
#Chat names settings
extraChatName="Extra_Monitoring"
fackupChatName="Fackup_Monitoring"
regularChatName="Regular_Monitoring"
#Do not send this message to "extra" chats
recoveryPattern="***** RECOVERY"
#Maximum MINUTES that service or host can be down
#If this value reched,
#all alarms for <regularChatName>
#will forced to <fackupChatName> or <extraChatName>.
critTimeDiffMins="30"
extraTimeDiffMins="60"
#Exit with error if any parameters are not provided
if [[ -z "$contactName" || -z "$messageText" || -z "$serviceName" ]]
then
echo "FAIL: cant parse all needed parameters" >> "$mesLogFile"
echo "1="$1",2="$2",3="$3"" >> "$mesLogFile"
exit 1
fi
#Exit with error if folder for counters not exists
mkdir -p "$countersDir"
if ! [[ -d "$countersDir" ]]
then
echo "FAIL: cant create counters folder" >> "$mesLogFile"
exit 1
fi
#Enable "extra" logic only for specific chat
if [[ "$contactName" == "$regularChatName" ]]
then
#extra monitoring logic
currentDate=`date +"%Y-%m-%d %H:%M:%S"`
serviceCounterFile=""$countersDir""$serviceName"_counter"
serviceDateFile=""$countersDir""$serviceName"_data"
#Before write - checking permissions
touch "$serviceCounterFile" && touch "$serviceDateFile"
if [[ "$?" != "0" ]]
then
echo "FAIL: cant access counter file" >> "$mesLogFile"
exit 1
fi
#Get current counter value
currentCounterAmount=`cat "$serviceCounterFile"`
previousDate=`cat "$serviceDateFile"`
#If it is a new count
if [[ -z "$currentCounterAmount" || -z "$serviceCounterFile" ]]
then
currentCounterAmount="0"
previousDate="$currentDate"
fi
#Calculate difference in minutes
curSecsConverted=$(date +%s -d "$currentDate")
prevSecsConverted=$(date +%s -d "$previousDate")
curTimeDiffMins=$(( ($curSecsConverted - $prevSecsConverted) / 60 ))
#If new notice not older than max TimeDiff
if [[ "$curTimeDiffMins" -le "$extraTimeDiffMins" ]] #<=
then
#Do not send this message to "extra" chats
isItRevocery=`echo "$messageText" | grep "$recoveryPattern"`
if [[ -z "$isItRevocery" ]]
then
#Just force message reciver - its a fuckup!
if [[ "$currentCounterAmount" -ge "$critTimeDiffMins" ]] # >=
then
contactName="$fackupChatName"
fi
#Just force message reciver - its a super fuckup!
if [[ "$currentCounterAmount" -ge "$extraTimeDiffMins" ]] # >=
then
contactName="$extraChatName"
fi
fi
#Increase counter with current time differense
currentCounterAmount=$((currentCounterAmount+curTimeDiffMins))
#And remember new values
echo "$currentCounterAmount" > "$serviceCounterFile"
echo "$currentDate" > "$serviceDateFile"
#Reset counter, hold time is over
else
echo "0" > "$serviceCounterFile"
echo "$currentDate" > "$serviceDateFile"
fi
fi
sendFunc #send telegram message
exit $?