network_speed网卡流量检测

#!/usr/bin/env python
#coding:utf-8 import sys
import os import atexit
import time
import psutil #print "Welcome,current system is",os.name," 3 seconds late start to get data..."
time.sleep(3) line_num = 1 #function of Get CPU State;
def getCPUstate(interval=1):
return (" CPU: " + str(psutil.cpu_percent(interval)) + "%")
#function of Get Memory
def getMemorystate():
phymem = psutil.virtual_memory()
line = "Memory: %5s%% %6s/%s"%(
phymem.percent,
str(int(phymem.used/1024/1024))+"M",
str(int(phymem.total/1024/1024))+"M"
)
return line
def bytes2human(n):
"""
>>> bytes2human(10000)
'9.8 K'
>>> bytes2human(100001221)
'95.4 M'
"""
for s in reversed(symbols):
if n >= prefix[s]:
return '%.2f B' % (n) def poll(interval):
"""Retrieve raw stats within an interval window."""
tot_before = psutil.net_io_counters()
pnic_before = psutil.net_io_counters(pernic=True)
# sleep some time
time.sleep(interval)
tot_after = psutil.net_io_counters()
pnic_after = psutil.net_io_counters(pernic=True)
# get cpu state
cpu_state = getCPUstate(interval)
# get memory
memory_state = getMemorystate()
return (tot_before, tot_after, pnic_before, pnic_after,cpu_state,memory_state) def refresh_window(tot_before, tot_after, pnic_before, pnic_after,cpu_state,memory_state):
if os.name == 'nt':
os.system("cls")
else:
os.system("clear")
"""Print stats on screen.""" #print current time #cpu state #memory
print(time.asctime()+" | "+cpu_state+" | "+memory_state) # totals
print(" NetStates:")
)
print("total packets: sent: %-10s received: %s" % (tot_after.packets_sent,
)
# per-network interface details: let's sort network interfaces so
# that the ones which generated more traffic are shown first
print("")
nic_names = pnic_after.keys()
#nic_names.sort(key=lambda x: sum(pnic_after[x]), reverse=True)
for name in nic_names:
stats_before = pnic_before[name]
stats_after = pnic_after[name]
templ = "%-15s %15s %15s"
print(templ % (name, "TOTAL", "PER-SEC"))
print(templ % (
"bytes-sent",
bytes2human(stats_after.bytes_sent),
bytes2human(stats_after.bytes_sent - stats_before.bytes_sent) + '/s',
))
print(templ % (
"bytes-recv",
bytes2human(stats_after.bytes_recv),
bytes2human(stats_after.bytes_recv - stats_before.bytes_recv) + '/s',
))
print(templ % (
"pkts-sent",
stats_after.packets_sent,
stats_after.packets_sent - stats_before.packets_sent,
))
print(templ % (
"pkts-recv",
stats_after.packets_recv,
stats_after.packets_recv - stats_before.packets_recv,
))
print("") try:
interval = 0
while 1:
args = poll(interval)
refresh_window(*args)
interval = 1
except (KeyboardInterrupt, SystemExit):
pass

网卡流量检测.py的更多相关文章

  1. python检测当前网卡流量信息,用于查看实时网速

    可以用来检测是否有挖矿程序在运行的一个子条件 # coding:utf-8 __author__ = 'chenhuachao' import wmi import time import platf ...

  2. shell习题第23题:检测网卡流量

    [题目要求] 写一个脚本,检测网卡流量并记录到日志,需要按照如下格式并一分钟统计一次(只需统计外网网卡,网卡名称eth0) 2019-06-07 1:11 eth0 input: 1000bps et ...

  3. 通过Nethogs查看服务器网卡流量情况

    在日常运维工作中,会碰到服务器带宽飙升致使网站异常情况.作为运维人员,我们要能非常清楚地了解到服务器网卡的流量情况,观察到网卡的流量是由哪些程序在占用着. 今天介绍一款linux下查看服务器网卡流量占 ...

  4. 用 Python 脚本实现对 Linux 服务器的网卡流量监控

    *这篇文章网上已经有相关代码,为了加深印象,我做了相关批注,希望对朋友们有帮助 工作原理:基于/proc文件系统 Linux 系统为管理员提供了非常好的方法,使其可以在系统运行时更改内核,而不需要重新 ...

  5. 用python实现实时监控网卡流量

    很多时候,我们是需要查看服务器的网卡当前跑了多大流量,但对于网卡流量的查询,在linux下似乎没有像top那样的原生命令.虽然top功能很强大,可以实时查看cpu.内存.进程的动态,但是却没有对网卡流 ...

  6. zabbix监控windows主机网卡流量

    监控windows主机网卡流量 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任.     欢迎加入:高级运维工程师之路 598432640 客户端配置:(172.30.1.120,wi ...

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

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

  8. linux查看网卡吞吐量和网卡流量用自带命令,iptraf查看。

    1 命令行直接输入:iptraf(如果没有,使用yum install iptraf安装) 2.打开进入选择ip镜像. 3. 选择监听测试的接口,所有接口. 4.开启监听状态日志,系统默认日志路径./ ...

  9. nagios监控mysql主机,nginx,磁盘IO,网卡流量

    http://blog.chinaunix.net/uid-28685162-id-3506260.html nagios安装完成,打开/usr/local/nagios/etc/nagios.cfg ...

随机推荐

  1. Python3 与 C# 面向对象之~继承与多态 Python3 与 C# 面向对象之~封装 Python3 与 NetCore 基础语法对比(Function专栏) [C#]C#时间日期操作 [C#]C#中字符串的操作 [ASP.NET]NTKO插件使用常见问题 我对C#的认知。

    Python3 与 C# 面向对象之-继承与多态   文章汇总:https://www.cnblogs.com/dotnetcrazy/p/9160514.html 目录: 2.继承 ¶ 2.1.单继 ...

  2. 一步一步教你在 Android 里创建自己的账号系统(一)

    大家假设喜欢我的博客,请关注一下我的微博,请点击这里(http://weibo.com/kifile),谢谢 转载请标明出处(http://blog.csdn.net/kifile),再次感谢 大家在 ...

  3. 车牌识别--OMAP4430处理器上測试

    OMAP4430(ME865) arm-linux-gcc 4.5.1(FriendlyARM) 软浮点执行结果: root@lj:/workspace/carid# arm-linux-gcc ca ...

  4. TensorFlow从入门到实战资料汇总 2017-02-02 06:08 | 数据派

    TensorFlow从入门到实战资料汇总 2017-02-02 06:08 | 数据派 来源:DataCastle数据城堡 TensorFlow是谷歌基于DistBelief进行研发的第二代人工智能学 ...

  5. 关于error:Cannot assign to 'self' outside of a method in the init family

    有时候我们重写父类的init方法时不注意将init后面的第一个字母写成了小写.在这种方法里面又调用父类的初始化方法(self = [super init];)时会报错,错误信息例如以下:error:C ...

  6. mysql could not be resolved: Name or service not known

    问题: mysql DNS反解:skip-name-resolve 错误日志有类似警告: 1.120119 16:26:04 [Warning] IP address '192.168.1.10' c ...

  7. erlang取列表中某个值的位置

    有个需求,比如在一个列表中,取出一个元素的位置,如果出现重复都取出.例如:List = [2,3,10,324,88,29,12],可以求大于某个值的位置,也可以取某个值的位置. 废话少说,直接上代码 ...

  8. 在mac下搭建Apacheserver

    Apache作为最流行的Webserver端软件之中的一个.它的长处与地位不言而喻.以下介绍下在mac下搭建Apacheserver的步骤: (1)"前往" –>" ...

  9. 海康DS NVR播放URL规则

    URL规定:rtsp://username:password@<address>:<port>/Streaming/Channels/<id>(?parm1=val ...

  10. 解决用EasyDarwin开源流媒体服务器做HLS直播时Flash Player卡住的问题

    最近在开发EasyDarwin开源流媒体服务器HLS直播的时候发现一个现象:在PC上用flash player播放HLS和在ios上面播放HLS时,效果明显不同,在ios上播放非常稳定,而在flash ...