Monitoring and recording ground server process/port usage Bandwidth traffic

Source: Internet
Author: User
Tags tidy

Due to the company's troubleshooting needs, it is necessary to record and save the high bandwidth-consuming processes or ports on the public server, so that you can check the problem later.

This problem tangled 1, 2 days, the use of Python should be able to meet the needs, but the novice python is not to take the shot, so can only rely on the Linux finished gadgets and Shell to achieve.

Linux iftop and nethogs two tools to meet the requirements, but neither of these tools are perfect to record the content, if the use of redirection to record content, then the recorded things are not text format, using Cat view is not a problem, but want to edit or filter, It's not going to work, so be interested to try it yourself.

Later colleagues found a new version of Iftop, Iftop-1.0pre3 (http://freecode.com/projects/iftop), may need to turn over the wall to access, and install iftop-1.0pre3 need to install some dependencies, yum and Apt-get have no way to get to, can only go to search compiled by themselves, relying on the installation is too complicated, you find it by yourself search.


After the iftop-1.0pre3 is installed, the content can be printed to the screen using the-t parameter, and the redirection is able to output the content normally.

#!/bin/bashline () {    cols= ' tput cols '     for k in   ' Seq 1 ${cols} '         do             echo -n  "-"          done}my_set () {    user= ' whoami '     group= ' groups '      timestamp= ' date +%f_%t '     days= "/tmp/net_status/' Date +%F '"     file_path= "/tmp/net_status/netinfo.log.log"     tmp_file_path= "/tmp /net_status/tmp_netinfo.log "    # file_path="/tmp/netinfo.log "     net_status_file= "${days}/' date +%h '. Log"     total_rate= ' Cat ${tmp_file_ path} | grep  "total send and receive rate:"  | awk  ' {print  $7} '}tidy () {   &nbsP;sudo /usr/local/sbin/iftop -l 20 -s 10 -o 10s -n -p -n  -t > ${tmp_file_path}    line_num= ' Cat ${tmp_file_path}|wc -l '     line_head= ' echo $ ((${line_num}-9)) '     line_tail= ' echo  $ ((${line_head}-3)) '     sudo cat ${tmp_file_path} | head -${line_ head} | tail -${line_tail} > ${file_path}    # sed - n  ' 4,43p '  ${tmp_file_path} > ${file_path}}check () {    if [  ! -d ${days} ];then        sudo mkdir $ Days        sudo chown -r ${user}.${group} ${days}     fi}output () {    m=-1    n=0     echo  ""   >> ${net_status_file}    echo ${timestamp} >> $ {net_status_file}    echo  "total send and receive rate: ${ Total_rate} " >> ${net_status_file}    line >> ${net_status _file}    echo  ""  >> ${net_status_file}    echo   "number\tsource addr&port \t   \t destination addr&port\t\ Tsend\t\treceive " >> ${net_status_file}    echo "  >>  ${net_status_file}    for i in  ' seq 1 20 '      do        m=$ (($m +2))          n=$ (($n +2))         net_sou= ' cat ${file_path} |  sed -n  "${m},${N}p " | awk  ' nr==1{print $2} ' '         net_des= ' cat  ${file_path} | sed -n  "${m},${n}p"  | awk  ' nr==2{print $1} '         rate_sou= ' cat ${file_path} | sed -n  ' ${m},${n}p " | awk  ' nr==1{print $6} '         rate_ Des= ' cat ${file_path} | sed -n  "${m},${n}p"  | awk  ' nr==2{print $5 } '         echo  ' no  $i \t${net_sou} \t - \t  ${net_des}\t\t$rate_sou\t\t$rate_des  " >> ${net_status_file}     done    line >> ${net_status_file}    echo  " >> ${net_status_file}    echo " " >> ${net_status_ File}    echo  ""  >> ${net_status_file}    echo  ""  >> $ {Net_status_file}} Main () {    my_set    tidy    check     output}main

Leaders anxious to script, so no content optimization, and do not use SQL for content storage, later time can be optimized or improved, making it easier to query retrieval, here is a record.



650) this.width=650; "src=" http://s2.51cto.com/wyfs02/M00/78/AD/wKiom1aBCs-BfSxcAAUJBvhBgmI917.jpg "title=" 1.jpg " alt= "Wkiom1abcs-bfsxcaaujbvhbgmi917.jpg"/>


This article is from "rookie East" blog, please be sure to keep this source http://radish.blog.51cto.com/5944322/1729277

Monitoring and recording ground server process/port usage Bandwidth traffic

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.