Solved

Calculate Network bandwidth Usage.

Posted on 2004-08-13
3
437 Views
Last Modified: 2013-12-27
Hi,

How can I calculate the Kbit/s being transfered through the network card?

# netstat -i 1 2 shows the number of packets but im intrested in Kbit/s

I cannot load any additional programs. Perl is available.

cheers.
0
Comment
Question by:campid
3 Comments
 
LVL 11

Accepted Solution

by:
jgiordano earned 125 total points
ID: 11793183
use the following KSH:

#!/bin/ksh
#=========================================#
# Net statistics for Solaris 2.x via kstat
# by Michael Roth (michael.roth@upc.at), 2004
VERS=0.3
# Output for:
# -l ... Link Status
# -ip ... Incomming Packets
# -op ... Outgoing Packets
# -c ... Collisions
# -ips ... Incomming Packets per Second
# -rbs ... Received Kbytes per Second
# -ops ... Outgoing Packets per Second
# -obs ... Outgoing Kbytes per Second
# -ie ... Incomming Errors
# -oe ... Outgoing Errors
# -is ... Interface Speed
# -a ... All Infos for Interface
# -as ... All Infos for all Interfaces
#=========================================#

if [ "$#" -ne "2" -a ! "$1" = "-as" ]; then
 echo "$0 Version $VERS"
 echo "Usage: $0 -options <interface>"
 echo "\nPlease use the following options:"
 echo " -l ... Link Status"
 echo " -ip ... Incomming Packets"
 echo " -op ... Outgoing Packets"
 echo " -c ... Collisions"
 echo " -ips ... Incomming Packets per Second"
 echo " -rbs ... Received Kbytes per Second"
 echo " -ops ... Outgoing Packets per Second"
 echo " -obs ... Outgoing Kbytes per Second"
 echo " -ie ... Incomming Errors"
 echo " -oe ... Outgoing Errors"
 echo " -is ... Interface Speed"
 echo " -a ... All Infos for Interface"
 echo " -as ... All Infos for all Interfaces"
 exit 1
fi

# Variables
kstat=/usr/bin/kstat
ifconfig=/sbin/ifconfig
grep=/usr/bin/grep
awk=/usr/bin/awk
tr=/usr/bin/tr
ndd=/usr/sbin/ndd
bc=/usr/bin/bc
cut=/usr/bin/cut
wc=/usr/bin/wc
uname=/usr/bin/uname
hostname=`/usr/bin/uname -n`
interface="$2"
interface_inst=`echo "$interface" | $tr -d "[:alpha:]" | $bc`
interface_name=`echo "$interface" | $tr -d "[:digit:]"`

#echo "Interface-Name: $interface_name , Interface-Instance: $interface_inst"

if [ -z "$interface_inst" -a ! "$1" = "-as" ]; then
 echo "Please add the instance to the interface eg. hme0!"
 exit 1
fi

if [ -z "$interface_name" -a ! "$1" = "-as" ]; then
 echo "Please enter a valid interfacename."
 exit 1
fi

if [ ! "$1" = "-as" ]; then
"$kstat" -q "$interface_name" ||
 {
 echo "Interface $interface_name not found on System $hostname!"
 exit 1
 }
fi

#
# Define functions
#
 stat () {
 if [ -z "$interface" ]; then
  echo "Error - Interface not defined!"
  exit 1
 fi
 value=`$kstat $interface_name:$interface_inst:$interface:$option | grep "$option" | $tr -s " " | $cut -d " "  -f 2 | $bc`

 exit_stat="$?"
 }

 stat_calc () {
 if [ -z "$interface" ]; then
  echo "Error - Interface not defined!"
  exit 1
 fi
 result=`$kstat $interface_name:$interface_inst:$interface:$option 1 2 | grep $option | tr -s " " | cut -d " " -f 2 | tr "\n" " " | $awk '{ printf ("%d", ($2-$1)/1024 ) }'`

 exit_stat="$?"
 }

#
# Main
#

