-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathping.sh
executable file
·50 lines (38 loc) · 1.22 KB
/
ping.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
#!/bin/bash
LOG_DIR="logs/"
PING_RESULTS_FILE="ping_results.log"
# Function to monitor today's logs for "Gateway Timeout" in new entries only
monitor_logs() {
local last_size=0
while true; do
TODAY=$(date +"%Y-%m-%d")
LOG_FILE="$LOG_DIR/info.$TODAY.log"
if [ -f "$LOG_FILE" ]; then
current_size=$(stat -c%s "$LOG_FILE")
if [ "$current_size" -gt "$last_size" ]; then
tail -n $((current_size - last_size)) "$LOG_FILE" | grep -q "Gateway Timeout" && ping_hosts
fi
last_size=$current_size
fi
sleep 10 # Check every 10 seconds
done
}
# Function to ping hosts and log results
ping_hosts() {
for host in "www.google.com" "api.telegram.org"; do
result=$(ping -c 4 "$host" 2>&1)
log_ping_results "$host" "$result"
done
}
# Function to log ping results
log_ping_results() {
local host="$1"
local result="$2"
current_time=$(date +"%Y-%m-%d %H:%M:%S")
echo "Current time: $current_time" >> "$PING_RESULTS_FILE"
echo "Ping result for $host:" >> "$PING_RESULTS_FILE"
echo "$result" >> "$PING_RESULTS_FILE"
echo "" >> "$PING_RESULTS_FILE"
}
# Start monitoring logs
monitor_logs