Bash script kiểm tra và thông báo Disk, CPU, RAM tới Telegram

Khá tương tự bash script cũ về PHP, Mysql, Nginx, phiên bản này sẽ kiểm tra Disk, CPU, RAM sau đó gửi thông báo tới Telegram nếu sử dụng tới ngưỡng quy định

Đầu tiên là tạo 1 con chat bot Telegram, ví dụ

  • Bot Token 6360723418:AAF1aya50fEi6sVuOwhXp53Ic19siP3gqLc
  • Chat ID 489842537″

Tạo 1 file bash bên trong /usr/local/bin/, có thể đặt tên là monitor_cpu_ram_hdd.sh

touch /usr/local/bin/monitor_cpu_ram_hdd.sh
nano /usr/local/bin/monitor_cpu_ram_hdd.sh

Nội dung bên trong điền vào

#!/bin/bash
while true
do

# Set the threshold for disk, CPU, and RAM usage
DISK_THRESHOLD=80 # in %
CPU_THRESHOLD=80 # in %
RAM_THRESHOLD=80 # in %

# Set the Telegram bot token and chat ID
TELEGRAM_BOT_TOKEN="6360723418:AAF1aya50fEi6sVuOwhXp53Ic19siP3gqLc"
TELEGRAM_CHAT_ID="489842537"

# Check disk usage and send a warning if it exceeds the threshold
disk_usage=$(df -h / | awk '/\// {print $(NF-1)}' | sed 's/%//')
if [ "$disk_usage" -ge "$DISK_THRESHOLD" ]; then
    message="WARNING: Disk usage is at ${disk_usage}% - Disk threshold $DISK_THRESHOLD %"
    curl -s -X POST "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage" -d "chat_id=${TELEGRAM_CHAT_ID}" -d "text=$message"
fi

# Check CPU usage and send a warning if it exceeds the threshold
cpu_usage=$(top -b -n 1 | awk '/^%Cpu/ {print $2}' | cut -d '.' -f 1)
if [ "$cpu_usage" -ge "$CPU_THRESHOLD" ]; then
    message="WARNING: CPU usage is at ${cpu_usage}% - CPU threshold $CPU_THRESHOLD %"
    curl -s -X POST "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage" -d "chat_id=${TELEGRAM_CHAT_ID}" -d "text=$message"
fi

# Check RAM usage and send a warning if it exceeds the threshold
ram_usage=$(free | awk '/^Mem/ {print $3/$2 * 100.0}' | cut -d '.' -f 1)
if [ "$ram_usage" -ge "$RAM_THRESHOLD" ]; then
    message="WARNING: RAM usage is at ${ram_usage}% - RAM threshold $RAM_THRESHOLD %"
    curl -s -X POST "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage" -d "chat_id=${TELEGRAM_CHAT_ID}" -d "text=$message"
fi

sleep 1
done
  • Sửa dòng 6, 7, 8, 11, 12 cho phù hợp với bot Telegram và ngưỡng bạn muốn thông báo (mình đang duy trì Disk, CPU, RAM ở ngưỡng 80%)
  • Sửa xong thì Ctrl+O -> Enter -> Ctrl+X để save và exit.

Ý nghĩa của đoạn code trên là nó cứ check tình trạng của Disk, CPU, RAM

  • Nếu Disk, CPU, RAM sử dụng < 80% thì không làm gì
  • Nếu Disk, CPU, RAM sử dụng > 80% thì gửi thông báo tới Telegram
  • Chạy xong thì ngừng 1s, sau đó chạy lại từ đầu 😀

Tiếp theo chạy các dòng code bên dưới để nó tự chạy ngầm và tự chạy lại sau khi reboot VPS

chmod +x /usr/local/bin/monitor_cpu_ram_hdd.sh
nohup /usr/local/bin/monitor_cpu_ram_hdd.sh >> ./out 2>&1 <&- &
crontab -l > monitor_service_cpu_ram_hdd
echo "@reboot nohup /usr/local/bin/monitor_cpu_ram_hdd.sh >> ./out 2>&1 <&- &" >> monitor_service_cpu_ram_hdd
crontab monitor_service_cpu_ram_hdd

Đoạn code này tác giả không quản lý giới hạn thông báo, nên nếu Disk, CPU, RAM sử dụng vượt ngưỡng là cứ liên tục gửi tin nhắn, khá lú người 😀

Có các tool monitor theo thời gian thực như này nếu VPS gặp lỗi cũng dễ bắt bệnh hơn

Comment policy: We love comments and appreciate the time that readers spend to share ideas and give feedback.
Notes: However, those deemed to be spam or solely promotional will be deleted.

You can create a Gravatar account, add avatar, then use that email to comment here, your account will have a more beautiful Avatar, easier to recognize with other members.

Please use real emails, you can receive notifications when comments are replied