@cryptotaxi247 / netdata-1 / commits / 3dfda5c48

Lint tc-qos-helper.sh.in (#5163)

* lint tc-qos-helper script * run automatic formatter on tc-qos-helper script

Paweł Krupa committed Jan 14, 2019 at 11:09 UTC 3dfda5c48b501736cb16f3b5eab45a73a39b3a5a
1 file changed +145 -164
collectors/tc.plugin/tc-qos-helper.sh.in
+145 -164
@@ -12,46 +12,44 @@
12 export PATH="${PATH}:/sbin:/usr/sbin:/usr/local/sbin"
13 export LC_ALL=C
14
15 -
15 # -----------------------------------------------------------------------------
16 # logging functions
17
19 -PROGRAM_FILE="$0"
20 -PROGRAM_NAME="$(basename $0)"
21 -PROGRAM_NAME="${PROGRAM_NAME/.plugin}"
18 +PROGRAM_NAME="$(basename "$0")"
19 +PROGRAM_NAME="${PROGRAM_NAME/.plugin/}"
20
21 logdate() {
24 - date "+%Y-%m-%d %H:%M:%S"
22 + date "+%Y-%m-%d %H:%M:%S"
23 }
24
25 log() {
28 - local status="${1}"
29 - shift
26 + local status="${1}"
27 + shift
28
31 - echo >&2 "$(logdate): ${PROGRAM_NAME}: ${status}: ${*}"
29 + echo >&2 "$(logdate): ${PROGRAM_NAME}: ${status}: ${*}"
30
31 }
32
33 warning() {
36 - log WARNING "${@}"
34 + log WARNING "${@}"
35 }
36
37 error() {
40 - log ERROR "${@}"
38 + log ERROR "${@}"
39 }
40
41 info() {
44 - log INFO "${@}"
42 + log INFO "${@}"
43 }
44
45 fatal() {
48 - log FATAL "${@}"
49 - exit 1
46 + log FATAL "${@}"
47 + exit 1
48 }
49
50 debug=0
51 debug() {
54 - [ $debug -eq 1 ] && log DEBUG "${@}"
52 + [ $debug -eq 1 ] && log DEBUG "${@}"
53 }
54
55 # -----------------------------------------------------------------------------
@@ -60,59 +58,55 @@ debug() {
58 # the default
59 fireqos_run_dir="/var/run/fireqos"
60
63 -function realdir {
64 - local r="$1"
65 - local t=$(readlink "$r")
61 +function realdir() {
62 + local r
63 + local t
64 + r="$1"
65 + t="$(readlink "$r")"
66
67 - while [ "$t" ]
68 - do
69 - r=$(cd $(dirname "$r") && cd $(dirname "$t") && pwd -P)/$(basename "$t")
70 - t=$(readlink "$r")
71 - done
67 + while [ "$t" ]; do
68 + r=$(cd "$(dirname "$r")" && cd "$(dirname "$t")" && pwd -P)/$(basename "$t")
69 + t=$(readlink "$r")
70 + done
71
73 - dirname "$r"
72 + dirname "$r"
73 }
74
76 -if [ ! -d "${fireqos_run_dir}" ]
77 - then
78 -
79 - # the fireqos executable - we will use it to find its config
80 - fireqos="$(which fireqos 2>/dev/null || command -v fireqos 2>/dev/null)"
75 +if [ ! -d "${fireqos_run_dir}" ]; then
76
82 - if [ ! -z "${fireqos}" ]
83 - then
77 + # the fireqos executable - we will use it to find its config
78 + fireqos="$(command -v fireqos 2>/dev/null)"
79
85 - fireqos_exec_dir="$(realdir ${fireqos})"
80 + if [ -n "${fireqos}" ]; then
81
87 - if [ ! -z "${fireqos_exec_dir}" -a "${fireqos_exec_dir}" != "." -a -f "${fireqos_exec_dir}/install.config" ]
88 - then
82 + fireqos_exec_dir="$(realdir "${fireqos}")"
83
90 - LOCALSTATEDIR=
91 - source "${fireqos_exec_dir}/install.config"
84 + if [ -n "${fireqos_exec_dir}" ] && [ "${fireqos_exec_dir}" != "." ] && [ -f "${fireqos_exec_dir}/install.config" ]; then
85 + LOCALSTATEDIR=
86 + #shellcheck source=/dev/null
87 + source "${fireqos_exec_dir}/install.config"
88
93 - if [ -d "${LOCALSTATEDIR}/run/fireqos" ]
94 - then
95 - fireqos_run_dir="${LOCALSTATEDIR}/run/fireqos"
96 - else
97 - warning "FireQoS is installed as '${fireqos}', its installation config at '${fireqos_exec_dir}/install.config' specifies local state data at '${LOCALSTATEDIR}/run/fireqos', but this directory is not found or is not readable (check the permissions of its parents)."
98 - fi
99 - else
100 - warning "Although FireQoS is installed on this system as '${fireqos}', I cannot find/read its installation configuration at '${fireqos_exec_dir}/install.config'."
101 - fi
102 - else
103 - warning "FireQoS is not installed on this system. Use FireQoS to apply traffic QoS and expose the class names to netdata. Check https://github.com/netdata/netdata/tree/master/collectors/tc.plugin#tcplugin"
104 - fi
89 + if [ -d "${LOCALSTATEDIR}/run/fireqos" ]; then
90 + fireqos_run_dir="${LOCALSTATEDIR}/run/fireqos"
91 + else
92 + warning "FireQoS is installed as '${fireqos}', its installation config at '${fireqos_exec_dir}/install.config' specifies local state data at '${LOCALSTATEDIR}/run/fireqos', but this directory is not found or is not readable (check the permissions of its parents)."
93 + fi
94 + else
95 + warning "Although FireQoS is installed on this system as '${fireqos}', I cannot find/read its installation configuration at '${fireqos_exec_dir}/install.config'."
96 + fi
97 + else
98 + warning "FireQoS is not installed on this system. Use FireQoS to apply traffic QoS and expose the class names to netdata. Check https://github.com/netdata/netdata/tree/master/collectors/tc.plugin#tcplugin"
99 + fi
100 fi
101
102 # -----------------------------------------------------------------------------
103
104 [ -z "${NETDATA_PLUGINS_DIR}" ] && NETDATA_PLUGINS_DIR="$(dirname "${0}")"
110 -[ -z "${NETDATA_USER_CONFIG_DIR}" ] && NETDATA_USER_CONFIG_DIR="@configdir_POST@"
105 +[ -z "${NETDATA_USER_CONFIG_DIR}" ] && NETDATA_USER_CONFIG_DIR="@configdir_POST@"
106 [ -z "${NETDATA_STOCK_CONFIG_DIR}" ] && NETDATA_STOCK_CONFIG_DIR="@libconfigdir_POST@"
107
108 plugins_dir="${NETDATA_PLUGINS_DIR}"
114 -tc="$(which tc 2>/dev/null || command -v tc 2>/dev/null)"
115 -
109 +tc="$(command -v tc 2>/dev/null)"
110
111 # -----------------------------------------------------------------------------
112 # user configuration
@@ -126,7 +120,6 @@ qos_exit_every=3600
120 # what to use? classes or qdiscs?
121 tc_show="qdisc" # can also be "class"
122
129 -
123 # -----------------------------------------------------------------------------
124 # check if we have a valid number for interval
125
@@ -135,52 +128,46 @@ update_every=$((t))
128 [ $((update_every)) -lt 1 ] && update_every=${NETDATA_UPDATE_EVERY}
129 [ $((update_every)) -lt 1 ] && update_every=1
130
138 -
131 # -----------------------------------------------------------------------------
132 # allow the user to override our defaults
133
142 -for CONFIG in "${NETDATA_STOCK_CONFIG_DIR}/tc-qos-helper.conf" "${NETDATA_USER_CONFIG_DIR}/tc-qos-helper.conf"
143 -do
144 - if [ -f "${CONFIG}" ]
145 - then
146 - info "Loading config file '${CONFIG}'..."
147 - source "${CONFIG}"
148 - [ $? -ne 0 ] && error "Failed to load config file '${CONFIG}'."
149 - else
150 - warning "Cannot find file '${CONFIG}'."
151 - fi
134 +for CONFIG in "${NETDATA_STOCK_CONFIG_DIR}/tc-qos-helper.conf" "${NETDATA_USER_CONFIG_DIR}/tc-qos-helper.conf"; do
135 + if [ -f "${CONFIG}" ]; then
136 + info "Loading config file '${CONFIG}'..."
137 + #shellcheck source=/dev/null
138 + source "${CONFIG}" || error "Failed to load config file '${CONFIG}'."
139 + else
140 + warning "Cannot find file '${CONFIG}'."
141 + fi
142 done
143
144 case "${tc_show}" in
155 - qdisc|class)
156 - ;;
145 +qdisc | class) ;;
146
158 - *)
159 - error "tc_show variable can be either 'qdisc' or 'class' but is set to '${tc_show}'. Assuming it is 'qdisc'."
160 - tc_show="qdisc"
161 - ;;
147 +*)
148 + error "tc_show variable can be either 'qdisc' or 'class' but is set to '${tc_show}'. Assuming it is 'qdisc'."
149 + tc_show="qdisc"
150 + ;;
151 esac
152
164 -
153 # -----------------------------------------------------------------------------
154 # default sleep function
155
156 LOOPSLEEPMS_LASTWORK=0
157 loopsleepms() {
170 - sleep $1
158 + sleep "$1"
159 }
160
161 # if found and included, this file overwrites loopsleepms()
162 # with a high resolution timer function for precise looping.
163 +#shellcheck source=/dev/null
164 . "${plugins_dir}/loopsleepms.sh.inc"
165
177 -
166 # -----------------------------------------------------------------------------
167 # final checks we can run
168
181 -if [ -z "${tc}" -o ! -x "${tc}" ]
182 - then
183 - fatal "cannot find command 'tc' in this system."
169 +if [ -z "${tc}" ] || [ ! -x "${tc}" ]; then
170 + fatal "cannot find command 'tc' in this system."
171 fi
172
173 tc_devices=
@@ -189,94 +176,91 @@ fix_names=
176 # -----------------------------------------------------------------------------
177
178 setclassname() {
192 - if [ "${tc_show}" = "qdisc" ]
193 - then
194 - echo "SETCLASSNAME $4 $2"
195 - else
196 - echo "SETCLASSNAME $3 $2"
197 - fi
179 + if [ "${tc_show}" = "qdisc" ]; then
180 + echo "SETCLASSNAME $4 $2"
181 + else
182 + echo "SETCLASSNAME $3 $2"
183 + fi
184 }
185
186 show_tc_cls() {
201 - [ "${tc_show}" = "qdisc" ] && return 1
202 -
203 - local x="${1}"
204 -
205 - if [ -f /etc/iproute2/tc_cls ]
206 - then
207 - local classid name rest
208 - while read classid name rest
209 - do
210 - [ -z "${classid}" -o -z "${name}" -o "${classid}" = "#" -o "${name}" = "#" -o "${classid:0:1}" = "#" -o "${name:0:1}" = "#" ] && continue
211 - setclassname "" "${name}" "${classid}"
212 - done </etc/iproute2/tc_cls
213 - return 0
214 - fi
215 - return 1
187 + [ "${tc_show}" = "qdisc" ] && return 1
188 +
189 + local x="${1}"
190 +
191 + if [ -f /etc/iproute2/tc_cls ]; then
192 + local classid name rest
193 + while read -r classid name rest; do
194 + if [ -z "${classid}" ] ||
195 + [ -z "${name}" ] ||
196 + [ "${classid}" = "#" ] ||
197 + [ "${name}" = "#" ] ||
198 + [ "${classid:0:1}" = "#" ] ||
199 + [ "${name:0:1}" = "#" ]; then
200 + continue
201 + fi
202 + setclassname "" "${name}" "${classid}"
203 + done </etc/iproute2/tc_cls
204 + return 0
205 + fi
206 + return 1
207 }
208
209 show_fireqos_names() {
219 - local x="${1}" name n interface_dev interface_classes interface_classes_monitor
220 -
221 - if [ -f "${fireqos_run_dir}/ifaces/${x}" ]
222 - then
223 - name="$(<"${fireqos_run_dir}/ifaces/${x}")"
224 - echo "SETDEVICENAME ${name}"
225 -
226 - interface_dev=
227 - interface_classes=
228 - interface_classes_monitor=
229 - source "${fireqos_run_dir}/${name}.conf"
230 - for n in ${interface_classes_monitor}
231 - do
232 - setclassname ${n//|/ }
233 - done
234 - [ ! -z "${interface_dev}" ] && echo "SETDEVICEGROUP ${interface_dev}"
235 -
236 - return 0
237 - fi
238 -
239 - return 1
210 + local x="${1}" name n interface_dev interface_classes_monitor
211 +
212 + if [ -f "${fireqos_run_dir}/ifaces/${x}" ]; then
213 + name="$(<"${fireqos_run_dir}/ifaces/${x}")"
214 + echo "SETDEVICENAME ${name}"
215 +
216 + #shellcheck source=/dev/null
217 + source "${fireqos_run_dir}/${name}.conf"
218 + for n in ${interface_classes_monitor}; do
219 + setclassname "${n//|/ }"
220 + done
221 + [ -n "${interface_dev}" ] && echo "SETDEVICEGROUP ${interface_dev}"
222 +
223 + return 0
224 + fi
225 +
226 + return 1
227 }
228
229 show_tc() {
243 - local x="${1}"
230 + local x="${1}"
231
245 - echo "BEGIN ${x}"
232 + echo "BEGIN ${x}"
233
247 - # netdata can parse the output of tc
248 - ${tc} -s ${tc_show} show dev ${x}
234 + # netdata can parse the output of tc
235 + ${tc} -s ${tc_show} show dev "${x}"
236
250 - # check FireQOS names for classes
251 - if [ ! -z "${fix_names}" ]
252 - then
253 - show_fireqos_names "${x}" || show_tc_cls "${x}"
254 - fi
237 + # check FireQOS names for classes
238 + if [ -n "${fix_names}" ]; then
239 + show_fireqos_names "${x}" || show_tc_cls "${x}"
240 + fi
241
256 - echo "END ${x}"
242 + echo "END ${x}"
243 }
244
245 find_tc_devices() {
260 - local count=0 devs= dev rest l
261 -
262 - # find all the devices in the system
263 - # without forking
264 - while IFS=":| " read dev rest
265 - do
266 - count=$((count + 1))
267 - [ ${count} -le 2 ] && continue
268 - devs="${devs} ${dev}"
269 - done </proc/net/dev
270 -
271 - # from all the devices find the ones
272 - # that have QoS defined
273 - # unfortunately, one fork per device cannot be avoided
274 - tc_devices=
275 - for dev in ${devs}
276 - do
277 - l="$(${tc} class show dev ${dev} 2>/dev/null)"
278 - [ ! -z "${l}" ] && tc_devices="${tc_devices} ${dev}"
279 - done
246 + local count=0 devs dev rest l
247 +
248 + # find all the devices in the system
249 + # without forking
250 + while IFS=":| " read -r dev rest; do
251 + count=$((count + 1))
252 + [ ${count} -le 2 ] && continue
253 + devs="${devs} ${dev}"
254 + done </proc/net/dev
255 +
256 + # from all the devices find the ones
257 + # that have QoS defined
258 + # unfortunately, one fork per device cannot be avoided
259 + tc_devices=
260 + for dev in ${devs}; do
261 + l="$(${tc} class show dev "${dev}" 2>/dev/null)"
262 + [ -n "${l}" ] && tc_devices="${tc_devices} ${dev}"
263 + done
264 }
265
266 # update devices and class names
@@ -289,27 +273,24 @@ exit_after=$((qos_exit_every / update_every))
273
274 c=0
275 gc=0
292 -while [ 1 ]
293 -do
294 - fix_names=
295 - c=$((c + 1))
296 - gc=$((gc + 1))
276 +while true; do
277 + fix_names=
278 + c=$((c + 1))
279 + gc=$((gc + 1))
280
298 - if [ ${c} -le 1 -o ${c} -ge ${names_every} ]
299 - then
300 - c=1
301 - fix_names="YES"
302 - find_tc_devices
303 - fi
281 + if [ ${c} -le 1 ] || [ ${c} -ge ${names_every} ]; then
282 + c=1
283 + fix_names="YES"
284 + find_tc_devices
285 + fi
286
305 - for d in ${tc_devices}
306 - do
307 - show_tc ${d}
308 - done
287 + for d in ${tc_devices}; do
288 + show_tc "${d}"
289 + done
290
310 - echo "WORKTIME ${LOOPSLEEPMS_LASTWORK}"
291 + echo "WORKTIME ${LOOPSLEEPMS_LASTWORK}"
292
312 - loopsleepms ${update_every}
293 + loopsleepms ${update_every}
294
314 - [ ${gc} -gt ${exit_after} ] && exit 0
295 + [ ${gc} -gt ${exit_after} ] && exit 0
296 done