]> arthur.barton.de Git - netdata.git/blob - charts.d/ap.chart.sh
Merge pull request #2021 from ktsaou/master
[netdata.git] / charts.d / ap.chart.sh
1 # no need for shebang - this file is loaded from charts.d.plugin
2
3 # netdata
4 # real-time performance and health monitoring, done right!
5 # (C) 2016 Costa Tsaousis <costa@tsaousis.gr>
6 # GPL v3+
7 #
8
9 # _update_every is a special variable - it holds the number of seconds
10 # between the calls of the _update() function
11 ap_update_every=
12 ap_priority=6900
13
14 declare -A ap_devs=()
15
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
20         local ev=$(run iw dev | awk '
21                 BEGIN {
22                         i = "";
23                         ssid = "";
24                         ap = 0;
25                 }
26                 /^[ \t]+Interface / {
27                         if( ap == 1 ) {
28                                 print "ap_devs[" i "]=\"" ssid "\""
29                         }
30
31                         i = $2;
32                         ssid = "";
33                         ap = 0;
34                 }
35                 /^[ \t]+ssid / { ssid = $2; }
36                 /^[ \t]+type AP$/ { ap = 1; }
37                 END {
38                         if( ap == 1 ) {
39                                 print "ap_devs[" i "]=\"" ssid "\""
40                         }
41                 }
42         ')
43         eval "${ev}"
44
45         # this should return:
46         #  - 0 to enable the chart
47         #  - 1 to disable the chart
48
49         [ ${#ap_devs[@]} -gt 0 ] && return 0
50         error "no devices found in AP mode, with 'iw dev'"
51         return 1
52 }
53
54 # _create is called once, to create the charts
55 ap_create() {
56         local ssid dev
57
58         for dev in "${!ap_devs[@]}"
59         do
60                 ssid="${ap_devs[${dev}]}"
61
62                 # create the chart with 3 dimensions
63                 cat <<EOF
64 CHART ap_clients.${dev} '' "Connected clients to ${ssid} on ${dev}" "clients" ${dev} ap.clients line $((ap_priority + 1)) $ap_update_every
65 DIMENSION clients '' absolute 1 1
66
67 CHART ap_bandwidth.${dev} '' "Bandwidth for ${ssid} on ${dev}" "kilobits/s" ${dev} ap.net area $((ap_priority + 2)) $ap_update_every
68 DIMENSION received '' incremental 8 1024
69 DIMENSION sent '' incremental -8 1024
70
71 CHART ap_packets.${dev} '' "Packets for ${ssid} on ${dev}" "packets/s" ${dev} ap.packets line $((ap_priority + 3)) $ap_update_every
72 DIMENSION received '' incremental 1 1
73 DIMENSION sent '' incremental -1 1
74
75 CHART ap_issues.${dev} '' "Transmit Issues for ${ssid} on ${dev}" "issues/s" ${dev} ap.issues line $((ap_priority + 4)) $ap_update_every
76 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 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         done
88
89         return 0
90 }
91
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).
96
97         # do all the work to collect / calculate the values
98         # for each dimension
99         # remember: KEEP IT SIMPLE AND SHORT
100
101         for dev in "${!ap_devs[@]}"
102         do
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 }
181