diff --git a/check_netio b/check_netio index 920ad608223cd97d0f4618e0c306b433cea16681..a42c5f3a3c7ff2ae304af2a77f02ad33c17bfe27 100755 --- a/check_netio +++ b/check_netio @@ -10,12 +10,13 @@ # 2023-08-22 v1.2 <axel.hahn@unibe.ch> send perf data on single interface too; interfaces in alphabetic order; no more tmp file # 2023-08-22 v1.3 <axel.hahn@unibe.ch> show status line with 2 digits after "." # 2023-09-22 v1.4 <axel.hahn@unibe.ch> fix syntax error -eq: unary operator expected +# 2024-01-10 v1.5 <axel.hahn@unibe.ch> added regex filter for include and exclude # ====================================================================== . $( dirname $0 )/inc_pluginfunctions -export self_APPVERSION=1.4 +export self_APPVERSION=1.5 # ---------------------------------------------------------------------- @@ -41,6 +42,10 @@ OPTIONS: -h this help PARAMETERS: + + -f REGEX filter interfaces by given regex. + -r REGEX remove interfaces by given regex + -i INTERFACE show this interface only. Quote multiple interfaces. The check returns unknown if an interface does not exist. @@ -51,6 +56,16 @@ EXAMPLES: $_self -i "eth0 eth1" show netio of given interfaces. + $_self -f "^eth" + show netio of interfaces beginning with "eth" + + $_self -f "^eth" -r "eth2" + show netio of interfaces matching "^eth" but not "eth2" + + $_self -f "^(enp|wlp)" + Example to include multiple interface names: use brackets + and divide expressions with pipe sign. + EOF } @@ -76,48 +91,55 @@ typeset -i iTotalTrans=0 data=$( cat /proc/net/dev | grep ":" | grep -vE "(lo|bond.*|ppp.*):") -allInterfaces=$( cut -f 1 -d ":" <<< "$data" | tr -d " " ) +sFilter=$( ph.getValueWithParam "." "f" "$@" ) +sRemove=$( ph.getValueWithParam "SOMETHING_INVALID" "r" "$@" ) + +allInterfaces=$( cut -f 1 -d ":" <<< "$data" | tr -d " " | grep -E "$sFilter" | grep -vE "$sRemove" ) + sInterface=$( ph.getValueWithParam "$allInterfaces" "i" "$@" | sort ) out="---------- Interfaces:" - # ----- loop over all interfaces iCountInterfaces=$( wc -w <<< "$sInterface" ) +if [ $iCountInterfaces -eq 0 ]; then + ph.abort "ERROR: No interface was found." +fi - for myinterface in $sInterface - do +for myinterface in $sInterface +do - line=$( grep "${myinterface}:" <<< "$data" ) - if [ -n "$line" ] ; then + line=$( grep "${myinterface}:" <<< "$data" ) + if [ -n "$line" ] ; then - # echo "SOURCE: $line" + # echo "SOURCE: $line" - # total value of byte - iRead=$( echo "$line" | awk '{ print $2 }') - iTrans=$(echo "$line" | awk '{ print $10 }') + # total value of byte + iRead=$( echo "$line" | awk '{ print $2 }') + iTrans=$(echo "$line" | awk '{ print $10 }') - # speed in byte per sec based on last stored value and its age - iSpeedRead=$( ph.perfdeltaspeed "netio-${myinterface}-rx" $iRead) - iSpeedTrans=$(ph.perfdeltaspeed "netio-${myinterface}-tx" $iTrans) + # speed in byte per sec based on last stored value and its age + iSpeedRead=$( ph.perfdeltaspeed "netio-${myinterface}-rx" $iRead) + iSpeedTrans=$(ph.perfdeltaspeed "netio-${myinterface}-tx" $iTrans) - out+=$( - printf "\n>>> %s\n" "${myinterface}" - printf "%15s %15s %10s %-10s \n" "receive:" $iRead $iSpeedRead "byte/s" - printf "%15s %15s %10s %-10s \n" "transmit:" $iTrans $iSpeedTrans "byte/s" - echo " " - ) - ph.perfadd "rx-${myinterface}" "$iSpeedRead" - ph.perfadd "tx-${myinterface}" "$iSpeedTrans" + out+=$( + printf "\n>>> %s\n" "${myinterface}" + printf "%15s %15s %10s %-10s \n" "receive:" $iRead $iSpeedRead "byte/s" + printf "%15s %15s %10s %-10s \n" "transmit:" $iTrans $iSpeedTrans "byte/s" + echo " " + ) + ph.perfadd "rx-${myinterface}" "$iSpeedRead" + ph.perfadd "tx-${myinterface}" "$iSpeedTrans" - iTotalRead=$iTotalRead+$iSpeedRead - iTotalTrans=$iTotalTrans+$iSpeedTrans - else - ph.setStatus "unknown" - out+=$( printf "\nERROR: The interface [${myinterface}] does not exist." ) - fi + iTotalRead=$iTotalRead+$iSpeedRead + iTotalTrans=$iTotalTrans+$iSpeedTrans + else + # ph.setStatus "unknown" + # out+=$( printf "\nERROR: The interface [${myinterface}] does not exist." ) + ph.abort "ERROR: The interface [${myinterface}] does not exist." + fi - done +done # ----- total if [ $iCountInterfaces -gt 1 ]; then diff --git a/docs/20_Checks/check_netio.md b/docs/20_Checks/check_netio.md index 07243bc81e45f2565b9a53a398f62e140af7c26b..ba5db8d20a4d349eed79db9c471614d22b644379 100644 --- a/docs/20_Checks/check_netio.md +++ b/docs/20_Checks/check_netio.md @@ -13,11 +13,10 @@ This plugin scans `/proc/net/dev` with the regex `eth|en[ops][0-9]*` to show a f ## Syntax ```text -> ./check_netio -h ______________________________________________________________________ CHECK_NETIO -v1.4 +v1.5 (c) Institute for Medical Education - University of Bern Licence: GNU GPL 3 @@ -38,6 +37,10 @@ OPTIONS: -h this help PARAMETERS: + + -f REGEX filter interfaces by given regex. + -r REGEX remove interfaces by given regex + -i INTERFACE show this interface only. Quote multiple interfaces. The check returns unknown if an interface does not exist. @@ -48,6 +51,16 @@ EXAMPLES: check_netio -i "eth0 eth1" show netio of given interfaces. + check_netio -f "^eth" + show netio of interfaces beginning with "eth" + + check_netio -f "^eth" -r "eth2" + show netio of interfaces matching "^eth" but not "eth2" + + check_netio -f "^(enp|wlp)" + Example to include multiple interface names: use brackets + and divide expressions with pipe sign. + ``` ## Examples @@ -74,10 +87,20 @@ If you enter a wrong interface the check returns with "UNKNOWN". `check_netio -i "whatever"`returns ```txt -UNKNOWN: Network IO ... IN 0 MB/s >> [host] >> 0 MB/s OUT +ERROR: the interface [whatever] does not exist. +``` ---- whatever: -UNKNOWN: the interface whatever does not exist. +### Filter interface list - |rx=0;; tx=0;; - ``` +The parameter `-f` is followes by pattern that an interface must match. +The opposite does the regexp behind `-r`: it removes interfaces that match that regex. + +You can combine both parameters. + +The regexp can be an extended regexp (`grep -E <pattern>`). This allows to use brackets and divide expressions with pipe sign, eg. `check_netio -f "^(enp|wlp)"` + +If your filter rules remove all existing interfaces you get an error message: + +```txt +ERROR: No interface was found. +```