Linux view an IP traffic--a case of thinking

Source: Internet
Author: User
Tags egrep

In my previous article shared a "real-time" view of some Linux network card traffic of small script, today to share a view of the local to a specific IP traffic between the idea. I have this requirement because: our storage is a Dell storage device that is used by NFS for multiple business machine mounts, and I need to know the traffic between each machine and the Dell storage.

In fact, if only to see this traffic, iftop This software is very good, through its own options can be satisfied, do not need to do any additional work.

My situation is: Because of the numerous NFS clients, and although mount a storage device but mount point IP is not the same, and I want to put this as a monitoring item in the Zabbix, so I need to use the shell to iftop further "encapsulation", in order to be "generic" And better combined with Zabbix.

Note: Iftop requires root privileges, and zabbix-agent is generally run as Zabbix user and is not a terminal (TTY) when invoking the custom script on the agent side via Zabbix-server, so it is necessary to modify the sudo configuration file. Allows the Zabbix user to perform sudo operations during a remote execution of a command.


The script content is as follows

#!/bin/sh# monitor traffic between machines and nfs server on Zabbix #by ljk 20160803net= ' 192.168.1.* '       #内网网段nfs_subnet = ' 192.168.1.2[0-9]{2} '      #nfs所在ip的正则, my mount point is 1.211-1.218nfs_ip=$ (DF  -h|egrep  "$nfs _subnet:" |awk -f :  ' {print $1} ')     # Gets the ipcd /etc/sysconfig/network-scriptsfor dev in $ of the native mount point (ls ifcfg-*|awk -f -   ' {print $2} ');d o    ip addr show  $dev |grep  ' 192.168.1.* '  >/dev/null     #判断哪个网卡有内网网段ip, then use Iftop to monitor the NIC     if [  $? -eq 0 ];then        using_dev= $dev       #拥有内网ip的网卡名赋予变量using_dev         break     fidoneif [ $1 = request ];then    result=$ (iftop -i   $using _dev -n -t -s 2 2>/dev/null | egrep -b 1  $nfs _ip|awk  ' $ (NF-4) = = "="  { print $ (NF-1)} ') elif [ $1 = response ];then    result=$ ( iftop -i  $using _dev -n -t -s 2 2>/dev/null | egrep -b  1  $nfs _ip|awk  ' $ (NF-4) = = "<="  {print $ (NF-1)} ') fi# the following sections are some additional work for Zabbix Iftop out of the results for (6.60mb 208kb 320b, etc.), the results of integration into the Zabbix to remove these units and the value of the conversion (Zabbix I unified with B units, only to Zabbix value) num=$ (echo   $result |tr -d  "a-za-z") unit= ' echo ${result: -2:1} ' case  $unit  in  &NBSP;&NBSP;&NBSP;[KK])  echo  "$num *1024" |BC;; &NBSP;&NBSP;&NBSP;&NBSP;[MM])  echo  "$num *1024*1024" |BC;; &NBSP;&NBSP;&NBSP;&NBSP;[GG])  echo  "$num *1024*1024*1024" |BC;; Esac


This article is from "Endeavor K" blog, please be sure to keep this source http://kaifly.blog.51cto.com/3209616/1840121

Linux view an IP traffic--a case of thinking

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.