@cryptotaxi247 / netdata-1 / commits / d0d109e57

lint all shell collectors code (#4690)

* run shfmt on all shell collector files * escape dollar sign

Paweł Krupa committed Nov 26, 2018 at 11:54 UTC d0d109e579befc9a586ef7ff3fa8f4f692c989d8
23 files changed +973 -1155
collectors/charts.d.plugin/ap/ap.chart.sh
+44 -47
@@ -56,8 +56,7 @@ ap_check() {
56 ap_create() {
57 local ssid dev
58
59 - for dev in "${!ap_devs[@]}"
60 - do
59 + for dev in "${!ap_devs[@]}"; do
60 ssid="${ap_devs[${dev}]}"
61
62 # create the chart with 3 dimensions
@@ -99,14 +98,13 @@ ap_update() {
98 # for each dimension
99 # remember: KEEP IT SIMPLE AND SHORT
100
102 - for dev in "${!ap_devs[@]}"
103 - do
104 - echo
105 - echo "DEVICE ${dev}"
101 + for dev in "${!ap_devs[@]}"; do
102 + echo
103 + echo "DEVICE ${dev}"
104 iw "${dev}" station dump
107 - done | awk "
105 + done | awk '
106 function zero_data() {
109 - dev = \"\";
107 + dev = "";
108 c = 0;
109 rb = 0;
110 tb = 0;
@@ -121,32 +119,32 @@ ap_update() {
119 e = 0;
120 }
121 function print_device() {
124 - if(dev != \"\" && length(dev) > 0) {
125 - print \"BEGIN ap_clients.\" dev;
126 - print \"SET clients = \" c;
127 - print \"END\";
128 - print \"BEGIN ap_bandwidth.\" dev;
129 - print \"SET received = \" rb;
130 - print \"SET sent = \" tb;
131 - print \"END\";
132 - print \"BEGIN ap_packets.\" dev;
133 - print \"SET received = \" rp;
134 - print \"SET sent = \" tp;
135 - print \"END\";
136 - print \"BEGIN ap_issues.\" dev;
137 - print \"SET retries = \" tr;
138 - print \"SET failures = \" tf;
139 - print \"END\";
122 + if(dev != "" && length(dev) > 0) {
123 + print "BEGIN ap_clients." dev;
124 + print "SET clients = " c;
125 + print "END";
126 + print "BEGIN ap_bandwidth." dev;
127 + print "SET received = " rb;
128 + print "SET sent = " tb;
129 + print "END";
130 + print "BEGIN ap_packets." dev;
131 + print "SET received = " rp;
132 + print "SET sent = " tp;
133 + print "END";
134 + print "BEGIN ap_issues." dev;
135 + print "SET retries = " tr;
136 + print "SET failures = " tf;
137 + print "END";
138
139 if( c == 0 ) c = 1;
142 - print \"BEGIN ap_signal.\" dev;
143 - print \"SET signal = \" int(s / c);
144 - print \"END\";
145 - print \"BEGIN ap_bitrate.\" dev;
146 - print \"SET receive = \" int(rt / c);
147 - print \"SET transmit = \" int(tt / c);
148 - print \"SET expected = \" int(e / c);
149 - print \"END\";
140 + print "BEGIN ap_signal." dev;
141 + print "SET signal = " int(s / c);
142 + print "END";
143 + print "BEGIN ap_bitrate." dev;
144 + print "SET receive = " int(rt / c);
145 + print "SET transmit = " int(tt / c);
146 + print "SET expected = " int(e / c);
147 + print "END";
148 }
149 zero_data();
150 }
@@ -155,28 +153,27 @@ ap_update() {
153 }
154 /^DEVICE / {
155 print_device();
158 - dev = \$2;
156 + dev = $2;
157 }
158 /^Station/ { c++; }
161 - /^[ \\t]+rx bytes:/ { rb += \$3; }
162 - /^[ \\t]+tx bytes:/ { tb += \$3; }
163 - /^[ \\t]+rx packets:/ { rp += \$3; }
164 - /^[ \\t]+tx packets:/ { tp += \$3; }
165 - /^[ \\t]+tx retries:/ { tr += \$3; }
166 - /^[ \\t]+tx failed:/ { tf += \$3; }
167 - /^[ \\t]+signal:/ { x = \$2; s += x * 1000; }
168 - /^[ \\t]+rx bitrate:/ { x = \$3; rt += x * 1000; }
169 - /^[ \\t]+tx bitrate:/ { x = \$3; tt += x * 1000; }
170 - /^[ \\t]+expected throughput:(.*)Mbps/ {
171 - x=\$3;
172 - sub(/Mbps/, \"\", x);
159 + /^[ \t]+rx bytes:/ { rb += $3; }
160 + /^[ \t]+tx bytes:/ { tb += $3; }
161 + /^[ \t]+rx packets:/ { rp += $3; }
162 + /^[ \t]+tx packets:/ { tp += $3; }
163 + /^[ \t]+tx retries:/ { tr += $3; }
164 + /^[ \t]+tx failed:/ { tf += $3; }
165 + /^[ \t]+signal:/ { x = $2; s += x * 1000; }
166 + /^[ \t]+rx bitrate:/ { x = $3; rt += x * 1000; }
167 + /^[ \t]+tx bitrate:/ { x = $3; tt += x * 1000; }
168 + /^[ \t]+expected throughput:(.*)Mbps/ {
169 + x=$3;
170 + sub(/Mbps/, "", x);
171 e += x * 1000;
172 }
173 END {
174 print_device();
175 }
178 - "
176 + '
177
178 return 0
179 }
182 -
collectors/charts.d.plugin/apache/apache.chart.sh
+36 -43
@@ -52,21 +52,20 @@ apache_key_connsasynckeepalive=
52 apache_key_connsasyncclosing=
53 apache_detect() {
54 local i=0
55 - for x in "${@}"
56 - do
55 + for x in "${@}"; do
56 case "${x}" in
58 - 'Total Accesses') apache_key_accesses=$((i + 1)) ;;
59 - 'Total kBytes') apache_key_kbytes=$((i + 1)) ;;
60 - 'ReqPerSec') apache_key_reqpersec=$((i + 1)) ;;
61 - 'BytesPerSec') apache_key_bytespersec=$((i + 1)) ;;
62 - 'BytesPerReq') apache_key_bytesperreq=$((i + 1)) ;;
63 - 'BusyWorkers') apache_key_busyworkers=$((i + 1)) ;;
64 - 'IdleWorkers') apache_key_idleworkers=$((i + 1));;
65 - 'ConnsTotal') apache_key_connstotal=$((i + 1)) ;;
66 - 'ConnsAsyncWriting') apache_key_connsasyncwriting=$((i + 1)) ;;
67 - 'ConnsAsyncKeepAlive') apache_key_connsasynckeepalive=$((i + 1)) ;;
68 - 'ConnsAsyncClosing') apache_key_connsasyncclosing=$((i + 1)) ;;
69 - 'Scoreboard') apache_key_scoreboard=$((i)) ;;
57 + 'Total Accesses') apache_key_accesses=$((i + 1)) ;;
58 + 'Total kBytes') apache_key_kbytes=$((i + 1)) ;;
59 + 'ReqPerSec') apache_key_reqpersec=$((i + 1)) ;;
60 + 'BytesPerSec') apache_key_bytespersec=$((i + 1)) ;;
61 + 'BytesPerReq') apache_key_bytesperreq=$((i + 1)) ;;
62 + 'BusyWorkers') apache_key_busyworkers=$((i + 1)) ;;
63 + 'IdleWorkers') apache_key_idleworkers=$((i + 1)) ;;
64 + 'ConnsTotal') apache_key_connstotal=$((i + 1)) ;;
65 + 'ConnsAsyncWriting') apache_key_connsasyncwriting=$((i + 1)) ;;
66 + 'ConnsAsyncKeepAlive') apache_key_connsasynckeepalive=$((i + 1)) ;;
67 + 'ConnsAsyncClosing') apache_key_connsasyncclosing=$((i + 1)) ;;
68 + 'Scoreboard') apache_key_scoreboard=$((i)) ;;
69 esac
70
71 i=$((i + 1))
@@ -74,20 +73,19 @@ apache_detect() {
73
74 # we will not check of the Conns*
75 # keys, since these are apache 2.4 specific
77 - [ -z "${apache_key_accesses}" ] && error "missing 'Total Accesses' from apache server: ${*}" && return 1
78 - [ -z "${apache_key_kbytes}" ] && error "missing 'Total kBytes' from apache server: ${*}" && return 1
79 - [ -z "${apache_key_reqpersec}" ] && error "missing 'ReqPerSec' from apache server: ${*}" && return 1
76 + [ -z "${apache_key_accesses}" ] && error "missing 'Total Accesses' from apache server: ${*}" && return 1
77 + [ -z "${apache_key_kbytes}" ] && error "missing 'Total kBytes' from apache server: ${*}" && return 1
78 + [ -z "${apache_key_reqpersec}" ] && error "missing 'ReqPerSec' from apache server: ${*}" && return 1
79 [ -z "${apache_key_bytespersec}" ] && error "missing 'BytesPerSec' from apache server: ${*}" && return 1
80 [ -z "${apache_key_bytesperreq}" ] && error "missing 'BytesPerReq' from apache server: ${*}" && return 1
81 [ -z "${apache_key_busyworkers}" ] && error "missing 'BusyWorkers' from apache server: ${*}" && return 1
82 [ -z "${apache_key_idleworkers}" ] && error "missing 'IdleWorkers' from apache server: ${*}" && return 1
84 - [ -z "${apache_key_scoreboard}" ] && error "missing 'Scoreboard' from apache server: ${*}" && return 1
83 + [ -z "${apache_key_scoreboard}" ] && error "missing 'Scoreboard' from apache server: ${*}" && return 1
84
86 - if [ ! -z "${apache_key_connstotal}" ] && \
87 - [ ! -z "${apache_key_connsasyncwriting}" ] && \
88 - [ ! -z "${apache_key_connsasynckeepalive}" ] && \
89 - [ ! -z "${apache_key_connsasyncclosing}" ]
90 - then
85 + if [ ! -z "${apache_key_connstotal}" ] &&
86 + [ ! -z "${apache_key_connsasyncwriting}" ] &&
87 + [ ! -z "${apache_key_connsasynckeepalive}" ] &&
88 + [ ! -z "${apache_key_connsasyncclosing}" ]; then
89 apache_has_conns=1
90 else
91 apache_has_conns=0
@@ -103,15 +101,13 @@ apache_get() {
101 ret=$?
102 IFS="${oIFS}"
103
106 - if [ $ret -ne 0 ] || [ "${#apache_response[@]}" -eq 0 ]
107 - then
104 + if [ $ret -ne 0 ] || [ "${#apache_response[@]}" -eq 0 ]; then
105 return 1
106 fi
107
108 # the last line on the apache output is "Scoreboard"
109 # we use this label to detect that the output has a new word count
113 - if [ ${apache_keys_detected} -eq 0 ] || [ "${apache_response[${apache_key_scoreboard}]}" != "Scoreboard" ]
114 - then
110 + if [ ${apache_keys_detected} -eq 0 ] || [ "${apache_response[${apache_key_scoreboard}]}" != "Scoreboard" ]; then
111 apache_detect "${apache_response[@]}" || return 1
112 apache_keys_detected=1
113 fi
@@ -131,20 +127,20 @@ apache_get() {
127 apache_busyworkers="${apache_response[${apache_key_busyworkers}]}"
128 apache_idleworkers="${apache_response[${apache_key_idleworkers}]}"
129
134 - if [ -z "${apache_accesses}" ] || \
135 - [ -z "${apache_kbytes}" ] || \
136 - [ -z "${apache_reqpersec}" ] || \
137 - [ -z "${apache_bytespersec}" ] || \
138 - [ -z "${apache_bytesperreq}" ] || \
139 - [ -z "${apache_busyworkers}" ]
140 - [ -z "${apache_idleworkers}" ]
130 + if
131 + [ -z "${apache_accesses}" ] ||
132 + [ -z "${apache_kbytes}" ] ||
133 + [ -z "${apache_reqpersec}" ] ||
134 + [ -z "${apache_bytespersec}" ] ||
135 + [ -z "${apache_bytesperreq}" ] ||
136 + [ -z "${apache_busyworkers}" ]
137 + [ -z "${apache_idleworkers}" ]
138 then
139 error "empty values got from apache server: ${apache_response[*]}"
140 return 1
141 fi
142
146 - if [ ${apache_has_conns} -eq 1 ]
147 - then
143 + if [ ${apache_has_conns} -eq 1 ]; then
144 apache_connstotal="${apache_response[${apache_key_connstotal}]}"
145 apache_connsasyncwriting="${apache_response[${apache_key_connsasyncwriting}]}"
146 apache_connsasynckeepalive="${apache_response[${apache_key_connsasynckeepalive}]}"
@@ -159,8 +155,7 @@ apache_check() {
155
156 apache_get
157 # shellcheck disable=2181
162 - if [ $? -ne 0 ]
163 - then
158 + if [ $? -ne 0 ]; then
159 # shellcheck disable=2154
160 error "cannot find stub_status on URL '${apache_url}'. Please set apache_url='http://apache.server:80/server-status?auto' in $confd/apache.conf"
161 return 1
@@ -191,8 +186,7 @@ CHART apache_local.net '' "apache Bandwidth" "kilobits/s" bandwidth apache.net a
186 DIMENSION sent '' incremental 8 1
187 EOF
188
194 - if [ ${apache_has_conns} -eq 1 ]
195 - then
189 + if [ ${apache_has_conns} -eq 1 ]; then
190 cat <<EOF2
191 CHART apache_local.connections '' "apache Connections" "connections" connections apache.connections line $((apache_priority + 2)) $apache_update_every
192 DIMENSION connections '' absolute 1 1
@@ -240,9 +234,8 @@ SET busy = $((apache_busyworkers))
234 END
235 VALUESEOF
236
243 - if [ ${apache_has_conns} -eq 1 ]
244 - then
245 - cat <<VALUESEOF2
237 + if [ ${apache_has_conns} -eq 1 ]; then
238 + cat <<VALUESEOF2
239 BEGIN apache_local.connections $1
240 SET connections = $((apache_connstotal))
241 END
collectors/charts.d.plugin/apcupsd/apcupsd.chart.sh
+39 -48
@@ -11,7 +11,7 @@ apcupsd_ip=
11 apcupsd_port=
12
13 declare -A apcupsd_sources=(
14 - ["local"]="127.0.0.1:3551"
14 + ["local"]="127.0.0.1:3551"
15 )
16
17 # how frequently to collect UPS data
@@ -35,46 +35,40 @@ apcupsd_check() {
35 require_cmd apcaccess || return 1
36
37 # backwards compatibility
38 - if [ "${apcupsd_ip}:${apcupsd_port}" != ":" ]
39 - then
40 - apcupsd_sources["local"]="${apcupsd_ip}:${apcupsd_port}"
38 + if [ "${apcupsd_ip}:${apcupsd_port}" != ":" ]; then
39 + apcupsd_sources["local"]="${apcupsd_ip}:${apcupsd_port}"
40 fi
41
43 - local host working=0 failed=0
44 - for host in "${!apcupsd_sources[@]}"
45 - do
46 - run apcupsd_get "${apcupsd_sources[${host}]}" >/dev/null
47 - # shellcheck disable=2181
48 - if [ $? -ne 0 ]
49 - then
50 - error "cannot get information for apcupsd server ${host} on ${apcupsd_sources[${host}]}."
51 - failed=$((failed + 1))
52 - elif [ "$(apcupsd_get "${apcupsd_sources[${host}]}" | awk '/^STATUS.*/{ print $3 }')" != "ONLINE" ]
53 - then
54 - error "APC UPS ${host} on ${apcupsd_sources[${host}]} is not online."
55 - failed=$((failed + 1))
56 - else
57 - working=$((working + 1))
58 - fi
59 - done
60 -
61 - if [ ${working} -eq 0 ]
62 - then
63 - error "No APC UPSes found available."
64 - return 1
65 - fi
42 + local host working=0 failed=0
43 + for host in "${!apcupsd_sources[@]}"; do
44 + run apcupsd_get "${apcupsd_sources[${host}]}" >/dev/null
45 + # shellcheck disable=2181
46 + if [ $? -ne 0 ]; then
47 + error "cannot get information for apcupsd server ${host} on ${apcupsd_sources[${host}]}."
48 + failed=$((failed + 1))
49 + elif [ "$(apcupsd_get "${apcupsd_sources[${host}]}" | awk '/^STATUS.*/{ print $3 }')" != "ONLINE" ]; then
50 + error "APC UPS ${host} on ${apcupsd_sources[${host}]} is not online."
51 + failed=$((failed + 1))
52 + else
53 + working=$((working + 1))
54 + fi
55 + done
56 +
57 + if [ ${working} -eq 0 ]; then
58 + error "No APC UPSes found available."
59 + return 1
60 + fi
61
62 return 0
63 }
64
65 apcupsd_create() {
71 - local host src
72 - for host in "${!apcupsd_sources[@]}"
73 - do
74 - src=${apcupsd_sources[${host}]}
66 + local host src
67 + for host in "${!apcupsd_sources[@]}"; do
68 + src=${apcupsd_sources[${host}]}
69
76 - # create the charts
77 - cat <<EOF
70 + # create the charts
71 + cat <<EOF
72 CHART apcupsd_${host}.charge '' "UPS Charge for ${host} on ${src}" "percentage" ups apcupsd.charge area $((apcupsd_priority + 1)) $apcupsd_update_every
73 DIMENSION battery_charge charge absolute 1 100
74
@@ -104,11 +98,10 @@ CHART apcupsd_${host}.time '' "UPS Time Remaining for ${host} on ${src}" "Minute
98 DIMENSION time time absolute 1 100
99
100 EOF
107 - done
101 + done
102 return 0
103 }
104
111 -
105 apcupsd_update() {
106 # the first argument to this function is the microseconds since last update
107 # pass this parameter to the BEGIN statement (see bellow).
@@ -117,10 +110,9 @@ apcupsd_update() {
110 # for each dimension
111 # remember: KEEP IT SIMPLE AND SHORT
112
120 - local host working=0 failed=0
121 - for host in "${!apcupsd_sources[@]}"
122 - do
123 - apcupsd_get "${apcupsd_sources[${host}]}" | awk "
113 + local host working=0 failed=0
114 + for host in "${!apcupsd_sources[@]}"; do
115 + apcupsd_get "${apcupsd_sources[${host}]}" | awk "
116
117 BEGIN {
118 battery_charge = 0;
@@ -185,15 +177,14 @@ END {
177 print \"SET time = \" time;
178 print \"END\"
179 }"
188 - # shellcheck disable=SC2181
189 - if [ $? -ne 0 ]
190 - then
191 - failed=$((failed + 1))
192 - error "failed to get values for APC UPS ${host} on ${apcupsd_sources[${host}]}" && return 1
193 - else
194 - working=$((working + 1))
195 - fi
196 - done
180 + # shellcheck disable=SC2181
181 + if [ $? -ne 0 ]; then
182 + failed=$((failed + 1))
183 + error "failed to get values for APC UPS ${host} on ${apcupsd_sources[${host}]}" && return 1
184 + else
185 + working=$((working + 1))
186 + fi
187 + done
188
189 [ $working -eq 0 ] && error "failed to get values from all APC UPSes" && return 1
190
collectors/charts.d.plugin/charts.d.dryrun-helper.sh
+26 -32
@@ -17,7 +17,7 @@ tmp1="$(mktemp)"
17 tmp2="$(mktemp)"
18
19 myset() {
20 - set | grep -v "^_=" | grep -v "^PIPESTATUS=" | grep -v "^BASH_LINENO="
20 + set | grep -v "^_=" | grep -v "^PIPESTATUS=" | grep -v "^BASH_LINENO="
21 }
22
23 # save 2 'set'
@@ -26,52 +26,46 @@ myset >"$tmp2"
26
27 # make sure they don't differ
28 diff "$tmp1" "$tmp2" >/dev/null 2>&1
29 -if [ $? -ne 0 ]
30 -then
31 - # they differ, we cannot do the check
32 - echo >&2 "$me: cannot check with diff."
33 - can_diff=0
29 +if [ $? -ne 0 ]; then
30 + # they differ, we cannot do the check
31 + echo >&2 "$me: cannot check with diff."
32 + can_diff=0
33 fi
34
35 # do it again, now including the script
36 myset >"$tmp1"
37
38 # 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 - echo >&2 "$me: cannot load config file $conf"
47 - rm "$tmp1" "$tmp2"
48 - exit 1
49 - fi
39 +if [ -f "$conf" ]; then
40 + # shellcheck source=/dev/null
41 + . "$conf"
42 + if [ $? -ne 0 ]; then
43 + echo >&2 "$me: cannot load config file $conf"
44 + rm "$tmp1" "$tmp2"
45 + exit 1
46 + fi
47 fi
48
49 # shellcheck source=/dev/null
50 . "$chart"
54 -if [ $? -ne 0 ]
55 -then
56 - echo >&2 "$me: cannot load chart file $chart"
57 - rm "$tmp1" "$tmp2"
58 - exit 1
51 +if [ $? -ne 0 ]; then
52 + echo >&2 "$me: cannot load chart file $chart"
53 + rm "$tmp1" "$tmp2"
54 + exit 1
55 fi
56
57 # remove all variables starting with the plugin name
58 myset | grep -v "^$name" >"$tmp2"
59
64 -if [ $can_diff -eq 1 ]
65 -then
66 - # check if they are different
67 - # make sure they don't differ
68 - diff "$tmp1" "$tmp2" >&2
69 - if [ $? -ne 0 ]
70 - then
71 - # they differ
72 - rm "$tmp1" "$tmp2"
73 - exit 1
74 - fi
60 +if [ $can_diff -eq 1 ]; then
61 + # check if they are different
62 + # make sure they don't differ
63 + diff "$tmp1" "$tmp2" >&2
64 + if [ $? -ne 0 ]; then
65 + # they differ
66 + rm "$tmp1" "$tmp2"
67 + exit 1
68 + fi
69 fi
70
71 rm "$tmp1" "$tmp2"
collectors/charts.d.plugin/charts.d.plugin.in
+436 -488
@@ -17,7 +17,7 @@ export PATH="${PATH}:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin"
17
18 PROGRAM_FILE="$0"
19 PROGRAM_NAME="$(basename $0)"
20 -PROGRAM_NAME="${PROGRAM_NAME/.plugin}"
20 +PROGRAM_NAME="${PROGRAM_NAME/.plugin/}"
21 MODULE_NAME="main"
22
23 # -----------------------------------------------------------------------------
@@ -26,72 +26,69 @@ MODULE_NAME="main"
26 debug=0
27 TMP_DIR=
28 chartsd_cleanup() {
29 - trap '' EXIT QUIT HUP INT TERM
30 -
31 - if [ ! -z "$TMP_DIR" -a -d "$TMP_DIR" ]
32 - then
33 - [ $debug -eq 1 ] && echo >&2 "$PROGRAM_NAME: cleaning up temporary directory $TMP_DIR ..."
34 - rm -rf "$TMP_DIR"
35 - fi
36 - exit 0
29 + trap '' EXIT QUIT HUP INT TERM
30 +
31 + if [ ! -z "$TMP_DIR" -a -d "$TMP_DIR" ]; then
32 + [ $debug -eq 1 ] && echo >&2 "$PROGRAM_NAME: cleaning up temporary directory $TMP_DIR ..."
33 + rm -rf "$TMP_DIR"
34 + fi
35 + exit 0
36 }
37 trap chartsd_cleanup EXIT QUIT HUP INT TERM
38
40 -if [ $UID = "0" ]
41 -then
42 - TMP_DIR="$( mktemp -d /var/run/netdata-${PROGRAM_NAME}-XXXXXXXXXX )"
39 +if [ $UID = "0" ]; then
40 + TMP_DIR="$(mktemp -d /var/run/netdata-${PROGRAM_NAME}-XXXXXXXXXX)"
41 else
44 - TMP_DIR="$( mktemp -d /tmp/.netdata-${PROGRAM_NAME}-XXXXXXXXXX )"
42 + TMP_DIR="$(mktemp -d /tmp/.netdata-${PROGRAM_NAME}-XXXXXXXXXX)"
43 fi
44
45 logdate() {
48 - date "+%Y-%m-%d %H:%M:%S"
46 + date "+%Y-%m-%d %H:%M:%S"
47 }
48
49 log() {
52 - local status="${1}"
53 - shift
50 + local status="${1}"
51 + shift
52
55 - echo >&2 "$(logdate): ${PROGRAM_NAME}: ${status}: ${MODULE_NAME}: ${*}"
53 + echo >&2 "$(logdate): ${PROGRAM_NAME}: ${status}: ${MODULE_NAME}: ${*}"
54
55 }
56
57 warning() {
60 - log WARNING "${@}"
58 + log WARNING "${@}"
59 }
60
61 error() {
64 - log ERROR "${@}"
62 + log ERROR "${@}"
63 }
64
65 info() {
68 - log INFO "${@}"
66 + log INFO "${@}"
67 }
68
69 fatal() {
72 - log FATAL "${@}"
73 - echo "DISABLE"
74 - exit 1
70 + log FATAL "${@}"
71 + echo "DISABLE"
72 + exit 1
73 }
74
75 debug() {
78 - [ $debug -eq 1 ] && log DEBUG "${@}"
76 + [ $debug -eq 1 ] && log DEBUG "${@}"
77 }
78
79 # -----------------------------------------------------------------------------
80 # check a few commands
81
82 require_cmd() {
85 - local x=$(which "${1}" 2>/dev/null || command -v "${1}" 2>/dev/null)
86 - if [ -z "${x}" -o ! -x "${x}" ]
87 - then
88 - warning "command '${1}' is not found in ${PATH}."
89 - eval "${1^^}_CMD=\"\""
90 - return 1
91 - fi
92 -
93 - eval "${1^^}_CMD=\"${x}\""
94 - return 0
83 + local x=$(which "${1}" 2>/dev/null || command -v "${1}" 2>/dev/null)
84 + if [ -z "${x}" -o ! -x "${x}" ]; then
85 + warning "command '${1}' is not found in ${PATH}."
86 + eval "${1^^}_CMD=\"\""
87 + return 1
88 + fi
89 +
90 + eval "${1^^}_CMD=\"${x}\""
91 + return 0
92 }
93
94 require_cmd date || exit 1
@@ -108,7 +105,7 @@ require_cmd curl || exit 1
105
106 # -----------------------------------------------------------------------------
107
111 -[ $(( ${BASH_VERSINFO[0]} )) -lt 4 ] && fatal "BASH version 4 or later is required, but found version: ${BASH_VERSION}. Please upgrade."
108 +[ $((BASH_VERSINFO[0])) -lt 4 ] && fatal "BASH version 4 or later is required, but found version: ${BASH_VERSION}. Please upgrade."
109
110 info "started from '$PROGRAM_FILE' with options: $*"
111
@@ -117,7 +114,7 @@ info "started from '$PROGRAM_FILE' with options: $*"
114 # netdata exposes a few environment variables for us
115
116 [ -z "${NETDATA_PLUGINS_DIR}" ] && NETDATA_PLUGINS_DIR="$(dirname "${0}")"
120 -[ -z "${NETDATA_USER_CONFIG_DIR}" ] && NETDATA_USER_CONFIG_DIR="@configdir_POST@"
117 +[ -z "${NETDATA_USER_CONFIG_DIR}" ] && NETDATA_USER_CONFIG_DIR="@configdir_POST@"
118 [ -z "${NETDATA_STOCK_CONFIG_DIR}" ] && NETDATA_STOCK_CONFIG_DIR="@libconfigdir_POST@"
119
120 pluginsd="${NETDATA_PLUGINS_DIR}"
@@ -127,7 +124,7 @@ olduserconfd="${NETDATA_USER_CONFIG_DIR}"
124 chartsd="$pluginsd/../charts.d"
125
126 minimum_update_frequency="${NETDATA_UPDATE_EVERY-1}"
130 -update_every=${minimum_update_frequency} # this will be overwritten by the command line
127 +update_every=${minimum_update_frequency} # this will be overwritten by the command line
128
129 # work around for non BASH shells
130 charts_create="_create"
@@ -169,53 +166,46 @@ enable_all_charts="yes"
166
167 check=0
168 chart_only=
172 -while [ ! -z "$1" ]
173 -do
174 - if [ "$1" = "check" ]
175 - then
176 - check=1
177 - shift
178 - continue
179 - fi
180 -
181 - if [ "$1" = "debug" -o "$1" = "all" ]
182 - then
183 - debug=1
184 - shift
185 - continue
186 - fi
187 -
188 - if [ -f "$chartsd/$1.chart.sh" ]
189 - then
190 - debug=1
191 - chart_only="$( echo $1.chart.sh | sed "s/\.chart\.sh$//g" )"
192 - shift
193 - continue
194 - fi
195 -
196 - if [ -f "$chartsd/$1" ]
197 - then
198 - debug=1
199 - chart_only="$( echo $1 | sed "s/\.chart\.sh$//g" )"
200 - shift
201 - continue
202 - fi
203 -
204 - # number check
205 - n="$1"
206 - x=$(( n ))
207 - if [ "$x" = "$n" ]
208 - then
209 - shift
210 - update_every=$x
211 - [ $update_every -lt $minimum_update_frequency ] && update_every=$minimum_update_frequency
212 - continue
213 - fi
214 -
215 - fatal "Cannot understand parameter $1. Aborting."
169 +while [ ! -z "$1" ]; do
170 + if [ "$1" = "check" ]; then
171 + check=1
172 + shift
173 + continue
174 + fi
175 +
176 + if [ "$1" = "debug" -o "$1" = "all" ]; then
177 + debug=1
178 + shift
179 + continue
180 + fi
181 +
182 + if [ -f "$chartsd/$1.chart.sh" ]; then
183 + debug=1
184 + chart_only="$(echo $1.chart.sh | sed "s/\.chart\.sh$//g")"
185 + shift
186 + continue
187 + fi
188 +
189 + if [ -f "$chartsd/$1" ]; then
190 + debug=1
191 + chart_only="$(echo $1 | sed "s/\.chart\.sh$//g")"
192 + shift
193 + continue
194 + fi
195 +
196 + # number check
197 + n="$1"
198 + x=$((n))
199 + if [ "$x" = "$n" ]; then
200 + shift
201 + update_every=$x
202 + [ $update_every -lt $minimum_update_frequency ] && update_every=$minimum_update_frequency
203 + continue
204 + fi
205 +
206 + fatal "Cannot understand parameter $1. Aborting."
207 done
208
218 -
209 # -----------------------------------------------------------------------------
210 # loop control
211
@@ -223,7 +213,7 @@ done
213 LOOPSLEEPMS_HIGHRES=0
214 now_ms=
215 current_time_ms_default() {
226 - now_ms="$(date +'%s')000"
216 + now_ms="$(date +'%s')000"
217 }
218 current_time_ms="current_time_ms_default"
219 current_time_ms_accuracy=1
@@ -238,20 +228,17 @@ source "$pluginsd/loopsleepms.sh.inc"
228 # -----------------------------------------------------------------------------
229 # load my configuration
230
241 -for myconfig in "${NETDATA_STOCK_CONFIG_DIR}/${PROGRAM_NAME}.conf" "${NETDATA_USER_CONFIG_DIR}/${PROGRAM_NAME}.conf"
242 -do
243 - if [ -f "$myconfig" ]
244 - then
245 - source "$myconfig"
246 - if [ $? -ne 0 ]
247 - then
248 - error "Config file '$myconfig' loaded with errors."
249 - else
250 - info "Configuration file '$myconfig' loaded."
251 - fi
252 - else
253 - warning "Configuration file '$myconfig' not found."
254 - fi
231 +for myconfig in "${NETDATA_STOCK_CONFIG_DIR}/${PROGRAM_NAME}.conf" "${NETDATA_USER_CONFIG_DIR}/${PROGRAM_NAME}.conf"; do
232 + if [ -f "$myconfig" ]; then
233 + source "$myconfig"
234 + if [ $? -ne 0 ]; then
235 + error "Config file '$myconfig' loaded with errors."
236 + else
237 + info "Configuration file '$myconfig' loaded."
238 + fi
239 + else
240 + warning "Configuration file '$myconfig' not found."
241 + fi
242 done
243
244 # make sure time_divisor is right
@@ -259,22 +246,20 @@ time_divisor=$((time_divisor))
246 [ $time_divisor -lt 10 ] && time_divisor=10
247 [ $time_divisor -gt 100 ] && time_divisor=100
248
262 -
249 # we check for the timeout command, after we load our
250 # configuration, so that the user may overwrite the
251 # timeout command we use, providing a function that
252 # can emulate the timeout command we need:
253 # > timeout SECONDS command ...
268 -if [ $check_for_timeout -eq 1 ]
269 - then
270 - require_cmd timeout || exit 1
254 +if [ $check_for_timeout -eq 1 ]; then
255 + require_cmd timeout || exit 1
256 fi
257
258 # -----------------------------------------------------------------------------
259 # internal checks
260
261 # netdata passes the requested update frequency as the first argument
277 -update_every=$(( update_every + 1 - 1)) # makes sure it is a number
262 +update_every=$((update_every + 1 - 1)) # makes sure it is a number
263 test $update_every -eq 0 && update_every=1 # if it is zero, make it 1
264
265 # check the charts.d directory
@@ -284,39 +269,37 @@ test $update_every -eq 0 && update_every=1 # if it is zero, make it 1
269 # library functions
270
271 fixid() {
287 - echo "$*" |\
288 - tr -c "[A-Z][a-z][0-9]" "_" |\
289 - sed -e "s|^_\+||g" -e "s|_\+$||g" -e "s|_\+|_|g" |\
290 - tr "[A-Z]" "[a-z]"
272 + echo "$*" |
273 + tr -c "[A-Z][a-z][0-9]" "_" |
274 + sed -e "s|^_\+||g" -e "s|_\+$||g" -e "s|_\+|_|g" |
275 + tr "[A-Z]" "[a-z]"
276 }
277
278 run() {
294 - local ret pid="${BASHPID}" t
295 -
296 - if [ "z${1}" = "z-t" -a "${2}" != "0" ]
297 - then
298 - t="${2}"
299 - shift 2
300 - timeout ${t} "${@}" 2>"${TMP_DIR}/run.${pid}"
301 - ret=$?
302 - else
303 - "${@}" 2>"${TMP_DIR}/run.${pid}"
304 - ret=$?
305 - fi
306 -
307 - if [ ${ret} -ne 0 ]
308 - then
309 - {
310 - printf "$(logdate): ${PROGRAM_NAME}: ${status}: ${MODULE_NAME}: command '"
311 - printf "%q " "${@}"
312 - printf "' failed with code ${ret}:\n --- BEGIN TRACE ---\n"
313 - cat "${TMP_DIR}/run.${pid}"
314 - printf " --- END TRACE ---\n"
315 - } >&2
316 - fi
317 - rm "${TMP_DIR}/run.${pid}"
318 -
319 - return ${ret}
279 + local ret pid="${BASHPID}" t
280 +
281 + if [ "z${1}" = "z-t" -a "${2}" != "0" ]; then
282 + t="${2}"
283 + shift 2
284 + timeout ${t} "${@}" 2>"${TMP_DIR}/run.${pid}"
285 + ret=$?
286 + else
287 + "${@}" 2>"${TMP_DIR}/run.${pid}"
288 + ret=$?
289 + fi
290 +
291 + if [ ${ret} -ne 0 ]; then
292 + {
293 + printf "$(logdate): ${PROGRAM_NAME}: ${status}: ${MODULE_NAME}: command '"
294 + printf "%q " "${@}"
295 + printf "' failed with code ${ret}:\n --- BEGIN TRACE ---\n"
296 + cat "${TMP_DIR}/run.${pid}"
297 + printf " --- END TRACE ---\n"
298 + } >&2
299 + fi
300 + rm "${TMP_DIR}/run.${pid}"
301 +
302 + return ${ret}
303 }
304
305 # convert any floating point number
@@ -325,166 +308,155 @@ run() {
308 # so that no fork is necessary
309 # the multiplier must be a power of 10
310 float2int() {
328 - local f m="$2" a b l v=($1)
329 - f=${v[0]}
330 -
331 - # the length of the multiplier - 1
332 - l=$(( ${#m} - 1 ))
333 -
334 - # check if the number is in scientific notation
335 - if [[ ${f} =~ ^[[:space:]]*(-)?[0-9.]+(e|E)(\+|-)[0-9]+ ]]
336 - then
337 - # convert it to decimal
338 - # unfortunately, this fork cannot be avoided
339 - # if you know of a way to avoid it, please let me know
340 - f=$(printf "%0.${l}f" ${f})
341 - fi
342 -
343 - # split the floating point number
344 - # in integer (a) and decimal (b)
345 - a=${f/.*/}
346 - b=${f/*./}
347 -
348 - # if the integer part is missing
349 - # set it to zero
350 - [ -z "${a}" ] && a="0"
351 -
352 - # strip leading zeros from the integer part
353 - # base 10 convertion
354 - a=$((10#$a))
355 -
356 - # check the length of the decimal part
357 - # against the length of the multiplier
358 - if [ ${#b} -gt ${l} ]
359 - then
360 - # too many digits - take the most significant
361 - b=${b:0:${l}}
362 -
363 - elif [ ${#b} -lt ${l} ]
364 - then
365 - # too few digits - pad with zero on the right
366 - local z="00000000000000000000000" r=$((l - ${#b}))
367 - b="${b}${z:0:${r}}"
368 - fi
369 -
370 - # strip leading zeros from the decimal part
371 - # base 10 convertion
372 - b=$((10#$b))
373 -
374 - # store the result
375 - FLOAT2INT_RESULT=$(( (a * m) + b ))
311 + local f m="$2" a b l v=($1)
312 + f=${v[0]}
313 +
314 + # the length of the multiplier - 1
315 + l=$((${#m} - 1))
316 +
317 + # check if the number is in scientific notation
318 + if [[ ${f} =~ ^[[:space:]]*(-)?[0-9.]+(e|E)(\+|-)[0-9]+ ]]; then
319 + # convert it to decimal
320 + # unfortunately, this fork cannot be avoided
321 + # if you know of a way to avoid it, please let me know
322 + f=$(printf "%0.${l}f" ${f})
323 + fi
324 +
325 + # split the floating point number
326 + # in integer (a) and decimal (b)
327 + a=${f/.*/}
328 + b=${f/*./}
329 +
330 + # if the integer part is missing
331 + # set it to zero
332 + [ -z "${a}" ] && a="0"
333 +
334 + # strip leading zeros from the integer part
335 + # base 10 convertion
336 + a=$((10#$a))
337 +
338 + # check the length of the decimal part
339 + # against the length of the multiplier
340 + if [ ${#b} -gt ${l} ]; then
341 + # too many digits - take the most significant
342 + b=${b:0:l}
343 +
344 + elif [ ${#b} -lt ${l} ]; then
345 + # too few digits - pad with zero on the right
346 + local z="00000000000000000000000" r=$((l - ${#b}))
347 + b="${b}${z:0:r}"
348 + fi
349 +
350 + # strip leading zeros from the decimal part
351 + # base 10 convertion
352 + b=$((10#$b))
353 +
354 + # store the result
355 + FLOAT2INT_RESULT=$(((a * m) + b))
356 }
357
378 -
358 # -----------------------------------------------------------------------------
359 # charts check functions
360
361 all_charts() {
383 - cd "$chartsd"
384 - [ $? -ne 0 ] && error "cannot cd to $chartsd" && return 1
362 + cd "$chartsd"
363 + [ $? -ne 0 ] && error "cannot cd to $chartsd" && return 1
364
386 - ls *.chart.sh | sed "s/\.chart\.sh$//g"
365 + ls *.chart.sh | sed "s/\.chart\.sh$//g"
366 }
367
368 declare -A charts_enable_keyword=(
390 - ['apache']="force"
391 - ['cpu_apps']="force"
392 - ['cpufreq']="force"
393 - ['example']="force"
394 - ['exim']="force"
395 - ['hddtemp']="force"
396 - ['load_average']="force"
397 - ['mem_apps']="force"
398 - ['mysql']="force"
399 - ['nginx']="force"
400 - ['phpfpm']="force"
401 - ['postfix']="force"
402 - ['sensors']="force"
403 - ['squid']="force"
404 - ['tomcat']="force"
405 - )
369 + ['apache']="force"
370 + ['cpu_apps']="force"
371 + ['cpufreq']="force"
372 + ['example']="force"
373 + ['exim']="force"
374 + ['hddtemp']="force"
375 + ['load_average']="force"
376 + ['mem_apps']="force"
377 + ['mysql']="force"
378 + ['nginx']="force"
379 + ['phpfpm']="force"
380 + ['postfix']="force"
381 + ['sensors']="force"
382 + ['squid']="force"
383 + ['tomcat']="force"
384 +)
385
386 all_enabled_charts() {
408 - local charts= enabled= required=
409 -
410 - # find all enabled charts
411 -
412 - for chart in $( all_charts )
413 - do
414 - MODULE_NAME="${chart}"
415 -
416 - eval "enabled=\$$chart"
417 - if [ -z "${enabled}" ]
418 - then
419 - enabled="${enable_all_charts}"
420 - fi
421 -
422 - required="${charts_enable_keyword[${chart}]}"
423 - [ -z "${required}" ] && required="yes"
424 -
425 - if [ ! "${enabled}" = "${required}" ]
426 - then
427 - info "is disabled. Add a line with $chart=$required in '${NETDATA_USER_CONFIG_DIR}/${PROGRAM_NAME}.conf' to enable it (or remove the line that disables it)."
428 - else
429 - debug "is enabled for auto-detection."
430 - local charts="$charts $chart"
431 - fi
432 - done
433 - MODULE_NAME="main"
434 -
435 - local charts2=
436 - for chart in $charts
437 - do
438 - MODULE_NAME="${chart}"
439 -
440 - # check the enabled charts
441 - local check="$( cat "$chartsd/$chart.chart.sh" | sed "s/^ \+//g" | grep "^$chart$charts_check()" )"
442 - if [ -z "$check" ]
443 - then
444 - error "module '$chart' does not seem to have a $chart$charts_check() function. Disabling it."
445 - continue
446 - fi
447 -
448 - local create="$( cat "$chartsd/$chart.chart.sh" | sed "s/^ \+//g" | grep "^$chart$charts_create()" )"
449 - if [ -z "$create" ]
450 - then
451 - error "module '$chart' does not seem to have a $chart$charts_create() function. Disabling it."
452 - continue
453 - fi
454 -
455 - local update="$( cat "$chartsd/$chart.chart.sh" | sed "s/^ \+//g" | grep "^$chart$charts_update()" )"
456 - if [ -z "$update" ]
457 - then
458 - error "module '$chart' does not seem to have a $chart$charts_update() function. Disabling it."
459 - continue
460 - fi
461 -
462 - # check its config
463 - #if [ -f "$userconfd/$chart.conf" ]
464 - #then
465 - # if [ ! -z "$( cat "$userconfd/$chart.conf" | sed "s/^ \+//g" | grep -v "^$" | grep -v "^#" | grep -v "^$chart$charts_undescore" )" ]
466 - # then
467 - # error "module's $chart config $userconfd/$chart.conf should only have lines starting with $chart$charts_undescore . Disabling it."
468 - # continue
469 - # fi
470 - #fi
471 -
472 - #if [ $dryrunner -eq 1 ]
473 - # then
474 - # "$pluginsd/charts.d.dryrun-helper.sh" "$chart" "$chartsd/$chart.chart.sh" "$userconfd/$chart.conf" >/dev/null
475 - # if [ $? -ne 0 ]
476 - # then
477 - # error "module's $chart did not pass the dry run check. This means it uses global variables not starting with $chart. Disabling it."
478 - # continue
479 - # fi
480 - #fi
481 -
482 - local charts2="$charts2 $chart"
483 - done
484 - MODULE_NAME="main"
485 -
486 - echo $charts2
487 - debug "enabled charts: $charts2"
387 + local charts= enabled= required=
388 +
389 + # find all enabled charts
390 +
391 + for chart in $(all_charts); do
392 + MODULE_NAME="${chart}"
393 +
394 + eval "enabled=\$$chart"
395 + if [ -z "${enabled}" ]; then
396 + enabled="${enable_all_charts}"
397 + fi
398 +
399 + required="${charts_enable_keyword[${chart}]}"
400 + [ -z "${required}" ] && required="yes"
401 +
402 + if [ ! "${enabled}" = "${required}" ]; then
403 + info "is disabled. Add a line with $chart=$required in '${NETDATA_USER_CONFIG_DIR}/${PROGRAM_NAME}.conf' to enable it (or remove the line that disables it)."
404 + else
405 + debug "is enabled for auto-detection."
406 + local charts="$charts $chart"
407 + fi
408 + done
409 + MODULE_NAME="main"
410 +
411 + local charts2=
412 + for chart in $charts; do
413 + MODULE_NAME="${chart}"
414 +
415 + # check the enabled charts
416 + local check="$(cat "$chartsd/$chart.chart.sh" | sed "s/^ \+//g" | grep "^$chart$charts_check()")"
417 + if [ -z "$check" ]; then
418 + error "module '$chart' does not seem to have a $chart$charts_check() function. Disabling it."
419 + continue
420 + fi
421 +
422 + local create="$(cat "$chartsd/$chart.chart.sh" | sed "s/^ \+//g" | grep "^$chart$charts_create()")"
423 + if [ -z "$create" ]; then
424 + error "module '$chart' does not seem to have a $chart$charts_create() function. Disabling it."
425 + continue
426 + fi
427 +
428 + local update="$(cat "$chartsd/$chart.chart.sh" | sed "s/^ \+//g" | grep "^$chart$charts_update()")"
429 + if [ -z "$update" ]; then
430 + error "module '$chart' does not seem to have a $chart$charts_update() function. Disabling it."
431 + continue
432 + fi
433 +
434 + # check its config
435 + #if [ -f "$userconfd/$chart.conf" ]
436 + #then
437 + # if [ ! -z "$( cat "$userconfd/$chart.conf" | sed "s/^ \+//g" | grep -v "^$" | grep -v "^#" | grep -v "^$chart$charts_undescore" )" ]
438 + # then
439 + # error "module's $chart config $userconfd/$chart.conf should only have lines starting with $chart$charts_undescore . Disabling it."
440 + # continue
441 + # fi
442 + #fi
443 +
444 + #if [ $dryrunner -eq 1 ]
445 + # then
446 + # "$pluginsd/charts.d.dryrun-helper.sh" "$chart" "$chartsd/$chart.chart.sh" "$userconfd/$chart.conf" >/dev/null
447 + # if [ $? -ne 0 ]
448 + # then
449 + # error "module's $chart did not pass the dry run check. This means it uses global variables not starting with $chart. Disabling it."
450 + # continue
451 + # fi
452 + #fi
453 +
454 + local charts2="$charts2 $chart"
455 + done
456 + MODULE_NAME="main"
457 +
458 + echo $charts2
459 + debug "enabled charts: $charts2"
460 }
461
462 # -----------------------------------------------------------------------------
@@ -493,63 +465,56 @@ all_enabled_charts() {
465 suffix_retries="_retries"
466 suffix_update_every="_update_every"
467 active_charts=
496 -for chart in $( all_enabled_charts )
497 -do
498 - MODULE_NAME="${chart}"
499 -
500 - debug "loading module: '$chartsd/$chart.chart.sh'"
501 -
502 - source "$chartsd/$chart.chart.sh"
503 - [ $? -ne 0 ] && warning "Module '$chartsd/$chart.chart.sh' loaded with errors."
504 -
505 - # first load the stock config
506 - if [ -f "$stockconfd/$chart.conf" ]
507 - then
508 - debug "loading module configuration: '$stockconfd/$chart.conf'"
509 - source "$stockconfd/$chart.conf"
510 - [ $? -ne 0 ] && warning "Config file '$stockconfd/$chart.conf' loaded with errors."
511 - else
512 - debug "not found module configuration: '$stockconfd/$chart.conf'"
513 - fi
514 -
515 - # then load the user config (it overwrites the stock)
516 - if [ -f "$userconfd/$chart.conf" ]
517 - then
518 - debug "loading module configuration: '$userconfd/$chart.conf'"
519 - source "$userconfd/$chart.conf"
520 - [ $? -ne 0 ] && warning "Config file '$userconfd/$chart.conf' loaded with errors."
521 - else
522 - debug "not found module configuration: '$userconfd/$chart.conf'"
523 -
524 - if [ -f "$olduserconfd/$chart.conf" ]
525 - then
526 - # support for very old netdata that had the charts.d module configs in /etc/netdata
527 - info "loading module configuration from obsolete location: '$olduserconfd/$chart.conf'"
528 - source "$olduserconfd/$chart.conf"
529 - [ $? -ne 0 ] && warning "Config file '$olduserconfd/$chart.conf' loaded with errors."
530 - fi
531 - fi
532 -
533 - eval "dt=\$$chart$suffix_update_every"
534 - dt=$(( dt + 1 - 1 )) # make sure it is a number
535 - if [ $dt -lt $update_every ]
536 - then
537 - eval "$chart$suffix_update_every=$update_every"
538 - fi
539 -
540 - $chart$charts_check
541 - if [ $? -eq 0 ]
542 - then
543 - debug "module '$chart' activated"
544 - active_charts="$active_charts $chart"
545 - else
546 - error "module's '$chart' check() function reports failure."
547 - fi
468 +for chart in $(all_enabled_charts); do
469 + MODULE_NAME="${chart}"
470 +
471 + debug "loading module: '$chartsd/$chart.chart.sh'"
472 +
473 + source "$chartsd/$chart.chart.sh"
474 + [ $? -ne 0 ] && warning "Module '$chartsd/$chart.chart.sh' loaded with errors."
475 +
476 + # first load the stock config
477 + if [ -f "$stockconfd/$chart.conf" ]; then
478 + debug "loading module configuration: '$stockconfd/$chart.conf'"
479 + source "$stockconfd/$chart.conf"
480 + [ $? -ne 0 ] && warning "Config file '$stockconfd/$chart.conf' loaded with errors."
481 + else
482 + debug "not found module configuration: '$stockconfd/$chart.conf'"
483 + fi
484 +
485 + # then load the user config (it overwrites the stock)
486 + if [ -f "$userconfd/$chart.conf" ]; then
487 + debug "loading module configuration: '$userconfd/$chart.conf'"
488 + source "$userconfd/$chart.conf"
489 + [ $? -ne 0 ] && warning "Config file '$userconfd/$chart.conf' loaded with errors."
490 + else
491 + debug "not found module configuration: '$userconfd/$chart.conf'"
492 +
493 + if [ -f "$olduserconfd/$chart.conf" ]; then
494 + # support for very old netdata that had the charts.d module configs in /etc/netdata
495 + info "loading module configuration from obsolete location: '$olduserconfd/$chart.conf'"
496 + source "$olduserconfd/$chart.conf"
497 + [ $? -ne 0 ] && warning "Config file '$olduserconfd/$chart.conf' loaded with errors."
498 + fi
499 + fi
500 +
501 + eval "dt=\$$chart$suffix_update_every"
502 + dt=$((dt + 1 - 1)) # make sure it is a number
503 + if [ $dt -lt $update_every ]; then
504 + eval "$chart$suffix_update_every=$update_every"
505 + fi
506 +
507 + $chart$charts_check
508 + if [ $? -eq 0 ]; then
509 + debug "module '$chart' activated"
510 + active_charts="$active_charts $chart"
511 + else
512 + error "module's '$chart' check() function reports failure."
513 + fi
514 done
515 MODULE_NAME="main"
516 debug "activated modules: $active_charts"
517
552 -
518 # -----------------------------------------------------------------------------
519 # check overwrites
520
@@ -558,28 +523,24 @@ debug_time=
523 test $debug -eq 1 && debug_time=tellwork
524
525 # if we only need a specific chart, remove all the others
561 -if [ ! -z "${chart_only}" ]
562 -then
563 - debug "requested to run only for: '${chart_only}'"
564 - check_charts=
565 - for chart in $active_charts
566 - do
567 - if [ "$chart" = "$chart_only" ]
568 - then
569 - check_charts="$chart"
570 - break
571 - fi
572 - done
573 - active_charts="$check_charts"
526 +if [ ! -z "${chart_only}" ]; then
527 + debug "requested to run only for: '${chart_only}'"
528 + check_charts=
529 + for chart in $active_charts; do
530 + if [ "$chart" = "$chart_only" ]; then
531 + check_charts="$chart"
532 + break
533 + fi
534 + done
535 + active_charts="$check_charts"
536 fi
537 debug "activated charts: $active_charts"
538
539 # stop if we just need a pre-check
578 -if [ $check -eq 1 ]
579 -then
580 - info "CHECK RESULT"
581 - info "Will run the charts: $active_charts"
582 - exit 0
540 +if [ $check -eq 1 ]; then
541 + info "CHECK RESULT"
542 + info "Will run the charts: $active_charts"
543 + exit 0
544 fi
545
546 # -----------------------------------------------------------------------------
@@ -590,24 +551,21 @@ cd "${TMP_DIR}" || exit 1
551 # create charts
552
553 run_charts=
593 -for chart in $active_charts
594 -do
595 - MODULE_NAME="${chart}"
596 -
597 - debug "calling '$chart$charts_create()'..."
598 - $chart$charts_create
599 - if [ $? -eq 0 ]
600 - then
601 - run_charts="$run_charts $chart"
602 - debug "'$chart' initialized."
603 - else
604 - error "module's '$chart' function '$chart$charts_create()' reports failure."
605 - fi
554 +for chart in $active_charts; do
555 + MODULE_NAME="${chart}"
556 +
557 + debug "calling '$chart$charts_create()'..."
558 + $chart$charts_create
559 + if [ $? -eq 0 ]; then
560 + run_charts="$run_charts $chart"
561 + debug "'$chart' initialized."
562 + else
563 + error "module's '$chart' function '$chart$charts_create()' reports failure."
564 + fi
565 done
566 MODULE_NAME="main"
567 debug "run_charts='$run_charts'"
568
610 -
569 # -----------------------------------------------------------------------------
570 # update dimensions
571
@@ -615,129 +573,119 @@ debug "run_charts='$run_charts'"
573
574 declare -A charts_last_update=() charts_update_every=() charts_retries=() charts_next_update=() charts_run_counter=() charts_serial_failures=()
575 global_update() {
618 - local exit_at \
619 - c=0 dt ret last_ms exec_start_ms exec_end_ms \
620 - chart now_charts=() next_charts=($run_charts) \
621 - next_ms x seconds millis
622 -
623 - # return the current time in ms in $now_ms
624 - ${current_time_ms}
625 -
626 - exit_at=$(( now_ms + (restart_timeout * 1000) ))
627 -
628 - for chart in $run_charts
629 - do
630 - eval "charts_update_every[$chart]=\$$chart$suffix_update_every"
631 - test -z "${charts_update_every[$chart]}" && charts_update_every[$chart]=$update_every
632 -
633 - eval "charts_retries[$chart]=\$$chart$suffix_retries"
634 - test -z "${charts_retries[$chart]}" && charts_retries[$chart]=10
635 -
636 - charts_last_update[$chart]=$((now_ms - (now_ms % (charts_update_every[$chart] * 1000) ) ))
637 - charts_next_update[$chart]=$(( charts_last_update[$chart] + (charts_update_every[$chart] * 1000) ))
638 - charts_run_counter[$chart]=0
639 - charts_serial_failures[$chart]=0
640 -
641 - echo "CHART netdata.plugin_chartsd_$chart '' 'Execution time for $chart plugin' 'milliseconds / run' charts.d netdata.plugin_charts area 145000 ${charts_update_every[$chart]}"
642 - echo "DIMENSION run_time 'run time' absolute 1 1"
643 - done
644 -
645 - # the main loop
646 - while [ "${#next_charts[@]}" -gt 0 ]
647 - do
648 - c=$((c + 1))
649 - now_charts=("${next_charts[@]}")
650 - next_charts=()
651 -
652 - # return the current time in ms in $now_ms
653 - ${current_time_ms}
654 -
655 - for chart in "${now_charts[@]}"
656 - do
657 - MODULE_NAME="${chart}"
658 -
659 - if [ ${now_ms} -ge ${charts_next_update[$chart]} ]
660 - then
661 - last_ms=${charts_last_update[$chart]}
662 - dt=$(( (now_ms - last_ms) ))
663 -
664 - charts_last_update[$chart]=${now_ms}
665 -
666 - while [ ${charts_next_update[$chart]} -lt ${now_ms} ]
667 - do
668 - charts_next_update[$chart]=$(( charts_next_update[$chart] + (charts_update_every[$chart] * 1000) ))
669 - done
670 -
671 - # the first call should not give a duration
672 - # so that netdata calibrates to current time
673 - dt=$(( dt * 1000 ))
674 - charts_run_counter[$chart]=$(( charts_run_counter[$chart] + 1 ))
675 - if [ ${charts_run_counter[$chart]} -eq 1 ]
676 - then
677 - dt=
678 - fi
679 -
680 - exec_start_ms=$now_ms
681 - $chart$charts_update $dt
682 - ret=$?
683 -
684 - # return the current time in ms in $now_ms
685 - ${current_time_ms}; exec_end_ms=$now_ms
686 -
687 - echo "BEGIN netdata.plugin_chartsd_$chart $dt"
688 - echo "SET run_time = $(( exec_end_ms - exec_start_ms ))"
689 - echo "END"
690 -
691 - if [ $ret -eq 0 ]
692 - then
693 - charts_serial_failures[$chart]=0
694 - next_charts+=($chart)
695 - else
696 - charts_serial_failures[$chart]=$(( charts_serial_failures[$chart] + 1 ))
697 -
698 - if [ ${charts_serial_failures[$chart]} -gt ${charts_retries[$chart]} ]
699 - then
700 - error "module's '$chart' update() function reported failure ${charts_serial_failures[$chart]} times. Disabling it."
701 - else
702 - error "module's '$chart' update() function reports failure. Will keep trying for a while."
703 - next_charts+=($chart)
704 - fi
705 - fi
706 - else
707 - next_charts+=($chart)
708 - fi
709 - done
710 - MODULE_NAME="${chart}"
711 -
712 - # wait the time you are required to
713 - next_ms=$((now_ms + (update_every * 1000 * 100) ))
714 - for x in "${charts_next_update[@]}"; do [ ${x} -lt ${next_ms} ] && next_ms=${x}; done
715 - next_ms=$((next_ms - now_ms))
716 -
717 - if [ ${LOOPSLEEPMS_HIGHRES} -eq 1 -a ${next_ms} -gt 0 ]
718 - then
719 - next_ms=$(( next_ms + current_time_ms_accuracy ))
720 - seconds=$(( next_ms / 1000 ))
721 - millis=$(( next_ms % 1000 ))
722 - if [ ${millis} -lt 10 ]
723 - then
724 - millis="00${millis}"
725 - elif [ ${millis} -lt 100 ]
726 - then
727 - millis="0${millis}"
728 - fi
729 -
730 - debug "sleeping for ${seconds}.${millis} seconds."
731 - ${mysleep} ${seconds}.${millis}
732 - else
733 - debug "sleeping for ${update_every} seconds."
734 - ${mysleep} $update_every
735 - fi
736 -
737 - test ${now_ms} -ge ${exit_at} && exit 0
738 - done
739 -
740 - fatal "nothing left to do, exiting..."
576 + local exit_at \
577 + c=0 dt ret last_ms exec_start_ms exec_end_ms \
578 + chart now_charts=() next_charts=($run_charts) \
579 + next_ms x seconds millis
580 +
581 + # return the current time in ms in $now_ms
582 + ${current_time_ms}
583 +
584 + exit_at=$((now_ms + (restart_timeout * 1000)))
585 +
586 + for chart in $run_charts; do
587 + eval "charts_update_every[$chart]=\$$chart$suffix_update_every"
588 + test -z "${charts_update_every[$chart]}" && charts_update_every[$chart]=$update_every
589 +
590 + eval "charts_retries[$chart]=\$$chart$suffix_retries"
591 + test -z "${charts_retries[$chart]}" && charts_retries[$chart]=10
592 +
593 + charts_last_update[$chart]=$((now_ms - (now_ms % (charts_update_every[$chart] * 1000))))
594 + charts_next_update[$chart]=$((charts_last_update[$chart] + (charts_update_every[$chart] * 1000)))
595 + charts_run_counter[$chart]=0
596 + charts_serial_failures[$chart]=0
597 +
598 + echo "CHART netdata.plugin_chartsd_$chart '' 'Execution time for $chart plugin' 'milliseconds / run' charts.d netdata.plugin_charts area 145000 ${charts_update_every[$chart]}"
599 + echo "DIMENSION run_time 'run time' absolute 1 1"
600 + done
601 +
602 + # the main loop
603 + while [ "${#next_charts[@]}" -gt 0 ]; do
604 + c=$((c + 1))
605 + now_charts=("${next_charts[@]}")
606 + next_charts=()
607 +
608 + # return the current time in ms in $now_ms
609 + ${current_time_ms}
610 +
611 + for chart in "${now_charts[@]}"; do
612 + MODULE_NAME="${chart}"
613 +
614 + if [ ${now_ms} -ge ${charts_next_update[$chart]} ]; then
615 + last_ms=${charts_last_update[$chart]}
616 + dt=$((now_ms - last_ms))
617 +
618 + charts_last_update[$chart]=${now_ms}
619 +
620 + while [ ${charts_next_update[$chart]} -lt ${now_ms} ]; do
621 + charts_next_update[$chart]=$((charts_next_update[$chart] + (charts_update_every[$chart] * 1000)))
622 + done
623 +
624 + # the first call should not give a duration
625 + # so that netdata calibrates to current time
626 + dt=$((dt * 1000))
627 + charts_run_counter[$chart]=$((charts_run_counter[$chart] + 1))
628 + if [ ${charts_run_counter[$chart]} -eq 1 ]; then
629 + dt=
630 + fi
631 +
632 + exec_start_ms=$now_ms
633 + $chart$charts_update $dt
634 + ret=$?
635 +
636 + # return the current time in ms in $now_ms
637 + ${current_time_ms}
638 + exec_end_ms=$now_ms
639 +
640 + echo "BEGIN netdata.plugin_chartsd_$chart $dt"
641 + echo "SET run_time = $((exec_end_ms - exec_start_ms))"
642 + echo "END"
643 +
644 + if [ $ret -eq 0 ]; then
645 + charts_serial_failures[$chart]=0
646 + next_charts+=($chart)
647 + else
648 + charts_serial_failures[$chart]=$((charts_serial_failures[$chart] + 1))
649 +
650 + if [ ${charts_serial_failures[$chart]} -gt ${charts_retries[$chart]} ]; then
651 + error "module's '$chart' update() function reported failure ${charts_serial_failures[$chart]} times. Disabling it."
652 + else
653 + error "module's '$chart' update() function reports failure. Will keep trying for a while."
654 + next_charts+=($chart)
655 + fi
656 + fi
657 + else
658 + next_charts+=($chart)
659 + fi
660 + done
661 + MODULE_NAME="${chart}"
662 +
663 + # wait the time you are required to
664 + next_ms=$((now_ms + (update_every * 1000 * 100)))
665 + for x in "${charts_next_update[@]}"; do [ ${x} -lt ${next_ms} ] && next_ms=${x}; done
666 + next_ms=$((next_ms - now_ms))
667 +
668 + if [ ${LOOPSLEEPMS_HIGHRES} -eq 1 -a ${next_ms} -gt 0 ]; then
669 + next_ms=$((next_ms + current_time_ms_accuracy))
670 + seconds=$((next_ms / 1000))
671 + millis=$((next_ms % 1000))
672 + if [ ${millis} -lt 10 ]; then
673 + millis="00${millis}"
674 + elif [ ${millis} -lt 100 ]; then
675 + millis="0${millis}"
676 + fi
677 +
678 + debug "sleeping for ${seconds}.${millis} seconds."
679 + ${mysleep} ${seconds}.${millis}
680 + else
681 + debug "sleeping for ${update_every} seconds."
682 + ${mysleep} $update_every
683 + fi
684 +
685 + test ${now_ms} -ge ${exit_at} && exit 0
686 + done
687 +
688 + fatal "nothing left to do, exiting..."
689 }
690
691 global_update
collectors/charts.d.plugin/cpu_apps/cpu_apps.chart.sh
+9 -11
@@ -23,8 +23,7 @@ cpu_apps_check() {
23 # - 0 to enable the chart
24 # - 1 to disable the chart
25
26 - if [ -z "$cpu_apps_apps" ]
27 - then
26 + if [ -z "$cpu_apps_apps" ]; then
27 error "manual configuration required: please set cpu_apps_apps='command1 command2 ...' in $confd/cpu_apps_apps.conf"
28 return 1
29 fi
@@ -38,8 +37,7 @@ cpu_apps_create() {
37 echo "CHART chartsd_apps.cpu '' 'Apps CPU' 'milliseconds / $cpu_apps_update_every sec' apps apps stacked 20001 $cpu_apps_update_every"
38
39 local x=
41 - for x in $cpu_apps_apps
42 - do
40 + for x in $cpu_apps_apps; do
41 echo "DIMENSION $x $x incremental 1000 $cpu_apps_clockticks"
42
43 # this string is needed later in the update() function
@@ -55,15 +53,15 @@ cpu_apps_update() {
53 # remember: KEEP IT SIMPLE AND SHORT
54
55 echo "BEGIN chartsd_apps.cpu"
58 - ps -o pid,comm -C "$cpu_apps_apps" |\
59 - grep -v "COMMAND" |\
56 + ps -o pid,comm -C "$cpu_apps_apps" |
57 + grep -v "COMMAND" |
58 (
61 - while read pid name
62 - do
63 - echo "$name+=`cat /proc/$pid/stat | cut -d ' ' -f 14-15`"
59 + while read pid name; do
60 + echo "$name+=$(cat /proc/$pid/stat | cut -d ' ' -f 14-15)"
61 done
65 - ) |\
66 - ( sed -e "s/ \+/ /g" -e "s/ /+/g";
62 + ) |
63 + (
64 + sed -e "s/ \+/ /g" -e "s/ /+/g"
65 echo "$cpu_apps_bc_finalze"
66 ) | bc
67 echo "END"
collectors/charts.d.plugin/cpufreq/cpufreq.chart.sh
+8 -10
@@ -30,7 +30,7 @@ cpufreq_check() {
30 # - 0 to enable the chart
31 # - 1 to disable the chart
32
33 - [ -z "$( cpufreq_find_all_files "$cpufreq_sys_dir" )" ] && return 1
33 + [ -z "$(cpufreq_find_all_files "$cpufreq_sys_dir")" ] && return 1
34 return 0
35 }
36
@@ -47,16 +47,15 @@ cpufreq_create() {
47 echo >>"$TMP_DIR/cpufreq.sh" "echo \"BEGIN cpu.cpufreq \$1\""
48
49 i=0
50 - for file in $( cpufreq_find_all_files "$cpufreq_sys_dir" | sort -u )
51 - do
52 - i=$(( i + 1 ))
53 - dir=$( dirname "$file" )
50 + for file in $(cpufreq_find_all_files "$cpufreq_sys_dir" | sort -u); do
51 + i=$((i + 1))
52 + dir=$(dirname "$file")
53 cpu=
54
56 - [ -f "$dir/affected_cpus" ] && cpu=$( cat "$dir/affected_cpus" )
55 + [ -f "$dir/affected_cpus" ] && cpu=$(cat "$dir/affected_cpus")
56 [ -z "$cpu" ] && cpu="$i.a"
57
59 - id="$( fixid "cpu$cpu" )"
58 + id="$(fixid "cpu$cpu")"
59
60 debug "file='$file', dir='$dir', cpu='$cpu', id='$id'"
61
@@ -68,7 +67,7 @@ cpufreq_create() {
67 [ $cpufreq_source_update -eq 1 ] && echo >>"$TMP_DIR/cpufreq.sh" "}"
68
69 # ok, load the function cpufreq_update() we created
71 - # shellcheck disable=SC1090
70 + # shellcheck disable=SC1090
71 [ $cpufreq_source_update -eq 1 ] && . "$TMP_DIR/cpufreq.sh"
72
73 return 0
@@ -82,9 +81,8 @@ cpufreq_update() {
81 # do all the work to collect / calculate the values
82 # for each dimension
83 # remember: KEEP IT SIMPLE AND SHORT
85 - # shellcheck disable=SC1090
84 + # shellcheck disable=SC1090
85 [ $cpufreq_source_update -eq 0 ] && . "$TMP_DIR/cpufreq.sh" "$1"
86
87 return 0
88 }
90 -
collectors/charts.d.plugin/example/example.chart.sh
+6 -9
@@ -44,23 +44,20 @@ example_get() {
44 example_value1=$RANDOM
45 example_value2=$RANDOM
46 example_value3=$RANDOM
47 - example_value4=$((8192 + (RANDOM * 16383 / 32767) ))
47 + example_value4=$((8192 + (RANDOM * 16383 / 32767)))
48
49 - if [ $example_count -gt 0 ]
50 - then
49 + if [ $example_count -gt 0 ]; then
50 example_count=$((example_count - 1))
51
53 - [ $example_last -gt 16383 ] && example_value4=$((example_last + (RANDOM * ( (32767 - example_last) / 2) / 32767)))
52 + [ $example_last -gt 16383 ] && example_value4=$((example_last + (RANDOM * ((32767 - example_last) / 2) / 32767)))
53 [ $example_last -le 16383 ] && example_value4=$((example_last - (RANDOM * (example_last / 2) / 32767)))
54 else
56 - example_count=$((1 + (RANDOM * 5 / 32767) ))
55 + example_count=$((1 + (RANDOM * 5 / 32767)))
56
58 - if [ $example_last -gt 16383 ] && [ $example_value4 -gt 16383 ]
59 - then
57 + if [ $example_last -gt 16383 ] && [ $example_value4 -gt 16383 ]; then
58 example_value4=$((example_value4 - 16383))
59 fi
62 - if [ $example_last -le 16383 ] && [ $example_value4 -lt 16383 ]
63 - then
60 + if [ $example_last -le 16383 ] && [ $example_value4 -lt 16383 ]; then
61 example_value4=$((example_value4 + 16383))
62 fi
63 fi
collectors/charts.d.plugin/exim/exim.chart.sh
+12 -14
@@ -17,14 +17,12 @@ exim_update_every=5
17 exim_priority=60000
18
19 exim_check() {
20 - if [ -z "${exim_command}" ]
21 - then
22 - require_cmd exim || return 1
23 - exim_command="${EXIM_CMD}"
24 - fi
25 -
26 - if [ "$(${exim_command} -bpc 2>&1 | grep -c denied)" -ne 0 ]
27 - then
20 + if [ -z "${exim_command}" ]; then
21 + require_cmd exim || return 1
22 + exim_command="${EXIM_CMD}"
23 + fi
24 +
25 + if [ "$(${exim_command} -bpc 2>&1 | grep -c denied)" -ne 0 ]; then
26 error "permission denied - please set 'queue_list_requires_admin = false' in your exim options file"
27 return 1
28 fi
@@ -33,16 +31,16 @@ exim_check() {
31 }
32
33 exim_create() {
36 - cat <<EOF
34 + cat <<EOF
35 CHART exim_local.qemails '' "Exim Queue Emails" "emails" queue exim.queued.emails line $((exim_priority + 1)) $exim_update_every
36 DIMENSION emails '' absolute 1 1
37 EOF
40 - return 0
38 + return 0
39 }
40
41 exim_update() {
44 - echo "BEGIN exim_local.qemails $1"
45 - echo "SET emails = $(run "${exim_command}" -bpc)"
46 - echo "END"
47 - return 0
42 + echo "BEGIN exim_local.qemails $1"
43 + echo "SET emails = $(run "${exim_command}" -bpc)"
44 + echo "END"
45 + return 0
46 }
collectors/charts.d.plugin/hddtemp/hddtemp.chart.sh
+12 -12
@@ -21,7 +21,7 @@ hddtemp_priority=90000
21
22 # _check is called once, to find out if this chart should be enabled or not
23 hddtemp_check() {
24 - require_cmd nc || return 1
24 + require_cmd nc || return 1
25 run nc $hddtemp_host $hddtemp_port && return 0 || return 1
26 }
27
@@ -29,17 +29,17 @@ hddtemp_check() {
29 hddtemp_create() {
30 if [ ${#hddtemp_disks[@]} -eq 0 ]; then
31 local all
32 - all=$(nc $hddtemp_host $hddtemp_port )
32 + all=$(nc $hddtemp_host $hddtemp_port)
33 unset hddtemp_disks
34 # shellcheck disable=SC2190,SC2207
35 - hddtemp_disks=( $(grep -Po '/dev/[^|]+' <<< "$all" | cut -c 6-) )
35 + hddtemp_disks=($(grep -Po '/dev/[^|]+' <<<"$all" | cut -c 6-))
36 fi
37 -# local disk_names
38 -# disk_names=(`sed -e 's/||/\n/g;s/^|//' <<< "$all" | cut -d '|' -f2 | tr ' ' '_'`)
37 + # local disk_names
38 + # disk_names=(`sed -e 's/||/\n/g;s/^|//' <<< "$all" | cut -d '|' -f2 | tr ' ' '_'`)
39
40 echo "CHART hddtemp.temperature 'disks_temp' 'temperature' 'Celsius' 'Disks temperature' 'hddtemp.temp' line $((hddtemp_priority)) $hddtemp_update_every"
41 - for i in $(seq 0 $((${#hddtemp_disks[@]}-1))); do
42 -# echo "DIMENSION ${hddtemp_disks[i]} ${disk_names[i]} absolute 1 1"
41 + for i in $(seq 0 $((${#hddtemp_disks[@]} - 1))); do
42 + # echo "DIMENSION ${hddtemp_disks[i]} ${disk_names[i]} absolute 1 1"
43 echo "DIMENSION ${hddtemp_disks[$i]} '' absolute 1 1"
44 done
45 return 0
@@ -49,12 +49,12 @@ hddtemp_create() {
49 #hddtemp_last=0
50 #hddtemp_count=0
51 hddtemp_update() {
52 -# local all=( `nc $hddtemp_host $hddtemp_port | sed -e 's/||/\n/g;s/^|//' | cut -d '|' -f3` )
53 -# local all=( `nc $hddtemp_host $hddtemp_port | awk 'BEGIN { FS="|" };{i=4; while (i <= NF) {print $i+0;i+=5;};}'` )
52 + # local all=( `nc $hddtemp_host $hddtemp_port | sed -e 's/||/\n/g;s/^|//' | cut -d '|' -f3` )
53 + # local all=( `nc $hddtemp_host $hddtemp_port | awk 'BEGIN { FS="|" };{i=4; while (i <= NF) {print $i+0;i+=5;};}'` )
54 OLD_IFS=$IFS
55 set -f
56 # shellcheck disable=SC2207
57 - IFS="|" all=( $(nc $hddtemp_host $hddtemp_port 2>/dev/null) )
57 + IFS="|" all=($(nc $hddtemp_host $hddtemp_port 2>/dev/null))
58 set +f
59 IFS=$OLD_IFS
60
@@ -66,9 +66,9 @@ hddtemp_update() {
66 # write the result of the work.
67 echo "BEGIN hddtemp.temperature $1"
68 end=${#hddtemp_disks[@]}
69 - for ((i=0; i<end; i++)); do
69 + for ((i = 0; i < end; i++)); do
70 # temperature - this will turn SLP to zero
71 - t=$(( ${all[ $((i * 5 + 3)) ]} ))
71 + t=$((all[$((i * 5 + 3))]))
72 echo "SET ${hddtemp_disks[$i]} = $t"
73 done
74 echo "END"
collectors/charts.d.plugin/libreswan/libreswan.chart.sh
+14 -18
@@ -39,8 +39,7 @@ declare -A libreswan_tunnel_charts=()
39
40 # run the ipsec command
41 libreswan_ipsec() {
42 - if [ ${libreswan_sudo} -ne 0 ]
43 - then
42 + if [ ${libreswan_sudo} -ne 0 ]; then
43 sudo -n "${IPSEC_CMD}" "${@}"
44 return $?
45 else
@@ -61,15 +60,15 @@ libreswan_get() {
60 libreswan_connected_tunnels=()
61
62 # convert the ipsec command output to a shell script
64 - # and source it to get the values
65 - # shellcheck disable=SC1090
63 + # and source it to get the values
64 + # shellcheck disable=SC1090
65 source <(
66 {
68 - libreswan_ipsec whack --status;
69 - libreswan_ipsec whack --trafficstatus;
67 + libreswan_ipsec whack --status
68 + libreswan_ipsec whack --trafficstatus
69 } | sed -n \
71 - -e "s|[0-9]\+ #\([0-9]\+\): \"\(.*\)\".*IPsec SA established.*newest IPSEC.*|libreswan_connected_tunnels[\"\1\"]=\"\2\"|p" \
72 - -e "s|[0-9]\+ #\([0-9]\+\): \"\(.*\)\",.* add_time=\([0-9]\+\),.* inBytes=\([0-9]\+\),.* outBytes=\([0-9]\+\).*|libreswan_traffic_in[\"\1\"]=\"\4\"; libreswan_traffic_out[\"\1\"]=\"\5\"; libreswan_established_add_time[\"\1\"]=\"\3\";|p"
70 + -e "s|[0-9]\+ #\([0-9]\+\): \"\(.*\)\".*IPsec SA established.*newest IPSEC.*|libreswan_connected_tunnels[\"\1\"]=\"\2\"|p" \
71 + -e "s|[0-9]\+ #\([0-9]\+\): \"\(.*\)\",.* add_time=\([0-9]\+\),.* inBytes=\([0-9]\+\),.* outBytes=\([0-9]\+\).*|libreswan_traffic_in[\"\1\"]=\"\4\"; libreswan_traffic_out[\"\1\"]=\"\5\"; libreswan_established_add_time[\"\1\"]=\"\3\";|p"
72 ) || return 1
73
74 # check we got some data
@@ -88,10 +87,9 @@ libreswan_check() {
87
88 # make sure it is libreswan
89 # shellcheck disable=SC2143
91 - if [ -z "$(ipsec --version | grep -i libreswan)" ]
92 - then
93 - error "ipsec command is not Libreswan. Disabling Libreswan plugin."
94 - return 1
90 + if [ -z "$(ipsec --version | grep -i libreswan)" ]; then
91 + error "ipsec command is not Libreswan. Disabling Libreswan plugin."
92 + return 1
93 fi
94
95 # check that we can collect data
@@ -125,8 +123,7 @@ EOF
123 # _create is called once, to create the charts
124 libreswan_create() {
125 local n
128 - for n in "${!libreswan_connected_tunnels[@]}"
129 - do
126 + for n in "${!libreswan_connected_tunnels[@]}"; do
127 libreswan_create_one "${n}"
128 done
129 return 0
@@ -143,10 +140,10 @@ libreswan_update_one() {
140
141 [ -z "${id}" ] && libreswan_create_one "${name}"
142
146 - uptime=$(( libreswan_now - libreswan_established_add_time[${n}] ))
143 + uptime=$((libreswan_now - libreswan_established_add_time[${n}]))
144 [ ${uptime} -lt 0 ] && uptime=0
145
149 - # write the result of the work.
146 + # write the result of the work.
147 cat <<VALUESEOF
148 BEGIN libreswan.${id}_net ${microseconds}
149 SET in = ${libreswan_traffic_in[${n}]}
@@ -167,8 +164,7 @@ libreswan_update() {
164 libreswan_now=$(date +%s)
165
166 local n
170 - for n in "${!libreswan_connected_tunnels[@]}"
171 - do
167 + for n in "${!libreswan_connected_tunnels[@]}"; do
168 libreswan_update_one "${n}" "${@}"
169 done
170
collectors/charts.d.plugin/load_average/load_average.chart.sh
+6 -8
@@ -21,8 +21,7 @@ load_average_check() {
21 # - 0 to enable the chart
22 # - 1 to disable the chart
23
24 - if [ ${load_average_update_every} -lt 5 ]
25 - then
24 + if [ ${load_average_update_every} -lt 5 ]; then
25 # there is no meaning for shorter than 5 seconds
26 # the kernel changes this value every 5 seconds
27 load_average_update_every=5
@@ -34,7 +33,7 @@ load_average_check() {
33
34 load_average_create() {
35 # create a chart with 3 dimensions
37 -cat <<EOF
36 + cat <<EOF
37 CHART system.load '' "System Load Average" "load" load system.load line $((load_priority + 1)) $load_average_update_every
38 DIMENSION load1 '1 min' absolute 1 100
39 DIMENSION load5 '5 mins' absolute 1 100
@@ -52,10 +51,10 @@ load_average_update() {
51 # here we parse the system average load
52 # it is decimal (with 2 decimal digits), so we remove the dot and
53 # at the definition we have divisor = 100, to have the graph show the right value
55 - loadavg="`cat /proc/loadavg | sed -e "s/\.//g"`"
56 - load1=`echo $loadavg | cut -d ' ' -f 1`
57 - load5=`echo $loadavg | cut -d ' ' -f 2`
58 - load15=`echo $loadavg | cut -d ' ' -f 3`
54 + loadavg="$(cat /proc/loadavg | sed -e "s/\.//g")"
55 + load1=$(echo $loadavg | cut -d ' ' -f 1)
56 + load5=$(echo $loadavg | cut -d ' ' -f 2)
57 + load15=$(echo $loadavg | cut -d ' ' -f 3)
58
59 # write the result of the work.
60 cat <<VALUESEOF
@@ -68,4 +67,3 @@ VALUESEOF
67
68 return 0
69 }
71 -
collectors/charts.d.plugin/loopsleepms.sh.inc
+132 -150
@@ -2,10 +2,9 @@
2 # SPDX-License-Identifier: GPL-3.0-or-later
3
4 LOOPSLEEP_DATE="$(which date 2>/dev/null || command -v date 2>/dev/null)"
5 -if [ -z "$LOOPSLEEP_DATE" ]
6 - then
7 - echo >&2 "$0: ERROR: Cannot find the command 'date' in the system path."
8 - exit 1
5 +if [ -z "$LOOPSLEEP_DATE" ]; then
6 + echo >&2 "$0: ERROR: Cannot find the command 'date' in the system path."
7 + exit 1
8 fi
9
10 # -----------------------------------------------------------------------------
@@ -14,14 +13,13 @@ fi
13 now_ms=
14 LOOPSLEEPMS_HIGHRES=1
15 test "$($LOOPSLEEP_DATE +%N)" = "%N" && LOOPSLEEPMS_HIGHRES=0
17 -test -z "$($LOOPSLEEP_DATE +%N)" && LOOPSLEEPMS_HIGHRES=0
16 +test -z "$($LOOPSLEEP_DATE +%N)" && LOOPSLEEPMS_HIGHRES=0
17 current_time_ms_from_date() {
19 - if [ $LOOPSLEEPMS_HIGHRES -eq 0 ]
20 - then
21 - now_ms="$($LOOPSLEEP_DATE +'%s')000"
22 - else
23 - now_ms="$(( $( $LOOPSLEEP_DATE +'%s * 1000 + %-N / 1000000' ) ))"
24 - fi
18 + if [ $LOOPSLEEPMS_HIGHRES -eq 0 ]; then
19 + now_ms="$($LOOPSLEEP_DATE +'%s')000"
20 + else
21 + now_ms="$(($($LOOPSLEEP_DATE +'%s * 1000 + %-N / 1000000')))"
22 + fi
23 }
24
25 # -----------------------------------------------------------------------------
@@ -32,55 +30,49 @@ current_time_ms_from_uptime_started="${now_ms}"
30 current_time_ms_from_uptime_last="${now_ms}"
31 current_time_ms_from_uptime_first=0
32 current_time_ms_from_uptime() {
35 - local up rest arr=() n
36 -
37 - read up rest </proc/uptime
38 - if [ $? -ne 0 ]
39 - then
40 - echo >&2 "$0: Cannot read /proc/uptime - falling back to current_time_ms_from_date()."
41 - current_time_ms="current_time_ms_from_date"
42 - current_time_ms_from_date
43 - current_time_ms_accuracy=1
44 - return
45 - fi
46 -
47 - arr=(${up//./ })
48 -
49 - if [ ${#arr[1]} -lt 1 ]
50 - then
51 - n="${arr[0]}000"
52 - elif [ ${#arr[1]} -lt 2 ]
53 - then
54 - n="${arr[0]}${arr[1]}00"
55 - elif [ ${#arr[1]} -lt 3 ]
56 - then
57 - n="${arr[0]}${arr[1]}0"
58 - else
59 - n="${arr[0]}${arr[1]}"
60 - fi
61 -
62 - now_ms=$((current_time_ms_from_uptime_started - current_time_ms_from_uptime_first + n))
63 -
64 - if [ "${now_ms}" -lt "${current_time_ms_from_uptime_last}" ]
65 - then
66 - echo >&2 "$0: Cannot use current_time_ms_from_uptime() - new time ${now_ms} is older than the last ${current_time_ms_from_uptime_last} - falling back to current_time_ms_from_date()."
67 - current_time_ms="current_time_ms_from_date"
68 - current_time_ms_from_date
69 - current_time_ms_accuracy=1
70 - fi
71 -
72 - current_time_ms_from_uptime_last="${now_ms}"
33 + local up rest arr=() n
34 +
35 + read up rest </proc/uptime
36 + if [ $? -ne 0 ]; then
37 + echo >&2 "$0: Cannot read /proc/uptime - falling back to current_time_ms_from_date()."
38 + current_time_ms="current_time_ms_from_date"
39 + current_time_ms_from_date
40 + current_time_ms_accuracy=1
41 + return
42 + fi
43 +
44 + arr=(${up//./ })
45 +
46 + if [ ${#arr[1]} -lt 1 ]; then
47 + n="${arr[0]}000"
48 + elif [ ${#arr[1]} -lt 2 ]; then
49 + n="${arr[0]}${arr[1]}00"
50 + elif [ ${#arr[1]} -lt 3 ]; then
51 + n="${arr[0]}${arr[1]}0"
52 + else
53 + n="${arr[0]}${arr[1]}"
54 + fi
55 +
56 + now_ms=$((current_time_ms_from_uptime_started - current_time_ms_from_uptime_first + n))
57 +
58 + if [ "${now_ms}" -lt "${current_time_ms_from_uptime_last}" ]; then
59 + echo >&2 "$0: Cannot use current_time_ms_from_uptime() - new time ${now_ms} is older than the last ${current_time_ms_from_uptime_last} - falling back to current_time_ms_from_date()."
60 + current_time_ms="current_time_ms_from_date"
61 + current_time_ms_from_date
62 + current_time_ms_accuracy=1
63 + fi
64 +
65 + current_time_ms_from_uptime_last="${now_ms}"
66 }
67 current_time_ms_from_uptime
68 current_time_ms_from_uptime_first="$((now_ms - current_time_ms_from_uptime_started))"
69 current_time_ms_from_uptime_last="${current_time_ms_from_uptime_first}"
70 current_time_ms="current_time_ms_from_uptime"
71 current_time_ms_accuracy=10
79 -if [ "${current_time_ms_from_uptime_first}" -eq 0 ]
80 - then
81 - echo >&2 "$0: Invalid setup for current_time_ms_from_uptime() - falling back to current_time_ms_from_date()."
82 - current_time_ms="current_time_ms_from_date"
83 - current_time_ms_accuracy=1
72 +if [ "${current_time_ms_from_uptime_first}" -eq 0 ]; then
73 + echo >&2 "$0: Invalid setup for current_time_ms_from_uptime() - falling back to current_time_ms_from_date()."
74 + current_time_ms="current_time_ms_from_date"
75 + current_time_ms_accuracy=1
76 fi
77
78 # -----------------------------------------------------------------------------
@@ -94,55 +86,48 @@ mysleep_fifo="${NETDATA_CACHE_DIR-/tmp}/.netdata_bash_sleep_timer_fifo"
86 [ -p "${mysleep_fifo}" ] && mysleep="mysleep_read"
87
88 mysleep_read() {
97 - read -t "${1}" <>"${mysleep_fifo}"
98 - ret=$?
99 - if [ $ret -le 128 ]
100 - then
101 - echo >&2 "$0: Cannot use read for sleeping (return code ${ret})."
102 - mysleep="sleep"
103 - ${mysleep} "${1}"
104 - fi
89 + read -t "${1}" <>"${mysleep_fifo}"
90 + ret=$?
91 + if [ $ret -le 128 ]; then
92 + echo >&2 "$0: Cannot use read for sleeping (return code ${ret})."
93 + mysleep="sleep"
94 + ${mysleep} "${1}"
95 + fi
96 }
97
98 # -----------------------------------------------------------------------------
99 # use bash loadable module for sleep
100
101 mysleep_builtin() {
111 - builtin sleep "${1}"
112 - ret=$?
113 - if [ $ret -ne 0 ]
114 - then
115 - echo >&2 "$0: Cannot use builtin sleep for sleeping (return code ${ret})."
116 - mysleep="sleep"
117 - ${mysleep} "${1}"
118 - fi
102 + builtin sleep "${1}"
103 + ret=$?
104 + if [ $ret -ne 0 ]; then
105 + echo >&2 "$0: Cannot use builtin sleep for sleeping (return code ${ret})."
106 + mysleep="sleep"
107 + ${mysleep} "${1}"
108 + fi
109 }
110
121 -if [ -z "${mysleep}" -a "$((BASH_VERSINFO[0] +0))" -ge 3 -a "${NETDATA_BASH_LOADABLES}" != "DISABLE" ]
122 - then
123 - # enable modules only for bash version 3+
124 -
125 - for bash_modules_path in ${BASH_LOADABLES_PATH//:/ } "$(pkg-config bash --variable=loadablesdir 2>/dev/null)" "/usr/lib/bash" "/lib/bash" "/lib64/bash" "/usr/local/lib/bash" "/usr/local/lib64/bash"
126 - do
127 - [ -z "${bash_modules_path}" -o ! -d "${bash_modules_path}" ] && continue
128 -
129 - # check for sleep
130 - for bash_module_sleep in "sleep" "sleep.so"
131 - do
132 - if [ -f "${bash_modules_path}/${bash_module_sleep}" ]
133 - then
134 - if enable -f "${bash_modules_path}/${bash_module_sleep}" sleep 2>/dev/null
135 - then
136 - mysleep="mysleep_builtin"
137 - # echo >&2 "$0: Using bash loadable ${bash_modules_path}/${bash_module_sleep} for sleep"
138 - break
139 - fi
140 - fi
141 -
142 - done
143 -
144 - [ ! -z "${mysleep}" ] && break
145 - done
111 +if [ -z "${mysleep}" -a "$((BASH_VERSINFO[0] + 0))" -ge 3 -a "${NETDATA_BASH_LOADABLES}" != "DISABLE" ]; then
112 + # enable modules only for bash version 3+
113 +
114 + for bash_modules_path in ${BASH_LOADABLES_PATH//:/ } "$(pkg-config bash --variable=loadablesdir 2>/dev/null)" "/usr/lib/bash" "/lib/bash" "/lib64/bash" "/usr/local/lib/bash" "/usr/local/lib64/bash"; do
115 + [ -z "${bash_modules_path}" -o ! -d "${bash_modules_path}" ] && continue
116 +
117 + # check for sleep
118 + for bash_module_sleep in "sleep" "sleep.so"; do
119 + if [ -f "${bash_modules_path}/${bash_module_sleep}" ]; then
120 + if enable -f "${bash_modules_path}/${bash_module_sleep}" sleep 2>/dev/null; then
121 + mysleep="mysleep_builtin"
122 + # echo >&2 "$0: Using bash loadable ${bash_modules_path}/${bash_module_sleep} for sleep"
123 + break
124 + fi
125 + fi
126 +
127 + done
128 +
129 + [ ! -z "${mysleep}" ] && break
130 + done
131 fi
132
133 # -----------------------------------------------------------------------------
@@ -150,7 +135,6 @@ fi
135
136 [ -z "${mysleep}" ] && mysleep="sleep"
137
153 -
138 # -----------------------------------------------------------------------------
139 # this function is used to sleep a fraction of a second
140 # it calculates the difference between every time is called
@@ -163,60 +147,58 @@ LOOPSLEEPMS_LASTSLEEP=0
147 LOOPSLEEPMS_LASTWORK=0
148
149 loopsleepms() {
166 - local tellwork=0 t="${1}" div s m now mstosleep
167 -
168 - if [ "${t}" = "tellwork" ]
169 - then
170 - tellwork=1
171 - shift
172 - t="${1}"
173 - fi
174 -
175 - # $t = the time in seconds to wait
176 -
177 - # if high resolution is not supported
178 - # just sleep the time requested, in seconds
179 - if [ ${LOOPSLEEPMS_HIGHRES} -eq 0 ]
180 - then
181 - sleep ${t}
182 - return
183 - fi
184 -
185 - # get the current time, in ms in ${now_ms}
186 - ${current_time_ms}
187 -
188 - # calculate ms since last run
189 - [ ${LOOPSLEEPMS_LASTRUN} -gt 0 ] && \
190 - LOOPSLEEPMS_LASTWORK=$((now_ms - LOOPSLEEPMS_LASTRUN - LOOPSLEEPMS_LASTSLEEP + current_time_ms_accuracy))
191 - # echo "# last loop's work took $LOOPSLEEPMS_LASTWORK ms"
192 -
193 - # remember this run
194 - LOOPSLEEPMS_LASTRUN=${now_ms}
195 -
196 - # calculate the next run
197 - LOOPSLEEPMS_NEXTRUN=$(( ( now_ms - ( now_ms % ( t * 1000 ) ) ) + ( t * 1000 ) ))
198 -
199 - # calculate ms to sleep
200 - mstosleep=$(( LOOPSLEEPMS_NEXTRUN - now_ms + current_time_ms_accuracy ))
201 - # echo "# mstosleep is $mstosleep ms"
202 -
203 - # if we are too slow, sleep some time
204 - test ${mstosleep} -lt 200 && mstosleep=200
205 -
206 - s=$(( mstosleep / 1000 ))
207 - m=$(( mstosleep - (s * 1000) ))
208 - [ "${m}" -lt 100 ] && m="0${m}"
209 - [ "${m}" -lt 10 ] && m="0${m}"
210 -
211 - test $tellwork -eq 1 && echo >&2 " >>> PERFORMANCE >>> WORK TOOK ${LOOPSLEEPMS_LASTWORK} ms ( $((LOOPSLEEPMS_LASTWORK * 100 / 1000)).$((LOOPSLEEPMS_LASTWORK % 10))% cpu ) >>> SLEEPING ${mstosleep} ms"
212 -
213 - # echo "# sleeping ${s}.${m}"
214 - # echo
215 - ${mysleep} ${s}.${m}
216 -
217 - # keep the values we need
218 - # for our next run
219 - LOOPSLEEPMS_LASTSLEEP=$mstosleep
150 + local tellwork=0 t="${1}" div s m now mstosleep
151 +
152 + if [ "${t}" = "tellwork" ]; then
153 + tellwork=1
154 + shift
155 + t="${1}"
156 + fi
157 +
158 + # $t = the time in seconds to wait
159 +
160 + # if high resolution is not supported
161 + # just sleep the time requested, in seconds
162 + if [ ${LOOPSLEEPMS_HIGHRES} -eq 0 ]; then
163 + sleep ${t}
164 + return
165 + fi
166 +
167 + # get the current time, in ms in ${now_ms}
168 + ${current_time_ms}
169 +
170 + # calculate ms since last run
171 + [ ${LOOPSLEEPMS_LASTRUN} -gt 0 ] &&
172 + LOOPSLEEPMS_LASTWORK=$((now_ms - LOOPSLEEPMS_LASTRUN - LOOPSLEEPMS_LASTSLEEP + current_time_ms_accuracy))
173 + # echo "# last loop's work took $LOOPSLEEPMS_LASTWORK ms"
174 +
175 + # remember this run
176 + LOOPSLEEPMS_LASTRUN=${now_ms}
177 +
178 + # calculate the next run
179 + LOOPSLEEPMS_NEXTRUN=$(((now_ms - (now_ms % (t * 1000))) + (t * 1000)))
180 +
181 + # calculate ms to sleep
182 + mstosleep=$((LOOPSLEEPMS_NEXTRUN - now_ms + current_time_ms_accuracy))
183 + # echo "# mstosleep is $mstosleep ms"
184 +
185 + # if we are too slow, sleep some time
186 + test ${mstosleep} -lt 200 && mstosleep=200
187 +
188 + s=$((mstosleep / 1000))
189 + m=$((mstosleep - (s * 1000)))
190 + [ "${m}" -lt 100 ] && m="0${m}"
191 + [ "${m}" -lt 10 ] && m="0${m}"
192 +
193 + test $tellwork -eq 1 && echo >&2 " >>> PERFORMANCE >>> WORK TOOK ${LOOPSLEEPMS_LASTWORK} ms ( $((LOOPSLEEPMS_LASTWORK * 100 / 1000)).$((LOOPSLEEPMS_LASTWORK % 10))% cpu ) >>> SLEEPING ${mstosleep} ms"
194 +
195 + # echo "# sleeping ${s}.${m}"
196 + # echo
197 + ${mysleep} ${s}.${m}
198 +
199 + # keep the values we need
200 + # for our next run
201 + LOOPSLEEPMS_LASTSLEEP=$mstosleep
202 }
203
204 # test it
collectors/charts.d.plugin/mem_apps/mem_apps.chart.sh
+6 -7
@@ -20,8 +20,7 @@ mem_apps_check() {
20 # - 0 to enable the chart
21 # - 1 to disable the chart
22
23 - if [ -z "$mem_apps_apps" ]
24 - then
23 + if [ -z "$mem_apps_apps" ]; then
24 error "manual configuration required: please set mem_apps_apps='command1 command2 ...' in $confd/mem_apps_apps.conf"
25 return 1
26 fi
@@ -35,8 +34,7 @@ mem_apps_create() {
34 echo "CHART chartsd_apps.mem '' 'Apps Memory' MB apps apps.mem stacked 20000 $mem_apps_update_every"
35
36 local x=
38 - for x in $mem_apps_apps
39 - do
37 + for x in $mem_apps_apps; do
38 echo "DIMENSION $x $x absolute 1 1024"
39
40 # this string is needed later in the update() function
@@ -52,9 +50,10 @@ mem_apps_update() {
50 # remember: KEEP IT SIMPLE AND SHORT
51
52 echo "BEGIN chartsd_apps.mem"
55 - ps -o comm,rss -C "$mem_apps_apps" |\
56 - grep -v "^COMMAND" |\
57 - ( sed -e "s/ \+/ /g" -e "s/ /+=/g";
53 + ps -o comm,rss -C "$mem_apps_apps" |
54 + grep -v "^COMMAND" |
55 + (
56 + sed -e "s/ \+/ /g" -e "s/ /+=/g"
57 echo "$mem_apps_bc_finalze"
58 ) | bc
59 echo "END"
collectors/charts.d.plugin/mysql/mysql.chart.sh
+25 -42
@@ -27,19 +27,19 @@ mysql_get() {
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 # 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]+" ))
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
34 local end=${#arr[@]}
35 - for ((i=2;i<end;i+=2)); do
36 - mysql_data["${arr[$i]}"]=${arr[$i+1]}
35 + for ((i = 2; i < end; i += 2)); do
36 + mysql_data["${arr[$i]}"]=${arr[i + 1]}
37 done
38
39 [ -z "${mysql_data[Connections]}" ] && return 1
40
41 mysql_data[Thread_cache_misses]=0
42 - [ $(( mysql_data[Connections] + 1 - 1 )) -gt 0 ] && mysql_data[Thread_cache_misses]=$(( mysql_data[Threads_created] * 10000 / mysql_data[Connections] ))
42 + [ $((mysql_data[Connections] + 1 - 1)) -gt 0 ] && mysql_data[Thread_cache_misses]=$((mysql_data[Threads_created] * 10000 / mysql_data[Connections]))
43
44 return 0
45 }
@@ -51,23 +51,20 @@ mysql_check() {
51
52 local x m mysql_cmd tryroot=0 unconfigured=0
53
54 - if [ "${1}" = "tryroot" ]
55 - then
54 + if [ "${1}" = "tryroot" ]; then
55 tryroot=1
56 shift
57 fi
58
60 - # shellcheck disable=SC2230
59 + # shellcheck disable=SC2230
60 [ -z "${mysql_cmd}" ] && mysql_cmd="$(which mysql 2>/dev/null || command -v mysql 2>/dev/null)"
61
63 - if [ ${#mysql_opts[@]} -eq 0 ]
64 - then
62 + if [ ${#mysql_opts[@]} -eq 0 ]; then
63 unconfigured=1
64
65 mysql_cmds[local]="$mysql_cmd"
66
69 - if [ $tryroot -eq 1 ]
70 - then
67 + if [ $tryroot -eq 1 ]; then
68 # the user has not configured us for mysql access
69 # if the root user is passwordless in mysql, we can
70 # attempt to connect to mysql as root
@@ -78,19 +75,16 @@ mysql_check() {
75 fi
76
77 # check once if the url works
81 - for m in "${!mysql_opts[@]}"
82 - do
78 + for m in "${!mysql_opts[@]}"; do
79 [ -z "${mysql_cmds[$m]}" ] && mysql_cmds[$m]="$mysql_cmd"
84 - if [ -z "${mysql_cmds[$m]}" ]
85 - then
80 + if [ -z "${mysql_cmds[$m]}" ]; then
81 # shellcheck disable=SC2154
82 error "cannot get mysql command for '${m}'. Please set mysql_cmds[$m]='/path/to/mysql', in $confd/mysql.conf"
83 fi
84
85 mysql_get "${mysql_cmds[$m]}" ${mysql_opts[$m]}
86 # shellcheck disable=SC2181
92 - if [ ! $? -eq 0 ]
93 - then
87 + if [ ! $? -eq 0 ]; then
88 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"
89 unset "mysql_cmds[$m]"
90 unset "mysql_opts[$m]"
@@ -98,13 +92,11 @@ mysql_check() {
92 continue
93 fi
94
101 - mysql_ids[$m]="$( fixid "$m" )"
95 + mysql_ids[$m]="$(fixid "$m")"
96 done
97
104 - if [ ${#mysql_opts[@]} -eq 0 ]
105 - then
106 - if [ ${unconfigured} -eq 1 ] && [ ${tryroot} -eq 0 ]
107 - then
98 + if [ ${#mysql_opts[@]} -eq 0 ]; then
99 + if [ ${unconfigured} -eq 1 ] && [ ${tryroot} -eq 0 ]; then
100 mysql_check tryroot "${@}"
101 return $?
102 else
@@ -120,8 +112,7 @@ mysql_create() {
112 local x
113
114 # create the charts
123 - for x in "${mysql_ids[@]}"
124 - do
115 + for x in "${mysql_ids[@]}"; do
116 cat <<EOF
117 CHART mysql_$x.net '' "mysql Bandwidth" "kilobits/s" bandwidth mysql.net area $((mysql_priority + 1)) $mysql_update_every
118 DIMENSION Bytes_received in incremental 8 1024
@@ -283,18 +274,16 @@ CHART mysql_$x.files_rate '' "mysql Opened Files Rate" "files/s" files mysql.fil
274 DIMENSION Opened_files files incremental 1 1
275 EOF
276
286 - if [ ! -z "${mysql_data[Binlog_stmt_cache_disk_use]}" ]
287 - then
288 - cat <<EOF
277 + if [ ! -z "${mysql_data[Binlog_stmt_cache_disk_use]}" ]; then
278 + cat <<EOF
279 CHART mysql_$x.binlog_stmt_cache '' "mysql Binlog Statement Cache" "statements/s" binlog mysql.binlog_stmt_cache line $((mysql_priority + 50)) $mysql_update_every
280 DIMENSION Binlog_stmt_cache_disk_use disk incremental 1 1
281 DIMENSION Binlog_stmt_cache_use all incremental 1 1
282 EOF
293 - fi
283 + fi
284
295 - if [ ! -z "${mysql_data[Connection_errors_accept]}" ]
296 - then
297 - cat <<EOF
285 + if [ ! -z "${mysql_data[Connection_errors_accept]}" ]; then
286 + cat <<EOF
287 CHART mysql_$x.connection_errors '' "mysql Connection Errors" "connections/s" connections mysql.connection_errors line $((mysql_priority + 51)) $mysql_update_every
288 DIMENSION Connection_errors_accept accept incremental 1 1
289 DIMENSION Connection_errors_internal internal incremental 1 1
@@ -303,13 +292,12 @@ DIMENSION Connection_errors_peer_addr peer_addr incremental 1 1
292 DIMENSION Connection_errors_select select incremental 1 1
293 DIMENSION Connection_errors_tcpwrap tcpwrap incremental 1 1
294 EOF
306 - fi
295 + fi
296
297 done
298 return 0
299 }
300
312 -
301 mysql_update() {
302 # the first argument to this function is the microseconds since last update
303 # pass this parameter to the BEGIN statement (see bellow).
@@ -319,14 +307,12 @@ mysql_update() {
307 # remember: KEEP IT SIMPLE AND SHORT
308
309 local m x
322 - for m in "${!mysql_ids[@]}"
323 - do
310 + for m in "${!mysql_ids[@]}"; do
311 x="${mysql_ids[$m]}"
312 mysql_get "${mysql_cmds[$m]}" ${mysql_opts[$m]}
313
314 # shellcheck disable=SC2181
328 - if [ $? -ne 0 ]
329 - then
315 + if [ $? -ne 0 ]; then
316 unset "mysql_ids[$m]"
317 unset "mysql_opts[$m]"
318 unset "mysql_cmds[$m]"
@@ -497,8 +483,7 @@ SET Opened_files = ${mysql_data[Opened_files]}
483 END
484 VALUESEOF
485
500 - if [ ! -z "${mysql_data[Binlog_stmt_cache_disk_use]}" ]
501 - then
486 + if [ ! -z "${mysql_data[Binlog_stmt_cache_disk_use]}" ]; then
487 cat <<VALUESEOF
488 BEGIN mysql_$x.binlog_stmt_cache $1
489 SET Binlog_stmt_cache_disk_use = ${mysql_data[Binlog_stmt_cache_disk_use]}
@@ -507,8 +492,7 @@ END
492 VALUESEOF
493 fi
494
510 - if [ ! -z "${mysql_data[Connection_errors_accept]}" ]
511 - then
495 + if [ ! -z "${mysql_data[Connection_errors_accept]}" ]; then
496 cat <<VALUESEOF
497 BEGIN mysql_$x.connection_errors $1
498 SET Connection_errors_accept = ${mysql_data[Connection_errors_accept]}
@@ -525,4 +509,3 @@ VALUESEOF
509 [ ${#mysql_ids[@]} -eq 0 ] && error "no mysql servers left active." && return 1
510 return 0
511 }
528 -
collectors/charts.d.plugin/nginx/nginx.chart.sh
+17 -20
@@ -32,16 +32,15 @@ nginx_get() {
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
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:" ]; then
44 error "Invalid response from nginx server: ${nginx_response[*]}"
45 return 1
46 fi
@@ -54,14 +53,13 @@ nginx_get() {
53 nginx_writing="${nginx_response[13]}"
54 nginx_waiting="${nginx_response[15]}"
55
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
56 + if [ -z "${nginx_active_connections}" ] ||
57 + [ -z "${nginx_accepts}" ] ||
58 + [ -z "${nginx_handled}" ] ||
59 + [ -z "${nginx_requests}" ] ||
60 + [ -z "${nginx_reading}" ] ||
61 + [ -z "${nginx_writing}" ] ||
62 + [ -z "${nginx_waiting}" ]; then
63 error "empty values got from nginx server: ${nginx_response[*]}"
64 return 1
65 fi
@@ -74,8 +72,7 @@ nginx_check() {
72
73 nginx_get
74 # shellcheck disable=2181
77 - if [ $? -ne 0 ]
78 - then
75 + if [ $? -ne 0 ]; then
76 # shellcheck disable=SC2154
77 error "cannot find stub_status on URL '${nginx_url}'. Please set nginx_url='http://nginx.server/stub_status' in $confd/nginx.conf"
78 return 1
collectors/charts.d.plugin/nut/nut.chart.sh
+13 -22
@@ -34,8 +34,7 @@ nut_get_all() {
34 nut_get() {
35 run -t $nut_timeout upsc "$1"
36
37 - if [ "${nut_clients_chart}" -eq "1" ]
38 - then
37 + if [ "${nut_clients_chart}" -eq "1" ]; then
38 printf "ups.connected_clients: "
39 run -t $nut_timeout upsc -c "$1" | wc -l
40 fi
@@ -51,27 +50,23 @@ nut_check() {
50
51 require_cmd upsc || return 1
52
54 - [ -z "$nut_ups" ] && nut_ups="$( nut_get_all )"
53 + [ -z "$nut_ups" ] && nut_ups="$(nut_get_all)"
54
56 - for x in $nut_ups
57 - do
55 + for x in $nut_ups; do
56 nut_get "$x" >/dev/null
57 # shellcheck disable=SC2181
60 - if [ $? -eq 0 ]
61 - then
62 - if [ ! -z "${nut_names[${x}]}" ]
63 - then
64 - nut_ids[$x]="$( fixid "${nut_names[${x}]}" )"
58 + if [ $? -eq 0 ]; then
59 + if [ ! -z "${nut_names[${x}]}" ]; then
60 + nut_ids[$x]="$(fixid "${nut_names[${x}]}")"
61 else
66 - nut_ids[$x]="$( fixid "$x" )"
62 + nut_ids[$x]="$(fixid "$x")"
63 fi
64 continue
65 fi
66 error "cannot get information for NUT UPS '$x'."
67 done
68
73 - if [ ${#nut_ids[@]} -eq 0 ]
74 - then
69 + if [ ${#nut_ids[@]} -eq 0 ]; then
70 # shellcheck disable=SC2154
71 error "Cannot find UPSes - please set nut_ups='ups_name' in $confd/nut.conf"
72 return 1
@@ -84,8 +79,7 @@ nut_create() {
79 # create the charts
80 local x
81
87 - for x in "${nut_ids[@]}"
88 - do
82 + for x in "${nut_ids[@]}"; do
83 cat <<EOF
84 CHART nut_$x.charge '' "UPS Charge" "percentage" ups nut.charge area $((nut_priority + 1)) $nut_update_every
85 DIMENSION battery_charge charge absolute 1 100
@@ -121,20 +115,18 @@ CHART nut_$x.temp '' "UPS Temperature" "temperature" ups nut.temperature line $(
115 DIMENSION temp temp absolute 1 100
116 EOF
117
124 - if [ "${nut_clients_chart}" = "1" ]
125 - then
126 - cat <<EOF2
118 + if [ "${nut_clients_chart}" = "1" ]; then
119 + cat <<EOF2
120 CHART nut_$x.clients '' "UPS Connected Clients" "clients" ups nut.clients area $((nut_priority + 9)) $nut_update_every
121 DIMENSION clients '' absolute 1 1
122 EOF2
130 - fi
123 + fi
124
125 done
126
127 return 0
128 }
129
137 -
130 nut_update() {
131 # the first argument to this function is the microseconds since last update
132 # pass this parameter to the BEGIN statement (see bellow).
@@ -144,8 +136,7 @@ nut_update() {
136 # remember: KEEP IT SIMPLE AND SHORT
137
138 local i x
147 - for i in "${!nut_ids[@]}"
148 - do
139 + for i in "${!nut_ids[@]}"; do
140 x="${nut_ids[$i]}"
141 nut_get "$i" | awk "
142 BEGIN {
collectors/charts.d.plugin/opensips/opensips.chart.sh
+4 -6
@@ -14,8 +14,8 @@ opensips_timeout=2
14 opensips_priority=80000
15
16 opensips_get_stats() {
17 - run -t $opensips_timeout "$opensips_cmd" $opensips_opts |\
18 - grep "^\(core\|dialog\|net\|registrar\|shmem\|siptrace\|sl\|tm\|uri\|usrloc\):[a-zA-Z0-9_-]\+[[:space:]]*[=:]\+[[:space:]]*[0-9]\+[[:space:]]*$" |\
17 + run -t $opensips_timeout "$opensips_cmd" $opensips_opts |
18 + grep "^\(core\|dialog\|net\|registrar\|shmem\|siptrace\|sl\|tm\|uri\|usrloc\):[a-zA-Z0-9_-]\+[[:space:]]*[=:]\+[[:space:]]*[0-9]\+[[:space:]]*$" |
19 sed \
20 -e "s|[[:space:]]*[=:]\+[[:space:]]*\([0-9]\+\)[[:space:]]*$|=\1|g" \
21 -e "s|[[:space:]:-]\+|_|g" \
@@ -29,8 +29,7 @@ opensips_get_stats() {
29 opensips_check() {
30 # if the user did not provide an opensips_cmd
31 # try to find it in the system
32 - if [ -z "$opensips_cmd" ]
33 - then
32 + if [ -z "$opensips_cmd" ]; then
33 require_cmd opensipsctl || return 1
34 fi
35
@@ -38,8 +37,7 @@ opensips_check() {
37 local x
38 x="$(opensips_get_stats | grep "^opensips_core_")"
39 # shellcheck disable=SC2181
41 - if [ ! $? -eq 0 ] || [ -z "$x" ]
42 - then
40 + if [ ! $? -eq 0 ] || [ -z "$x" ]; then
41 error "cannot get global status. Please set opensips_opts='options' whatever needed to get connected to opensips server, in $confd/opensips.conf"
42 return 1
43 fi
collectors/charts.d.plugin/phpfpm/phpfpm.chart.sh
+10 -39
@@ -44,17 +44,7 @@ phpfpm_get() {
44 return 1
45 fi
46
47 - if [[ "${phpfpm_response[0]}" != "pool:" \
48 - || "${phpfpm_response[2]}" != "process" \
49 - || "${phpfpm_response[5]}" != "start" \
50 - || "${phpfpm_response[12]}" != "accepted" \
51 - || "${phpfpm_response[15]}" != "listen" \
52 - || "${phpfpm_response[16]}" != "queue:" \
53 - || "${phpfpm_response[26]}" != "idle" \
54 - || "${phpfpm_response[29]}" != "active" \
55 - || "${phpfpm_response[32]}" != "total" \
56 - ]]
57 - then
47 + if [[ ${phpfpm_response[0]} != "pool:" || ${phpfpm_response[2]} != "process" || ${phpfpm_response[5]} != "start" || ${phpfpm_response[12]} != "accepted" || ${phpfpm_response[15]} != "listen" || ${phpfpm_response[16]} != "queue:" || ${phpfpm_response[26]} != "idle" || ${phpfpm_response[29]} != "active" || ${phpfpm_response[32]} != "total" ]]; then
48 error "invalid response from phpfpm status server: ${phpfpm_response[*]}"
49 return 1
50 fi
@@ -71,27 +61,13 @@ phpfpm_get() {
61 phpfpm_total_processes="${phpfpm_response[34]}"
62 phpfpm_max_active_processes="${phpfpm_response[38]}"
63 phpfpm_max_children_reached="${phpfpm_response[42]}"
74 - if [ "${phpfpm_response[43]}" == "slow" ]
75 - then
76 - phpfpm_slow_requests="${phpfpm_response[45]}"
64 + if [ "${phpfpm_response[43]}" == "slow" ]; then
65 + phpfpm_slow_requests="${phpfpm_response[45]}"
66 else
78 - phpfpm_slow_requests="-1"
67 + phpfpm_slow_requests="-1"
68 fi
69
81 - if [[ -z "${phpfpm_pool}" \
82 - || -z "${phpfpm_start_time}" \
83 - || -z "${phpfpm_start_since}" \
84 - || -z "${phpfpm_accepted_conn}" \
85 - || -z "${phpfpm_listen_queue}" \
86 - || -z "${phpfpm_max_listen_queue}" \
87 - || -z "${phpfpm_listen_queue_len}" \
88 - || -z "${phpfpm_idle_processes}" \
89 - || -z "${phpfpm_active_processes}" \
90 - || -z "${phpfpm_total_processes}" \
91 - || -z "${phpfpm_max_active_processes}" \
92 - || -z "${phpfpm_max_children_reached}" \
93 - ]]
94 - then
70 + if [[ -z ${phpfpm_pool} || -z ${phpfpm_start_time} || -z ${phpfpm_start_since} || -z ${phpfpm_accepted_conn} || -z ${phpfpm_listen_queue} || -z ${phpfpm_max_listen_queue} || -z ${phpfpm_listen_queue_len} || -z ${phpfpm_idle_processes} || -z ${phpfpm_active_processes} || -z ${phpfpm_total_processes} || -z ${phpfpm_max_active_processes} || -z ${phpfpm_max_children_reached} ]]; then
71 error "empty values got from phpfpm status server: ${phpfpm_response[*]}"
72 return 1
73 fi
@@ -106,8 +82,7 @@ phpfpm_check() {
82 fi
83
84 local m
109 - for m in "${!phpfpm_urls[@]}"
110 - do
85 + for m in "${!phpfpm_urls[@]}"; do
86 phpfpm_get "${phpfpm_curl_opts[$m]}" "${phpfpm_urls[$m]}"
87 # shellcheck disable=SC2181
88 if [ $? -ne 0 ]; then
@@ -133,8 +108,7 @@ phpfpm_check() {
108 # _create is called once, to create the charts
109 phpfpm_create() {
110 local m
136 - for m in "${!phpfpm_urls[@]}"
137 - do
111 + for m in "${!phpfpm_urls[@]}"; do
112 cat <<EOF
113 CHART phpfpm_$m.connections '' "PHP-FPM Active Connections" "connections" phpfpm phpfpm.connections line $((phpfpm_priority + 1)) $phpfpm_update_every
114 DIMENSION active '' absolute 1 1
@@ -147,8 +121,7 @@ DIMENSION requests '' incremental 1 1
121 CHART phpfpm_$m.performance '' "PHP-FPM Performance" "status" phpfpm phpfpm.performance line $((phpfpm_priority + 3)) $phpfpm_update_every
122 DIMENSION reached 'max children reached' absolute 1 1
123 EOF
150 - if [ $((phpfpm_slow_requests)) -ne -1 ]
151 - then
124 + if [ $((phpfpm_slow_requests)) -ne -1 ]; then
125 echo "DIMENSION slow 'slow requests' absolute 1 1"
126 fi
127 done
@@ -166,8 +139,7 @@ phpfpm_update() {
139 # remember: KEEP IT SIMPLE AND SHORT
140
141 local m
169 - for m in "${!phpfpm_urls[@]}"
170 - do
142 + for m in "${!phpfpm_urls[@]}"; do
143 phpfpm_get "${phpfpm_curl_opts[$m]}" "${phpfpm_urls[$m]}"
144 # shellcheck disable=SC2181
145 if [ $? -ne 0 ]; then
@@ -187,8 +159,7 @@ END
159 BEGIN phpfpm_$m.performance $1
160 SET reached = $((phpfpm_max_children_reached))
161 EOF
190 - if [ $((phpfpm_slow_requests)) -ne -1 ]
191 - then
162 + if [ $((phpfpm_slow_requests)) -ne -1 ]; then
163 echo "SET slow = $((phpfpm_slow_requests))"
164 fi
165 echo "END"
collectors/charts.d.plugin/postfix/postfix.chart.sh
+6 -8
@@ -22,14 +22,12 @@ postfix_check() {
22 # - 1 to disable the chart
23
24 # try to find the postqueue executable
25 - if [ -z "$postfix_postqueue" ] || [ ! -x "$postfix_postqueue" ]
26 - then
25 + if [ -z "$postfix_postqueue" ] || [ ! -x "$postfix_postqueue" ]; then
26 # shellcheck disable=SC2230
27 postfix_postqueue="$(which postqueue 2>/dev/null || command -v postqueue 2>/dev/null)"
28 fi
29
31 - if [ -z "$postfix_postqueue" ] || [ ! -x "$postfix_postqueue" ]
32 - then
30 + if [ -z "$postfix_postqueue" ] || [ ! -x "$postfix_postqueue" ]; then
31 # shellcheck disable=SC2154
32 error "cannot find postqueue. Please set 'postfix_postqueue=/path/to/postqueue' in $confd/postfix.conf"
33 return 1
@@ -39,7 +37,7 @@ postfix_check() {
37 }
38
39 postfix_create() {
42 -cat <<EOF
40 + cat <<EOF
41 CHART postfix_local.qemails '' "Postfix Queue Emails" "emails" queue postfix.queued.emails line $((postfix_priority + 1)) $postfix_update_every
42 DIMENSION emails '' absolute 1 1
43 CHART postfix_local.qsize '' "Postfix Queue Emails Size" "emails size in KB" queue postfix.queued.size area $((postfix_priority + 2)) $postfix_update_every
@@ -70,9 +68,9 @@ postfix_update() {
68 postfix_q_emails=0
69 postfix_q_size=0
70
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" |\
71 + eval "$(run "$postfix_postqueue" -p |
72 + grep "^--" |
73 + sed -e "s/-- \([0-9]\+\) Kbytes in \([0-9]\+\) Requests.$/local postfix_q_size=\1\nlocal postfix_q_emails=\2/g" |
74 grep -E "^local postfix_q_(emails|size)=[0-9]+$")"
75
76 # write the result of the work.
collectors/charts.d.plugin/sensors/sensors.chart.sh
+95 -100
@@ -38,8 +38,7 @@ sensors_find_all_files() {
38
39 sensors_find_all_dirs() {
40 # shellcheck disable=SC2162
41 - sensors_find_all_files "$1" | while read
42 - do
41 + sensors_find_all_files "$1" | while read; do
42 dirname "$REPLY"
43 done | sort -u
44 }
@@ -51,7 +50,7 @@ sensors_check() {
50 # - 0 to enable the chart
51 # - 1 to disable the chart
52
54 - [ -z "$( sensors_find_all_files "$sensors_sys_dir" )" ] && error "no sensors found in '$sensors_sys_dir'." && return 1
53 + [ -z "$(sensors_find_all_files "$sensors_sys_dir")" ] && error "no sensors found in '$sensors_sys_dir'." && return 1
54 return 0
55 }
56
@@ -60,15 +59,14 @@ sensors_check_files() {
59 # also remove not needed sensors
60
61 local f v excluded
63 - for f in "$@"
64 - do
62 + for f in "$@"; do
63 [ ! -f "$f" ] && continue
64 for ex in "${sensors_excluded[@]}"; do
65 [[ $f =~ .*$ex$ ]] && excluded='1' && break
66 done
67
70 - [ "$excluded" != "1" ] && v="$( cat "$f" )" || v=0
71 - v=$(( v + 1 - 1 ))
68 + [ "$excluded" != "1" ] && v="$(cat "$f")" || v=0
69 + v=$((v + 1 - 1))
70 [ $v -ne 0 ] && echo "$f" && continue
71 excluded=
72
@@ -81,15 +79,14 @@ sensors_check_temp_type() {
79 # disabled sensors have the value 0
80
81 local f t v
84 - for f in "$@"
85 - do
82 + for f in "$@"; do
83 # shellcheck disable=SC2001
87 - t=$( echo "$f" | sed "s|_input$|_type|g" )
84 + t=$(echo "$f" | sed "s|_input$|_type|g")
85 [ "$f" = "$t" ] && echo "$f" && continue
86 [ ! -f "$t" ] && echo "$f" && continue
87
91 - v="$( cat "$t" )"
92 - v=$(( v + 1 - 1 ))
88 + v="$(cat "$t")"
89 + v=$((v + 1 - 1))
90 [ $v -ne 0 ] && echo "$f" && continue
91
92 error "$f is disabled"
@@ -105,120 +102,119 @@ sensors_create() {
102 # - the highest speed we can achieve -
103 [ $sensors_source_update -eq 1 ] && echo >"$TMP_DIR/sensors.sh" "sensors_update() {"
104
108 - for path in $( sensors_find_all_dirs "$sensors_sys_dir" | sort -u )
109 - do
110 - dir=$( basename "$path" )
105 + for path in $(sensors_find_all_dirs "$sensors_sys_dir" | sort -u); do
106 + dir=$(basename "$path")
107 device=
108 subsystem=
109 id=
110 type=
111 name=
112
117 - [ -h "$path/device" ] && device=$( readlink -f "$path/device" )
118 - [ ! -z "$device" ] && device=$( basename "$device" )
113 + [ -h "$path/device" ] && device=$(readlink -f "$path/device")
114 + [ ! -z "$device" ] && device=$(basename "$device")
115 [ -z "$device" ] && device="$dir"
116
121 - [ -h "$path/subsystem" ] && subsystem=$( readlink -f "$path/subsystem" )
122 - [ ! -z "$subsystem" ] && subsystem=$( basename "$subsystem" )
117 + [ -h "$path/subsystem" ] && subsystem=$(readlink -f "$path/subsystem")
118 + [ ! -z "$subsystem" ] && subsystem=$(basename "$subsystem")
119 [ -z "$subsystem" ] && subsystem="$dir"
120
125 - [ -f "$path/name" ] && name=$( cat "$path/name" )
121 + [ -f "$path/name" ] && name=$(cat "$path/name")
122 [ -z "$name" ] && name="$dir"
123
128 - [ -f "$path/type" ] && type=$( cat "$path/type" )
124 + [ -f "$path/type" ] && type=$(cat "$path/type")
125 [ -z "$type" ] && type="$dir"
126
131 - id="$( fixid "$device.$subsystem.$dir" )"
127 + id="$(fixid "$device.$subsystem.$dir")"
128
129 debug "path='$path', dir='$dir', device='$device', subsystem='$subsystem', id='$id', name='$name'"
130
135 - for mode in temperature voltage fans power current energy humidity
136 - do
131 + for mode in temperature voltage fans power current energy humidity; do
132 files=
133 multiplier=1
134 divisor=1
135 algorithm="absolute"
136
137 case $mode in
143 - temperature)
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"
149 - echo >>"$TMP_DIR/sensors.sh" "echo \"BEGIN sensors.temp_$id \$1\""
150 - divisor=1000
151 - ;;
152 -
153 - voltage)
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"
158 - echo >>"$TMP_DIR/sensors.sh" "echo \"BEGIN sensors.volt_$id \$1\""
159 - divisor=1000
160 - ;;
161 -
162 - current)
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"
167 - echo >>"$TMP_DIR/sensors.sh" "echo \"BEGIN sensors.curr_$id \$1\""
168 - divisor=1000
169 - ;;
170 -
171 - power)
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"
176 - echo >>"$TMP_DIR/sensors.sh" "echo \"BEGIN sensors.power_$id \$1\""
177 - divisor=1000000
178 - ;;
179 -
180 - fans)
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"
185 - echo >>"$TMP_DIR/sensors.sh" "echo \"BEGIN sensors.fan_$id \$1\""
186 - ;;
187 -
188 - energy)
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"
193 - echo >>"$TMP_DIR/sensors.sh" "echo \"BEGIN sensors.energy_$id \$1\""
194 - algorithm="incremental"
195 - divisor=1000000
196 - ;;
197 -
198 - humidity)
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"
203 - echo >>"$TMP_DIR/sensors.sh" "echo \"BEGIN sensors.humidity_$id \$1\""
204 - divisor=1000
205 - ;;
206 -
207 - *)
208 - continue
209 - ;;
138 + temperature)
139 + files="$(
140 + ls "$path"/temp*_input 2>/dev/null
141 + ls "$path/temp" 2>/dev/null
142 + )"
143 + files="$(sensors_check_files "$files")"
144 + files="$(sensors_check_temp_type "$files")"
145 + [ -z "$files" ] && continue
146 + 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\""
148 + divisor=1000
149 + ;;
150 +
151 + voltage)
152 + files="$(ls "$path"/in*_input 2>/dev/null)"
153 + files="$(sensors_check_files "$files")"
154 + [ -z "$files" ] && continue
155 + 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\""
157 + divisor=1000
158 + ;;
159 +
160 + current)
161 + files="$(ls "$path"/curr*_input 2>/dev/null)"
162 + files="$(sensors_check_files "$files")"
163 + [ -z "$files" ] && continue
164 + 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\""
166 + divisor=1000
167 + ;;
168 +
169 + power)
170 + files="$(ls "$path"/power*_input 2>/dev/null)"
171 + files="$(sensors_check_files "$files")"
172 + [ -z "$files" ] && continue
173 + 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\""
175 + divisor=1000000
176 + ;;
177 +
178 + fans)
179 + files="$(ls "$path"/fan*_input 2>/dev/null)"
180 + files="$(sensors_check_files "$files")"
181 + [ -z "$files" ] && continue
182 + 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\""
184 + ;;
185 +
186 + energy)
187 + files="$(ls "$path"/energy*_input 2>/dev/null)"
188 + files="$(sensors_check_files "$files")"
189 + [ -z "$files" ] && continue
190 + 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\""
192 + algorithm="incremental"
193 + divisor=1000000
194 + ;;
195 +
196 + humidity)
197 + files="$(ls "$path"/humidity*_input 2>/dev/null)"
198 + files="$(sensors_check_files "$files")"
199 + [ -z "$files" ] && continue
200 + 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\""
202 + divisor=1000
203 + ;;
204 +
205 + *)
206 + continue
207 + ;;
208 esac
209
212 - for x in $files
213 - do
210 + for x in $files; do
211 file="$x"
215 - fid="$( fixid "$file" )"
216 - lfile="$( basename "$file" | sed "s|_input$|_label|g" )"
217 - labelname="$( basename "$file" | sed "s|_input$||g" )"
212 + fid="$(fixid "$file")"
213 + lfile="$(basename "$file" | sed "s|_input$|_label|g")"
214 + labelname="$(basename "$file" | sed "s|_input$||g")"
215
219 - if [ ! "$path/$lfile" = "$file" ] && [ -f "$path/$lfile" ]
220 - then
221 - labelname="$( cat "$path/$lfile" )"
216 + if [ ! "$path/$lfile" = "$file" ] && [ -f "$path/$lfile" ]; then
217 + labelname="$(cat "$path/$lfile")"
218 fi
219
220 echo "DIMENSION $fid '$labelname' $algorithm $multiplier $divisor"
@@ -252,4 +248,3 @@ sensors_update() {
248
249 return 0
250 }
255 -
collectors/charts.d.plugin/squid/squid.chart.sh
+7 -13
@@ -25,13 +25,10 @@ squid_get_stats() {
25 squid_autodetect() {
26 local host="127.0.0.1" port url x
27
28 - for port in 3128 8080
29 - do
30 - for url in "cache_object://$host:$port/counters" "/squid-internal-mgr/counters"
31 - do
28 + for port in 3128 8080; do
29 + for url in "cache_object://$host:$port/counters" "/squid-internal-mgr/counters"; do
30 x=$(squid_get_stats_internal "$host" "$port" "$url" | grep client_http.requests)
33 - if [ ! -z "$x" ]
34 - then
31 + if [ ! -z "$x" ]; then
32 squid_host="$host"
33 squid_port="$port"
34 squid_url="$url"
@@ -50,8 +47,7 @@ squid_check() {
47 require_cmd sed || return 1
48 require_cmd egrep || return 1
49
53 - if [ -z "$squid_host" ] || [ -z "$squid_port" ] || [ -z "$squid_url" ]
54 - then
50 + if [ -z "$squid_host" ] || [ -z "$squid_port" ] || [ -z "$squid_url" ]; then
51 squid_autodetect || return 1
52 fi
53
@@ -59,8 +55,7 @@ squid_check() {
55 local x
56 x="$(squid_get_stats | grep client_http.requests)"
57 # shellcheck disable=SC2181
62 - if [ ! $? -eq 0 ] || [ -z "$x" ]
63 - then
58 + if [ ! $? -eq 0 ] || [ -z "$x" ]; then
59 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"
60 return 1
61 fi
@@ -93,7 +88,6 @@ EOF
88 return 0
89 }
90
96 -
91 squid_update() {
92 # the first argument to this function is the microseconds since last update
93 # pass this parameter to the BEGIN statement (see bellow).
@@ -114,8 +108,8 @@ squid_update() {
108 # even if something goes wrong, no other code can be executed
109
110 # 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" |\
111 + eval "$(squid_get_stats |
112 + sed -e "s/ \+/ /g" -e "s/\./_/g" -e "s/^\([a-z0-9_]\+\) *= *\([0-9]\+\)$/local squid_\1=\2/g" |
113 grep -E "^local squid_(client_http|server_all)_[a-z0-9_]+=[0-9]+$")"
114
115 # write the result of the work.
collectors/charts.d.plugin/tomcat/tomcat.chart.sh
+10 -8
@@ -39,17 +39,16 @@ tomcat_check() {
39
40 require_cmd xmlstarlet || return 1
41
42 -
42 # check if url, username, passwords are set
43 if [ -z "${tomcat_url}" ]; then
45 - error "tomcat url is unset or set to the empty string"
44 + error "tomcat url is unset or set to the empty string"
45 return 1
46 fi
47 if [ -z "${tomcat_user}" ]; then
48 # check backwards compatibility
49 # shellcheck disable=SC2154
50 if [ -z "${tomcatUser}" ]; then
52 - error "tomcat user is unset or set to the empty string"
51 + error "tomcat user is unset or set to the empty string"
52 return 1
53 else
54 tomcat_user="${tomcatUser}"
@@ -59,7 +58,7 @@ tomcat_check() {
58 # check backwards compatibility
59 # shellcheck disable=SC2154
60 if [ -z "${tomcatPassword}" ]; then
62 - error "tomcat password is unset or set to the empty string"
61 + error "tomcat password is unset or set to the empty string"
62 return 1
63 else
64 tomcat_password="${tomcatPassword}"
@@ -69,8 +68,7 @@ tomcat_check() {
68 # check if we can get to tomcat's status page
69 tomcat_get
70 # shellcheck disable=2181
72 - if [ $? -ne 0 ]
73 - then
71 + if [ $? -ne 0 ]; then
72 error "cannot get to status page on URL '${tomcat_url}'. Please make sure tomcat url, username and password are correct."
73 return 1
74 fi
@@ -84,8 +82,12 @@ tomcat_check() {
82
83 tomcat_get() {
84 # collect tomcat values
87 - tomcat_port="$(IFS=/ read -ra a <<< "$tomcat_url"; hostport=${a[2]}; echo "${hostport#*:}")"
88 - mapfile -t lines < <(run curl -u "$tomcat_user":"$tomcat_password" -Ss ${tomcat_curl_opts} "$tomcat_url" |\
85 + tomcat_port="$(
86 + IFS=/ read -ra a <<<"$tomcat_url"
87 + hostport=${a[2]}
88 + echo "${hostport#*:}"
89 + )"
90 + mapfile -t lines < <(run curl -u "$tomcat_user":"$tomcat_password" -Ss ${tomcat_curl_opts} "$tomcat_url" |
91 run xmlstarlet sel \
92 -t -m "/status/jvm/memory" -v @free \
93 -n -m "/status/connector[@name='\"http-bio-$tomcat_port\"']/threadInfo" -v @currentThreadCount \