checacessmyvpsbot/ping-vps.sh
nekochemist 878600ca30 new file: .env.example
new file:   .gitignore
	new file:   README.md
	new file:   install.sh
	new file:   ping-vps.service
	new file:   ping-vps.sh
	new file:   ping-vps.timer
2026-04-17 04:41:50 +05:00

37 lines
1.4 KiB
Bash
Executable file
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# /usr/local/bin/ping_monitor.sh
source "$(dirname "$0")/.env"
send_matrix() {
curl -s -X PUT \
"${MATRIX_SERVER}/_matrix/client/v3/rooms/${MATRIX_ROOM}/send/m.room.message/$(date +%s%N)" \
-H "Authorization: Bearer ${MATRIX_TOKEN}" \
-H "Content-Type: application/json" \
-d "{\"msgtype\":\"m.text\",\"body\":\"$1\"}"
}
send_telegram() {
curl -s -X POST "https://api.telegram.org/bot${BOT_TOKEN}/sendMessage" \
-d chat_id="${CHAT_ID}" \
-d text="$1"
}
# Инициализация state
[ ! -f "$STATE_FILE" ] && echo "up" >"$STATE_FILE"
PREV_STATE=$(cat "$STATE_FILE")
if ping -c 3 -W 5 "$TARGET_IP" >/dev/null 2>&1; then
CURRENT_STATE="up"
# Уведомить о восстановлении, если был down
if [ "$PREV_STATE" = "down" ]; then
send_matrix "✅ Сервер восстановлен: ${TARGET_IP} снова доступен"
send_telegram "✅ Сервер восстановлен: ${TARGET_IP} снова доступен"
fi
else
CURRENT_STATE="down"
# Уведомить только если статус изменился (не спамить)
if [ "$PREV_STATE" = "up" ]; then
send_matrix "🔴 Сервер недоступен: ${TARGET_IP} не отвечает на ping"
send_telegram "🔴 Сервер недоступен: ${TARGET_IP} не отвечает на ping"
fi
fi
echo "$CURRENT_STATE" >"$STATE_FILE"