Extract numbers from output file and compare to value

Hi,

I developed a script which finally originates a similar output:

net0:up,Tx=475198bps,Rx=31009bps net1:up,Tx=39596bps,Rx=35678bps

Of course the figures change and also the amount of interfaces (ex: could be more then net0 and net1). This is done automatically.

The next step i'm trying to achieve is to extract these figures and compare them with a value. if the figures are larger then the value then i originate an output like:

net0 Tx is higher then the threshold

However i cannot understand how to make the script intelligent enough to output which receive is (transmit OR receive is higher then the threshold specified AND also which interface or interfaces

I tried to use the for loop like:

TX=`sed 's/:up//g' test.txt | sed 's/bps//g' | awk -F',' '{print$2}' | cut -c4-`
RX=`sed 's/:up//g' test.txt | sed 's/bps//g' | awk -F',' '{print$3}' | cut -c4-`

for tx in $TX
do
if [[ $tx -gt 9 ]]; then
INT=`grep $tx test.txt | awk -F',' '{print$1}' | cut -f1 -d":"`
echo -n "$INT tx is higher then usual "
fi
done

for rx in $RX
do
if [[ $rx -ge 0 ]]; then
INT=`grep $rx test.txt | awk -F',' '{print$1}' | cut -f1 -d":"`
echo -n "$INT rx is higher then usual "
fi
done

But with this scenario i cannot make an efficient output that i might know that for example both Tx/Rx are high or one of the interfaces has a high Tx.
What i'm trying to achieve is an output similar to this:

The following interfaces has high Tx usage: net0
The following interfaces has both high Tx/Rx usage: net0 net1

The script developed is in bash.

Could you kindly give me some hints?

Rgds,

Matthew

Hello Matthew, you could do something like:

read -a intf < test.txt
for ((i=0; i<${#intf[@]}; i++))
do
  IFS=',:=' read ifname status x tx x rx <<< "${intf}"
  echo "This is interface ${ifname} with status ${status} and rx ${rx%bps} and tx ${tx%bps}"
done

Which should give you the freedom to add if statements in the loop

2 Likes
sed 's/net/\nnet/g' test.txt | awk '
/^net.*:.*Tx *=.*Rx *=/ {
   tx=$0;
   sub(".*Tx= *", "", tx);
   sub("[^0-9].*", "", tx);
   rx=$0;
   sub(".*Rx= *", "", rx);
   sub("[^0-9].*", "", rx);
   net=$0;
   sub(" *:.*", "", net);
   if (tx > tx_max) txs=txs net " ";
   if (rx > rx_max) rxs=rxs net " ";
   if (rx > rx_max && tx > tx_max) rxtx=rxtx net " ";
}
END {
      print "The following interfaces have high Tx usage:", txs
      print "The following interfaces have high Rx usage:", rxs
      print "The following interfaces have both high Tx/Rx usage:", rxtx
}
' tx_max=9 rx_max=0
1 Like

I like the shell solution in post#2, it is also a bit closer to the attempt in post#1.
Looping over the values is somewhat simpler than over the indexes

read -a intf < test.txt
for i in "${intf[@]}"
do
  IFS=',:=' read ifname status _ tx _ rx <<< "$i"
  # strip from the end up to a non-digit: % or bps or .0 or .0%
  echo "This is interface ${ifname} with status ${status} and rx ${rx%%[!0-9]*} and tx ${tx%%[!0-9]*}"
done
1 Like

Thank you for your replies. I will have a go at your codes code and let you know!