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