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