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. mysql pdo事务

    /* 开始一个事务,关闭自动提交 */直到调用commit结束事务时才提交 $dbh->beginTransaction(); bool PDO::commit ( void ) 提交一个事务, ...

  2. C++ Primer学习笔记(二)

    题外话:一工作起来就没有大段的时间学习了,如何充分利用碎片时间是个好问题. 接  C++ Primer学习笔记(一)   27.与 vector 类型相比,数组的显著缺陷在于:数组的长度是固定的,无法 ...

  3. map用法小例子

    一. Map< Key , Value > m_Eg; 一般赋值表示成: TypeElem  value; m_Eg[key]  =  value; 或 m_Eg.insert(make_ ...

  4. e621. Activating a Keystroke When Any Child Component Has Focus

    Normally, a keystroke registered on a component is activated when the component has the focus. This ...

  5. 记录一下我的GDB配置

    一:为了更好的在GDB中显示STL容器.我们首先要下载一个python脚本 PS:要确定你所安装的GDB能够运行python脚本 cd ~ mkdir .gdb cd .gdb svn co svn: ...

  6. 怎样设置easyui中datagrid行高

    $('#face_table2').datagrid({            title: '信息',            iconCls: 'icon-save',   url: 'callro ...

  7. VC++ :传统剪贴板的延迟提交技术

    传统剪贴板存在的局限 传统剪贴板有一个局限性:剪贴板上的所有数据都要保存在内存上. 对于文本字符串和其它简单数据类型,可以快速有效地传递.但是,对于比较大的数据,清空剪贴板之前,数据都要占用较大的内存 ...

  8. easyui------添加中文文件

    添加中文文件: 官网地址:http://www.jeasyui.net/download/去jquery-easyui官网下载的文件里面找到easyui-lang-zh_CN.js文件,添加入代码里面 ...

  9. Linux select 机制深入分析

    Linux select 机制深入分析            作为IO复用的实现方式.select是提高了抽象和batch处理的级别,不是传统方式那样堵塞在真正IO读写的系统调用上.而是堵塞在sele ...

  10. 浅谈Nutch插件机制(含开发实例)

    plugin(插件)为nutch提供了一些功能强大的部件,举个例子,HtmlParser就是使用比较普遍的用来分析nutch抓取的html文件的插件. 为什么nutch要使用这样的plugin系统? ...