@cryptotaxi247 / netdata-1 / commits / 52a7628bc

multiple APC UPSes; fixes #3167

Costa Tsaousis (ktsaou) committed Jan 3, 2018 at 14:17 UTC 52a7628bc90eb491ab86f01e1bec9eacfe488772
2 files changed +76 -34
charts.d/apcupsd.chart.sh
+72 -32
@@ -6,8 +6,12 @@
6 # GPL v3+
7 #
8
9 -apcupsd_ip=127.0.0.1
10 -apcupsd_port=3551
9 +apcupsd_ip=
10 +apcupsd_port=
11 +
12 +declare -A apcupsd_sources=(
13 + ["local"]="127.0.0.1:3551"
14 +)
15
16 # how frequently to collect UPS data
17 apcupsd_update_every=10
@@ -18,7 +22,7 @@ apcupsd_timeout=3
22 apcupsd_priority=90000
23
24 apcupsd_get() {
21 - run -t $apcupsd_timeout apcaccess status "$1:$2"
25 + run -t $apcupsd_timeout apcaccess status "$1"
26 }
27
28 apcupsd_check() {
@@ -29,52 +33,76 @@ apcupsd_check() {
33
34 require_cmd apcaccess || return 1
35
32 - run apcupsd_get $apcupsd_ip $apcupsd_port >/dev/null
33 - if [ $? -ne 0 ]
34 - then
35 - error "cannot get information for apcupsd server."
36 - return 1
37 - elif [ $(apcupsd_get $apcupsd_ip $apcupsd_port | awk '/^STATUS.*/{ print $3 }') != "ONLINE" ]
38 - then
39 - error "APC UPS not online."
40 - return 1
36 + # backwards compatibility
37 + if [ "${apcupsd_ip}:${apcupsd_port}" != ":" ]
38 + then
39 + apcupsd_sources["local"]="${apcupsd_ip}:${apcupsd_port}"
40 fi
41
42 + local host working=0 failed=0
43 + for host in "${!apcupsd_sources[@]}"
44 + do
45 + run apcupsd_get "${apcupsd_sources[${host}]}" >/dev/null
46 + if [ $? -ne 0 ]
47 + then
48 + error "cannot get information for apcupsd server ${host} on ${apcupsd_sources[${host}]}."
49 + failed=$((failed + 1))
50 + elif [ $(apcupsd_get "${apcupsd_sources[${host}]}" | awk '/^STATUS.*/{ print $3 }') != "ONLINE" ]
51 + then
52 + error "APC UPS ${host} on ${apcupsd_sources[${host}]} is not online."
53 + failed=$((failed + 1))
54 + else
55 + working=$((working + 1))
56 + fi
57 + done
58 +
59 + if [ ${working} -eq 0 ]
60 + then
61 + error "No APC UPSes found available."
62 + return 1
63 + fi
64 +
65 return 0
66 }
67
68 apcupsd_create() {
47 - # create the charts
48 - cat <<EOF
49 -CHART apcupsd.charge '' "UPS Charge" "percentage" ups apcupsd.charge area $((apcupsd_priority + 1)) $apcupsd_update_every
69 + local host src
70 + for host in "${!apcupsd_sources[@]}"
71 + do
72 + src=${apcupsd_sources[${host}]}
73 +
74 + # create the charts
75 + cat <<EOF
76 +CHART apcupsd_${host}.charge '' "UPS Charge for ${host} on ${src}" "percentage" ups apcupsd.charge area $((apcupsd_priority + 1)) $apcupsd_update_every
77 DIMENSION battery_charge charge absolute 1 100
78
52 -CHART apcupsd.battery_voltage '' "UPS Battery Voltage" "Volts" ups apcupsd.battery.voltage line $((apcupsd_priority + 3)) $apcupsd_update_every
79 +CHART apcupsd_${host}.battery_voltage '' "UPS Battery Voltage for ${host} on ${src}" "Volts" ups apcupsd.battery.voltage line $((apcupsd_priority + 3)) $apcupsd_update_every
80 DIMENSION battery_voltage voltage absolute 1 100
81 DIMENSION battery_voltage_nominal nominal absolute 1 100
82
56 -CHART apcupsd.input_voltage '' "UPS Input Voltage" "Volts" input apcupsd.input.voltage line $((apcupsd_priority + 4)) $apcupsd_update_every
83 +CHART apcupsd_${host}.input_voltage '' "UPS Input Voltage for ${host} on ${src}" "Volts" input apcupsd.input.voltage line $((apcupsd_priority + 4)) $apcupsd_update_every
84 DIMENSION input_voltage voltage absolute 1 100
85 DIMENSION input_voltage_min min absolute 1 100
86 DIMENSION input_voltage_max max absolute 1 100
87
61 -CHART apcupsd.input_frequency '' "UPS Input Frequency" "Hz" input apcupsd.input.frequency line $((apcupsd_priority + 5)) $apcupsd_update_every
88 +CHART apcupsd_${host}.input_frequency '' "UPS Input Frequency for ${host} on ${src}" "Hz" input apcupsd.input.frequency line $((apcupsd_priority + 5)) $apcupsd_update_every
89 DIMENSION input_frequency frequency absolute 1 100
90
64 -CHART apcupsd.output_voltage '' "UPS Output Voltage" "Volts" output apcupsd.output.voltage line $((apcupsd_priority + 6)) $apcupsd_update_every
91 +CHART apcupsd_${host}.output_voltage '' "UPS Output Voltage for ${host} on ${src}" "Volts" output apcupsd.output.voltage line $((apcupsd_priority + 6)) $apcupsd_update_every
92 DIMENSION output_voltage voltage absolute 1 100
93 DIMENSION output_voltage_nominal nominal absolute 1 100
94
68 -CHART apcupsd.load '' "UPS Load" "percentage" ups apcupsd.load area $((apcupsd_priority)) $apcupsd_update_every
95 +CHART apcupsd_${host}.load '' "UPS Load for ${host} on ${src}" "percentage" ups apcupsd.load area $((apcupsd_priority)) $apcupsd_update_every
96 DIMENSION load load absolute 1 100
97
71 -CHART apcupsd.temp '' "UPS Temperature" "Celsius" ups apcupsd.temperature line $((apcupsd_priority + 7)) $apcupsd_update_every
98 +CHART apcupsd_${host}.temp '' "UPS Temperature for ${host} on ${src}" "Celsius" ups apcupsd.temperature line $((apcupsd_priority + 7)) $apcupsd_update_every
99 DIMENSION temp temp absolute 1 100
100
74 -CHART apcupsd.time '' "UPS Time Remaining" "Minutes" ups apcupsd.time area $((apcupsd_priority + 2)) $apcupsd_update_every
101 +CHART apcupsd_${host}.time '' "UPS Time Remaining for ${host} on ${src}" "Minutes" ups apcupsd.time area $((apcupsd_priority + 2)) $apcupsd_update_every
102 DIMENSION time time absolute 1 100
103
104 EOF
105 + done
106 return 0
107 }
108
@@ -87,7 +115,10 @@ apcupsd_update() {
115 # for each dimension
116 # remember: KEEP IT SIMPLE AND SHORT
117
90 - apcupsd_get $apcupsd_ip $apcupsd_port | awk "
118 + local host working=0 failed=0
119 + for host in "${!apcupsd_sources[@]}"
120 + do
121 + apcupsd_get "${apcupsd_sources[${host}]}" | awk "
122
123 BEGIN {
124 battery_charge = 0;
@@ -116,43 +147,52 @@ BEGIN {
147 /^ITEMP.*/ { temp = \$3 * 100 };
148 /^TIMELEFT.*/ { time = \$3 * 100 };
149 END {
119 - print \"BEGIN apcupsd.charge $1\";
150 + print \"BEGIN apcupsd_${host}.charge $1\";
151 print \"SET battery_charge = \" battery_charge;
152 print \"END\"
153
123 - print \"BEGIN apcupsd.battery_voltage $1\";
154 + print \"BEGIN apcupsd_${host}.battery_voltage $1\";
155 print \"SET battery_voltage = \" battery_voltage;
156 print \"SET battery_voltage_nominal = \" battery_voltage_nominal;
157 print \"END\"
158
128 - print \"BEGIN apcupsd.input_voltage $1\";
159 + print \"BEGIN apcupsd_${host}.input_voltage $1\";
160 print \"SET input_voltage = \" input_voltage;
161 print \"SET input_voltage_min = \" input_voltage_min;
162 print \"SET input_voltage_max = \" input_voltage_max;
163 print \"END\"
164
134 - print \"BEGIN apcupsd.input_frequency $1\";
165 + print \"BEGIN apcupsd_${host}.input_frequency $1\";
166 print \"SET input_frequency = \" input_frequency;
167 print \"END\"
168
138 - print \"BEGIN apcupsd.output_voltage $1\";
169 + print \"BEGIN apcupsd_${host}.output_voltage $1\";
170 print \"SET output_voltage = \" output_voltage;
171 print \"SET output_voltage_nominal = \" output_voltage_nominal;
172 print \"END\"
173
143 - print \"BEGIN apcupsd.load $1\";
174 + print \"BEGIN apcupsd_${host}.load $1\";
175 print \"SET load = \" load;
176 print \"END\"
177
147 - print \"BEGIN apcupsd.temp $1\";
178 + print \"BEGIN apcupsd_${host}.temp $1\";
179 print \"SET temp = \" temp;
180 print \"END\"
181
151 - print \"BEGIN apcupsd.time $1\";
182 + print \"BEGIN apcupsd_${host}.time $1\";
183 print \"SET time = \" time;
184 print \"END\"
185 }"
155 - [ $? -ne 0 ] && error "failed to get values" && return 1
186 + if [ $? -ne 0 ]
187 + then
188 + failed=$((failed + 1))
189 + error "failed to get values for APC UPS ${host} on ${apcupsd_sources[${host}]}" && return 1
190 + else
191 + working=$((working + 1))
192 + fi
193 + done
194 +
195 + [ $working -eq 0 ] && error "failed to get values from all APC UPSes" && return 1
196
197 return 0
198 }
conf.d/charts.d/apcupsd.conf
+4 -2
@@ -5,8 +5,10 @@
5 # (C) 2016 Costa Tsaousis <costa@tsaousis.gr>
6 # GPL v3+
7
8 -#apcupsd_ip=127.0.0.1
9 -#apcupsd_port=3551
8 +# add all your APC UPSes in this array - uncomment it too
9 +#declare -A apcupsd_sources=(
10 +# ["local"]="127.0.0.1:3551"
11 +#)
12
13 # how long to wait for apcupsd to respond
14 #apcupsd_timeout=3