LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 06-30-2016, 10:28 AM   #1
rabble1357
LQ Newbie
 
Registered: Jan 2016
Posts: 5

Rep: Reputation: Disabled
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
 
Old 07-01-2016, 12:24 AM   #2
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
You mean that MbitConTotalOut is sometimes negative?

There are a lot of calculations in that script. Why don't you just echo all intermediate values that lead to MbitConTotalOut? That should tell you quickly where the error is.

Or run the script with the -x option, e.g. bash -x bandwidthscript. This will output all lines that are executed, and all values that are used.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with Bandwidth Script rabble1357 Linux - Networking 1 06-30-2016 10:29 AM
download manager script with bandwidth limiting steve51184 Linux - Server 1 08-03-2008 04:59 PM
ip bandwidth limiter script problems mrweirdo Linux - Networking 0 07-18-2005 10:50 PM
Bandwidth Allocation Script error.. joirnange Linux - Networking 17 01-10-2005 08:15 PM
How do you limit bandwidth on your webserver account with a script??? timmy_laf Programming 1 01-08-2004 03:15 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 09:03 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration