监控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 ...
 
随机推荐
- 历届蓝桥杯C/C++省赛试题
			
2012年第三届蓝桥杯C/C++程序设计本科B组省赛 2013年第四届蓝桥杯C/C++程序设计本科B组省赛 2014年第五届蓝桥杯C/C++程序设计本科B组省赛 2015年第六届蓝桥杯C/C++程序设 ...
 - 指定Android Studio编译工程时的源文件编码
			
统一设置为UTF8编码在工程的根目录下的build.gradle文件中,添加定义.tasks. withType(JavaCompile) { options.encoding = " ...
 - imx6dl uboot 移植
			
新版的BSP引进的设备树的机制,在uboot中还添加了menuconfig的配置菜单. 参考官网的文档进行uboot移植,本文使用的cpu是imx6dl,uboot版本2015.04. 我要添加一个名 ...
 - ConfigParser.NoSectionError: No section: 'MongoDB'
			
场景:手动执行bat文件正常,schtasks定时执行bat文件时报错. 原因:定时执行时,ini配置文件找不到.Windows 下用 schtasks 定时执行脚本的默认起始路径为:C:\Windo ...
 - e679. 浮雕化图像
			
This example demonstrates a 3x3 kernel that embosses an image. Kernel kernel = new Kernel(3, 3, new ...
 - CentOS定位、查找文件的命令
			
定位.查找文件的命令 命令 功能 命令 功能 which 从path中找出文件的位置 find 找出所有符合要求的文件 whereis 找出特定程序的路径 locate 从索引中找出文件位置 9.1 ...
 - Kafka学习之一深度解析
			
背景介绍 Kafka简介 Kafka是一种分布式的,基于发布/订阅的消息系统.主要设计目标如下: 以时间复杂度为O(1)的方式提供消息持久化能力,即使对TB级以上数据也能保证常数时间的访问性能 高吞吐 ...
 - ASP.NET动态添加用户控件的方法
			
本文实例讲述了ASP.NET动态添加用户控件的方法.分享给大家供大家参考.具体实现方法如下: 为了让用户控件能ASP.NET页面实现动态添加,首先写一个接口IGetUCable,这个接口有一个函数,返 ...
 - apk 反编译工具的使用
			
在学习android 开发的时候,我们经常回尝试使用到别人的apk,希望能了解别人怎么编写的代码,于是想要一个能实现其反编译的软件,将软件反编译出来,查看其代码. 工具/原料 反编译软件dex2jar ...
 - tiny6410nfs挂载问题
			
一.制作根文件系统 1.下载最新版的 busybox 地址:http://www.busybox.net/downloads/ 2.编译busybox.先make menuconfig ,修改以下:B ...