-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheckServerStatus.sh
More file actions
56 lines (48 loc) · 1.97 KB
/
checkServerStatus.sh
File metadata and controls
56 lines (48 loc) · 1.97 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
46
47
48
49
50
51
52
53
54
55
56
#!/bin/bash
# What this script does?
# This script checks for reverse tunneled port, in my case it's 2222
# These ports will be checked under an infinite while loop
# If the port doesn't exist, The current machine will send you the message to the Telegram Channel Instantly
# By Default the accuracy is set as 5 seconds
# Please enter your Telegram bot token and channel ID below
BOT_TOKEN='' # Your Bot Token which is issued by BotFather
CHANNEL_ID='' # Prepend -100 to the channel ID
CHECK_PORT='2222' # Which port to check?
# Function to send styled message to Telegram channel
send_telegram_message() {
local message="$1"
curl -s -X POST "https://api.telegram.org/bot$BOT_TOKEN/sendMessage" \
-H "Content-Type: application/json" \
-d "{\"chat_id\": \"$CHANNEL_ID\", \"text\": \"$message\", \"parse_mode\": \"HTML\", \"disable_notification\": false}"
}
uptime=0
downtime=0
previous_status="up"
while true; do
# Check if port is open
nc -z localhost $CHECK_PORT > /dev/null 2>&1
PORT_STATUS=$?
if [ $PORT_STATUS -ne 0 ]; then
if [[ "$previous_status" == "up" ]]; then
message="<b>⚠️ Server is down! ⚠️</b>\n\nLast Uptime: $((uptime / 60)) minutes\nLast Downtime: $((downtime / 60)) minutes"
send_telegram_message "$message"
downtime=0
previous_status="down"
fi
while [ $PORT_STATUS -ne 0 ]; do
sleep 5 # Wait for 5 second before rechecking
nc -z localhost 2222 > /dev/null 2>&1
PORT_STATUS=$?
downtime=$((downtime + 5))
done
else
if [[ "$previous_status" == "down" ]]; then
message="<b>✅ Server is up! ✅</b>\n\nLast Downtime: $((downtime / 60)) minutes\nLast Uptime: $((uptime / 60)) minutes"
send_telegram_message "$message"
uptime=0
previous_status="up"
fi
uptime=$((uptime + 5))
fi
sleep 5 # Wait for 5 second before rechecking
done