forked from patricegautier/unifiZabbix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolarpointBattery.sh
executable file
·94 lines (65 loc) · 1.77 KB
/
solarpointBattery.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
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
#!/bin/bash
# This script will retrieve battery information from a SunMax SolarPoint device in JSON format
#
# With thanks to @pfanntec in the UI community who got the JSON RPC access rolling
#
usage()
{
echo "Usage "${0}" -u userName -p password [-v] targetDevice"
echo "-u user name for Solarpoint device"
echo "-p password for Solarpoint device"
echo "-h help"
exit 2
}
unset TARGET
unset PASSWORD
unset VERBOSE
unset USERNAME
while getopts 'u:p:vh' OPT
do
case $OPT in
u) USERNAME=${OPTARG} ;;
p) PASSWORD=${OPTARG} ;;
v) VERBOSE=true ;;
h) usage ;;
esac
done
shift $((OPTIND-1))
TARGET=$@
if [ -z "$TARGET" ]; then
echo "Missing Target device"
usage;
fi
if [ -z "$USERNAME" ]; then
echo "Missing user name"
usage;
fi
if [ -z "$PASSWORD" ]; then
echo "Missing password"
usage;
fi
if ! [[ -z ${VERBOSE} ]]; then
set -x
fi
SessionJSON=$(curl -s https://$TARGET/ubus\
--insecure \
-H "Content-Type: application/json" \
-H "Accept: application/json"\
--data "{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"call\",\"params\":[\"00000000000000000000000000000000\",\"session\",\"login\",{\"username\":\"$USERNAME\",\"password\":\"$PASSWORD\",\"timeout\":30}]}")
if ! [[ -z ${VERBOSE} ]]; then
echo ${SessionJSON}
fi
SESSION_ID=$(echo ${SessionJSON} | jq ".result[1].ubus_rpc_session")
if ! [[ -z ${VERBOSE} ]]; then
echo SessionID=${SESSION_ID}
fi
if [[ -z ${SESSION_ID} ]]; then
echo "Could not retrieve Session ID: "${SessionJSON}
else
BatteryData=$(curl -s https://$TARGET/ubus\
--insecure\
-H "Content-Type: application/json"\
-H "Accept: application/json"\
--data "{\"jsonrpc\":\"2.0\",\"id\":344,\"method\":\"call\",\"params\":[$SESSION_ID,\"battery\",\"stats\",{\"timeout\":30}]}")
echo ${BatteryData}
fi