-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpacket_loss_monitor.sh
More file actions
executable file
·45 lines (36 loc) · 1.28 KB
/
packet_loss_monitor.sh
File metadata and controls
executable file
·45 lines (36 loc) · 1.28 KB
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
get_webhook_id() {
grep -oP "(?<=^WEBHOOK_ID=).*" config.cfg
}
#static discord webhook URL
WEBHOOK_BASE_URL="https://discord.com/api/webhooks/"
WEBHOOK_ID=$(get_webhook_id)
#construct the full webhook URL
WEBHOOK_URL="${WEBHOOK_BASE_URL}${WEBHOOK_ID}"
#config
TARGET="142.254.147.37"
THRESHOLD=2
SLEEP_INTERVAL=100 #in seconds
PACKETS_TO_SEND=100
send_discord_message(){
local message=$1
curl -X POST -H "Content-Type: application/json" \
-d "{\"content\": \"$message\"}" "$WEBHOOK_URL"
}
while true; do
#ping the target and get latency stats
ping_output=$(ping -c $PACKETS_TO_SEND $TARGET)
packet_loss=$(echo "$ping_output" | grep -oP '\d+(?=% packet loss)')
rtt_stats=$(echo "$ping_output" | grep 'rtt min/avg/max/mdev' | awk -F'=' '{ print $2 }' | tr -d ' ')
read min_rtt avg_rtt max_rtt mdev <<< $(echo "$rtt_stats" | tr '/' ' ')
#discord message
message="**🚨 High Packet Loss Alert**"
message+="\n**Packet Loss($PACKETS_TO_SEND):** ${packet_loss}%\n"
message+="**RTT Statistics:**\n"
message+="\`RTT Avg:\` **${avg_rtt} ms** | \`RTT Mdev:\` **${mdev} ms**"
#check criteria and send discord message
if (( packet_loss > THRESHOLD )); then
send_discord_message "$message"
fi
sleep $SLEEP_INTERVAL
done