Files
onduleur/check-ups-runtime.sh
2025-12-26 17:05:22 +01:00

18 lines
1014 B
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.

#!/bin/bash
WEBHOOK="https://discord.com/api/webhooks/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
LOGFILE="/var/log/ups-shutdown.log"
UPS_DATA=$(upsc eaton@localhost 2>/dev/null)
STATUS=$(echo "$UPS_DATA" | grep '^ups.status:' | awk '{print $2}')
RUNTIME=$(echo "$UPS_DATA" | grep '^battery.runtime:' | awk '{print $2}')
BATTERY_CHARGE=$(echo "$UPS_DATA" | grep '^battery.charge:' | awk '{print $2}')
MODEL=$(echo "$UPS_DATA" | grep '^device.model:' | cut -d ':' -f2- | sed 's/^ *//')
if [[ "$STATUS" == "OB" && "$RUNTIME" -lt 300 ]]; then
MESSAGE="⏱ *$(hostname)* — autonomie critique détectée !\n🔋 Batterie : ${BATTERY_CHARGE}%\n⏳ Autonomie : ${RUNTIME} sec\n🖥 Modèle : $MODEL"
echo "$(date '+%F %T') ⚠️ Recheck runtime < 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
fi