python版监控linux流量

直接上代码,使用OptionParser来传入参数

#coding:utf-8
#-------------
#Author:Hu
#Data:20150520
#------------- from __future__ import division
import re
import time
from optparse import OptionParser def getbandwidth(eth='eth0',intevel=1):
a=open('/proc/net/dev')
data=a.read()
patten=eth + '.*'
if not re.search(patten,data):
print "The ETHname not have"
exit(1)
Rev_old=re.search(patten,data).group().replace(':',' ').split()[1]
Send_old=re.search(patten,data).group().replace(':',' ').split()[9]
a.close() while True:
#print intevel
time.sleep(int(intevel))
a=open('/proc/net/dev')
data=a.read()
Rev=re.search(patten,data).group().replace(':',' ').split()[1]
Send=re.search(patten,data).group().replace(':',' ').split()[9]
diff_Rev=int(Rev)-int(Rev_old)
diff_Sen=int(Send)-int(Send_old)
diff_M=diff_Rev*8/1024/1024/int(intevel)
diff_S=diff_Sen*8/1024/1024/int(intevel)
print time.strftime("%Y%m%d %H:%M:%S") + ' The Recevie is %6.2f Mbps(byte is %d)' % (diff_M,diff_Rev) + ' The Send is %6.2f Mbps(byte is %d)' % (diff_S,diff_Sen)
Rev_old=Rev
Send_old=Send
a.close()
if __name__=='__main__': import sys
usage='''%prog [-i ethname] [-t interveltime]
Example:%prog -i eth0 -t 1'''
parser=OptionParser(usage=usage,version='2.0_20150602') parser.add_option('-i','--interface',dest='interface',default='eth0',help='Wann to interface')
parser.add_option('-t','--time',dest='intevel',type='int',default='',help='The intevel time')
(options,args)=parser.parse_args()
print "The interafce is %s and the intevel time is %d" % (options.interface,options.intevel)
getbandwidth(options.interface,options.intevel)

使用方法:

'''%prog [-i ethname] [-t interveltime]
Example:%prog -i eth0 -t 1'''
默认是eth0 ,时间间隔是1 效果如下:
[root@iZ94nv1rj5tZ tools]# ./bandwidth3.py -version
Usage: bandwidth3.py [-i ethname] [-t interveltime]
Example:bandwidth3.py -i eth0 -t bandwidth3.py: error: no such option: -v
[root@iZ94nv1rj5tZ tools]# ./bandwidth3.py -i eth1 -t
The interafce is eth1 and the intevel time is
:: The Recevie is 0.01 Mbps(byte is ) The Send is 0.00 Mbps(byte is )
:: The Recevie is 0.02 Mbps(byte is ) The Send is 0.00 Mbps(byte is )
:: The Recevie is 0.08 Mbps(byte is ) The Send is 0.00 Mbps(byte is )
:: The Recevie is 0.02 Mbps(byte is ) The Send is 0.00 Mbps(byte is )
:: The Recevie is 0.02 Mbps(byte is ) The Send is 0.00 Mbps(byte is )
:: The Recevie is 0.09 Mbps(byte is ) The Send is 0.00 Mbps(byte is )

2016-01-08 更新:

最新在centos 7中测试时,发现centos的网卡与6的有点不一样,如下:

[root@localhost tools]# cat /proc/net/dev
Inter-| Receive | Transmit
face |bytes packets errs drop fifo frame compressed multicast|bytes packets errs drop fifo colls carrier compressed
eno3:
eno4:
eno5:
enp0s29u1u1u5:
eth2:
lo:
virbr0-nic:
virbr0:

centos 6如下:

[root@localhost tools]# cat /proc/net/dev
Inter-| Receive | Transmit
face |bytes packets errs drop fifo frame compressed multicast|bytes packets errs drop fifo colls carrier compressed
lo:
eth0:
eth1:
usb0:

与老版的linux有区别,centos 7网卡名后有空格,但centos 6以下的网卡名后没空格。

所以,如果该脚步要在7中使用,要将
Send_old=re.search(patten,data).group().replace(':',' ').split()[9]
改成:
Send_old=re.search(patten,data).group().replace(':',' ').split()[10]
即可。

如有问题,请联系362299908@qq.com

