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