网卡流量检测.py
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的更多相关文章
- python检测当前网卡流量信息,用于查看实时网速
可以用来检测是否有挖矿程序在运行的一个子条件 # coding:utf-8 __author__ = 'chenhuachao' import wmi import time import platf ...
- shell习题第23题:检测网卡流量
[题目要求] 写一个脚本,检测网卡流量并记录到日志,需要按照如下格式并一分钟统计一次(只需统计外网网卡,网卡名称eth0) 2019-06-07 1:11 eth0 input: 1000bps et ...
- 通过Nethogs查看服务器网卡流量情况
在日常运维工作中,会碰到服务器带宽飙升致使网站异常情况.作为运维人员,我们要能非常清楚地了解到服务器网卡的流量情况,观察到网卡的流量是由哪些程序在占用着. 今天介绍一款linux下查看服务器网卡流量占 ...
- 用 Python 脚本实现对 Linux 服务器的网卡流量监控
*这篇文章网上已经有相关代码,为了加深印象,我做了相关批注,希望对朋友们有帮助 工作原理:基于/proc文件系统 Linux 系统为管理员提供了非常好的方法,使其可以在系统运行时更改内核,而不需要重新 ...
- 用python实现实时监控网卡流量
很多时候,我们是需要查看服务器的网卡当前跑了多大流量,但对于网卡流量的查询,在linux下似乎没有像top那样的原生命令.虽然top功能很强大,可以实时查看cpu.内存.进程的动态,但是却没有对网卡流 ...
- zabbix监控windows主机网卡流量
监控windows主机网卡流量 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 欢迎加入:高级运维工程师之路 598432640 客户端配置:(172.30.1.120,wi ...
- 监控linux服务器网卡流量
监控linux服务器网卡流量 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 欢迎加入:高级运维工程师之路 598432640 前言:众所周知,我们安装zabbix服务器 ...
- linux查看网卡吞吐量和网卡流量用自带命令,iptraf查看。
1 命令行直接输入:iptraf(如果没有,使用yum install iptraf安装) 2.打开进入选择ip镜像. 3. 选择监听测试的接口,所有接口. 4.开启监听状态日志,系统默认日志路径./ ...
- nagios监控mysql主机,nginx,磁盘IO,网卡流量
http://blog.chinaunix.net/uid-28685162-id-3506260.html nagios安装完成,打开/usr/local/nagios/etc/nagios.cfg ...
随机推荐
- js 数组的迭代
es5新增加的迭代方法(every,filter,forEach,map,some) arr1 = [1,2,3,4,5,6]; 1,every(); every() 方法使用指定函数检测数组中的所有 ...
- Oracle oledb 打包并集成到程序安装包(VC ADO訪问Oracle)
近期有一个项目和oracle交互,我用的是ADO的方式进行试问操作. 首先把数据库连接的东东记录一下,老了记性不好啊! 操作例如以下: //连接串这么写的话就能够不用配置 tnsnames.ora配置 ...
- Git bare repo with multiple branches
http://stackoverflow.com/questions/9324762/git-bare-repo-with-multiple-branches Q: I want to make a ...
- FastDFS的配置、部署与API使用解读(6)FastDFS配置详解之Storage配置(转)
1 基本配置 disabled #func:该配置文件是否生效 #valu: ## true:无效 ## false:生效 disabled=false group_name #func:本stora ...
- Ubuntu16.04下屏幕侧边栏的设置
ubnutu的任务栏都是在左侧: zhang@zhang-virtual-machine:~$ gsettings set com.canonical.Unity.Launcher launcher- ...
- kubernetes之故障现场二,节点名称冲突
系列目录 问题描述:测试环境由于异常断电导致服务器重启一后,有一个节点的状态一直是NotReady.通过journalctl -f -u kubelet没有错误日志输出.通过tail /var/log ...
- caffe学习--caffe入门classification00学习--ipython
首先,数据文件和模型文件都已经下载并处理好,不提. cd "caffe-root-dir " ----------------------------------分割线---- ...
- 成长为 iOS 大 V 的秘密
成长为 iOS 大 V 的秘密 前言 毫不谦虚地说,我是国内 iOS 开发的大 V.我从 2011 年底开始自学 iOS 开发,经过 3 年时间,到 2014 年底,我不但写作了上百篇 iOS 相 ...
- asp.net core 初探 二
今天用@宇内流云大大的jexus 体验一下生产环境的发布,运行. 生产环境: centos 7 jexus 5.8.1 独立版 包含了mono (mono安装真心痛苦……) 开发环境就是昨天的Ubun ...
- ffmpeg编码常见问题排查方法
播放问题排查: 一旦我们遇到视频播放不了,第一件事,就是要找几个别的播放器也播放看看,做一下对比测试,或者对码流做一些基础分析,以便更好的定位问题的源头,而各个平台比较常见的播放/分析工具有如下几个: ...