-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathups.sh
executable file
·124 lines (103 loc) · 3.01 KB
/
ups.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/bin/bash
DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
CONF="${DIR}/ups.conf"
TEST="$1"
CACHE_STATUS=''
log() {
local -r type="$1"
shift
echo -e "$(date) [${type}]: $*"
}
push() {
local -r status="$1"
local -r bcharge="$2"
local -r upsname="${3:-UPS}"
local -r timeleft="$4"
# shellcheck disable=SC2153
RTITLE="${TITLE//#upsname/$upsname}"
# shellcheck disable=SC2153
RTEXT=$(echo "${TEXT}" | sed "s/#status/${status}/; s/#bcharge/${bcharge}/; s/#upsname/${upsname}/; s/#timeleft/${timeleft}/;")
if [ -z "$RTITLE" ]; then
RTITLE="Status ${upsname}"
fi
if [ -z "$RTEXT" ]; then
RTEXT="Status UPS: ${status} Battery Charge: ${bcharge}% Time Left: ${timeleft}"
fi
RUN=$(curl -sd "type=${TYPE}&id=${ID}&key=${KEY}&ttl=${TTL}&uid=${UIDD}&uids=${UIDS}&title=${RTITLE}&text=${RTEXT}" https://pushall.ru/api.php)
log "INFO" "Message sending result: ${RUN}"
}
check() {
if [ -z "$TEST" ]; then
# shellcheck disable=SC2002
UPSNAME=$(cat "${FILE}" | awk '/^(UPSNAME).*:/ {$1=$2="";print $0}' | sed -e 's/^[[:space:]]*//')
# shellcheck disable=SC2002
STATUS=$(cat "${FILE}" | awk '/^(STATUS).*:/ {print $3}')
# shellcheck disable=SC2002
BCHARGE=$(cat "${FILE}" | awk '/^(BCHARGE).*:/ {print $3}' | bc)
# shellcheck disable=SC2002
TIMELEFT=$(cat "${FILE}" | awk '/^(TIMELEFT).*:/ {$1=$2="";print $0}' | sed -e 's/^[[:space:]]*//')
else
UPSNAME=""
array=("ONLINE" "ONBATT")
BCHARGE=$(( (RANDOM % 100 ) + 1 ))
TIMELEFT=$(( (RANDOM % 100 ) + 1 ))
TIMELEFT="${TIMELEFT} Minutes"
size="${#array[@]}"
index=$((RANDOM % size))
STATUS="${array[$index]}"
fi
if [ -z "$STATUS" ]; then
log "WARNING" "UPS Status not found in ${FILE}"
return
fi
if [ "$CACHE_STATUS" != "$STATUS" ]; then
if [ -z "$TEST" ]; then
CACHE_STATUS="${STATUS}"
fi
push "${STATUS}" "${BCHARGE}" "${UPSNAME}" "${TIMELEFT}"
else
if [ -n "$TEST" ]; then
log "INFO" "Skipped"
fi
fi
}
if ! which apcupsd &> /dev/null; then
log "ERROR" "apcupsd not installed"
exit 1
fi
if [ ! -f "$CONF" ]; then
log "ERROR" "File configuration not found. Please copy ups.example.conf to ups.conf"
exit 1
else
# shellcheck disable=SC1090
source "${CONF}"
fi
if [ -z "$TYPE" ] || [ -z "$ID" ] || [ -z "$KEY" ] || [ -z "$FILE" ]; then
log "ERROR" "The config file is configured incorrectly"
exit 1
fi
if [ -z "$TEST" ] && [ ! -f "$FILE" ]; then
log "ERROR" "File ${FILE} not found"
exit 1
fi
if [ "$TYPE" = "multicast" ] && [ -z "$UIDS" ]; then
log "ERROR" "You are using multicast request type. UIDS must be set"
exit 1
elif [ "$TYPE" = "unicast" ] && [ -z "$UIDD" ]; then
log "ERROR" "You are using unicast request type. UIDD must be set"
exit 1
fi
log "INFO" "Service started successfully"
while :
do
check
if [ -n "$TEST" ]; then
exit 0
fi
if [ "$CACHE_STATUS" = "ONBATT" ] && [ "$BATT_INTERVAL" -gt 0 ]; then
CACHE_STATUS=""
sleep "${BATT_INTERVAL}"
else
sleep "${INTERVAL:-15}"
fi
done