forked from pistonov/BGPalerter_recovery_notify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBGPalerter_recovery_notify.sh
45 lines (36 loc) · 1.39 KB
/
BGPalerter_recovery_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
#!/bin/bash
#
LOG_FILE="/home/BGPalerter/logs/reports.log"
TELEGRAM_BOT_KEY="bot<_BOT_ID_>"
URL="https://api.telegram.org/bot$TELEGRAM_BOT_KEY/sendMessage"
TELEGRAM_CHAT_ID="_CHAT_ID_"
#
NOW=$(date +%s)
LAST_LOG=$(($NOW - $(date +%s -r $LOG_FILE)))
if [ "$LAST_LOG" -le "121" ]; then
PATERN=$(cat $LOG_FILE | grep "has been withdrawn")
while read -r line; do
LOG_TIME=$(echo $line | cut -d' ' -f1)
LOG_TIME=$(date -d$LOG_TIME +'%s')
LOG_TIME=$(($NOW - $LOG_TIME))
if [ "$LOG_TIME" -le "121" ]; then
PREFIX_ALERT=$(echo $line | cut -d' ' -f5)
PREFIX_MASK=$(echo "bgp_"$PREFIX_ALERT | sed -r 's/\//_/g')
echo $PREFIX_ALERT > /tmp/$PREFIX_MASK
fi
done <<< "$PATERN"
fi
for file in /tmp/bgp_* ; do
if [ -f $file ]; then
PREF_FILE_TIME=$(($NOW - $(date +%s -r $file)))
if [ "$PREF_FILE_TIME" -ge "900" ]; then
PREFIX=$(cat $file)
PEER_COUNT_CURL=$(curl -s --max-time $TIME https://stat.ripe.net/data/looking-glass/data.json?resource=$PREFIX)
PEER_COUNT=$(echo $PEER_COUNT_CURL | jq '.data.rrcs[].peers' | jq '.[].peer' | wc -l)
if [ "$PEER_COUNT" -ge "40" ]; then
curl -s --max-time $TIME -d "chat_id=$GROUP_ID&disable_web_page_preview=1&text=$PREFIX announced again" $URL >/dev/null
rm $file
fi
fi
fi
done