Linux traffic monitoring shell script _linux Shell

Source: Internet
Author: User
Recently more busy, for a long time did not update the blog, today is not busy writing a traffic monitoring script. The test has passed under the CentOS, has the need friend to try, has the bug or needs to add other function words can leave a message oh.

First, foot source code
# vi/etc/rc.d/traffic_monitor.sh
----------------------------------------------
Copy Code code as follows:
#!/bin/bash
Path=/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin;
Export PATH
function Traffic_monitor {
# system version
os_name=$ (sed-n ' 1p '/etc/issue)
# The name of the network port
Eth=$1
#判断网卡存在与否, exit if not present
if [!-d/sys/class/net/$eth];then
Echo-e "Network-interface not Found"
ECHO-E "You system have network-interface:\n ' ls/sys/class/net '"
Exit 5
Fi
While ["1"]
Todo
# status
status= "Fine"
# Get the current time the network port to receive and send traffic
rxpre=$ (Cat/proc/net/dev | grep $eth | tr: "" | awk ' {print $} ')
txpre=$ (Cat/proc/net/dev | grep $eth | tr: "" | awk ' {print $} ')
# get 1 seconds after the port to receive and send traffic
Sleep 1
rxnext=$ (Cat/proc/net/dev | grep $eth | tr: "" | awk ' {print $} ')
txnext=$ (Cat/proc/net/dev | grep $eth | tr: "" | awk ' {print $} ')
Clear
# get the actual inbound and outbound traffic for the 1 seconds
rx=$ ((${rxnext}-${rxpre}))
tx=$ ((${txnext}-${txpre}))
# to judge the incoming traffic if the MB is greater than the MB order, the KB order of magnitude is displayed.
if [[$RX-lt 1024]];then
rx= "${RX}B/S"
elif [[$RX-gt 1048576]];then
rx=$ (echo $RX | awk ' {print $1/1048576 ' MB/S "} ')
$STATUS = "Busy"
Else
rx=$ (echo $RX | awk ' {print $1/1024 ' kb/s '} ')
Fi
# to determine if the send traffic is greater than MB, display MB units, otherwise the KB order of magnitude
if [[$TX-lt 1024]];then
tx= "${TX}B/S"
elif [[$TX-gt 1048576]];then
tx=$ (echo $TX | awk ' {print $1/1048576 ' MB/S "} ')
Else
tx=$ (echo $TX | awk ' {print $1/1024 ' kb/s '} ')
Fi
# Print Information
Echo-e "==================================="
Echo-e "Welcome to Traffic_monitor Stage"
Echo-e "Version 1.0"
Echo-e "Since 2014.2.26"
Echo-e "Created by Showerlee"
Echo-e "Blog:http://www.showerlee.com"
Echo-e "==================================="
Echo-e "System: $OS _name"
Echo-e "Date: ' Date +%f '"
Echo-e "Time: ' Date +%k:%m:%s '"
Echo-e "Port: $"
Echo-e "Status: $STATUS"
Echo-e "\ t RX \ttx"
echo "------------------------------"
# Print real-time traffic
Echo-e "$eth \ t $RX $TX"
echo "------------------------------"
# Exit Info
Echo-e "Press ' CTRL + C ' to exit"
Done
}
# Judging execution parameters
if [[n ' $]];then
# Executive function
Traffic_monitor $
Else
Echo-e "None parameter,please Add system netport after Run" script! \nexample: ' sh traffic_monitor eth0 ' "
Fi

----------------------------------------------
Ii. Effect of implementation
Copy Code code as follows:
# sh traffic_monitor.sh eth0


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.