Single-machine traffic statistic function implemented by shell script _linux Shell

Source: Internet
Author: User

On the internet to see this single flow of the script, very good.

Copy Code code as follows:

#!/bin/sh
Usage () {
echo "Usage: $ [i INTERFACE] [-S INTERVAL] [-C COUNT]"
Echo
echo "-I INTERFACE"
echo "The interface to monitor, and the default is Eth0."
echo "-S INTERVAL"
echo "The time to wait in seconds between measurements, and the default is 3 seconds."
echo "-C COUNT"
echo "The number of times to measure, and the default is."
Exit 3
}
Readargs () {
While ["$#"-GT 0]; Todo
Case "$1″in
-I.)
If ["$"]; Then
Interface= "$2″
shift; Shift
Else
echo "Missing a value for $."
Echo
Shift
Usage
Fi
;;
-s)
If ["$"]; Then
Sleep= "$2″
shift; Shift
Else
echo "Missing a value for $."
Echo
Shift
Usage
Fi
;;
-c)
If ["$"]; Then
Counter= "$2″
shift; Shift
Else
echo "Missing a value for $."
Echo
Shift
Usage
Fi
;;
*)
echo "Unknown option $."
Echo
Shift
Usage
;;
Esac
Done
}
Checkargs () {
if [! "$interface"]; Then
Interface= "Eth0″
Fi
if [! "$sleep"]; Then
Sleep= "3″
Fi
if [! "$counter"]; Then
Counter= "10″
Fi
}
Printrxbytes () {
/sbin/ifconfig "$interface" | grep "RX bytes" | Cut-d:-f2 | awk ' {print '} '
}
Printtxbytes () {
/sbin/ifconfig "$interface" | grep "TX bytes" | Cut-d:-f3 | awk ' {print '} '
}
Bytestohumanreadable () {
Multiplier= "0″
Number= "$1″
While ["$number"-ge 1024]; Todo
multiplier=$ (($multiplier + 1))
number=$ (($number/1024))
Done
Case "$multiplier" in
1)
echo "$number Kb"
;;
2)
echo "$number Mb"
;;
3)
echo "$number Gb"
;;
4)
echo "$number Tb"
;;
*)
echo "$ b"
;;
Esac
}

Printresults () {
While ["$counter"-ge 0]; Todo
Counter=$ (($counter – 1))
If ["$rxbytes"]; Then
Oldrxbytes= "$rxbytes"
Oldtxbytes= "$txbytes"
Fi
rxbytes=$ (Printrxbytes)
txbytes=$ (Printtxbytes)
If ["$oldrxbytes"-A "$rxbytes"-A "$oldtxbytes"-A "$txbytes"]; Then
echo "$ (/bin/date +%y%m%d-%h%m%s) rxbytes = $ (bytestohumanreadable $ (($rxbytes-$oldrxbytes)) Txbytes = $ (bytestohumanr Eadable $ (($txbytes – $oldtxbytes))) "
Else
echo "Monitoring $interface every $sleep seconds. (Rxbyte total = $ (bytestohumanreadable $rxbytes) txbytes total = $ (bytestohumanreadable $txbytes)) "
Fi
Sleep "$sleep"
Done
}
Readargs "$@"
Checkargs
Printresults

The test is as follows:

Every three seconds of traffic, the total output of 999 lines, you can output to the file, which: the Front for the time, Rx Packets is to receive packets, that is, download, Tx Packets is to send packets, that is, upload.

Copy Code code as follows:

[Root@host] #sh t.sh-c 999 monitoring eth0 every 3 seconds. (Rxbyte total = 6 TB txbytes total = 5 TB)
20101105-201539 rxbytes = 126 kb txbytes = 658 kb
20101105-201542 rxbytes = kb txbytes = 487 kb
20101105-201545 rxbytes = 159 kb txbytes = 668 kb
20101105-201548 rxbytes = kb Txbytes = 725 kb
20101105-201551 rxbytes = $ kb txbytes = 704 kb
20101105-201554 rxbytes = MB Txbytes = 726 kb
20101105-201558 rxbytes = MB Txbytes = 850 kb
20101105-201601 rxbytes = 102 KB txbytes = 703 kb
20101105-201604 rxbytes = kb Txbytes = 693 kb
20101105-201607 rxbytes = txbytes = 730 kb
20101105-201610 rxbytes = txbytes KB = 711 kb
20101105-201613 rxbytes = 431 KB txbytes = 703 kb
20101105-201616 rxbytes = kb Txbytes = 527 kb
20101105-201619 rxbytes = 239 kb txbytes = 825 kb
20101105-201622 rxbytes = 117 kb txbytes = 801 kb
20101105-201625 rxbytes = kb Txbytes = 913 kb
20101105-201628 rxbytes = kb Txbytes = 322 Kb
20101105-201631 rxbytes = MB Txbytes = kb
20101105-201634 rxbytes = kb txbytes = 191 kb
20101105-201637 rxbytes = 174 kb txbytes = 481 kb
20101105-201640 rxbytes = MB Txbytes = 383 kb
20101105-201643 rxbytes = kb txbytes = 496 kb
20101105-201646 rxbytes = 108 kb txbytes = KB
20101105-201649 rxbytes = MB Txbytes = 639 Kb
20101105-201652 rxbytes = txbytes = 629 kb
20101105-201655 rxbytes = MB Txbytes = 496 kb
20101105-201658 rxbytes = MB Txbytes = 537 kb
20101105-201701 rxbytes = 114 kb Txbytes = 641 kb

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.