Solved

Calculate Network bandwidth Usage.

Posted on 2004-08-13
3
447 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
[X]
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
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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
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…
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.
Suggested Courses

617 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