]> arthur.barton.de Git - netdata.git/blobdiff - plugins.d/tc-qos-helper.sh
More actionable notification message for pagerduty
[netdata.git] / plugins.d / tc-qos-helper.sh
index a0edadac34605027fd90fd702b2cca4e7125810a..7e1e92cf33a27c1cb7bea133970d2cdca238a4d4 100755 (executable)
@@ -1,16 +1,64 @@
 #!/usr/bin/env bash
 
+# netdata
+# real-time performance and health monitoring, done right!
+# (C) 2016 Costa Tsaousis <costa@tsaousis.gr>
+# GPL v3+
+#
+# This script is a helper to allow netdata collect tc data
+# parsing tc output has been implemented in C, inside netdata
+# This script allows setting names to dimensions.
+
 export PATH="${PATH}:/sbin:/usr/sbin:/usr/local/sbin"
+export LC_ALL=C
 
 PROGRAM_FILE="$0"
 PROGRAM_NAME="$(basename $0)"
 PROGRAM_NAME="${PROGRAM_NAME/.plugin}"
 
+# -----------------------------------------------------------------------------
+
+logdate() {
+    date "+%Y-%m-%d %H:%M:%S"
+}
+
+log() {
+    local status="${1}"
+    shift
+
+    echo >&2 "$(logdate): ${PROGRAM_NAME}: ${status}: ${*}"
+
+}
+
+warning() {
+    log WARNING "${@}"
+}
+
+error() {
+    log ERROR "${@}"
+}
+
+info() {
+    log INFO "${@}"
+}
+
+fatal() {
+    log FATAL "${@}"
+    exit 1
+}
+
+debug=0
+debug() {
+    [ $debug -eq 1 ] && log DEBUG "${@}"
+}
+
+# -----------------------------------------------------------------------------
+
 plugins_dir="${NETDATA_PLUGINS_DIR}"
 [ -z "$plugins_dir" ] && plugins_dir="$( dirname $PROGRAM_FILE )"
 
 config_dir=${NETDATA_CONFIG_DIR-/etc/netdata}
-tc="$(which tc 2>/dev/null)"
+tc="$(which tc 2>/dev/null || command -v tc 2>/dev/null)"
 fireqos_run_dir="/var/run/fireqos"
 qos_get_class_names_every=120
 qos_exit_every=3600
@@ -23,21 +71,14 @@ update_every=$((t))
 
 # allow the user to override our defaults
 if [ -f "${config_dir}/tc-qos-helper.conf" ]
-       then
-       source "${config_dir}/tc-qos-helper.conf"
+    then
+    source "${config_dir}/tc-qos-helper.conf"
 fi
 
-# default time function
-now_ms=
-current_time_ms() {
-       now_ms="$(date +'%s')000"
-}
-
 # default sleep function
 LOOPSLEEPMS_LASTWORK=0
 loopsleepms() {
-       [ "$1" = "tellwork" ] && shift
-       sleep $1
+    sleep $1
 }
 
 # if found and included, this file overwrites loopsleepms()
@@ -45,49 +86,48 @@ loopsleepms() {
 . "${plugins_dir}/loopsleepms.sh.inc"
 
 if [ -z "${tc}" -o ! -x "${tc}" ]
-       then
-       echo >&2 "${PROGRAM_NAME}: Cannot find command 'tc' in this system."
-       exit 1
+    then
+    fatal "cannot find command 'tc' in this system."
 fi
 
 devices=
 fix_names=
 
 setclassname() {
-       echo "SETCLASSNAME $3 $2"
+    echo "SETCLASSNAME $3 $2"
 }
 
 show_tc() {
-       local x="${1}" interface_dev interface_classes interface_classes_monitor
-
-       echo "BEGIN ${x}"
-       ${tc} -s class show dev ${x}
-
-       # check FireQOS names for classes
-       if [ ! -z "${fix_names}" -a -f "${fireqos_run_dir}/ifaces/${x}" ]
-       then
-               name="$(<"${fireqos_run_dir}/ifaces/${x}")"
-               echo "SETDEVICENAME ${name}"
-
-               interface_dev=
-               interface_classes=
-               interface_classes_monitor=
-               source "${fireqos_run_dir}/${name}.conf"
-               for n in ${interface_classes_monitor}
-               do
-                       setclassname ${n//|/ }
-               done
-               [ ! -z "${interface_dev}" ] && echo "SETDEVICEGROUP ${interface_dev}"
-       fi
-       echo "END ${x}"
+    local x="${1}" interface_dev interface_classes interface_classes_monitor
+
+    echo "BEGIN ${x}"
+    ${tc} -s class show dev ${x}
+
+    # check FireQOS names for classes
+    if [ ! -z "${fix_names}" -a -f "${fireqos_run_dir}/ifaces/${x}" ]
+    then
+        name="$(<"${fireqos_run_dir}/ifaces/${x}")"
+        echo "SETDEVICENAME ${name}"
+
+        interface_dev=
+        interface_classes=
+        interface_classes_monitor=
+        source "${fireqos_run_dir}/${name}.conf"
+        for n in ${interface_classes_monitor}
+        do
+            setclassname ${n//|/ }
+        done
+        [ ! -z "${interface_dev}" ] && echo "SETDEVICEGROUP ${interface_dev}"
+    fi
+    echo "END ${x}"
 }
 
 all_devices() {
-       cat /proc/net/dev | grep ":" | cut -d ':' -f 1 | while read dev
-       do
-               l=$(${tc} class show dev ${dev} | wc -l)
-               [ $l -ne 0 ] && echo ${dev}
-       done
+    cat /proc/net/dev | grep ":" | cut -d ':' -f 1 | while read dev
+    do
+        l=$(${tc} class show dev ${dev} | wc -l)
+        [ $l -ne 0 ] && echo ${dev}
+    done
 }
 
 # update devices and class names
@@ -102,25 +142,25 @@ c=0
 gc=0
 while [ 1 ]
 do
-       fix_names=
-       c=$((c + 1))
-       gc=$((gc + 1))
+    fix_names=
+    c=$((c + 1))
+    gc=$((gc + 1))
 
-       if [ ${c} -le 1 -o ${c} -ge ${names_every} ]
-       then
-               c=1
-               fix_names="YES"
-               devices="$( all_devices )"
-       fi
+    if [ ${c} -le 1 -o ${c} -ge ${names_every} ]
+    then
+        c=1
+        fix_names="YES"
+        devices="$( all_devices )"
+    fi
 
-       for d in ${devices}
-       do
-               show_tc ${d}
-       done
+    for d in ${devices}
+    do
+        show_tc ${d}
+    done
 
-       echo "WORKTIME ${LOOPSLEEPMS_LASTWORK}"
+    echo "WORKTIME ${LOOPSLEEPMS_LASTWORK}"
 
-       loopsleepms ${update_every}
+    loopsleepms ${update_every}
 
-       [ ${gc} -gt ${exit_after} ] && exit 0
+    [ ${gc} -gt ${exit_after} ] && exit 0
 done