@cryptotaxi247 / netdata-1 / commits / a1b6a7f53

[cleanup crusade] Shellcheck (#4261)

* lint libreswan module * disable linting obsolete shell modules * shellcheck more shell modules * fix review; lint helper scripts * more shellcheck * fix SC2235 in apache.chart.sh * shellcheck last portion of charts.d scripts * final shellcheck; make travis blocking * fix cppcheck * fix indents; fix expansion * fix local var assignments

Paweł Krupa committed Sep 29, 2018 at 01:09 UTC a1b6a7f5328974aaddd7f631135c58e1d620c820
27 files changed +203 -150
.travis.yml
+1 -1
@@ -32,7 +32,7 @@ jobs:
32 script: docker run --rm -v $(pwd)/web:/code eeacms/csslint sh -c "csslint /code/*.css" || echo "OK"
33 - name: "bash"
34 install: skip
35 - script: docker run --rm --volume "$(pwd)":/project:ro --entrypoint sh koalaman/shellcheck-alpine:v0.5.0 -c 'for file in $(find /project/ -type f -name "*.sh" ! -path "*/makeself/*" ); do if ! shellcheck --format=gcc $file; then export FAILED=true; fi; done; if [ "$FAILED" != "" ]; then exit 1; fi' || echo "OK"
35 + script: docker run --rm --volume "$(pwd)":/project:ro --entrypoint sh koalaman/shellcheck-alpine:v0.5.0 -c 'for file in $(find /project/ -type f -name "*.sh" ! -path "*/makeself/*" ); do if ! shellcheck --format=gcc $file; then export FAILED=true; fi; done; if [ "$FAILED" != "" ]; then exit 1; fi'
36 - stage: "test"
37 name: "C"
38 install: sudo apt-get install -y libcap2-bin zlib1g-dev uuid-dev fakeroot libipmimonitoring-dev libmnl-dev libnetfilter-acct-dev
.travis/containerized_build.sh
+1 -1
@@ -2,6 +2,6 @@
2
3 set -e
4
5 -docker build -t dev-image -f .travis/images/Dockerfile.$1 .
5 +docker build -t dev-image -f ".travis/images/Dockerfile.$1" .
6
7 docker run -it -w /code dev-image ./netdata-installer.sh --dont-wait --dont-start-it --install /tmp
.travis/create_artifacts.sh
+4 -3
@@ -1,8 +1,9 @@
1 #!/bin/bash
2 +# shellcheck disable=SC2230
3
4 if [ ! -f .gitignore ]
5 then
5 - echo "Run as ./travis/$(basename $0) from top level directory of git repository"
6 + echo "Run as ./travis/$(basename "$0") from top level directory of git repository"
7 exit 1
8 fi
9
@@ -19,6 +20,6 @@ fakeroot ./packaging/git-build
20 python -c 'import os,sys,fcntl; flags = fcntl.fcntl(sys.stdout, fcntl.F_GETFL); fcntl.fcntl(sys.stdout, fcntl.F_SETFL, flags&~os.O_NONBLOCK);'
21 # make self-extractor
22 ./makeself/build-x86_64-static.sh
22 -for i in *.tar.gz; do sha512sum -b $i > $i.sha; done
23 -for i in *.gz.run; do sha512sum -b $i > $i.sha; done
23 +for i in *.tar.gz; do sha512sum -b "$i" > "$i.sha"; done
24 +for i in *.gz.run; do sha512sum -b "$i" > "$i.sha"; done
25 ./.travis/deploy-if-have-key
charts.d/apache.chart.sh
+4 -1
@@ -103,7 +103,10 @@ apache_get() {
103 ret=$?
104 IFS="${oIFS}"
105
106 - ([ $ret -ne 0 ] || [ "${#apache_response[@]}" -eq 0 ]) && return 1
106 + if [ $ret -ne 0 ] || [ "${#apache_response[@]}" -eq 0 ]
107 + then
108 + return 1
109 + fi
110
111 # the last line on the apache output is "Scoreboard"
112 # we use this label to detect that the output has a new word count
charts.d/cpu_apps.chart.sh
+1 -1
@@ -1,4 +1,4 @@
1 -# shellcheck shell=bash
1 +# shellcheck shell=bash disable=SC2154,SC1072,SC1073,SC2009,SC2162,SC2006,SC2002,SC2086,SC1117
2 # no need for shebang - this file is loaded from charts.d.plugin
3 # SPDX-License-Identifier: GPL-3.0+
4
charts.d/libreswan.chart.sh
+5 -3
@@ -1,4 +1,4 @@
1 -# shellcheck shell=bash
1 +# shellcheck shell=bash disable=SC1117
2 # no need for shebang - this file is loaded from charts.d.plugin
3 # SPDX-License-Identifier: GPL-3.0+
4
@@ -61,7 +61,8 @@ libreswan_get() {
61 libreswan_connected_tunnels=()
62
63 # convert the ipsec command output to a shell script
64 - # and source it to get the values
64 + # and source it to get the values
65 + # shellcheck disable=SC1090
66 source <(
67 {
68 libreswan_ipsec whack --status;
@@ -86,6 +87,7 @@ libreswan_check() {
87 require_cmd ipsec || return 1
88
89 # make sure it is libreswan
90 + # shellcheck disable=SC2143
91 if [ -z "$(ipsec --version | grep -i libreswan)" ]
92 then
93 error "ipsec command is not Libreswan. Disabling Libreswan plugin."
@@ -141,7 +143,7 @@ libreswan_update_one() {
143
144 [ -z "${id}" ] && libreswan_create_one "${name}"
145
144 - uptime=$(( ${libreswan_now} - ${libreswan_established_add_time[${n}]} ))
146 + uptime=$(( libreswan_now - libreswan_established_add_time[${n}] ))
147 [ ${uptime} -lt 0 ] && uptime=0
148
149 # write the result of the work.
charts.d/load_average.chart.sh
+1 -1
@@ -1,4 +1,4 @@
1 -# shellcheck shell=bash
1 +# shellcheck shell=bash disable=SC2154,SC1072,SC1073,SC2009,SC2162,SC2006,SC2002,SC2086,SC1117
2 # no need for shebang - this file is loaded from charts.d.plugin
3 # SPDX-License-Identifier: GPL-3.0+
4
charts.d/mem_apps.chart.sh
+1 -1
@@ -1,4 +1,4 @@
1 -# shellcheck shell=bash
1 +# shellcheck shell=bash disable=SC2154,SC1072,SC1073,SC2009,SC2162,SC2006,SC2002,SC2086,SC1117
2 # no need for shebang - this file is loaded from charts.d.plugin
3 # SPDX-License-Identifier: GPL-3.0+
4
charts.d/mysql.chart.sh
+15 -10
@@ -26,7 +26,8 @@ mysql_get() {
26 IFS=$'\t'$'\n'
27 #arr=($(run "${@}" -e "SHOW GLOBAL STATUS WHERE value REGEXP '^[0-9]';" | egrep "^(Bytes|Slow_|Que|Handl|Table|Selec|Sort_|Creat|Conne|Abort|Binlo|Threa|Innod|Qcach|Key_|Open)" ))
28 #arr=($(run "${@}" -N -e "SHOW GLOBAL STATUS;" | egrep "^(Bytes|Slow_|Que|Handl|Table|Selec|Sort_|Creat|Conne|Abort|Binlo|Threa|Innod|Qcach|Key_|Open)[^ ]+\s[0-9]" ))
29 - arr=($(run "${@}" -N -e "SHOW GLOBAL STATUS;" | egrep "^(Bytes|Slow_|Que|Handl|Table|Selec|Sort_|Creat|Conne|Abort|Binlo|Threa|Innod|Qcach|Key_|Open)[^[:space:]]+[[:space:]]+[0-9]+" ))
29 + # shellcheck disable=SC2207
30 + arr=($(run "${@}" -N -e "SHOW GLOBAL STATUS;" | grep -E "^(Bytes|Slow_|Que|Handl|Table|Selec|Sort_|Creat|Conne|Abort|Binlo|Threa|Innod|Qcach|Key_|Open)[^[:space:]]+[[:space:]]+[0-9]+" ))
31 IFS="${oIFS}"
32
33 [ "${#arr[@]}" -lt 3 ] && return 1
@@ -56,6 +57,7 @@ mysql_check() {
57 shift
58 fi
59
60 + # shellcheck disable=SC2230
61 [ -z "${mysql_cmd}" ] && mysql_cmd="$(which mysql 2>/dev/null || command -v mysql 2>/dev/null)"
62
63 if [ ${#mysql_opts[@]} -eq 0 ]
@@ -81,16 +83,18 @@ mysql_check() {
83 [ -z "${mysql_cmds[$m]}" ] && mysql_cmds[$m]="$mysql_cmd"
84 if [ -z "${mysql_cmds[$m]}" ]
85 then
84 - error "cannot get mysql command for '$m'. Please set mysql_cmds[$m]='/path/to/mysql', in $confd/mysql.conf"
86 + # shellcheck disable=SC2154
87 + error "cannot get mysql command for '${m}'. Please set mysql_cmds[$m]='/path/to/mysql', in $confd/mysql.conf"
88 fi
89
90 mysql_get "${mysql_cmds[$m]}" ${mysql_opts[$m]}
91 + # shellcheck disable=SC2181
92 if [ ! $? -eq 0 ]
93 then
94 error "cannot get global status for '$m'. Please set mysql_opts[$m]='options' to whatever needed to get connected to the mysql server, in $confd/mysql.conf"
91 - unset mysql_cmds[$m]
92 - unset mysql_opts[$m]
93 - unset mysql_ids[$m]
95 + unset "mysql_cmds[$m]"
96 + unset "mysql_opts[$m]"
97 + unset "mysql_ids[$m]"
98 continue
99 fi
100
@@ -99,7 +103,7 @@ mysql_check() {
103
104 if [ ${#mysql_opts[@]} -eq 0 ]
105 then
102 - if [ ${unconfigured} -eq 1 -a ${tryroot} -eq 0 ]
106 + if [ ${unconfigured} -eq 1 ] && [ ${tryroot} -eq 0 ]
107 then
108 mysql_check tryroot "${@}"
109 return $?
@@ -320,12 +324,13 @@ mysql_update() {
324 x="${mysql_ids[$m]}"
325 mysql_get "${mysql_cmds[$m]}" ${mysql_opts[$m]}
326
327 + # shellcheck disable=SC2181
328 if [ $? -ne 0 ]
329 then
325 - unset mysql_ids[$m]
326 - unset mysql_opts[$m]
327 - unset mysql_cmds[$m]
328 - error "failed to get values for '$m', disabling it."
330 + unset "mysql_ids[$m]"
331 + unset "mysql_opts[$m]"
332 + unset "mysql_cmds[$m]"
333 + error "failed to get values for '${m}', disabling it."
334 continue
335 fi
336
charts.d/nginx.chart.sh
+25 -23
@@ -27,20 +27,21 @@ nginx_reading=0
27 nginx_writing=0
28 nginx_waiting=0
29 nginx_get() {
30 + # shellcheck disable=SC2207
31 nginx_response=($(run curl -Ss ${nginx_curl_opts} "${nginx_url}"))
31 - [ $? -ne 0 -o "${#nginx_response[@]}" -eq 0 ] && return 1
32 -
33 - if [ "${nginx_response[0]}" != "Active" \
34 - -o "${nginx_response[1]}" != "connections:" \
35 - -o "${nginx_response[3]}" != "server" \
36 - -o "${nginx_response[4]}" != "accepts" \
37 - -o "${nginx_response[5]}" != "handled" \
38 - -o "${nginx_response[6]}" != "requests" \
39 - -o "${nginx_response[10]}" != "Reading:" \
40 - -o "${nginx_response[12]}" != "Writing:" \
41 - -o "${nginx_response[14]}" != "Waiting:" \
42 - ]
43 - then
32 + # shellcheck disable=SC2181
33 + if [ $? -ne 0 ] || [ "${#nginx_response[@]}" -eq 0 ]; then return 1; fi
34 +
35 + if [ "${nginx_response[0]}" != "Active" ] ||\
36 + [ "${nginx_response[1]}" != "connections:" ] ||\
37 + [ "${nginx_response[3]}" != "server" ] ||\
38 + [ "${nginx_response[4]}" != "accepts" ] ||\
39 + [ "${nginx_response[5]}" != "handled" ] ||\
40 + [ "${nginx_response[6]}" != "requests" ] ||\
41 + [ "${nginx_response[10]}" != "Reading:" ] ||\
42 + [ "${nginx_response[12]}" != "Writing:" ] ||\
43 + [ "${nginx_response[14]}" != "Waiting:" ]
44 + then
45 error "Invalid response from nginx server: ${nginx_response[*]}"
46 return 1
47 fi
@@ -53,15 +54,14 @@ nginx_get() {
54 nginx_writing="${nginx_response[13]}"
55 nginx_waiting="${nginx_response[15]}"
56
56 - if [ -z "${nginx_active_connections}" \
57 - -o -z "${nginx_accepts}" \
58 - -o -z "${nginx_handled}" \
59 - -o -z "${nginx_requests}" \
60 - -o -z "${nginx_reading}" \
61 - -o -z "${nginx_writing}" \
62 - -o -z "${nginx_waiting}" \
63 - ]
64 - then
57 + if [ -z "${nginx_active_connections}" ] ||\
58 + [ -z "${nginx_accepts}" ] ||\
59 + [ -z "${nginx_handled}" ] ||\
60 + [ -z "${nginx_requests}" ] ||\
61 + [ -z "${nginx_reading}" ] ||\
62 + [ -z "${nginx_writing}" ] ||\
63 + [ -z "${nginx_waiting}" ]
64 + then
65 error "empty values got from nginx server: ${nginx_response[*]}"
66 return 1
67 fi
@@ -73,8 +73,10 @@ nginx_get() {
73 nginx_check() {
74
75 nginx_get
76 + # shellcheck disable=2181
77 if [ $? -ne 0 ]
77 - then
78 + then
79 + # shellcheck disable=SC2154
80 error "cannot find stub_status on URL '${nginx_url}'. Please set nginx_url='http://nginx.server/stub_status' in $confd/nginx.conf"
81 return 1
82 fi
charts.d/nut.chart.sh
+11 -8
@@ -56,21 +56,23 @@ nut_check() {
56 for x in $nut_ups
57 do
58 nut_get "$x" >/dev/null
59 + # shellcheck disable=SC2181
60 if [ $? -eq 0 ]
61 + then
62 + if [ ! -z "${nut_names[${x}]}" ]
63 then
61 - if [ ! -z "${nut_names[${x}]}" ]
62 - then
63 - nut_ids[$x]="$( fixid "${nut_names[${x}]}" )"
64 - else
65 - nut_ids[$x]="$( fixid "$x" )"
66 - fi
64 + nut_ids[$x]="$( fixid "${nut_names[${x}]}" )"
65 + else
66 + nut_ids[$x]="$( fixid "$x" )"
67 + fi
68 continue
69 fi
70 error "cannot get information for NUT UPS '$x'."
71 done
72
73 if [ ${#nut_ids[@]} -eq 0 ]
73 - then
74 + then
75 + # shellcheck disable=SC2154
76 error "Cannot find UPSes - please set nut_ups='ups_name' in $confd/nut.conf"
77 return 1
78 fi
@@ -230,7 +232,8 @@ END {
232 print \"END\"
233 }
234 }"
233 - [ $? -ne 0 ] && unset nut_ids[$i] && error "failed to get values for '$i', disabling it."
235 + # shellcheck disable=2181
236 + [ $? -ne 0 ] && unset "nut_ids[$i]" && error "failed to get values for '$i', disabling it."
237 done
238
239 [ ${#nut_ids[@]} -eq 0 ] && error "no UPSes left active." && return 1
charts.d/opensips.chart.sh
+6 -3
@@ -1,4 +1,4 @@
1 -# shellcheck shell=bash
1 +# shellcheck shell=bash disable=SC1117,SC2154,SC2086
2 # no need for shebang - this file is loaded from charts.d.plugin
3 # SPDX-License-Identifier: GPL-3.0+
4
@@ -35,8 +35,10 @@ opensips_check() {
35 fi
36
37 # check once if the command works
38 - local x="$(opensips_get_stats | grep "^opensips_core_")"
39 - if [ ! $? -eq 0 -o -z "$x" ]
38 + local x
39 + x="$(opensips_get_stats | grep "^opensips_core_")"
40 + # shellcheck disable=SC2181
41 + if [ ! $? -eq 0 ] || [ -z "$x" ]
42 then
43 error "cannot get global status. Please set opensips_opts='options' whatever needed to get connected to opensips server, in $confd/opensips.conf"
44 return 1
@@ -219,6 +221,7 @@ opensips_update() {
221
222 opensips_command_failed=0
223 eval "local $(opensips_get_stats)"
224 + # shellcheck disable=SC2181
225 [ $? -ne 0 ] && return 1
226
227 [ $opensips_command_failed -eq 1 ] && error "failed to get values, disabling." && return 1
charts.d/phpfpm.chart.sh
+10 -7
@@ -37,8 +37,12 @@ phpfpm_slow_requests=0
37 phpfpm_get() {
38 local opts="${1}" url="${2}"
39
40 + # shellcheck disable=SC2207,2086
41 phpfpm_response=($(run curl -Ss ${opts} "${url}"))
41 - [ $? -ne 0 -o "${#phpfpm_response[@]}" -eq 0 ] && return 1
42 + # shellcheck disable=SC2181
43 + if [ $? -ne 0 ] || [ "${#phpfpm_response[@]}" -eq 0 ]; then
44 + return 1
45 + fi
46
47 if [[ "${phpfpm_response[0]}" != "pool:" \
48 || "${phpfpm_response[2]}" != "process" \
@@ -105,9 +109,11 @@ phpfpm_check() {
109 for m in "${!phpfpm_urls[@]}"
110 do
111 phpfpm_get "${phpfpm_curl_opts[$m]}" "${phpfpm_urls[$m]}"
112 + # shellcheck disable=SC2181
113 if [ $? -ne 0 ]; then
109 - error "cannot find status on URL '${phpfpm_url[$m]}'. Please set phpfpm_urls[$m]='http://localhost/status' in $confd/phpfpm.conf"
110 - unset phpfpm_urls[$m]
114 + # shellcheck disable=SC2154
115 + error "cannot find status on URL '${phpfpm_urls[$m]}'. Please set phpfpm_urls[$m]='http://localhost/status' in $confd/phpfpm.conf"
116 + unset "phpfpm_urls[$m]"
117 continue
118 fi
119 done
@@ -163,6 +169,7 @@ phpfpm_update() {
169 for m in "${!phpfpm_urls[@]}"
170 do
171 phpfpm_get "${phpfpm_curl_opts[$m]}" "${phpfpm_urls[$m]}"
172 + # shellcheck disable=SC2181
173 if [ $? -ne 0 ]; then
174 continue
175 fi
@@ -189,7 +196,3 @@ EOF
196
197 return 0
198 }
192 -
193 -phpfpm_check
194 -phpfpm_create
195 -phpfpm_update
charts.d/postfix.chart.sh
+7 -5
@@ -1,4 +1,4 @@
1 -# shellcheck shell=bash
1 +# shellcheck shell=bash disable=SC1117
2 # no need for shebang - this file is loaded from charts.d.plugin
3 # SPDX-License-Identifier: GPL-3.0+
4
@@ -22,13 +22,15 @@ postfix_check() {
22 # - 1 to disable the chart
23
24 # try to find the postqueue executable
25 - if [ -z "$postfix_postqueue" -o ! -x "$postfix_postqueue" ]
25 + if [ -z "$postfix_postqueue" ] || [ ! -x "$postfix_postqueue" ]
26 then
27 + # shellcheck disable=SC2230
28 postfix_postqueue="$(which postqueue 2>/dev/null || command -v postqueue 2>/dev/null)"
29 fi
30
30 - if [ -z "$postfix_postqueue" -o ! -x "$postfix_postqueue" ]
31 + if [ -z "$postfix_postqueue" ] || [ ! -x "$postfix_postqueue" ]
32 then
33 + # shellcheck disable=SC2154
34 error "cannot find postqueue. Please set 'postfix_postqueue=/path/to/postqueue' in $confd/postfix.conf"
35 return 1
36 fi
@@ -68,10 +70,10 @@ postfix_update() {
70 postfix_q_emails=0
71 postfix_q_size=0
72
71 - eval "$(run $postfix_postqueue -p |\
73 + eval "$(run "$postfix_postqueue" -p |\
74 grep "^--" |\
75 sed -e "s/-- \([0-9]\+\) Kbytes in \([0-9]\+\) Requests.$/local postfix_q_size=\1\nlocal postfix_q_emails=\2/g" |\
74 - egrep "^local postfix_q_(emails|size)=[0-9]+$")"
76 + grep -E "^local postfix_q_(emails|size)=[0-9]+$")"
77
78 # write the result of the work.
79 cat <<VALUESEOF
charts.d/sensors.chart.sh
+57 -53
@@ -33,13 +33,14 @@ sensors_priority=90000
33 declare -A sensors_excluded=()
34
35 sensors_find_all_files() {
36 - find $1 -maxdepth $sensors_sys_depth -name \*_input -o -name temp 2>/dev/null
36 + find "$1" -maxdepth $sensors_sys_depth -name \*_input -o -name temp 2>/dev/null
37 }
38
39 sensors_find_all_dirs() {
40 - sensors_find_all_files $1 | while read
40 + # shellcheck disable=SC2162
41 + sensors_find_all_files "$1" | while read
42 do
42 - dirname $REPLY
43 + dirname "$REPLY"
44 done | sort -u
45 }
46
@@ -50,7 +51,7 @@ sensors_check() {
51 # - 0 to enable the chart
52 # - 1 to disable the chart
53
53 - [ -z "$( sensors_find_all_files $sensors_sys_dir )" ] && error "no sensors found in '$sensors_sys_dir'." && return 1
54 + [ -z "$( sensors_find_all_files "$sensors_sys_dir" )" ] && error "no sensors found in '$sensors_sys_dir'." && return 1
55 return 0
56 }
57
@@ -58,15 +59,15 @@ sensors_check_files() {
59 # we only need sensors that report a non-zero value
60 # also remove not needed sensors
61
61 - local f= v= excluded=
62 - for f in $*
62 + local f v excluded
63 + for f in "$@"
64 do
65 [ ! -f "$f" ] && continue
65 - for ex in ${sensors_excluded[@]}; do
66 + for ex in "${sensors_excluded[@]}"; do
67 [[ $f =~ .*$ex$ ]] && excluded='1' && break
68 done
69
69 - [ "$excluded" != "1" ] && v="$( cat $f )" || v=0
70 + [ "$excluded" != "1" ] && v="$( cat "$f" )" || v=0
71 v=$(( v + 1 - 1 ))
72 [ $v -ne 0 ] && echo "$f" && continue
73 excluded=
@@ -79,14 +80,15 @@ sensors_check_temp_type() {
80 # valid temp types are 1 to 6
81 # disabled sensors have the value 0
82
82 - local f= t= v=
83 - for f in $*
83 + local f t v
84 + for f in "$@"
85 do
85 - t=$( echo $f | sed "s|_input$|_type|g" )
86 + # shellcheck disable=SC2001
87 + t=$( echo "$f" | sed "s|_input$|_type|g" )
88 [ "$f" = "$t" ] && echo "$f" && continue
89 [ ! -f "$t" ] && echo "$f" && continue
90
89 - v="$( cat $t )"
91 + v="$( cat "$t" )"
92 v=$(( v + 1 - 1 ))
93 [ $v -ne 0 ] && echo "$f" && continue
94
@@ -96,34 +98,34 @@ sensors_check_temp_type() {
98
99 # _create is called once, to create the charts
100 sensors_create() {
99 - local path= dir= name= x= file= lfile= labelname= labelid= device= subsystem= id= type= mode= files= multiplier= divisor=
101 + local path dir name x file lfile labelname device subsystem id type mode files multiplier divisor
102
103 # we create a script with the source of the
104 # sensors_update() function
105 # - the highest speed we can achieve -
104 - [ $sensors_source_update -eq 1 ] && echo >$TMP_DIR/sensors.sh "sensors_update() {"
106 + [ $sensors_source_update -eq 1 ] && echo >"$TMP_DIR/sensors.sh" "sensors_update() {"
107
106 - for path in $( sensors_find_all_dirs $sensors_sys_dir | sort -u )
108 + for path in $( sensors_find_all_dirs "$sensors_sys_dir" | sort -u )
109 do
108 - dir=$( basename $path )
110 + dir=$( basename "$path" )
111 device=
112 subsystem=
113 id=
114 type=
115 name=
116
115 - [ -h $path/device ] && device=$( readlink -f $path/device )
116 - [ ! -z "$device" ] && device=$( basename $device )
117 + [ -h "$path/device" ] && device=$( readlink -f "$path/device" )
118 + [ ! -z "$device" ] && device=$( basename "$device" )
119 [ -z "$device" ] && device="$dir"
120
119 - [ -h $path/subsystem ] && subsystem=$( readlink -f $path/subsystem )
120 - [ ! -z "$subsystem" ] && subsystem=$( basename $subsystem )
121 + [ -h "$path/subsystem" ] && subsystem=$( readlink -f "$path/subsystem" )
122 + [ ! -z "$subsystem" ] && subsystem=$( basename "$subsystem" )
123 [ -z "$subsystem" ] && subsystem="$dir"
124
123 - [ -f $path/name ] && name=$( cat $path/name )
125 + [ -f "$path/name" ] && name=$( cat "$path/name" )
126 [ -z "$name" ] && name="$dir"
127
126 - [ -f $path/type ] && type=$( cat $path/type )
128 + [ -f "$path/type" ] && type=$( cat "$path/type" )
129 [ -z "$type" ] && type="$dir"
130
131 id="$( fixid "$device.$subsystem.$dir" )"
@@ -139,66 +141,66 @@ sensors_create() {
141
142 case $mode in
143 temperature)
142 - files="$( ls $path/temp*_input 2>/dev/null; ls $path/temp 2>/dev/null )"
143 - files="$( sensors_check_files $files )"
144 - files="$( sensors_check_temp_type $files )"
144 + files="$( ls "$path"/temp*_input 2>/dev/null; ls "$path/temp" 2>/dev/null )"
145 + files="$( sensors_check_files "$files" )"
146 + files="$( sensors_check_temp_type "$files" )"
147 [ -z "$files" ] && continue
148 echo "CHART sensors.temp_$id '' '$name Temperature' 'Celsius' 'temperature' 'sensors.temp' line $((sensors_priority + 1)) $sensors_update_every"
147 - echo >>$TMP_DIR/sensors.sh "echo \"BEGIN sensors.temp_$id \$1\""
149 + echo >>"$TMP_DIR/sensors.sh" "echo \"BEGIN sensors.temp_$id \$1\""
150 divisor=1000
151 ;;
152
153 voltage)
152 - files="$( ls $path/in*_input 2>/dev/null )"
153 - files="$( sensors_check_files $files )"
154 + files="$( ls "$path"/in*_input 2>/dev/null )"
155 + files="$( sensors_check_files "$files" )"
156 [ -z "$files" ] && continue
157 echo "CHART sensors.volt_$id '' '$name Voltage' 'Volts' 'voltage' 'sensors.volt' line $((sensors_priority + 2)) $sensors_update_every"
156 - echo >>$TMP_DIR/sensors.sh "echo \"BEGIN sensors.volt_$id \$1\""
158 + echo >>"$TMP_DIR/sensors.sh" "echo \"BEGIN sensors.volt_$id \$1\""
159 divisor=1000
160 ;;
161
162 current)
161 - files="$( ls $path/curr*_input 2>/dev/null )"
162 - files="$( sensors_check_files $files )"
163 + files="$( ls "$path"/curr*_input 2>/dev/null )"
164 + files="$( sensors_check_files "$files" )"
165 [ -z "$files" ] && continue
166 echo "CHART sensors.curr_$id '' '$name Current' 'Ampere' 'current' 'sensors.curr' line $((sensors_priority + 3)) $sensors_update_every"
165 - echo >>$TMP_DIR/sensors.sh "echo \"BEGIN sensors.curr_$id \$1\""
167 + echo >>"$TMP_DIR/sensors.sh" "echo \"BEGIN sensors.curr_$id \$1\""
168 divisor=1000
169 ;;
170
171 power)
170 - files="$( ls $path/power*_input 2>/dev/null )"
171 - files="$( sensors_check_files $files )"
172 + files="$( ls "$path"/power*_input 2>/dev/null )"
173 + files="$( sensors_check_files "$files" )"
174 [ -z "$files" ] && continue
175 echo "CHART sensors.power_$id '' '$name Power' 'Watt' 'power' 'sensors.power' line $((sensors_priority + 4)) $sensors_update_every"
174 - echo >>$TMP_DIR/sensors.sh "echo \"BEGIN sensors.power_$id \$1\""
176 + echo >>"$TMP_DIR/sensors.sh" "echo \"BEGIN sensors.power_$id \$1\""
177 divisor=1000000
178 ;;
179
180 fans)
179 - files="$( ls $path/fan*_input 2>/dev/null )"
180 - files="$( sensors_check_files $files )"
181 + files="$( ls "$path"/fan*_input 2>/dev/null )"
182 + files="$( sensors_check_files "$files" )"
183 [ -z "$files" ] && continue
184 echo "CHART sensors.fan_$id '' '$name Fans Speed' 'Rotations / Minute' 'fans' 'sensors.fans' line $((sensors_priority + 5)) $sensors_update_every"
183 - echo >>$TMP_DIR/sensors.sh "echo \"BEGIN sensors.fan_$id \$1\""
185 + echo >>"$TMP_DIR/sensors.sh" "echo \"BEGIN sensors.fan_$id \$1\""
186 ;;
187
188 energy)
187 - files="$( ls $path/energy*_input 2>/dev/null )"
188 - files="$( sensors_check_files $files )"
189 + files="$( ls "$path"/energy*_input 2>/dev/null )"
190 + files="$( sensors_check_files "$files" )"
191 [ -z "$files" ] && continue
192 echo "CHART sensors.energy_$id '' '$name Energy' 'Joule' 'energy' 'sensors.energy' areastack $((sensors_priority + 6)) $sensors_update_every"
191 - echo >>$TMP_DIR/sensors.sh "echo \"BEGIN sensors.energy_$id \$1\""
193 + echo >>"$TMP_DIR/sensors.sh" "echo \"BEGIN sensors.energy_$id \$1\""
194 algorithm="incremental"
195 divisor=1000000
196 ;;
197
198 humidity)
197 - files="$( ls $path/humidity*_input 2>/dev/null )"
198 - files="$( sensors_check_files $files )"
199 + files="$( ls "$path"/humidity*_input 2>/dev/null )"
200 + files="$( sensors_check_files "$files" )"
201 [ -z "$files" ] && continue
202 echo "CHART sensors.humidity_$id '' '$name Humidity' 'Percent' 'humidity' 'sensors.humidity' line $((sensors_priority + 7)) $sensors_update_every"
201 - echo >>$TMP_DIR/sensors.sh "echo \"BEGIN sensors.humidity_$id \$1\""
203 + echo >>"$TMP_DIR/sensors.sh" "echo \"BEGIN sensors.humidity_$id \$1\""
204 divisor=1000
205 ;;
206
@@ -211,26 +213,27 @@ sensors_create() {
213 do
214 file="$x"
215 fid="$( fixid "$file" )"
214 - lfile="$( basename $file | sed "s|_input$|_label|g" )"
215 - labelname="$( basename $file | sed "s|_input$||g" )"
216 + lfile="$( basename "$file" | sed "s|_input$|_label|g" )"
217 + labelname="$( basename "$file" | sed "s|_input$||g" )"
218
217 - if [ ! "$path/$lfile" = "$file" -a -f "$path/$lfile" ]
218 - then
219 + if [ ! "$path/$lfile" = "$file" ] && [ -f "$path/$lfile" ]
220 + then
221 labelname="$( cat "$path/$lfile" )"
222 fi
223
224 echo "DIMENSION $fid '$labelname' $algorithm $multiplier $divisor"
223 - echo >>$TMP_DIR/sensors.sh "echo \"SET $fid = \"\$(< $file )"
225 + echo >>"$TMP_DIR/sensors.sh" "echo \"SET $fid = \"\$(< $file )"
226 done
227
226 - echo >>$TMP_DIR/sensors.sh "echo END"
228 + echo >>"$TMP_DIR/sensors.sh" "echo END"
229 done
230 done
231
230 - [ $sensors_source_update -eq 1 ] && echo >>$TMP_DIR/sensors.sh "}"
232 + [ $sensors_source_update -eq 1 ] && echo >>"$TMP_DIR/sensors.sh" "}"
233
234 # ok, load the function sensors_update() we created
233 - [ $sensors_source_update -eq 1 ] && . $TMP_DIR/sensors.sh
235 + # shellcheck source=/dev/null
236 + [ $sensors_source_update -eq 1 ] && . "$TMP_DIR/sensors.sh"
237
238 return 0
239 }
@@ -244,7 +247,8 @@ sensors_update() {
247 # for each dimension
248 # remember: KEEP IT SIMPLE AND SHORT
249
247 - [ $sensors_source_update -eq 0 ] && . $TMP_DIR/sensors.sh $1
250 + # shellcheck source=/dev/null
251 + [ $sensors_source_update -eq 0 ] && . "$TMP_DIR/sensors.sh" "$1"
252
253 return 0
254 }
charts.d/squid.chart.sh
+11 -9
@@ -1,4 +1,4 @@
1 -# shellcheck shell=bash
1 +# shellcheck shell=bash disable=SC2154
2 # no need for shebang - this file is loaded from charts.d.plugin
3 # SPDX-License-Identifier: GPL-3.0+
4
@@ -10,13 +10,12 @@
10 squid_host=
11 squid_port=
12 squid_url=
13 -squid_timeout=2
13 squid_update_every=2
14 squid_priority=60000
15
16 squid_get_stats_internal() {
17 local host="$1" port="$2" url="$3"
19 - run squidclient -h $host -p $port $url
18 + run squidclient -h "$host" -p "$port" "$url"
19 }
20
21 squid_get_stats() {
@@ -32,7 +31,7 @@ squid_autodetect() {
31 do
32 x=$(squid_get_stats_internal "$host" "$port" "$url" | grep client_http.requests)
33 if [ ! -z "$x" ]
35 - then
34 + then
35 squid_host="$host"
36 squid_port="$port"
37 squid_url="$url"
@@ -51,14 +50,16 @@ squid_check() {
50 require_cmd sed || return 1
51 require_cmd egrep || return 1
52
54 - if [ -z "$squid_host" -o -z "$squid_port" -o -z "$squid_url" ]
55 - then
53 + if [ -z "$squid_host" ] || [ -z "$squid_port" ] || [ -z "$squid_url" ]
54 + then
55 squid_autodetect || return 1
56 fi
57
58 # check once if the url works
60 - local x="$(squid_get_stats | grep client_http.requests)"
61 - if [ ! $? -eq 0 -o -z "$x" ]
59 + local x
60 + x="$(squid_get_stats | grep client_http.requests)"
61 + # shellcheck disable=SC2181
62 + if [ ! $? -eq 0 ] || [ -z "$x" ]
63 then
64 error "cannot fetch URL '$squid_url' by connecting to $squid_host:$squid_port. Please set squid_url='url' and squid_host='host' and squid_port='port' in $confd/squid.conf"
65 return 1
@@ -112,9 +113,10 @@ squid_update() {
113 # prepare the script and always grep at the end the lines that are usefull, so that
114 # even if something goes wrong, no other code can be executed
115
116 + # shellcheck disable=SC1117
117 eval "$(squid_get_stats |\
118 sed -e "s/ \+/ /g" -e "s/\./_/g" -e "s/^\([a-z0-9_]\+\) *= *\([0-9]\+\)$/local squid_\1=\2/g" |\
117 - egrep "^local squid_(client_http|server_all)_[a-z0-9_]+=[0-9]+$")"
119 + grep -E "^local squid_(client_http|server_all)_[a-z0-9_]+=[0-9]+$")"
120
121 # write the result of the work.
122 cat <<VALUESEOF
charts.d/tomcat.chart.sh
+3 -1
@@ -47,6 +47,7 @@ tomcat_check() {
47 fi
48 if [ -z "${tomcat_user}" ]; then
49 # check backwards compatibility
50 + # shellcheck disable=SC2154
51 if [ -z "${tomcatUser}" ]; then
52 error "tomcat user is unset or set to the empty string"
53 return 1
@@ -56,6 +57,7 @@ tomcat_check() {
57 fi
58 if [ -z "${tomcat_password}" ]; then
59 # check backwards compatibility
60 + # shellcheck disable=SC2154
61 if [ -z "${tomcatPassword}" ]; then
62 error "tomcat password is unset or set to the empty string"
63 return 1
@@ -66,6 +68,7 @@ tomcat_check() {
68
69 # check if we can get to tomcat's status page
70 tomcat_get
71 + # shellcheck disable=2181
72 if [ $? -ne 0 ]
73 then
74 error "cannot get to status page on URL '${tomcat_url}'. Please make sure tomcat url, username and password are correct."
@@ -117,7 +120,6 @@ EOF
120
121 # _update is called continuously, to collect the values
122 tomcat_update() {
120 - local reqs net
123 # the first argument to this function is the microseconds since last update
124 # pass this parameter to the BEGIN statement (see bellow).
125
coverity-scan.sh
+3
@@ -1,4 +1,5 @@
1 #!/usr/bin/env bash
2 +# shellcheck disable=SC2235
3
4 # To run this script you need to provide API token. This can be done either by:
5 # - Putting token in ".coverity-token" file
@@ -15,6 +16,7 @@ if [ -z "${token}" ]; then
16 exit 1
17 fi
18
19 +# shellcheck disable=SC2230
20 covbuild="$(which cov-build 2>/dev/null || command -v cov-build 2>/dev/null)"
21 ([ -z "${covbuild}" ] && [ -f .coverity-build ]) && covbuild="$(<.coverity-build)"
22 if [ -z "${covbuild}" ]; then
@@ -26,6 +28,7 @@ if [ -z "${covbuild}" ]; then
28 tar -x -C /tmp/coverity/ -f /tmp/coverity_tool.tar.gz
29 sudo mv /tmp/coverity/cov-analysis-linux64-2017.07 /opt/coverity
30 export PATH=${PATH}:/opt/coverity/bin/
31 + # shellcheck disable=SC2230
32 covbuild="$(which cov-build 2>/dev/null || command -v cov-build 2>/dev/null)"
33 else
34 echo "Save command the full filename of cov-build in .coverity-build"
cppcheck.sh
+2
@@ -2,6 +2,7 @@
2
3 # echo >>/tmp/cppcheck.log "cppcheck ${*}"
4
5 +# shellcheck disable=SC2230
6 cppcheck=$(which cppcheck 2>/dev/null || command -v cppcheck 2>/dev/null)
7 [ -z "${cppcheck}" ] && echo >&2 "install cppcheck." && exit 1
8
@@ -17,6 +18,7 @@ cd "${base}/src" || exit 1
18
19 file="${1}"
20 shift
21 +# shellcheck disable=SC2235
22 ([ "${file}" = "${base}" ] || [ -z "${file}" ]) && file="${base}/src"
23
24 "${cppcheck}" \
docker/build.sh
+1 -1
@@ -43,7 +43,7 @@ docker push "${REPOSITORY}:latest"
43 if [ "$REPOSITORY" != "netdata" ]; then
44 echo "$OLD_DOCKER_PASSWORD" | docker login -u "$OLD_DOCKER_USERNAME" --password-stdin
45 for ARCH in amd64 i386 armhf aarch64; do
46 - docker tag ${REPOSITORY}:${ARCH}${VERSION} firehol/netdata:${ARCH}${VERSION}
46 + docker tag "${REPOSITORY}:${ARCH}${VERSION}" "firehol/netdata:${ARCH}${VERSION}"
47 docker push "firehol/netdata:${ARCH}${VERSION}"
48 done
49 docker tag "${REPOSITORY}:latest" "firehol/netdata:latest"
installer/functions.sh
+4 -2
@@ -10,8 +10,8 @@
10 # checking the availability of commands
11
12 which_cmd() {
13 - which "${1}" 2>/dev/null || \
14 - command -v "${1}" 2>/dev/null
13 + # shellcheck disable=SC2230
14 + which "${1}" 2>/dev/null || command -v "${1}" 2>/dev/null
15 }
16
17 check_cmd() {
@@ -312,6 +312,7 @@ portable_add_user() {
312
313 echo >&2 "Adding ${username} user account with home ${homedir} ..."
314
315 + # shellcheck disable=SC2230
316 local nologin="$(which nologin 2>/dev/null || command -v nologin 2>/dev/null || echo '/bin/false')"
317
318 # Linux
@@ -444,6 +445,7 @@ issystemd() {
445 [ ! -d /lib/systemd/system ] && return 1
446
447 # if there is no systemctl command, it is not systemd
448 + # shellcheck disable=SC2230
449 systemctl=$(which systemctl 2>/dev/null || command -v systemctl 2>/dev/null)
450 [ -z "${systemctl}" -o ! -x "${systemctl}" ] && return 1
451
kickstart-static64.sh
+3 -2
@@ -5,14 +5,15 @@
5 umask 022
6
7 # make sure UID is set
8 +# shellcheck disable=SC2155
9 [ -z "${UID}" ] && export UID="$(id -u)"
10
11 # ---------------------------------------------------------------------------------------------------------------------
12 # library functions copied from installer/functions.sh
13
14 which_cmd() {
14 - which "${1}" 2>/dev/null || \
15 - command -v "${1}" 2>/dev/null
15 + # shellcheck disable=SC2230
16 + which "${1}" 2>/dev/null || command -v "${1}" 2>/dev/null
17 }
18
19 check_cmd() {
kickstart.sh
+2 -2
@@ -34,8 +34,8 @@ umask 022
34 # library functions copied from installer/functions.sh
35
36 which_cmd() {
37 - which "${1}" 2>/dev/null || \
38 - command -v "${1}" 2>/dev/null
37 + # shellcheck disable=SC2230
38 + which "${1}" 2>/dev/null || command -v "${1}" 2>/dev/null
39 }
40
41 check_cmd() {
netdata-installer.sh
+2
@@ -83,6 +83,7 @@ REINSTALL_COMMAND="${REINSTALL_COMMAND// --dont-wait/}"
83 REINSTALL_COMMAND="${REINSTALL_COMMAND// --dont-start-it/}"
84 [ "${REINSTALL_COMMAND:0:1}" != "." -a "${REINSTALL_COMMAND:0:1}" != "/" -a -f "./${0}" ] && REINSTALL_COMMAND="./${REINSTALL_COMMAND}"
85
86 +# shellcheck disable=SC2230
87 setcap="$(which setcap 2>/dev/null || command -v setcap 2>/dev/null)"
88
89 ME="$0"
@@ -172,6 +173,7 @@ For the plugins, you will at least need:
173 USAGE
174 }
175
176 +# shellcheck disable=SC2230
177 md5sum="$(which md5sum 2>/dev/null || command -v md5sum 2>/dev/null || command -v md5 2>/dev/null)"
178 get_git_config_signatures() {
179 local x s file md5
plugins.d/alarm-test.sh
+1 -1
@@ -8,5 +8,5 @@
8 # Script to test alarm notifications for netdata
9
10 dir="$(dirname "${0}")"
11 -${dir}/alarm-notify.sh test "${1}"
11 +"${dir}/alarm-notify.sh" test "${1}"
12 exit $?
plugins.d/cgroup-network-helper.sh
+16 -9
@@ -1,4 +1,5 @@
1 #!/usr/bin/env bash
2 +# shellcheck disable=SC1117
3
4 # cgroup-network-helper.sh
5 # detect container and virtual machine interfaces
@@ -23,6 +24,7 @@
24 # -----------------------------------------------------------------------------
25
26 # the system path is cleared by cgroup-network
27 +# shellcheck source=/dev/null
28 [ -f /etc/profile ] && source /etc/profile
29
30 export LC_ALL=C
@@ -66,7 +68,7 @@ debug() {
68 # -----------------------------------------------------------------------------
69 # check for BASH v4+ (required for associative arrays)
70
69 -[ $(( ${BASH_VERSINFO[0]} )) -lt 4 ] && \
71 +[ $(( BASH_VERSINFO[0] )) -lt 4 ] && \
72 fatal "BASH version 4 or later is required (this is ${BASH_VERSION})."
73
74 # -----------------------------------------------------------------------------
@@ -86,7 +88,7 @@ do
88 shift
89 done
90
89 -if [ -z "${pid}" -a -z "${cgroup}" ]
91 +if [ -z "${pid}" ] && [ -z "${cgroup}" ]
92 then
93 fatal "Either --pid or --cgroup is required"
94 fi
@@ -115,7 +117,7 @@ proc_pid_fdinfo_iff() {
117
118 debug "Searching for tun/tap interfaces for pid ${p}..."
119 set_source "fdinfo"
118 - grep ^iff:.* "${NETDATA_HOST_PREFIX}/proc/${p}/fdinfo"/* 2>/dev/null | cut -f 2
120 + grep "^iff:.*" "${NETDATA_HOST_PREFIX}/proc/${p}/fdinfo"/* 2>/dev/null | cut -f 2
121 }
122
123 find_tun_tap_interfaces_for_cgroup() {
@@ -128,7 +130,7 @@ find_tun_tap_interfaces_for_cgroup() {
130 local p
131 for p in $(< "${c}/emulator/cgroup.procs" )
132 do
131 - proc_pid_fdinfo_iff ${p}
133 + proc_pid_fdinfo_iff "${p}"
134 done
135 fi
136 }
@@ -154,11 +156,14 @@ virsh_find_all_interfaces_for_cgroup() {
156 local c="${1}" # the cgroup path
157
158 # the virsh command
157 - local virsh="$(which virsh 2>/dev/null || command -v virsh 2>/dev/null)"
159 + local virsh
160 + # shellcheck disable=SC2230
161 + virsh="$(which virsh 2>/dev/null || command -v virsh 2>/dev/null)"
162
163 if [ ! -z "${virsh}" ]
164 then
161 - local d="$(virsh_cgroup_to_domain_name "${c}")"
165 + local d
166 + d="$(virsh_cgroup_to_domain_name "${c}")"
167
168 if [ ! -z "${d}" ]
169 then
@@ -167,7 +172,7 @@ virsh_find_all_interfaces_for_cgroup() {
172 # match only 'network' interfaces from virsh output
173
174 set_source "virsh"
170 - "${virsh}" -r domiflist ${d} |\
175 + "${virsh}" -r domiflist "${d}" |\
176 sed -n \
177 -e "s|^\([^[:space:]]\+\)[[:space:]]\+network[[:space:]]\+\([^[:space:]]\+\)[[:space:]]\+[^[:space:]]\+[[:space:]]\+[^[:space:]]\+$|\1 \1_\2|p" \
178 -e "s|^\([^[:space:]]\+\)[[:space:]]\+bridge[[:space:]]\+\([^[:space:]]\+\)[[:space:]]\+[^[:space:]]\+[[:space:]]\+[^[:space:]]\+$|\1 \1_\2|p"
@@ -188,7 +193,7 @@ find_all_interfaces_of_pid_or_cgroup() {
193 then
194 # we have been called with a pid
195
191 - proc_pid_fdinfo_iff ${p}
196 + proc_pid_fdinfo_iff "${p}"
197
198 elif [ ! -z "${c}" ]
199 then
@@ -219,6 +224,7 @@ declare -A devs=()
224 # store all interfaces found in the associative array
225 # this will also give the unique devices, as seen by the host
226 last_src=
227 +# shellcheck disable=SC2162
228 while read host_device guest_device
229 do
230 [ -z "${host_device}" ] && continue
@@ -231,8 +237,9 @@ do
237 # when we run in debug, show the source
238 debug "Found host device '${host_device}', guest device '${guest_device}', detected via '${last_src}'"
239
234 - [ -z "${devs[${host_device}]}" -o "${devs[${host_device}]}" = "${host_device}" ] && \
240 + if [ -z "${devs[${host_device}]}" ] || [ "${devs[${host_device}]}" = "${host_device}" ]; then
241 devs[${host_device}]="${guest_device}"
242 + fi
243
244 done < <( find_all_interfaces_of_pid_or_cgroup "${pid}" "${cgroup}" )
245
plugins.d/charts.d.dryrun-helper.sh
+6 -2
@@ -1,6 +1,8 @@
1 #!/usr/bin/env bash
2 # SPDX-License-Identifier: GPL-3.0+
3
4 +# shellcheck disable=SC2181
5 +
6 # will stop the script for any error
7 set -e
8
@@ -11,8 +13,8 @@ conf="$3"
13
14 can_diff=1
15
14 -tmp1="`mktemp`"
15 -tmp2="`mktemp`"
16 +tmp1="$(mktemp)"
17 +tmp2="$(mktemp)"
18
19 myset() {
20 set | grep -v "^_=" | grep -v "^PIPESTATUS=" | grep -v "^BASH_LINENO="
@@ -37,6 +39,7 @@ myset >"$tmp1"
39 # include the plugin and its config
40 if [ -f "$conf" ]
41 then
42 + # shellcheck source=/dev/null
43 . "$conf"
44 if [ $? -ne 0 ]
45 then
@@ -46,6 +49,7 @@ then
49 fi
50 fi
51
52 +# shellcheck source=/dev/null
53 . "$chart"
54 if [ $? -ne 0 ]
55 then