-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathram_check.sh
40 lines (32 loc) · 1.08 KB
/
ram_check.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
#!/bin/bash
# Telegram Configuration
BOT_TOKEN="" # Telegram bot token
CHAT_ID="" # Group ID
THREAD_ID="" # Thread ID within the group
# Function to send Telegram notification
send_tg_message() {
local message=$1
curl -s -X POST "https://api.telegram.org/bot${BOT_TOKEN}/sendMessage" \
-d chat_id="${CHAT_ID}" \
-d message_thread_id="${THREAD_ID}" \
-d text="${message}" \
-d parse_mode="HTML"
}
# Get total used memory in gigabytes
USED_RAM=$(free -g | grep Mem | awk '{print $3}')
# Check if usage exceeds 14GB
if [ ${USED_RAM} -ge 15 ]; then
# Prepare Telegram notification
ALERT_MSG="⚠️ High RAM usage on $(hostname)!
Used: ${USED_RAM}GB
Threshold: 15GB
🔄 Initiating server reboot."
# Send Telegram notification
send_tg_message "$ALERT_MSG"
# Log to system log
logger "RAM Monitor: Memory usage exceeded 15GB (Current: ${USED_RAM}GB). Executing immediate reboot..."
# Execute reboot with sudo
sudo /sbin/reboot
#else
# logger "RAM Monitor: Memory usage is normal (${USED_RAM}GB)"
fi