4 # real-time performance and health monitoring, done right!
5 # (C) 2016 Costa Tsaousis <costa@tsaousis.gr>
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.
12 export PATH="${PATH}:/sbin:/usr/sbin:/usr/local/sbin"
16 PROGRAM_NAME="$(basename $0)"
17 PROGRAM_NAME="${PROGRAM_NAME/.plugin}"
19 # -----------------------------------------------------------------------------
22 date "+%Y-%m-%d %H:%M:%S"
29 echo >&2 "$(logdate): ${PROGRAM_NAME}: ${status}: ${*}"
52 [ $debug -eq 1 ] && log DEBUG "${@}"
55 # -----------------------------------------------------------------------------
57 plugins_dir="${NETDATA_PLUGINS_DIR}"
58 [ -z "$plugins_dir" ] && plugins_dir="$( dirname $PROGRAM_FILE )"
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
66 tc_show="qdisc" # can also be "class"
68 # check if we have a valid number for interval
71 [ $((update_every)) -lt 1 ] && update_every=${NETDATA_UPDATE_EVERY}
72 [ $((update_every)) -lt 1 ] && update_every=1
74 # allow the user to override our defaults
75 if [ -f "${config_dir}/tc-qos-helper.conf" ]
77 source "${config_dir}/tc-qos-helper.conf"
85 error "tc_show variable can be either 'qdisc' or 'class' but is set to '${tc_show}'. Assuming it is 'qdisc'."
90 # default sleep function
91 LOOPSLEEPMS_LASTWORK=0
96 # if found and included, this file overwrites loopsleepms()
97 # with a high resolution timer function for precise looping.
98 . "${plugins_dir}/loopsleepms.sh.inc"
100 if [ -z "${tc}" -o ! -x "${tc}" ]
102 fatal "cannot find command 'tc' in this system."
109 if [ "${tc_show}" = "qdisc" ]
111 echo "SETCLASSNAME $4 $2"
113 echo "SETCLASSNAME $3 $2"
118 [ "${tc_show}" = "qdisc" ] && return 1
122 if [ -f /etc/iproute2/tc_cls ]
124 local classid name rest
125 while read classid name rest
127 [ -z "${classid}" -o -z "${name}" -o "${classid}" = "#" -o "${name}" = "#" -o "${classid:0:1}" = "#" -o "${name:0:1}" = "#" ] && continue
128 setclassname "" "${name}" "${classid}"
129 done </etc/iproute2/tc_cls
135 show_fireqos_names() {
136 local x="${1}" name n interface_dev interface_classes interface_classes_monitor
138 if [ -f "${fireqos_run_dir}/ifaces/${x}" ]
140 name="$(<"${fireqos_run_dir}/ifaces/${x}")"
141 echo "SETDEVICENAME ${name}"
145 interface_classes_monitor=
146 source "${fireqos_run_dir}/${name}.conf"
147 for n in ${interface_classes_monitor}
149 setclassname ${n//|/ }
151 [ ! -z "${interface_dev}" ] && echo "SETDEVICEGROUP ${interface_dev}"
164 # netdata can parse the output of tc
165 ${tc} -s ${tc_show} show dev ${x}
167 # check FireQOS names for classes
168 if [ ! -z "${fix_names}" ]
170 show_fireqos_names "${x}" || show_tc_cls "${x}"
177 local count=0 devs= dev rest l
179 # find all the devices in the system
181 while IFS=":| " read dev rest
184 [ ${count} -le 2 ] && continue
185 devs="${devs} ${dev}"
188 # from all the devices find the ones
189 # that have QoS defined
190 # unfortunately, one fork per device cannot be avoided
194 l="$(${tc} class show dev ${dev} 2>/dev/null)"
195 [ ! -z "${l}" ] && tc_devices="${tc_devices} ${dev}"
199 # update devices and class names
200 # once every 2 minutes
201 names_every=$((qos_get_class_names_every / update_every))
203 # exit this script every hour
204 # it will be restarted automatically
205 exit_after=$((qos_exit_every / update_every))
215 if [ ${c} -le 1 -o ${c} -ge ${names_every} ]
222 for d in ${tc_devices}
227 echo "WORKTIME ${LOOPSLEEPMS_LASTWORK}"
229 loopsleepms ${update_every}
231 [ ${gc} -gt ${exit_after} ] && exit 0