@cryptotaxi247 / netdata-1 / commits / cdcb97f91

fix division by zero in charts.d ap module - also optimize it to call awk once per iteration; fixes #1897

Costa Tsaousis (ktsaou) committed Mar 6, 2017 at 20:33 UTC cdcb97f918181d626694ae63bfbcf996a1c84780
1 file changed +78 -64
charts.d/ap.chart.sh
+78 -64
@@ -16,7 +16,7 @@ declare -A ap_devs=()
16 # _check is called once, to find out if this chart should be enabled or not
17 ap_check() {
18 require_cmd iw || return 1
19 -
19 +
20 local ev=$(run iw dev | awk '
21 BEGIN {
22 i = "";
@@ -77,20 +77,19 @@ DIMENSION retries 'tx retries' incremental 1 1
77 DIMENSION failures 'tx failures' incremental -1 1
78
79 CHART ap_signal.${dev} '' "Average Signal for ${ssid} on ${dev}" "dBm" ${dev} ap.signal line $((ap_priority + 5)) $ap_update_every
80 -DIMENSION signal 'average signal' absolute 1 1
80 +DIMENSION signal 'average signal' absolute 1 1000
81
82 CHART ap_bitrate.${dev} '' "Bitrate for ${ssid} on ${dev}" "Mbps" ${dev} ap.bitrate line $((ap_priority + 6)) $ap_update_every
83 DIMENSION receive '' absolute 1 1000
84 DIMENSION transmit '' absolute -1 1000
85 DIMENSION expected 'expected throughput' absolute 1 1000
86 EOF
87 -
87 done
88
89 return 0
90 }
91
93 -# _update is called continiously, to collect the values
92 +# _update is called continuously, to collect the values
93 ap_update() {
94 # the first argument to this function is the microseconds since last update
95 # pass this parameter to the BEGIN statement (see bellow).
@@ -101,66 +100,81 @@ ap_update() {
100
101 for dev in "${!ap_devs[@]}"
102 do
104 - iw ${dev} station dump |\
105 - awk "
106 - BEGIN {
107 - c = 0;
108 - rb = 0;
109 - tb = 0;
110 - rp = 0;
111 - tp = 0;
112 - tr = 0;
113 - tf = 0;
114 - tt = 0;
115 - rt = 0;
116 - s = 0;
117 - g = 0;
118 - e = 0;
119 - }
120 - /^Station/ { c++; }
121 - /^[ \\t]+rx bytes:/ { rb += \$3 }
122 - /^[ \\t]+tx bytes:/ { tb += \$3 }
123 - /^[ \\t]+rx packets:/ { rp += \$3 }
124 - /^[ \\t]+tx packets:/ { tp += \$3 }
125 - /^[ \\t]+tx retries:/ { tr += \$3 }
126 - /^[ \\t]+tx failed:/ { tf += \$3 }
127 - /^[ \\t]+signal:/ { s += \$2; }
128 - /^[ \\t]+rx bitrate:/ { x = \$3; rt += x * 1000; }
129 - /^[ \\t]+tx bitrate:/ { x = \$3; tt += x * 1000; }
130 - /^[ \\t]+expected throughput:(.*)Mbps/ {
131 - x=\$3;
132 - sub(/Mbps/, \"\", x);
133 - e += x * 1000;
134 - }
135 - END {
136 - print \"BEGIN ap_clients.${dev}\"
137 - print \"SET clients = \" c;
138 - print \"END\"
139 - print \"BEGIN ap_bandwidth.${dev}\"
140 - print \"SET received = \" rb;
141 - print \"SET sent = \" tb;
142 - print \"END\"
143 - print \"BEGIN ap_packets.${dev}\"
144 - print \"SET received = \" rp;
145 - print \"SET sent = \" tp;
146 - print \"END\"
147 - print \"BEGIN ap_issues.${dev}\"
148 - print \"SET retries = \" tr;
149 - print \"SET failures = \" tf;
150 - print \"END\"
151 - print \"BEGIN ap_signal.${dev}\"
152 - print \"SET signal = \" s / c;
153 - print \"END\"
154 -
155 - if( c == 0 ) c = 1;
156 - print \"BEGIN ap_bitrate.${dev}\"
157 - print \"SET receive = \" rt / c;
158 - print \"SET transmit = \" tt / c;
159 - print \"SET expected = \" e / c;
160 - print \"END\"
161 - }
162 - "
163 - done
103 + echo
104 + echo "DEVICE ${dev}"
105 + iw ${dev} station dump
106 + done | awk "
107 + function zero_data() {
108 + dev = \"\";
109 + c = 0;
110 + rb = 0;
111 + tb = 0;
112 + rp = 0;
113 + tp = 0;
114 + tr = 0;
115 + tf = 0;
116 + tt = 0;
117 + rt = 0;
118 + s = 0;
119 + g = 0;
120 + e = 0;
121 + }
122 + function print_device() {
123 + if(dev != \"\" && length(dev) > 0) {
124 + print \"BEGIN ap_clients.\" dev;
125 + print \"SET clients = \" c;
126 + print \"END\";
127 + print \"BEGIN ap_bandwidth.\" dev;
128 + print \"SET received = \" rb;
129 + print \"SET sent = \" tb;
130 + print \"END\";
131 + print \"BEGIN ap_packets.\" dev;
132 + print \"SET received = \" rp;
133 + print \"SET sent = \" tp;
134 + print \"END\";
135 + print \"BEGIN ap_issues.\" dev;
136 + print \"SET retries = \" tr;
137 + print \"SET failures = \" tf;
138 + print \"END\";
139 +
140 + if( c == 0 ) c = 1;
141 + print \"BEGIN ap_signal.\" dev;
142 + print \"SET signal = \" int(s / c);
143 + print \"END\";
144 + print \"BEGIN ap_bitrate.\" dev;
145 + print \"SET receive = \" int(rt / c);
146 + print \"SET transmit = \" int(tt / c);
147 + print \"SET expected = \" int(e / c);
148 + print \"END\";
149 + }
150 + zero_data();
151 + }
152 + BEGIN {
153 + zero_data();
154 + }
155 + /^DEVICE / {
156 + print_device();
157 + dev = \$2;
158 + }
159 + /^Station/ { c++; }
160 + /^[ \\t]+rx bytes:/ { rb += \$3; }
161 + /^[ \\t]+tx bytes:/ { tb += \$3; }
162 + /^[ \\t]+rx packets:/ { rp += \$3; }
163 + /^[ \\t]+tx packets:/ { tp += \$3; }
164 + /^[ \\t]+tx retries:/ { tr += \$3; }
165 + /^[ \\t]+tx failed:/ { tf += \$3; }
166 + /^[ \\t]+signal:/ { x = \$2; s += x * 1000; }
167 + /^[ \\t]+rx bitrate:/ { x = \$3; rt += x * 1000; }
168 + /^[ \\t]+tx bitrate:/ { x = \$3; tt += x * 1000; }
169 + /^[ \\t]+expected throughput:(.*)Mbps/ {
170 + x=\$3;
171 + sub(/Mbps/, \"\", x);
172 + e += x * 1000;
173 + }
174 + END {
175 + print_device();
176 + }
177 + "
178
179 return 0
180 }