]> arthur.barton.de Git - netdata.git/blob - charts.d/ap.chart.sh
477d414859eb6201c8147c4293246956eb86cfaf
[netdata.git] / charts.d / ap.chart.sh
1 # no need for shebang - this file is loaded from charts.d.plugin
2
3 # _update_every is a special variable - it holds the number of seconds
4 # between the calls of the _update() function
5 ap_update_every=
6 ap_priority=6900
7
8 declare -A ap_devs=()
9
10 # _check is called once, to find out if this chart should be enabled or not
11 ap_check() {
12         require_cmd iw || return 1
13         
14         local ev=$(run iw dev | awk '
15                 BEGIN {
16                         i = "";
17                         ssid = "";
18                         ap = 0;
19                 }
20                 /^[ \t]+Interface / {
21                         if( ap == 1 ) {
22                                 print "ap_devs[" i "]=\"" ssid "\""
23                         }
24
25                         i = $2;
26                         ssid = "";
27                         ap = 0;
28                 }
29                 /^[ \t]+ssid / { ssid = $2; }
30                 /^[ \t]+type AP$/ { ap = 1; }
31                 END {
32                         if( ap == 1 ) {
33                                 print "ap_devs[" i "]=\"" ssid "\""
34                         }
35                 }
36         ')
37         eval "${ev}"
38
39         # this should return:
40         #  - 0 to enable the chart
41         #  - 1 to disable the chart
42
43         [ ${#ap_devs[@]} -gt 0 ] && return 0
44         error "no devices found in AP mode, with 'iw dev'"
45         return 1
46 }
47
48 # _create is called once, to create the charts
49 ap_create() {
50         local ssid dev
51
52         for dev in "${!ap_devs[@]}"
53         do
54                 ssid="${ap_devs[${dev}]}"
55
56                 # create the chart with 3 dimensions
57                 cat <<EOF
58 CHART ap_clients.${dev} '' "Connected clients to ${ssid} on ${dev}" "clients" ${dev} ap.clients line $((ap_priority + 1)) $ap_update_every
59 DIMENSION clients '' absolute 1 1
60
61 CHART ap_bandwidth.${dev} '' "Bandwidth for ${ssid} on ${dev}" "kilobits/s" ${dev} ap.net area $((ap_priority + 2)) $ap_update_every
62 DIMENSION received '' incremental 8 1024
63 DIMENSION sent '' incremental -8 1024
64
65 CHART ap_packets.${dev} '' "Packets for ${ssid} on ${dev}" "packets/s" ${dev} ap.packets line $((ap_priority + 3)) $ap_update_every
66 DIMENSION received '' incremental 1 1
67 DIMENSION sent '' incremental -1 1
68
69 CHART ap_issues.${dev} '' "Transmit Issues for ${ssid} on ${dev}" "issues/s" ${dev} ap.issues line $((ap_priority + 4)) $ap_update_every
70 DIMENSION retries 'tx retries' incremental 1 1
71 DIMENSION failures 'tx failures' incremental -1 1
72
73 CHART ap_signal.${dev} '' "Average Signal for ${ssid} on ${dev}" "dBm" ${dev} ap.signal line $((ap_priority + 5)) $ap_update_every
74 DIMENSION signal 'average signal' absolute 1 1
75
76 CHART ap_bitrate.${dev} '' "Bitrate for ${ssid} on ${dev}" "Mbps" ${dev} ap.bitrate line $((ap_priority + 6)) $ap_update_every
77 DIMENSION receive '' absolute 1 1000
78 DIMENSION transmit '' absolute -1 1000
79 DIMENSION expected 'expected throughput' absolute 1 1000
80 EOF
81
82         done
83
84         return 0
85 }
86
87 # _update is called continiously, to collect the values
88 ap_update() {
89         # the first argument to this function is the microseconds since last update
90         # pass this parameter to the BEGIN statement (see bellow).
91
92         # do all the work to collect / calculate the values
93         # for each dimension
94         # remember: KEEP IT SIMPLE AND SHORT
95
96         for dev in "${!ap_devs[@]}"
97         do
98                 iw ${dev} station dump |\
99                         awk "
100                                 BEGIN {
101                                         c = 0;
102                                         rb = 0;
103                                         tb = 0;
104                                         rp = 0;
105                                         tp = 0;
106                                         tr = 0;
107                                         tf = 0;
108                                         tt = 0;
109                                         rt = 0;
110                                         s = 0;
111                                         g = 0;
112                                         e = 0;
113                                 }
114                                 /^Station/           { c++; }
115                                 /^[ \\t]+rx bytes:/   { rb += \$3 }
116                                 /^[ \\t]+tx bytes:/   { tb += \$3 }
117                                 /^[ \\t]+rx packets:/ { rp += \$3 }
118                                 /^[ \\t]+tx packets:/ { tp += \$3 }
119                                 /^[ \\t]+tx retries:/ { tr += \$3 }
120                                 /^[ \\t]+tx failed:/  { tf += \$3 }
121                                 /^[ \\t]+signal:/     { s  += \$2; }
122                                 /^[ \\t]+rx bitrate:/ { x = \$3; rt += x * 1000; }
123                                 /^[ \\t]+tx bitrate:/ { x = \$3; tt += x * 1000; }
124                                 /^[ \\t]+expected throughput:(.*)Mbps/ {
125                                         x=\$3;
126                                         sub(/Mbps/, \"\", x);
127                                         e += x * 1000;
128                                 }
129                                 END {
130                                         print \"BEGIN ap_clients.${dev}\"
131                                         print \"SET clients = \" c;
132                                         print \"END\"
133                                         print \"BEGIN ap_bandwidth.${dev}\"
134                                         print \"SET received = \" rb;
135                                         print \"SET sent = \" tb;
136                                         print \"END\"
137                                         print \"BEGIN ap_packets.${dev}\"
138                                         print \"SET received = \" rp;
139                                         print \"SET sent = \" tp;
140                                         print \"END\"
141                                         print \"BEGIN ap_issues.${dev}\"
142                                         print \"SET retries = \" tr;
143                                         print \"SET failures = \" tf;
144                                         print \"END\"
145                                         print \"BEGIN ap_signal.${dev}\"
146                                         print \"SET signal = \" s / c;
147                                         print \"END\"
148
149                                         if( c == 0 ) c = 1;
150                                         print \"BEGIN ap_bitrate.${dev}\"
151                                         print \"SET receive = \" rt / c;
152                                         print \"SET transmit = \" tt / c;
153                                         print \"SET expected = \" e / c;
154                                         print \"END\"
155                                 }
156                                 "
157         done
158
159         return 0
160 }
161