]> arthur.barton.de Git - netdata.git/blob - plugins.d/tc-qos-helper.sh
bff5217d28a449e81d96ec0fb55e34a14631045d
[netdata.git] / plugins.d / tc-qos-helper.sh
1 #!/usr/bin/env bash
2
3 export PATH="${PATH}:/sbin:/usr/sbin:/usr/local/sbin"
4
5 PROGRAM_FILE="$0"
6 PROGRAM_NAME="$(basename $0)"
7 PROGRAM_NAME="${PROGRAM_NAME/.plugin}"
8
9 plugins_dir="${NETDATA_PLUGINS_DIR}"
10 [ -z "$plugins_dir" ] && plugins_dir="$( dirname $PROGRAM_FILE )"
11
12 config_dir=${NETDATA_CONFIG_DIR-/etc/netdata}
13 tc="$(which tc 2>/dev/null)"
14 fireqos_run_dir="/var/run/fireqos"
15 qos_get_class_names_every=120
16 qos_exit_every=3600
17
18 # check if we have a valid number for interval
19 t=${1}
20 update_every=$((t))
21 [ $((update_every)) -lt 1 ] && update_every=${NETDATA_UPDATE_EVERY}
22 [ $((update_every)) -lt 1 ] && update_every=1
23
24 # allow the user to override our defaults
25 if [ -f "${config_dir}/tc-qos-helper.conf" ]
26     then
27     source "${config_dir}/tc-qos-helper.conf"
28 fi
29
30 # default time function
31 now_ms=
32 current_time_ms() {
33     now_ms="$(date +'%s')000"
34 }
35
36 # default sleep function
37 LOOPSLEEPMS_LASTWORK=0
38 loopsleepms() {
39     [ "$1" = "tellwork" ] && shift
40     sleep $1
41 }
42
43 # if found and included, this file overwrites loopsleepms()
44 # with a high resolution timer function for precise looping.
45 . "${plugins_dir}/loopsleepms.sh.inc"
46
47 if [ -z "${tc}" -o ! -x "${tc}" ]
48     then
49     echo >&2 "${PROGRAM_NAME}: Cannot find command 'tc' in this system."
50     exit 1
51 fi
52
53 devices=
54 fix_names=
55
56 setclassname() {
57     echo "SETCLASSNAME $3 $2"
58 }
59
60 show_tc() {
61     local x="${1}" interface_dev interface_classes interface_classes_monitor
62
63     echo "BEGIN ${x}"
64     ${tc} -s class show dev ${x}
65
66     # check FireQOS names for classes
67     if [ ! -z "${fix_names}" -a -f "${fireqos_run_dir}/ifaces/${x}" ]
68     then
69         name="$(<"${fireqos_run_dir}/ifaces/${x}")"
70         echo "SETDEVICENAME ${name}"
71
72         interface_dev=
73         interface_classes=
74         interface_classes_monitor=
75         source "${fireqos_run_dir}/${name}.conf"
76         for n in ${interface_classes_monitor}
77         do
78             setclassname ${n//|/ }
79         done
80         [ ! -z "${interface_dev}" ] && echo "SETDEVICEGROUP ${interface_dev}"
81     fi
82     echo "END ${x}"
83 }
84
85 all_devices() {
86     cat /proc/net/dev | grep ":" | cut -d ':' -f 1 | while read dev
87     do
88         l=$(${tc} class show dev ${dev} | wc -l)
89         [ $l -ne 0 ] && echo ${dev}
90     done
91 }
92
93 # update devices and class names
94 # once every 2 minutes
95 names_every=$((qos_get_class_names_every / update_every))
96
97 # exit this script every hour
98 # it will be restarted automatically
99 exit_after=$((qos_exit_every / update_every))
100
101 c=0
102 gc=0
103 while [ 1 ]
104 do
105     fix_names=
106     c=$((c + 1))
107     gc=$((gc + 1))
108
109     if [ ${c} -le 1 -o ${c} -ge ${names_every} ]
110     then
111         c=1
112         fix_names="YES"
113         devices="$( all_devices )"
114     fi
115
116     for d in ${devices}
117     do
118         show_tc ${d}
119     done
120
121     echo "WORKTIME ${LOOPSLEEPMS_LASTWORK}"
122
123     loopsleepms ${update_every}
124
125     [ ${gc} -gt ${exit_after} ] && exit 0
126 done