]> arthur.barton.de Git - netdata.git/blob - plugins.d/tc-qos-helper.sh
Merge pull request #888 from rsanger/fix_hddtemp
[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 sleep function
31 LOOPSLEEPMS_LASTWORK=0
32 loopsleepms() {
33     sleep $1
34 }
35
36 # if found and included, this file overwrites loopsleepms()
37 # with a high resolution timer function for precise looping.
38 . "${plugins_dir}/loopsleepms.sh.inc"
39
40 if [ -z "${tc}" -o ! -x "${tc}" ]
41     then
42     echo >&2 "${PROGRAM_NAME}: Cannot find command 'tc' in this system."
43     exit 1
44 fi
45
46 devices=
47 fix_names=
48
49 setclassname() {
50     echo "SETCLASSNAME $3 $2"
51 }
52
53 show_tc() {
54     local x="${1}" interface_dev interface_classes interface_classes_monitor
55
56     echo "BEGIN ${x}"
57     ${tc} -s class show dev ${x}
58
59     # check FireQOS names for classes
60     if [ ! -z "${fix_names}" -a -f "${fireqos_run_dir}/ifaces/${x}" ]
61     then
62         name="$(<"${fireqos_run_dir}/ifaces/${x}")"
63         echo "SETDEVICENAME ${name}"
64
65         interface_dev=
66         interface_classes=
67         interface_classes_monitor=
68         source "${fireqos_run_dir}/${name}.conf"
69         for n in ${interface_classes_monitor}
70         do
71             setclassname ${n//|/ }
72         done
73         [ ! -z "${interface_dev}" ] && echo "SETDEVICEGROUP ${interface_dev}"
74     fi
75     echo "END ${x}"
76 }
77
78 all_devices() {
79     cat /proc/net/dev | grep ":" | cut -d ':' -f 1 | while read dev
80     do
81         l=$(${tc} class show dev ${dev} | wc -l)
82         [ $l -ne 0 ] && echo ${dev}
83     done
84 }
85
86 # update devices and class names
87 # once every 2 minutes
88 names_every=$((qos_get_class_names_every / update_every))
89
90 # exit this script every hour
91 # it will be restarted automatically
92 exit_after=$((qos_exit_every / update_every))
93
94 c=0
95 gc=0
96 while [ 1 ]
97 do
98     fix_names=
99     c=$((c + 1))
100     gc=$((gc + 1))
101
102     if [ ${c} -le 1 -o ${c} -ge ${names_every} ]
103     then
104         c=1
105         fix_names="YES"
106         devices="$( all_devices )"
107     fi
108
109     for d in ${devices}
110     do
111         show_tc ${d}
112     done
113
114     echo "WORKTIME ${LOOPSLEEPMS_LASTWORK}"
115
116     loopsleepms ${update_every}
117
118     [ ${gc} -gt ${exit_after} ] && exit 0
119 done