-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck-isp.sh
executable file
·54 lines (45 loc) · 1.41 KB
/
check-isp.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
#!/bin/bash
ISP='8.8.8.8'
BASE_URL='https://uptimekuma.yourdomain.com/api/push/HhcsE3Mc3r?status=up&msg=OK&ping='
# Set up logging
LOG_FILE="/boot/config/plugins/user.scripts/scripts/check-isp/check-isp.log"
MAX_LOG_SIZE=1048576 # 1 MB
# Function to log messages
log() {
local TIMESTAMP
TIMESTAMP=$(date +"%Y-%m-%d %H:%M:%S")
echo "$TIMESTAMP $1" >> "$LOG_FILE"
}
# Function to check if log file exists and create if not
check_log_file() {
if [ ! -f "$LOG_FILE" ]; then
touch "$LOG_FILE"
fi
}
# Function to check log file size and delete if it exceeds the limit
rotate_log_file() {
if [ $(stat -c%s "$LOG_FILE") -ge $MAX_LOG_SIZE ]; then
rm "$LOG_FILE"
echo "Deleted $LOG_FILE due to size limit reached."
touch "$LOG_FILE"
fi
}
# Main script execution
check_log_file
rotate_log_file
log "START"
log "ISP: $ISP"
log "BASE_URL: $BASE_URL"
# Perform the ping test and extract the average ping time
PING_RESULT=$(ping -c 1 "$ISP" 2>/dev/null | awk -F'time=' '/time=/{ print $2 }' | awk '{ print $1 }')
PING_RESULT=${PING_RESULT:-'N/A'}
# Construct the request URL with the dynamic ping value
URL="${BASE_URL}${PING_RESULT}"
log "FULL_URL: $URL"
# Execute the HTTP request
RESPONSE=$(curl --max-time 5 -s -o /dev/null -w "%{http_code}" "$URL")
if [ $? -eq 0 ]; then
log "CURL command executed. Response status: $RESPONSE"
else
log "Failed to execute CURL command."
fi