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