]> arthur.barton.de Git - netdata.git/blob - plugins.d/tc-qos-helper.sh
Merge pull request #1065 from facetoe/postgres_plugin
[netdata.git] / plugins.d / tc-qos-helper.sh
1 #!/usr/bin/env bash
2
3 # netdata
4 # real-time performance and health monitoring, done right!
5 # (C) 2016 Costa Tsaousis <costa@tsaousis.gr>
6 # GPL v3+
7 #
8 # This script is a helper to allow netdata collect tc data
9 # parsing tc output has been implemented in C, inside netdata
10 # This script allows setting names to dimensions.
11
12 export PATH="${PATH}:/sbin:/usr/sbin:/usr/local/sbin"
13 export LC_ALL=C
14
15 PROGRAM_FILE="$0"
16 PROGRAM_NAME="$(basename $0)"
17 PROGRAM_NAME="${PROGRAM_NAME/.plugin}"
18
19 # -----------------------------------------------------------------------------
20
21 logdate() {
22     date "+%Y-%m-%d %H:%M:%S"
23 }
24
25 log() {
26     local status="${1}"
27     shift
28
29     echo >&2 "$(logdate): ${PROGRAM_NAME}: ${status}: ${*}"
30
31 }
32
33 warning() {
34     log WARNING "${@}"
35 }
36
37 error() {
38     log ERROR "${@}"
39 }
40
41 info() {
42     log INFO "${@}"
43 }
44
45 fatal() {
46     log FATAL "${@}"
47     exit 1
48 }
49
50 debug=0
51 debug() {
52     [ $debug -eq 1 ] && log DEBUG "${@}"
53 }
54
55 # -----------------------------------------------------------------------------
56
57 plugins_dir="${NETDATA_PLUGINS_DIR}"
58 [ -z "$plugins_dir" ] && plugins_dir="$( dirname $PROGRAM_FILE )"
59
60 config_dir=${NETDATA_CONFIG_DIR-/etc/netdata}
61 tc="$(which tc 2>/dev/null)"
62 fireqos_run_dir="/var/run/fireqos"
63 qos_get_class_names_every=120
64 qos_exit_every=3600
65
66 # check if we have a valid number for interval
67 t=${1}
68 update_every=$((t))
69 [ $((update_every)) -lt 1 ] && update_every=${NETDATA_UPDATE_EVERY}
70 [ $((update_every)) -lt 1 ] && update_every=1
71
72 # allow the user to override our defaults
73 if [ -f "${config_dir}/tc-qos-helper.conf" ]
74     then
75     source "${config_dir}/tc-qos-helper.conf"
76 fi
77
78 # default sleep function
79 LOOPSLEEPMS_LASTWORK=0
80 loopsleepms() {
81     sleep $1
82 }
83
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"
87
88 if [ -z "${tc}" -o ! -x "${tc}" ]
89     then
90     fatal "cannot find command 'tc' in this system."
91 fi
92
93 devices=
94 fix_names=
95
96 setclassname() {
97     echo "SETCLASSNAME $3 $2"
98 }
99
100 show_tc() {
101     local x="${1}" interface_dev interface_classes interface_classes_monitor
102
103     echo "BEGIN ${x}"
104     ${tc} -s class show dev ${x}
105
106     # check FireQOS names for classes
107     if [ ! -z "${fix_names}" -a -f "${fireqos_run_dir}/ifaces/${x}" ]
108     then
109         name="$(<"${fireqos_run_dir}/ifaces/${x}")"
110         echo "SETDEVICENAME ${name}"
111
112         interface_dev=
113         interface_classes=
114         interface_classes_monitor=
115         source "${fireqos_run_dir}/${name}.conf"
116         for n in ${interface_classes_monitor}
117         do
118             setclassname ${n//|/ }
119         done
120         [ ! -z "${interface_dev}" ] && echo "SETDEVICEGROUP ${interface_dev}"
121     fi
122     echo "END ${x}"
123 }
124
125 all_devices() {
126     cat /proc/net/dev | grep ":" | cut -d ':' -f 1 | while read dev
127     do
128         l=$(${tc} class show dev ${dev} | wc -l)
129         [ $l -ne 0 ] && echo ${dev}
130     done
131 }
132
133 # update devices and class names
134 # once every 2 minutes
135 names_every=$((qos_get_class_names_every / update_every))
136
137 # exit this script every hour
138 # it will be restarted automatically
139 exit_after=$((qos_exit_every / update_every))
140
141 c=0
142 gc=0
143 while [ 1 ]
144 do
145     fix_names=
146     c=$((c + 1))
147     gc=$((gc + 1))
148
149     if [ ${c} -le 1 -o ${c} -ge ${names_every} ]
150     then
151         c=1
152         fix_names="YES"
153         devices="$( all_devices )"
154     fi
155
156     for d in ${devices}
157     do
158         show_tc ${d}
159     done
160
161     echo "WORKTIME ${LOOPSLEEPMS_LASTWORK}"
162
163     loopsleepms ${update_every}
164
165     [ ${gc} -gt ${exit_after} ] && exit 0
166 done