监控linux流量python版的更多相关文章

  1. 监控linux流量shell版

    想要实时查看linux流量情况,又不想再去下第三方工具,可以直接写脚步运行! 系统:centos 6.5 原理:从/proc/net/dev中获取到流量情况,再通过换算并除以间隔时间来得到流量单位M ...

  2. linux下python版webshell后门查杀工具

    使用说明: 1.查杀指定路径:python webshell.py 路径 2.按时间查找文件:python webshell.py 路径 “2013-09-28 00:00:00″ # -*- cod ...

  3. 第6章:使用Python监控Linux系统

    1.Python编写的监控工具 1).多功能系统资源统计工具dstat dstat是一个用Python编写的多功能系统资源统计工具,用来取代Linux下的vmstat,iostat,netstat和i ...

  4. 通过snmp监控linux

    一.linux snmpd安装 yum install -y net-snmp net-snmp-utils 二.snmp的配置(vim /etc/snmp/snmpd.conf) com2sec n ...

  5. python glances来监控linux服务器CPU 内存 IO使用

    什么是 Glances? Glances 是一个由 Python 编写,使用 psutil 库来从系统抓取信息的基于 curses 开发的跨平台命令行系统监视工具. 通过 Glances,我们可以监视 ...

  6. Linux服务器上监控网络带宽的18个常用命令nload, iftop,iptraf-ng, nethogs, vnstat. nagios,运用Ntop监控网络流量

    Linux服务器上监控网络带宽的18个常用命令 本文介绍了一些可以用来监控网络使用情况的Linux命令行工具.这些工具可以监控通过网络接口传输的数据,并测量目前哪些数据所传输的速度.入站流量和出站流量 ...

  7. 监控linux服务器网卡流量

    监控linux服务器网卡流量 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任.       欢迎加入:高级运维工程师之路 598432640 前言:众所周知,我们安装zabbix服务器 ...

  8. Zabbix 微信报警Python版(带监控项波动图片)

    #!/usr/bin/python # -*- coding: UTF- -*- #Function: 微信报警python版(带波动图) #Environment: python import ur ...

  9. 使用Python监控Linux系统

    一.Python编写的监控工具 一.多功能系统资源统计工具dstat 1.dstat介绍 dstat是一个用Python语言实现的多功能系统资源统计工具,用来取代Linux下的vmstat.iosta ...

随机推荐

  1. SpringMVC & SpringBoot小记

    SpringMVC 1.SpringMVC常用注解 https://blog.csdn.net/lipinganq/article/details/79155072 :@Component.@Serv ...

  2. C# json字符串格式

    [{\"MenId\":\"1\",\"MenName\":\"内容管理\",\"ParentId\" ...

  3. r语言 函数

    R语言实际上是函数的集合,用户可以使用base,stats等包中的基本函数,也可以自己编写函数完成一定的功能.但是初学者往往认为编写R函数十分困难,或者难以理解.这里对如何编写R函数进行简要的介绍. ...

  4. Python 变量类型和运算符

    -*- coding:utf-8 -*- ''' if语法 if conditon: [tab键] command [tab键] command ... else: [tab键] command [t ...

  5. netsnmp编译动态库

    .编译动态库 将写完的snmp代理程序编译生成动态库 gcc -c -fpic telnetConfig.c -o telnetConfig.o -I/usr/local/net-snmp/inclu ...

  6. -lpthread和-pthread的区别

    编译程序包括 预编译, 编译,汇编,链接,包含头文件了,仅能说明有了线程函数的声明, 但是还没有实现, 加上-lpthread是在链接阶段,链接这个库.<stdio.h>等都是静态库,不需 ...

  7. python不同package下相互调用

    路径如: /path/dir1/a.py /path/dir2/b.py 调用举例如: 在/path/dir2/b.py中调用 a模块 import sys sys.path.append(" ...

  8. windowmasker 标记基因组中的重复序列和低复杂度序列

    下载地址:ftp://ftp.ncbi.nlm.nih.gov/pub/agarwala/windowmasker/ 在这个目录下 其中windowmasker 为linux 平台的可执行文件 win ...

  9. IOS URL scheme

    常用URL scheme查询 http://handleopenurl.com/scheme QQ: mqq://新浪微博: weibo:// (sinaweibo://)腾讯微博: tencentw ...

  10. spark 安装配置

    最佳参考链接 https://opensourceteam.gitbooks.io/bigdata/content/spark/install/spark-160-bin-hadoop26an_zhu ...