case "$1" in
 "-l")
  # Link status
  option=link_up
  stat
  echo "Interface $2 $option value is $value."
  exit "$exit_stat"
 ;;

 "-ip")
  # Incomming packets
  option=ipackets
  stat
  echo "Interface $2 has $value $option."
  exit "$exit_stat"
 ;;

 "-op")
  # Outgoing packets
  option=opackets
  stat
  echo "Interface $2 has $value $option."
  exit "$exit_stat"
 ;;

 "-c")
  # Collisions
  option=collisions
  stat
  echo "Interface $2 $option value is $value."
  exit "$exit_stat"
  ;;

  "-ips")
  # Incomming packets per second
  option=ipackets
  stat_calc
  echo "Interface $2 $option has $result packets/s."
  exit "$exit_stat"
  ;;

  "-rbs")
  # Received Kbytes per second
  option=rbytes
  stat_calc
  echo "Interface $2 $option has $result Kbyte/s."
  exit "$exit_stat"
  ;;

  "-ops")
  # Outgoing packets per second
  option=opackets
  stat_calc
  echo "Interface $2 $option has $result packets/s."
  exit "$exit_stat"
  ;;

  "-obs")
  # Outgoing Kbytes per second
  option=obytes
  stat_calc
  echo "Interface $2 $option has $result Kbyte/s."
  exit "$exit_stat"
  ;;

  "-ie")
  # Incomming errors
  option=ierrors
  stat
  echo "Interface $2 $option value is $value."
  exit "$exit_stat"
  ;;

  "-oe")
  # Outgoing Errors
  option=oerrors
  stat
  echo "Interface $2 $option value is $value."
  exit "$exit_stat"
  ;;

  "-is")
  # Interface Speed
  option=ifspeed
  stat
  echo "Interface $2 $option value is $value."
  exit "$exit_stat"
  ;;

  "-a")
  # All info for the interface
  i=$2
  option=ipackets
  stat_calc
  echo "Interface $i $option has $result packets/s."

  option=rbytes
  stat_calc
  echo "Interface $i $option has $result Kbytes/s."

  option=opackets
  stat_calc
  echo "Interface $i $option has $result packets/s."

  option=obytes
  stat_calc
  echo "Interface $i $option has $result Kbytes/s."

  option=collisions
  stat
  echo "Interface $i $option value is $value."

  option=ierrors
  stat
  echo "Interface $i $option value is $value."

  option=oerrors
  stat
  echo "Interface $i $option value is $value."

  option=ifspeed
  stat
  echo "Interface $i $option value is $value."

  option=link_duplex
  stat
  echo "Interface $i $option value is $value."

  option=link_up
  stat
  echo "Interface $i $option value is $value."

  echo "==="
  ;;

  "-as")
   # All info - output for all interfaces on the system
   echo "\nOutput for all Interfaces:\n"
   Interfaces=`$ifconfig -a | $grep "flags=" | grep -v "LOOPBACK" | $awk ' { printf $1 "\n" } ' | $tr -d ":"`
 
   for i in $Interfaces
   do
    interface_inst=`echo $i | $tr -d "[:alpha:]" | $bc`
    interface_name=`echo $i | $tr -d "[:digit:]"`
    interface=$i

    option=ipackets
    stat_calc
    echo "Interface $i $option has $result packets/s."

    option=rbytes
    stat_calc
    echo "Interface $i $option has $result Kbytes/s."

    option=opackets
    stat_calc
    echo "Interface $i $option has $result packets/s."

    option=obytes
    stat_calc
    echo "Interface $i $option has $result Kbytes/s."

    option=collisions
    stat
    echo "Interface $i $option value is $value."

    option=ierrors
    stat
    echo "Interface $i $option value is $value."

    option=oerrors
    stat
    echo "Interface $i $option value is $value."
   
    option=ifspeed
    stat
    echo "Interface $i $option value is $value."

    option=link_duplex
    stat
    echo "Interface $i $option value is $value."

    option=link_up
    stat
    echo "Interface $i $option value is $value."

    echo "==="
   done
  ;;

 *)
 echo "$0 Version $VERS"
 echo "\nUsage: $0 -options <interface>"
 echo "\nPlease use the following options:"
 echo " -l ... Link Status"
 echo " -ip ... Incomming Packets"
 echo " -op ... Outgoing Packets"
 echo " -c ... Collisions"
 echo " -ips ... Incomming Packets per Second"
 echo " -rbs ... Received Kbytes per Second"
 echo " -ops ... Outgoing Packets per Second"
 echo " -obs ... Outgoing Kbytes per Second"
 echo " -ie ... Incomming Errors"
 echo " -oe ... Outgoing Errors"
 echo " -is ... Interface Speed"
 echo " -a ... All Infos for Interface"
 echo " -as ... All Infos for all Interfaces"
 exit 1
 ;;

esac
exit 0
       


###
### This script is submitted to BigAdmin by a user
### of the BigAdmin community.
### Sun Microsystems, Inc. is not responsible for the
### contents or the code enclosed.
###
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
I promised to write further about my project, and here I am.  First, I needed to setup the Primary Server.  You can read how in this article: Setup FreeBSD Server with full HDD encryption (http://www.experts-exchange.com/OS/Unix/BSD/FreeBSD/A_3660-S…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now