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