]> arthur.barton.de Git - netdata.git/blob - plugins.d/tc-qos-helper.sh
Merge pull request #1615 from lowfive/discord_notify
[netdata.git] / plugins.d / tc-qos-helper.sh
1 #!/usr/bin/env bash
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 # This script is a helper to allow netdata collect tc data.
9 # tc output parsing has been implemented in C, inside netdata
10 # This script allows setting names to dimensions.
11
12 export PATH="${PATH}:/sbin:/usr/sbin:/usr/local/sbin"
13 export LC_ALL=C
14
15 PROGRAM_FILE="$0"
16 PROGRAM_NAME="$(basename $0)"
17 PROGRAM_NAME="${PROGRAM_NAME/.plugin}"
18
19 # -----------------------------------------------------------------------------
20
21 logdate() {
22     date "+%Y-%m-%d %H:%M:%S"
23 }
24
25 log() {
26     local status="${1}"
27     shift
28
29     echo >&2 "$(logdate): ${PROGRAM_NAME}: ${status}: ${*}"
30
31 }
32
33 warning() {
34     log WARNING "${@}"
35 }
36
37 error() {
38     log ERROR "${@}"
39 }
40
41 info() {
42     log INFO "${@}"
43 }
44
45 fatal() {
46     log FATAL "${@}"
47     exit 1
48 }
49
50 debug=0
51 debug() {
52     [ $debug -eq 1 ] && log DEBUG "${@}"
53 }
54
55 # -----------------------------------------------------------------------------
56
57 plugins_dir="${NETDATA_PLUGINS_DIR}"
58 [ -z "$plugins_dir" ] && plugins_dir="$( dirname $PROGRAM_FILE )"
59
60 config_dir=${NETDATA_CONFIG_DIR-/etc/netdata}
61 tc="$(which tc 2>/dev/null || command -v tc 2>/dev/null)"
62 fireqos_run_dir="/var/run/fireqos"
63 qos_get_class_names_every=120
64 qos_exit_every=3600
65
66 # check if we have a valid number for interval
67 t=${1}
68 update_every=$((t))
69 [ $((update_every)) -lt 1 ] && update_every=${NETDATA_UPDATE_EVERY}
70 [ $((update_every)) -lt 1 ] && update_every=1
71
72 # allow the user to override our defaults
73 if [ -f "${config_dir}/tc-qos-helper.conf" ]
74     then
75     source "${config_dir}/tc-qos-helper.conf"
76 fi
77
78 # default sleep function
79 LOOPSLEEPMS_LASTWORK=0
80 loopsleepms() {
81     sleep $1
82 }
83
84 # if found and included, this file overwrites loopsleepms()
85 # with a high resolution timer function for precise looping.
86 . "${plugins_dir}/loopsleepms.sh.inc"
87
88 if [ -z "${tc}" -o ! -x "${tc}" ]
89     then
90     fatal "cannot find command 'tc' in this system."
91 fi
92
93 tc_devices=
94 fix_names=
95
96 setclassname() {
97     echo "SETCLASSNAME $3 $2"
98 }
99
100 show_tc_cls() {
101     local x="${1}"
102
103     if [ -f /etc/iproute2/tc_cls ]
104     then
105         local classid name rest
106         while read classid name rest
107         do
108             [ -z "${classid}" -o -z "${name}" -o "${classid}" = "#" -o "${name}" = "#" -o "${classid:0:1}" = "#" -o "${name:0:1}" = "#" ] && continue
109             setclassname "" "${name}" "${classid}"
110         done </etc/iproute2/tc_cls
111         return 0
112     fi
113
114     return 1
115 }
116
117 show_fireqos_names() {
118     local x="${1}" name n interface_dev interface_classes interface_classes_monitor
119
120     if [ -f "${fireqos_run_dir}/ifaces/${x}" ]
121     then
122         name="$(<"${fireqos_run_dir}/ifaces/${x}")"
123         echo "SETDEVICENAME ${name}"
124
125         interface_dev=
126         interface_classes=
127         interface_classes_monitor=
128         source "${fireqos_run_dir}/${name}.conf"
129         for n in ${interface_classes_monitor}
130         do
131             setclassname ${n//|/ }
132         done
133         [ ! -z "${interface_dev}" ] && echo "SETDEVICEGROUP ${interface_dev}"
134
135         return 0
136     fi
137
138     return 1
139 }
140
141 show_tc() {
142     local x="${1}"
143
144     echo "BEGIN ${x}"
145
146     # netdata can parse the output of tc
147     ${tc} -s class show dev ${x}
148
149     # check FireQOS names for classes
150     if [ ! -z "${fix_names}" ]
151     then
152         show_fireqos_names "${x}" || show_tc_cls "${x}"
153     fi
154
155     echo "END ${x}"
156 }
157
158 find_tc_devices() {
159     local count=0 devs= dev rest l
160
161     # find all the devices in the system
162     # without forking
163     while IFS=":| " read dev rest
164     do
165         count=$((count + 1))
166         [ ${count} -le 2 ] && continue
167         devs="${devs} ${dev}"
168     done </proc/net/dev
169
170     # from all the devices find the ones
171     # that have QoS defined
172     # unfortunately, one fork per device cannot be avoided
173     tc_devices=
174     for dev in ${devs}
175     do
176         l="$(${tc} class show dev ${dev} 2>/dev/null)"
177         [ ! -z "${l}" ] && tc_devices="${tc_devices} ${dev}"
178     done
179 }
180
181 # update devices and class names
182 # once every 2 minutes
183 names_every=$((qos_get_class_names_every / update_every))
184
185 # exit this script every hour
186 # it will be restarted automatically
187 exit_after=$((qos_exit_every / update_every))
188
189 c=0
190 gc=0
191 while [ 1 ]
192 do
193     fix_names=
194     c=$((c + 1))
195     gc=$((gc + 1))
196
197     if [ ${c} -le 1 -o ${c} -ge ${names_every} ]
198     then
199         c=1
200         fix_names="YES"
201         find_tc_devices
202     fi
203
204     for d in ${tc_devices}
205     do
206         show_tc ${d}
207     done
208
209     echo "WORKTIME ${LOOPSLEEPMS_LASTWORK}"
210
211     loopsleepms ${update_every}
212
213     [ ${gc} -gt ${exit_after} ] && exit 0
214 done