Results 1 to 10 of 10

Thread: Script to monitor network interface card utilization?

  1. #1
    Join Date
    Sep 2012
    Beans
    94
    Distro
    Ubuntu 12.04 Precise Pangolin

    Script to monitor network interface card utilization?

    Hi All,

    I want to ask, how can i write a script that can check current nic load, for e.g incoming traffic.
    How did we measure (if it use any parameter) the nic load, "heavy" load or "low" load.

    Thanks!
    Last edited by termvrl; June 5th, 2013 at 05:09 PM.

  2. #2
    Join Date
    Jul 2007
    Location
    Mississauga, ON
    Beans
    123
    Distro
    Lubuntu 12.10 Quantal Quetzal

    Re: Script to monitor network interface card utilization?

    I would run a crontab job that extracts the network usage (e.g. ifconfig eth0|grep "RX bytes"), say every minute and append it to a history file, then do the math to calculate the average usage between the last and the current sample.
    Windows by necessity, Linux by choice
    Lubuntu and loving it!

  3. #3
    Join Date
    Jan 2009
    Location
    ::1
    Beans
    2,485

    Re: Script to monitor network interface card utilization?

    If nethogs and ntop don't do what you want, and you're able to do some scripting, I would go for inspecting /proc/net/dev

    A hint:

    Code:
    sander@hapee:~$ cat /proc/net/dev | grep wlan0 | cut -c9- | awk '{ print $1 " " $9 }'  ; sleep 10; cat /proc/net/dev | grep wlan0 | cut -c9- | awk '{ print $1 " " $9 }'
    2468014158 67043735
    2524567250 68250702
    sander@hapee:~$
    HTH

  4. #4
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    2,172
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: Script to monitor network interface card utilization?

    Quote Originally Posted by sanderj View Post
    If nethogs and ntop don't do what you want, and you're able to do some scripting, I would go for inspecting /proc/net/dev

    A hint:

    Code:
    sander@hapee:~$ cat /proc/net/dev | grep wlan0 | cut -c9- | awk '{ print $1 " " $9 }'  ; sleep 10; cat /proc/net/dev | grep wlan0 | cut -c9- | awk '{ print $1 " " $9 }'
    2468014158 67043735
    2524567250 68250702
    sander@hapee:~$
    HTH
    My own little "netrate" script:

    Code:
    #!/bin/bash
    
    dev=$1
    [[ -z $1 ]] && dev=$(grep -o "eth." /proc/net/dev | head -1)
    
    function getcount
    {
        echo $(grep $dev /proc/net/dev | tr ':' ' ' | tr -s ' ' | cut -d ' ' -f 3,11)
    }
        
    current=($(getcount))
    [[ -z $current ]] && echo "No network device \"$dev\"" && exit 1
    
    printf "%10s %4s %4s \n" Device Recv Send
    
    for i in $(seq 1000)
    do
        sleep 1
        new=($(getcount))
        recvdiff=$(( ${new[0]} - ${current[0]} ))
        senddiff=$(( ${new[1]} - ${current[1]} ))
        recvdiff=$(( $recvdiff / 1024 ))
        senddiff=$(( $senddiff / 1024 ))
        printf "%10s %4d %4d\r" $dev $recvdiff $senddiff
        current=(${new[*]})
    done

  5. #5
    Join Date
    Jan 2009
    Location
    ::1
    Beans
    2,485

    Re: Script to monitor network interface card utilization?

    Quote Originally Posted by ofnuts View Post
    My own little "netrate" script:

    Code:
    #!/bin/bash
    
    dev=$1
    [[ -z $1 ]] && dev=$(grep -o "eth." /proc/net/dev | head -1)
    
    function getcount
    {
        echo $(grep $dev /proc/net/dev | tr ':' ' ' | tr -s ' ' | cut -d ' ' -f 3,11)
    }
        
    current=($(getcount))
    [[ -z $current ]] && echo "No network device \"$dev\"" && exit 1
    
    printf "%10s %4s %4s \n" Device Recv Send
    
    for i in $(seq 1000)
    do
        sleep 1
        new=($(getcount))
        recvdiff=$(( ${new[0]} - ${current[0]} ))
        senddiff=$(( ${new[1]} - ${current[1]} ))
        recvdiff=$(( $recvdiff / 1024 ))
        senddiff=$(( $senddiff / 1024 ))
        printf "%10s %4d %4d\r" $dev $recvdiff $senddiff
        current=(${new[*]})
    done
    Ooohhhhh ... beautiful! And I like the "\r" very much ... clever!

  6. #6
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    2,172
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: Script to monitor network interface card utilization?

    Quote Originally Posted by sanderj View Post
    Ooohhhhh ... beautiful! And I like the "\r" very much ... clever!
    A very old trick...

  7. #7
    Join Date
    Sep 2012
    Beans
    94
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Script to monitor network interface card utilization?

    Hi All,
    Thanks for your reply. What i want to do is, i want to check if the incoming traffic is heavy or not. If the incoming traffic is too heavy, it will return 1, and if not, it will return 0.

    I try to use ofnuts script, but the result is show like this:-
    root@ubuntu:/home/Sh# ./Nic.sh
    Device Recv Send
    eth0 0 0

    Thanks.

  8. #8
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    2,172
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: Script to monitor network interface card utilization?

    All the script does is get the appropriate line from /proc/net/dev, extract the sent and received by counts at one-second intervals, and compute the difference (and display it divided by 1024). If you just want the received count:
    Code:
    received=$(grep eth1 /proc/net/dev | awk '{print $2}')
    Do it once, sleep one sec (or shorter), get the new value, compute the difference and act if that is above some trigger value.

  9. #9
    Join Date
    Jan 2009
    Location
    ::1
    Beans
    2,485

    Re: Script to monitor network interface card utilization?

    Quote Originally Posted by termvrl View Post
    Hi All,
    Thanks for your reply. What i want to do is, i want to check if the incoming traffic is heavy or not. If the incoming traffic is too heavy, it will return 1, and if not, it will return 0.

    I try to use ofnuts script, but the result is show like this:-
    root@ubuntu:/home/Sh# ./Nic.sh
    Device Recv Send
    eth0 0 0

    Thanks.
    Questions:
    1) are you connected via fixed ethernet, or wireless? If wireless, use "./Nic.sh wlan0"
    2) are you doing any traffic? Cause traffic with "wget http://ftp.belnet.be/ubuntu.com/ubun...sktop-i386.iso -O /dev/null "

    HTH

  10. #10
    Join Date
    Sep 2012
    Beans
    94
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Script to monitor network interface card utilization?

    Hi thanks for your tips.

    After generate some traffic, i able to see the "Recv".

    Thanks again!

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •