]> arthur.barton.de Git - netdata.git/blob - charts.d/nut.chart.sh
Merge pull request #2021 from ktsaou/master
[netdata.git] / charts.d / nut.chart.sh
1 # no need for shebang - this file is loaded from charts.d.plugin
2
3 # netdata
4 # real-time performance and health monitoring, done right!
5 # (C) 2016-2017 Costa Tsaousis <costa@tsaousis.gr>
6 # GPL v3+
7 #
8
9 # a space separated list of UPS names
10 # if empty, the list returned by 'upsc -l' will be used
11 nut_ups=
12
13 # how frequently to collect UPS data
14 nut_update_every=2
15
16 # how much time in seconds, to wait for nut to respond
17 nut_timeout=2
18
19 # set this to 1, to enable another chart showing the number
20 # of UPS clients connected to upsd
21 nut_clients_chart=0
22
23 # the priority of nut related to other charts
24 nut_priority=90000
25
26 declare -A nut_ids=()
27
28 nut_get_all() {
29         run -t $nut_timeout upsc -l
30 }
31
32 nut_get() {
33         run -t $nut_timeout upsc "$1"
34
35         if [ "${nut_clients_chart}" -eq "1" ]
36                 then
37                 printf "ups.connected_clients: "
38                 run -t $nut_timeout upsc -c "$1" | wc -l
39         fi
40 }
41
42 nut_check() {
43
44         # this should return:
45         #  - 0 to enable the chart
46         #  - 1 to disable the chart
47
48         local x
49
50         require_cmd upsc || return 1
51
52         [ -z "$nut_ups" ] && nut_ups="$( nut_get_all )"
53
54         for x in $nut_ups
55         do
56                 nut_get "$x" >/dev/null
57                 if [ $? -eq 0 ]
58                         then
59                         nut_ids[$x]="$( fixid "$x" )"
60                         continue
61                 fi
62                 error "cannot get information for NUT UPS '$x'."
63         done
64
65         if [ ${#nut_ids[@]} -eq 0 ]
66                 then
67                 error "Cannot find UPSes - please set nut_ups='ups_name' in $confd/nut.conf"
68                 return 1
69         fi
70
71         return 0
72 }
73
74 nut_create() {
75         # create the charts
76         local x
77
78         for x in "${nut_ids[@]}"
79         do
80                 cat <<EOF
81 CHART nut_$x.charge '' "UPS Charge" "percentage" ups nut.charge area $((nut_priority + 1)) $nut_update_every
82 DIMENSION battery_charge charge absolute 1 100
83
84 CHART nut_$x.battery_voltage '' "UPS Battery Voltage" "Volts" ups nut.battery.voltage line $((nut_priority + 2)) $nut_update_every
85 DIMENSION battery_voltage voltage absolute 1 100
86 DIMENSION battery_voltage_high high absolute 1 100
87 DIMENSION battery_voltage_low low absolute 1 100
88 DIMENSION battery_voltage_nominal nominal absolute 1 100
89
90 CHART nut_$x.input_voltage '' "UPS Input Voltage" "Volts" input nut.input.voltage line $((nut_priority + 3)) $nut_update_every
91 DIMENSION input_voltage voltage absolute 1 100
92 DIMENSION input_voltage_fault fault absolute 1 100
93 DIMENSION input_voltage_nominal nominal absolute 1 100
94
95 CHART nut_$x.input_current '' "UPS Input Current" "Ampere" input nut.input.current line $((nut_priority + 4)) $nut_update_every
96 DIMENSION input_current_nominal nominal absolute 1 100
97
98 CHART nut_$x.input_frequency '' "UPS Input Frequency" "Hz" input nut.input.frequency line $((nut_priority + 5)) $nut_update_every
99 DIMENSION input_frequency frequency absolute 1 100
100 DIMENSION input_frequency_nominal nominal absolute 1 100
101
102 CHART nut_$x.output_voltage '' "UPS Output Voltage" "Volts" output nut.output.voltage line $((nut_priority + 6)) $nut_update_every
103 DIMENSION output_voltage voltage absolute 1 100
104
105 CHART nut_$x.load '' "UPS Load" "percentage" ups nut.load area $((nut_priority)) $nut_update_every
106 DIMENSION load load absolute 1 100
107
108 CHART nut_$x.temp '' "UPS Temperature" "temperature" ups nut.temperature line $((nut_priority + 7)) $nut_update_every
109 DIMENSION temp temp absolute 1 100
110 EOF
111
112         if [ "${nut_clients_chart}" = "1" ]
113                 then
114                 cat <<EOF2
115 CHART nut_$x.clients '' "UPS Connected Clients" "clients" ups nut.clients area $((nut_priority + 8)) $nut_update_every
116 DIMENSION clients '' absolute 1 1
117 EOF2
118         fi
119
120         done
121
122         return 0
123 }
124
125
126 nut_update() {
127         # the first argument to this function is the microseconds since last update
128         # pass this parameter to the BEGIN statement (see bellow).
129
130         # do all the work to collect / calculate the values
131         # for each dimension
132         # remember: KEEP IT SIMPLE AND SHORT
133
134         local i x
135         for i in "${!nut_ids[@]}"
136         do
137                 x="${nut_ids[$i]}"
138                 nut_get "$i" | awk "
139 BEGIN {
140         battery_charge = 0;
141         battery_voltage = 0;
142         battery_voltage_high = 0;
143         battery_voltage_low = 0;
144         battery_voltage_nominal = 0;
145         input_voltage = 0;
146         input_voltage_fault = 0;
147         input_voltage_nominal = 0;
148         input_current_nominal = 0;
149         input_frequency = 0;
150         input_frequency_nominal = 0;
151         output_voltage = 0;
152         load = 0;
153         temp = 0;
154         client = 0;
155         do_clients = ${nut_clients_chart};
156 }
157 /^battery.charge: .*/                   { battery_charge = \$2 * 100 };
158 /^battery.voltage: .*/                  { battery_voltage = \$2 * 100 };
159 /^battery.voltage.high: .*/             { battery_voltage_high = \$2 * 100 };
160 /^battery.voltage.low: .*/              { battery_voltage_low = \$2 * 100 };
161 /^battery.voltage.nominal: .*/  { battery_voltage_nominal = \$2 * 100 };
162 /^input.voltage: .*/                    { input_voltage = \$2 * 100 };
163 /^input.voltage.fault: .*/              { input_voltage_fault = \$2 * 100 };
164 /^input.voltage.nominal: .*/    { input_voltage_nominal = \$2 * 100 };
165 /^input.current.nominal: .*/    { input_current_nominal = \$2 * 100 };
166 /^input.frequency: .*/                  { input_frequency = \$2 * 100 };
167 /^input.frequency.nominal: .*/  { input_frequency_nominal = \$2 * 100 };
168 /^output.voltage: .*/                   { output_voltage = \$2 * 100 };
169 /^ups.load: .*/                                 { load = \$2 * 100 };
170 /^ups.temperature: .*/                  { temp = \$2 * 100 };
171 /^ups.connected_clients: .*/    { clients = \$2 };
172 END {
173         print \"BEGIN nut_$x.charge $1\";
174         print \"SET battery_charge = \" battery_charge;
175         print \"END\"
176
177         print \"BEGIN nut_$x.battery_voltage $1\";
178         print \"SET battery_voltage = \" battery_voltage;
179         print \"SET battery_voltage_high = \" battery_voltage_high;
180         print \"SET battery_voltage_low = \" battery_voltage_low;
181         print \"SET battery_voltage_nominal = \" battery_voltage_nominal;
182         print \"END\"
183
184         print \"BEGIN nut_$x.input_voltage $1\";
185         print \"SET input_voltage = \" input_voltage;
186         print \"SET input_voltage_fault = \" input_voltage_fault;
187         print \"SET input_voltage_nominal = \" input_voltage_nominal;
188         print \"END\"
189
190         print \"BEGIN nut_$x.input_current $1\";
191         print \"SET input_current_nominal = \" input_current_nominal;
192         print \"END\"
193
194         print \"BEGIN nut_$x.input_frequency $1\";
195         print \"SET input_frequency = \" input_frequency;
196         print \"SET input_frequency_nominal = \" input_frequency_nominal;
197         print \"END\"
198
199         print \"BEGIN nut_$x.output_voltage $1\";
200         print \"SET output_voltage = \" output_voltage;
201         print \"END\"
202
203         print \"BEGIN nut_$x.load $1\";
204         print \"SET load = \" load;
205         print \"END\"
206
207         print \"BEGIN nut_$x.temp $1\";
208         print \"SET temp = \" temp;
209         print \"END\"
210
211         if(do_clients) {
212                 print \"BEGIN nut_$x.clients $1\";
213                 print \"SET clients = \" clients;
214                 print \"END\"
215         }
216 }"
217                 [ $? -ne 0 ] && unset nut_ids[$i] && error "failed to get values for '$i', disabling it."
218         done
219
220         [ ${#nut_ids[@]} -eq 0 ] && error "no UPSes left active." && return 1
221         return 0
222 }