Shell script to monitor interface usage - Unix

This is a discussion on Shell script to monitor interface usage - Unix ; Hello I have written a short script to see how much my interface is pushing through. I was just wandering if I am doing the calculation correctly. Could someone comment? #!/usr/bin/bash # interface=hme0 # # clear echo "Bandwidth calculator" echo ...

+ Reply to Thread
Results 1 to 5 of 5

Thread: Shell script to monitor interface usage

  1. Shell script to monitor interface usage

    Hello
    I have written a short script to see how much my interface is pushing
    through. I was just wandering if I am doing the calculation correctly.
    Could someone comment?

    #!/usr/bin/bash
    #
    interface=hme0
    #
    #
    clear
    echo "Bandwidth calculator"
    echo "===================="
    while true
    do
    inout=`netstat -I $interface | sed -n '2p' | awk {'print $5 + $7'}`
    sleep 5
    inout2=`netstat -I $interface | sed -n '2p' | awk {'print $5 + $7'}`
    packets=`expr "$inout2" - "$inout"`
    echo "$interface" $(( $packets * 1500 * 8 / 5 /1024 /1024 )) "Mb/s"
    done


    Thanks

  2. Re: Shell script to monitor interface usage

    cconnell_1@lycos.com wrote:
    > Hello
    > I have written a short script to see how much my interface is pushing
    > through. I was just wandering if I am doing the calculation correctly.
    > Could someone comment?
    >
    > #!/usr/bin/bash
    > #
    > interface=hme0
    > #
    > #
    > clear
    > echo "Bandwidth calculator"
    > echo "===================="
    > while true
    > do
    > inout=`netstat -I $interface | sed -n '2p' | awk {'print $5 + $7'}`
    > sleep 5
    > inout2=`netstat -I $interface | sed -n '2p' | awk {'print $5 + $7'}`
    > packets=`expr "$inout2" - "$inout"`
    > echo "$interface" $(( $packets * 1500 * 8 / 5 /1024 /1024 )) "Mb/s"
    > done
    >


    A comment on shell coding:
    smarter is

    inout=`netstat -I $interface | awk 'NR==2 {print $5 + $7}'`
    and
    packets=$(( $inout2 - $inout ))

    And probably even smarter is this one-and-a-half-liner:

    netstat -I $interface 5 |
    awk 'NR>=4 {print ($1+$3)*1500*8/5/1024/1024,"MB/s"}'

    Don't know if really each counted packet has the MTU size.
    But IMHO your method is okay to get a rough figure.

    --
    echo imhcea\.lophc.tcs.hmo |
    sed 's2\(....\)\(.\{5\}\)2\2\122;s1\(.\)\(.\)1\2\11g;1 s;\.;::;2'

  3. Re: Shell script to monitor interface usage

    On Sep 26, 6:49*pm, cconnel...@lycos.com wrote:
    > Hello
    > I have written a short script to see how much my interface is pushing
    > through. I was just wandering if I am doing the calculation correctly.
    > Could someone comment?
    >
    > #!/usr/bin/bash
    > #
    > interface=hme0
    > #
    > #
    > clear
    > echo "Bandwidth calculator"
    > echo "===================="
    > while true
    > do
    > inout=`netstat -I $interface | *sed -n '2p' | awk {'print $5 + $7'}`
    > sleep 5
    > inout2=`netstat -I $interface | *sed -n '2p' | awk {'print $5 + $7'}`
    > packets=`expr "$inout2" - "$inout"`
    > echo "$interface" $(( $packets * 1500 * 8 / 5 /1024 /1024 )) "Mb/s"
    > done
    >
    > Thanks


    What OS?

  4. Re: Shell script to monitor interface usage

    On Sep 29, 1:51*pm, edcrosbys wrote:
    > On Sep 26, 6:49*pm, cconnel...@lycos.com wrote:
    >
    >
    >
    >
    >
    > > Hello
    > > I have written a short script to see how much my interface is pushing
    > > through. I was just wandering if I am doing the calculation correctly.
    > > Could someone comment?

    >
    > > #!/usr/bin/bash
    > > #
    > > interface=hme0
    > > #
    > > #
    > > clear
    > > echo "Bandwidth calculator"
    > > echo "===================="
    > > while true
    > > do
    > > inout=`netstat -I $interface | *sed -n '2p' | awk {'print $5 + $7'}`
    > > sleep 5
    > > inout2=`netstat -I $interface | *sed -n '2p' | awk {'print $5 + $7'}`
    > > packets=`expr "$inout2" - "$inout"`
    > > echo "$interface" $(( $packets * 1500 * 8 / 5 /1024 /1024 )) "Mb/s"
    > > done

    >
    > > Thanks

    >
    > What OS?- Hide quoted text -
    >
    > - Show quoted text -



    This is solaris 10 on x86. Thanks for the suggestions. I modfied the
    script to use snmp values from inifoctets and ifoutoctets. This gives
    similar results. Is this an ok way to meaure interface bandwidth? I
    cant afford to set up mrtg etc and need to get it via a simple script.

    #!/usr/bin/bash
    #
    #
    #
    # Important - Set your variables here:
    #
    interface=bnx25001
    community=public
    interval=5
    #
    #
    clear
    echo "Bandwidth calculator"
    echo "===================="
    echo
    echo -n "Getting Interface information for $interface ..."
    instance=`snmpwalk -v2c -c $community localhost | grep "ifDescr" |
    grep $interface | awk -F\. '{print $2}' | awk '{print $1}'`
    if [ -z $instance ]; then
    echo
    echo "Error finding interface from SNMP or wrong community
    exit now."
    echo
    exit 1
    else
    echo "OK"
    echo
    fi
    while true
    do
    bytes_before=0;bytes_after=0;
    echo -n "Measuring bandwidth for $interface over $interval
    second interval .."
    bytes_before=`snmpget -v2c -c $community localhost RFC1213-
    MIB::ifInOctets.$instance RFC1213-MIB::ifOutOctets.$instance | awk
    '{sum+=$4} END{print sum}'`
    sleep $interval
    bytes_after=`snmpget -v2c -c $community localhost RFC1213-
    MIB::ifInOctets.$instance RFC1213-MIB::ifOutOctets.$instance | awk
    '{sum+=$4} END{print sum}'`
    echo $[($bytes_after - $bytes_before)*8/1048576/$interval] "Mb/
    s"
    sleep 1
    done



  5. Re: Shell script to monitor interface usage

    On Sep 29, 2:32*pm, cconnel...@lycos.com wrote:
    > On Sep 29, 1:51*pm, edcrosbys wrote:
    >
    >
    >
    > > On Sep 26, 6:49*pm, cconnel...@lycos.com wrote:

    >
    > > > Hello
    > > > I have written a short script to see how much my interface is pushing
    > > > through. I was just wandering if I am doing the calculation correctly..
    > > > Could someone comment?

    >
    > > > #!/usr/bin/bash
    > > > #
    > > > interface=hme0
    > > > #
    > > > #
    > > > clear
    > > > echo "Bandwidth calculator"
    > > > echo "===================="
    > > > while true
    > > > do
    > > > inout=`netstat -I $interface | *sed -n '2p' | awk {'print $5 + $7'}`
    > > > sleep 5
    > > > inout2=`netstat -I $interface | *sed -n '2p' | awk {'print $5 + $7'}`
    > > > packets=`expr "$inout2" - "$inout"`
    > > > echo "$interface" $(( $packets * 1500 * 8 / 5 /1024 /1024 )) "Mb/s"
    > > > done

    >
    > > > Thanks

    >
    > > What OS?- Hide quoted text -

    >
    > > - Show quoted text -

    >
    > This is solaris 10 on x86. Thanks for the suggestions. I modfied the
    > script to use snmp values from inifoctets and ifoutoctets. This gives
    > similar results. Is this an ok way to meaure interface bandwidth? I
    > cant afford to set up mrtg etc and need to get it via a simple script.
    >
    > #!/usr/bin/bash
    > #
    > #
    > #
    > # Important - Set your variables here:
    > #
    > interface=bnx25001
    > community=public
    > interval=5
    > #
    > #
    > clear
    > echo "Bandwidth calculator"
    > echo "===================="
    > echo
    > echo -n "Getting Interface information for $interface ..."
    > instance=`snmpwalk -v2c -c $community localhost | grep "ifDescr" |
    > grep $interface | awk -F\. '{print $2}' | awk '{print $1}'`
    > if [ -z $instance ]; then
    > * * * * echo
    > * * * * echo "Error finding interface from SNMP or wrong community
    > exit now."
    > * * * * echo
    > * * * * exit 1
    > else
    > * * * * echo "OK"
    > * * * * echo
    > fi
    > while true
    > do
    > * * * * bytes_before=0;bytes_after=0;
    > * * * * echo -n "Measuring bandwidth for $interface over $interval
    > second interval .."
    > * * * * bytes_before=`snmpget -v2c -c $community localhost RFC1213-
    > MIB::ifInOctets.$instance RFC1213-MIB::ifOutOctets.$instance | awk
    > '{sum+=$4} END{print sum}'`
    > * * * * sleep $interval
    > * * * * bytes_after=`snmpget -v2c -c $community localhost RFC1213-
    > MIB::ifInOctets.$instance RFC1213-MIB::ifOutOctets.$instance | awk
    > '{sum+=$4} END{print sum}'`
    > * * * * echo $[($bytes_after - $bytes_before)*8/1048576/$interval] "Mb/
    > s"
    > * * * * sleep 1
    > done


    Not sure about the script. But here's what I've used for Sun boxes
    throughput...
    http://www.brendangregg.com/K9Toolkit/nicstat


+ Reply to Thread