监控linux流量python版
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版的更多相关文章
- 监控linux流量shell版
想要实时查看linux流量情况,又不想再去下第三方工具,可以直接写脚步运行! 系统:centos 6.5 原理:从/proc/net/dev中获取到流量情况,再通过换算并除以间隔时间来得到流量单位M ...
- linux下python版webshell后门查杀工具
使用说明: 1.查杀指定路径:python webshell.py 路径 2.按时间查找文件:python webshell.py 路径 “2013-09-28 00:00:00″ # -*- cod ...
- 第6章:使用Python监控Linux系统
1.Python编写的监控工具 1).多功能系统资源统计工具dstat dstat是一个用Python编写的多功能系统资源统计工具,用来取代Linux下的vmstat,iostat,netstat和i ...
- 通过snmp监控linux
一.linux snmpd安装 yum install -y net-snmp net-snmp-utils 二.snmp的配置(vim /etc/snmp/snmpd.conf) com2sec n ...
- python glances来监控linux服务器CPU 内存 IO使用
什么是 Glances? Glances 是一个由 Python 编写,使用 psutil 库来从系统抓取信息的基于 curses 开发的跨平台命令行系统监视工具. 通过 Glances,我们可以监视 ...
- Linux服务器上监控网络带宽的18个常用命令nload, iftop,iptraf-ng, nethogs, vnstat. nagios,运用Ntop监控网络流量
Linux服务器上监控网络带宽的18个常用命令 本文介绍了一些可以用来监控网络使用情况的Linux命令行工具.这些工具可以监控通过网络接口传输的数据,并测量目前哪些数据所传输的速度.入站流量和出站流量 ...
- 监控linux服务器网卡流量
监控linux服务器网卡流量 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 欢迎加入:高级运维工程师之路 598432640 前言:众所周知,我们安装zabbix服务器 ...
- Zabbix 微信报警Python版(带监控项波动图片)
#!/usr/bin/python # -*- coding: UTF- -*- #Function: 微信报警python版(带波动图) #Environment: python import ur ...
- 使用Python监控Linux系统
一.Python编写的监控工具 一.多功能系统资源统计工具dstat 1.dstat介绍 dstat是一个用Python语言实现的多功能系统资源统计工具,用来取代Linux下的vmstat.iosta ...
随机推荐
- js学习笔记15----子节点和兄弟节点的操作
1.元素.firstChild : 只读属性,第一个子节点 标准下:会包含文本类型的子节点. 非标准下:只包含元素类型子节点. 元素.firstElementChild : 只读属性,第一个元素子节点 ...
- LoadRunner性能分析指标解释
Transactions(用户事务分析) 用户事务分析是站在用户角度进行的基础性能分析. 1.Transation Sunmmary(事务综述) 对事务进行综合分析是性能分析的第一步,通过分析测试时间 ...
- IntelliJ IDEA 14 利用JRebel实现热部署
特别鸣谢:http://wlb.wlb.blog.163.com/blog/static/467413201522095132658/ ©IntelliJ IDEA开源社①群 185441009 鸣谢 ...
- zebra/quagga
参考:http://blog.chinaunix.net/uid-25513153-id-212328.html 一.zebra安装 .编译安装 vim ./lib/zebra.h + 增加: #if ...
- The configuration file 'appsettings.json' was not found and is not optional
问: .Net Core: Application startup exception: System.IO.FileNotFoundException: The configuration file ...
- 開始学习swift,资料汇总帖
最近開始学习swift,以后mac和ios开发就指望它,曾经学oc半途而废了.主要原因是oc等语法实在能适应,如今有swift了.语法有js,scala,python,c++,oc等语言的影子,又一次 ...
- 【Java面试题】20 运行时异常和一般异常有何区别
Throwable 是所有 Java 程序中错误处理的父类 ,有两种资类: Error 和 Exception . Error :表示由 JVM 所侦测到的无法预期的错误,由于这是属于 JVM 层次的 ...
- mysql 启动报错--发现系统错误2,系统找不到指定的文件。
解决方法: 控制面板--找到mysql程序--修复
- 数据结构 http://www.cnblogs.com/sun-haiyu/p/7704654.html
数据结构与算法--从平衡二叉树(AVL)到红黑树 上节学习了二叉查找树.算法的性能取决于树的形状,而树的形状取决于插入键的顺序.在最好的情况下,n个结点的树是完全平衡的,如下图“最好情况”所示,此时树 ...
- VS2010属性表的建立与灵活运用
问题引入:在VS2010当中,进行opencv.QT等的编程时,总是需要配置很多属性还有依赖项等,为了减少每次都重复配置属性的工作量,现在可以运行属性表这个东西来简化配置.opencv也可以这样建立使 ...