diff --git a/agent/bin/datasid-netmon.sh b/agent/bin/datasid-netmon.sh index 4e0f356b7b98ef7e13c4cf1ffe3b6c6ef8ff0075..d528120242d21f744bedba888f40925200c99f5a 100755 --- a/agent/bin/datasid-netmon.sh +++ b/agent/bin/datasid-netmon.sh @@ -123,8 +123,23 @@ OUTPUT_COUNT=$(sudo $IPTABLES -nvxL OUTPUT | grep DS_OUTPUT_COUNTS | \ {pkts+=$1; bytes+=$2} \ END{print pkts,bytes}') +# Add new entry to traffic file echo "$INTERVALID $INPUT_COUNT $OUTPUT_COUNT" >> $TRAFFIC +# If this script is run more than once in less than a five minutes interval we +# might end up with duplicated lines, which could mess with the XML file +# later, so better make sure this doesn't happen. +if [ $(grep -cw "^$INTERVALID" $TRAFFIC) -gt 1 ]; then + # If we are unfortunate enough to have duplicated lines we sum up them with + # the new network data aquired. + IN_OUT=$(awk -v inp=$INTERVALID 'BEGIN{rxpkts=0; rxbytes=0; txpkts=0; txbytes=0} $1==inp \ + {rxpkts+=$2; rxbytes+=$3; txpkts+=$4; txbytes+=$5} \ + END{print rxpkts,rxbytes,txpkts,txbytes}' $TRAFFIC) + + # Remove all lines and add only the sum of them. + sed -i "/^$INTERVALID/d" $TRAFFIC + echo "$INTERVALID $IN_OUT" >> $TRAFFIC +fi #------------------------------------------------------------------------------ # Create or update iptables rules (used for counting) based on the interfaces