-
Notifications
You must be signed in to change notification settings - Fork 0
/
send_ntfy
107 lines (64 loc) · 1.79 KB
/
send_ntfy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/bin/bash
ntfy_topic="YOUR_NTFY_TOPIC"
UPS_name="" # set your UPS name here, use "upsc -l" to find it
alert_at=(25 50 75 85 90 95 98)
if [[ -z "$UPS_name" ]]; then
UPS_name=$(upsc -l 2>/dev/null)
if [[ "$UPS_name" ]]; then
echo "UPS_name not set, using \"$UPS_name\""
actual=$(upsc $UPS_name battery.charge 2>/dev/null)
actual=$((actual))
if ! [[ "$actual" =~ ^[0-9]+$ ]]; then
echo "ERROR : charge on the UPS not found" >&2; exit 1
fi
else
echo "UPS_name not set, nothing found"
exit 1
fi
fi
declare previous_msg=""
#quit if process already started
nb_process=$(pgrep -c send_ntfy)
if [ $nb_process -ne 1 ]; then
echo "send_ntfy is already running $nb_process"
exit 1
fi
function go_test() {
actual=$(upsc $UPS_name battery.charge 2>/dev/null)
actual=$((actual))
#echo battery : $actual
go_alert=-1
for i in "${alert_at[@]}"; do
(( actual <= i )) && go_alert=$i && break
done
if [ $go_alert -ne -1 ]; then
echo Supprime alerte egale et superieur a $go_alert
ii=0
for i in "${alert_at[@]}"; do
(( go_alert <= i )) && alert_at[$ii]=0
((ii++))
done
send_alert
fi
}
function send_alert() {
secs=$(upsc $UPS_name battery.runtime 2>/dev/null)
charge=$(upsc $UPS_name battery.charge 2>/dev/null)
msg="$charge%, $((secs/60)) mins left (estimated)"
if [ "$previous_msg" = "$msg" ]; then
return
fi
previous_msg=$msg
curl -H "Tags: rotating_light" -H "Title: UPS Offline" -d "$msg" ntfy.sh/$ntfy_topic
}
while true; do
status=$(upsc $UPS_name ups.status 2>/dev/null)
if [ "$status" = "OL" ]; then
charge=$(upsc $UPS_name battery.charge 2>/dev/null)
curl -H "Tags: electric_plug" -H "Title: UPS Online" -d "$charge%" ntfy.sh/$ntfy_topic
echo "Battery is Online, exiting."
exit 0
fi
go_test
sleep 5
done