Linux Server traffic Monitoring scripts (network traffic)

Source: Internet
Author: User
Tags current time sleep

Example, in the CentOS test

First look at the results after the test

Code

# vi/etc/rc.d/traffic_monitor.sh

----------------------------------------------

#!/bin/bash

Path=/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin;

Export PATH


function Traffic_monitor {

os_name=$ (sed-n ' 1p '/etc/issue)

While ["1"]

Todo

# The name of the network port

Eth=$1

# 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 $} ')

# to determine if the fetch value is empty, the network port does not exist

if [[$RXpre = = ' "]]; Then

echo "Error parameter,please input the right port after run the script!"

Exit 0

Fi

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.111cn.net

The code is as follows Copy Code

"

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

----------------------------------------------

# sh traffic_monitor.sh eth0

Look at a paragraph that's only for the network

The code is as follows Copy Code

#!/bin/bash


If [-N "$"]; Then
Eth_name=$1
Else
Eth_name= "Eth0"
Fi

I=0

send_o= ' Ifconfig $eth _name | grep bytes | awk ' {print $} ' | Awk-f: ' {print $} '
recv_o= ' Ifconfig $eth _name | grep bytes | awk ' {print $} ' | Awk-f: ' {print $} '
send_n= $send _o
recv_n= $recv _o

While [$i-le 100000]; Todo
Send_l= $send _n
Recv_l= $recv _n
Sleep 1
Send_n= ' Ifconfig $eth _name | grep bytes | awk ' {print $} ' | Awk-f: ' {print $} '
Recv_n= ' Ifconfig $eth _name | grep bytes | awk ' {print $} ' | Awk-f: ' {print $} '
i= ' expr $i + 1 '
send_r= ' expr $send _n-$send _l '
recv_r= ' expr $recv _n-$recv _l '
total_r= ' expr $send _r + $recv _r '
Send_ra= ' Expr ($send _n-$send _o)/$i '
Recv_ra= ' Expr ($recv _n-$recv _o)/$i '
Total_ra= ' expr $send _ra + $recv _ra '
Sendn= ' Ifconfig $eth _name | grep bytes | Awk-f (' {print $} ' | awk-f) ' {print $} '
Recvn= ' Ifconfig $eth _name | grep bytes | Awk-f (' {print $} ' | awk-f) ' {print $} '
Clear
echo "Last Second:send rate: $send _r bytes/sec Recv rate: $RECV _r bytes/sec Total rate: $total _r bytes/sec"
echo "Average value:send rate: $send _ra bytes/sec Recv rate: $RECV _ra bytes/sec Total rate: $total _ra bytes/sec"
echo "Total traffic after Startup:send traffic: $sendn RECV traffic: $RECVN"
Done

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.