Monitoring server load is a critical task for system administrators. Excessive load can slow services down or cause them to fail entirely. This article shows how to send automatic Telegram notifications when load exceeds a specified threshold.
Preparing a Telegram bot
Creating the bot
- Open Telegram and find @BotFather
- Send
/newbotand follow the instructions - Get the new bot's API token
Getting the chat ID
There are several ways to obtain a chat ID:
- Through the getUpdates API:
curl -s "https://api.telegram.org/bot<YOUR_BOT_TOKEN>/getUpdates" | jq - Using a dedicated bot such as @userinfobot
- Through a group chat, after adding your bot to the group
Setting up the monitoring script
Creating check_load.sh
The script checks the five-minute load average and compares it with a threshold.
#!/bin/bash
# Конфигурационные параметры
BOT_TOKEN="YOUR_BOT_TOKEN"
CHAT_ID="YOUR_CHAT_ID"
LOAD_THRESHOLD=80 # Пороговое значение нагрузки
HOSTNAME=$(hostname)
LOG_FILE="/var/log/load_monitor.log"
# Получаем среднюю нагрузку за 5 минут
LOAD=$(cat /proc/loadavg | awk '{print $2}')
# Преобразуем в целое число (удаляем десятичную часть)
LOAD_INT=$(printf "%.0f" "$LOAD")
# Логируем текущую нагрузку
echo "[$(date '+%Y-%m-%d %H:%M:%S')] Текущая средняя нагрузка: $LOAD" >> "$LOG_FILE"
# Проверяем превышение порога
if [ "$LOAD_INT" -gt "$LOAD_THRESHOLD" ]; then
MESSAGE="🚨 ВНИМАНИЕ! Высокая нагрузка на сервере $HOSTNAME
▫️ Текущая нагрузка: $LOAD
▫️ Пороговое значение: $LOAD_THRESHOLD
▫️ Время: $(date '+%Y-%m-%d %H:%M:%S')"
echo "$MESSAGE" >> "$LOG_FILE"
# Отправляем сообщение в Telegram
curl -s -X POST "https://api.telegram.org/bot$BOT_TOKEN/sendMessage" \
-d chat_id="$CHAT_ID" \
-d text="$MESSAGE" \
-d parse_mode="Markdown"
fiScheduling automatic execution
Setting permissions and testing
sudo chmod +x /usr/local/bin/check_load.sh
sudo /usr/local/bin/check_load.shAdding the script to cron
To check regularly, for example every 10 minutes:
sudo crontab -eAdd this line:
*/10 * * * * /usr/local/bin/check_load.shAdditional monitoring
Monitoring other metrics
Extend the script to check:
- Memory usage
- Free disk space
- The number of active processes
- CPU temperature
An example of an additional check:
# Проверка свободной памяти
FREE_MEM=$(free -m | awk '/Mem:/ {print $4}')
if [ "$FREE_MEM" -lt 100 ]; then
# Добавить предупреждение о нехватке памяти
fi
# Проверка места на диске
DISK_USAGE=$(df -h / | awk '/\// {print $5}' | tr -d '%')
if [ "$DISK_USAGE" -gt 90 ]; then
# Добавить предупреждение о нехватке места
fiError handling and logging
Consider adding these error-handling features:
- Check Telegram API availability
- Log failed delivery attempts
- Limit notification frequency
Conclusion
Server load monitoring with Telegram notifications helps you respond promptly to critical situations. Extend the script to monitor other important metrics or integrate more sophisticated alerting systems.
For more advanced monitoring, consider dedicated systems such as Zabbix, Prometheus or Nagios, which offer flexible notifications and data visualization.