Help with Bandwidth Script
Hello everyone I am having a little issue with my bandwidth script and I am not sure really what the issue is. I was hoping to get a fresh set of eyes on it. Almost everything works correctly but I am getting the wrong value for data going out. The calculations and results for the IN results is working perfectly but I am getting the wrong value for total MB going out and sometimes it is a negative value and that throws off the bandwidth calculations. I have removed the IP address for security purposes. Here is the script below, any help you could provide would be much appreciated!
_______________________________________________________________
while true
do
bytes_beforeTOT=0;bytes_afterTOT=0;
bytes_beforeOUT=0;bytes_afterOUT=0;
bytes_beforeIN=0;bytes_afterIN=0;
data=0.01;
bytes_beforeOUT=$((snmpget -v2c -c primealerts (IP ADDRESS) 1.3.6.1.2.1.2.2.1.16.529) | awk '{print $4}')
bytes_beforeIN=$((snmpget -v2c -c primealerts (IP ADDRESS) 1.3.6.1.2.1.2.2.1.10.529) | awk '{print $4}')
#####################
#Conversion for Out Data Before
KilobitOut=$((bytes_beforeOUT / 1024))
MegabitOut=$((KilobitOut / 1024))
#########################
#Conversion for In Data Before
KilobitIn=$((bytes_beforeIN / 1024))
MegabitIn=$((KilobitIn / 1024))
#########################
sleep 300
bytes_afterOUT=$((snmpget -v2c -c primealerts (IP ADDRESS) 1.3.6.1.2.1.2.2.1.16.529) | awk '{print $4}')
bytes_afterIN=$((snmpget -v2c -c primealerts (IP ADDRESS) 1.3.6.1.2.1.2.2.1.10.529) | awk '{print $4}')
#########################
#Conversion for Out Data After
KilobitOutAfter=$((bytes_afterOUT / 1024))
MegabitOutAfter=$((KilobitOutAfter / 1024))
#########################
#Conversion for In Data After
KilobitInAfter=$((bytes_afterIN / 1024))
MegabitInAfter=$((KilobitInAfter / 1024))
#########################
#Calculations
TotalMbitIn=$(($MegabitInAfter - $MegabitIn))
TotalMbitOut=$(($MegabitOutAfter - $MegabitOut))
###################################
#New Conversion Gbit per second In
MbitConTotal=$((TotalMbitIn * 8))
MbitPerSecond=$(echo "$MbitConTotal / 300" | bc -l)
GbitPerSecond=$(echo "$MbitPerSecond / 1000" | bc -l)
echo "$TotalMbitIn total megabits In"
echo "$TotalMbitOut total megabits Out"
if [ 1 -eq "$(echo "${GbitPerSecond} >= ${data}" | bc)" ]
then
GbsIn=$(printf "%0.2f\n" "$GbitPerSecond")
echo "$GbsIn IN"
else
echo "0.01 IN"
fi
###################################
#New Conversion Gbit per second Out
MbitConTotalOut=$((TotalMbitOut * 8))
MbitPerSecondOut=$(echo "$MbitConTotalOut / 300" | bc -l)
GbitPerSecondOut=$(echo "$MbitPerSecondOut / 1000" | bc -l)
if [ 1 -eq "$(echo "${GbitPerSecondOut} >= ${data}" | bc)" ]
then
GbsOut=$(printf "%0.2f\n" "$GbitPerSecondOut")
echo "$GbsOut OUT"
else
echo "0.01 OUT"
fi
echo
date +'%r'
echo "END OF RUN"
echo
done
|