Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Calculate Network bandwidth Usage.

Posted on 2004-08-13
3
442 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
CPU monthly average? 12 131
Vmware Vswitch  VS  Redhat  Bridge 1 115
dot directory in FreeBSD??? 4 87
retrieve nuemaric value from txt file in shell script 3 73
Installing FreeBSD… FreeBSD is a darling of an operating system. The stability and usability make it a clear choice for servers and desktops (for the cunning). Savvy?  The Ports collection makes available every popular FOSS application and packag…
Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…

839 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