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 # check if we have a valid number for interval
69 [ $((update_every)) -lt 1 ] && update_every=${NETDATA_UPDATE_EVERY}
70 [ $((update_every)) -lt 1 ] && update_every=1
72 # allow the user to override our defaults
73 if [ -f "${config_dir}/tc-qos-helper.conf" ]
75 source "${config_dir}/tc-qos-helper.conf"
78 # default sleep function
79 LOOPSLEEPMS_LASTWORK=0
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"
88 if [ -z "${tc}" -o ! -x "${tc}" ]
90 fatal "cannot find command 'tc' in this system."
97 echo "SETCLASSNAME $3 $2"
103 if [ -f /etc/iproute2/tc_cls ]
105 local classid name rest
106 while read classid name rest
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
117 show_fireqos_names() {
118 local x="${1}" name n interface_dev interface_classes interface_classes_monitor
120 if [ -f "${fireqos_run_dir}/ifaces/${x}" ]
122 name="$(<"${fireqos_run_dir}/ifaces/${x}")"
123 echo "SETDEVICENAME ${name}"
127 interface_classes_monitor=
128 source "${fireqos_run_dir}/${name}.conf"
129 for n in ${interface_classes_monitor}
131 setclassname ${n//|/ }
133 [ ! -z "${interface_dev}" ] && echo "SETDEVICEGROUP ${interface_dev}"
146 # netdata can parse the output of tc
147 ${tc} -s class show dev ${x}
149 # check FireQOS names for classes
150 if [ ! -z "${fix_names}" ]
152 show_fireqos_names "${x}" || show_tc_cls "${x}"
159 local count=0 devs= dev rest l
161 # find all the devices in the system
163 while IFS=":| " read dev rest
166 [ ${count} -le 2 ] && continue
167 devs="${devs} ${dev}"
170 # from all the devices find the ones
171 # that have QoS defined
172 # unfortunately, one fork per device cannot be avoided
176 l="$(${tc} class show dev ${dev} 2>/dev/null)"
177 [ ! -z "${l}" ] && tc_devices="${tc_devices} ${dev}"
181 # update devices and class names
182 # once every 2 minutes
183 names_every=$((qos_get_class_names_every / update_every))
185 # exit this script every hour
186 # it will be restarted automatically
187 exit_after=$((qos_exit_every / update_every))
197 if [ ${c} -le 1 -o ${c} -ge ${names_every} ]
204 for d in ${tc_devices}
209 echo "WORKTIME ${LOOPSLEEPMS_LASTWORK}"
211 loopsleepms ${update_every}
213 [ ${gc} -gt ${exit_after} ] && exit 0