Files
check-ups-runtime/check-ups-runtime.sh
2025-11-03 17:03:23 +01:00

26 lines
1.3 KiB
Bash
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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
WEBHOOK="https://discord.com/api/webhooks/1234567890987654321/jsdkghfojahjgoaropihjgoirhjfsdighajsdfnbgkvjasznbfgousoghsoaghoasdhgjnsdfjnv;jsdfnvjfsdgnd"
LOGFILE="/var/log/ups-shutdown.log"
STATEFILE="/tmp/ups-runtime-alert.sent"
UPS_DATA=$(upsc eaton@localhost 2>/dev/null)
STATUS=$(echo "$UPS_DATA" | awk -F': ' '/^ups.status:/ {print $2}')
RUNTIME=$(echo "$UPS_DATA" | awk -F': ' '/^battery.runtime:/ {print $2}')
BATTERY_CHARGE=$(echo "$UPS_DATA" | awk -F': ' '/^battery.charge:/ {print $2}')
MODEL=$(echo "$UPS_DATA" | awk -F': ' '/^device.model:/ {print $2}')
# Si autonomie < 300 sec et que message pas encore envoyé
if [[ "$RUNTIME" -lt 300 ]]; then
if [[ ! -f "$STATEFILE" ]]; then
MESSAGE="⏱ *$(hostname)* — ⚠️ Autonomie critique UPS à $(date '+%F %T')\n🔋 Batterie : ${BATTERY_CHARGE} %\n⏳ Autonomie : ${RUNTIME} sec\n🖥 Modèle : ${MODEL}"
echo "$(date '+%F %T') 🚨 ALERTE: autonomie < 300 sec (batt=$BATTERY_CHARGE%, runtime=$RUNTIME sec)" >> "$LOGFILE"
jq -n --arg content "$MESSAGE" '{content: $content}' | \
curl -s -H "Content-Type: application/json" -X POST -d @- "$WEBHOOK" > /dev/null
touch "$STATEFILE"
fi
else
# Si autonomie > 300 sec, reset le flag dalerte
[[ -f "$STATEFILE" ]] && rm -f "$STATEFILE"
fi