监控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 ...
随机推荐
- Hibernate-使用事务
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletExcepti ...
- ZooKeeper的架构
ZooKeeper的架构 看看下面的图表.它描述了ZooKeeper的“客户端-服务器架构”. 作为ZooKeeper架构的一部分的每个组件在下表中进行了说明. 部分 描述 Client(客户端) 客 ...
- (转)c++多态实现的机制
原文地址:http://blog.csdn.net/zyq0335/article/details/7657465 1 什么是多态?多态性可以简单的概括为“1个接口,多种方法”,在程序运行的过程中才决 ...
- (转)分析kernel的initcall函数
分析kernel的initcall函数 来源: ChinaUnix博客 日期: 2008.07.19 21:24 (共有条评论) 我要评论 分析kernel的initcall函数Autho ...
- KO 数据库分类系统介绍
KEGG Orthology数据库不仅对基因的功能进行了扩充和整理,还对功能进行了细致的分类: 针对基因的功能,共有3级分类,第一级有6个大类: 打开下面这个链接 http://www.genome. ...
- Mysql利用match...against进行全文检索
在电商项目中,最核心的功能之一就是搜索功能,搜索做的好,整个电商平台就是个优秀的平台.一般搜索功能都使用搜索引擎如Lucene.solr.elasticsearch等,虽然这功能比较强大,但是对于一些 ...
- mui.back()返回刷新功能
使用场景:操作某个步骤需要获取用户信息,如果用户未登陆,则跳转登录页面,登陆成功之后使用mui.back()返回父页面,父页面必须刷新. 子页面:登陆页 父页面:跳转登录页的页面 1. 在子页面初始化 ...
- c++ _int64 转成string
_i64toa(a,buffer,10); scanf("%I64d",&a);printf("%I64d",a); 就可以正确输入输出了.当使用uns ...
- 关于write solid code中的memset
文中说明memset可以通过操作整形以加速程序执行速度,这一点值得肯定,问题在于unicore或arm中协处理器有地址访问对齐检查,如果我们如此操作,编译器最终使用str指令来完成,那么当地址未对齐时 ...
- htaccess文件中RewriteRule 规则参数介绍
.htaccess 文件 <IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-d Rew ...