-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupsinfo.sh
executable file
·53 lines (43 loc) · 1.02 KB
/
upsinfo.sh
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
#!/bin/bash
# Fields
#1 battery.charge:
#2 battery.voltage:
#3 input.voltage:
#4 input.voltage.nominal:
#5 output.voltage:
#6 ups.load:
#7 battery.runtime:
UPSNAME=Basement_UPS
HOST=localhost
UPSINFO1=$(upsc $UPSNAME@$HOST \
| grep \
-e battery.charge: \
-e battery.voltage: \
-e input.voltage: \
-e input.voltage.nominal: \
-e output.voltage: \
-e ups.load: \
| awk -F: '{print $2}')
# The default output of battery.runtime is in seconds
# Get the battery.runtime info and then divide by 60
# to convert seconds to minutes
UPSINFO2=$(upsc $UPSNAME@$HOST battery.runtime)
UPSINFO2=$(( UPSINFO2/60 ))
UPSINFO=$(echo $UPSINFO1 $UPSINFO2)
if [ -z "$UPSINFO" ]
then
echo "Sorry, no data from upsc"
exit
fi
n=1
JSON=$(echo "{"
for i in $UPSINFO
do echo "\"field$n\":$i,"
(( n++ ))
done
echo "}")
TYPE="Content-Type: application/json"
TOKEN="X-THINGSPEAKAPIKEY: XXXXXXXXXXXXXXXX"
URL="https://api.thingspeak.com/update.json"
PAYLOAD=$(echo $JSON | sed 's/, }/ }/')
curl -m 40 -o /dev/null -s -H "$TYPE" -H "$TOKEN" -d "$PAYLOAD" "$URL"