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. Web开发者用什么编辑器?

    写在前面的话:从事web前端开发也有一段时间了,今天主要想分享的是文字(代码)编辑器.对于编辑器每个人都有自己的偏爱,也分不同语言的编码者,这里我就拿我接触过的来说说吧! Web开发者用什么编辑器? ...

  2. Legacy BIOS Boot 是如何启动或引导的

    现在Windows 8 64位操作系统全面采用UEFI引导启动的方式,与过去的Legacy启动有什么区别呢?今天就让我们一起来了解下. Legacy BIOS UEFI Boot 是如何启动或引导的 ...

  3. 日常沟通的 3 种模式zz

    一.日常沟通的 3 种模式 根据NLP (Neuro-Linguistic Programming,神经语言程序学),日常沟通的 3 种模式分别是:上堆.下切和平行,它们是什么意思呢? 1.上堆 意思 ...

  4. Asp.net core 初探

    写这篇博客的主要目的是加深自己的印象. 后续每天都会写一些自己的学习心得. Ubuntu :16.04 桌面版 .net core : dotnet-dev-1.0.0-preview2-003121 ...

  5. 版本控制器 git

    摘要:版本控制器是码农必备的工具,很多常用的,像svn,git,cvs等,工作中用过svn,Tortoisehg,firefly,其实大同小异,现在简单介绍下git,以及它的一些常用命令. 在wind ...

  6. SQL还有多少"理所当然";还有那些"就是这样"

    前言废话——sql是程序员的饭碗,繁琐but万能,但能干并不意味着适合干,每当多表关联寻找外键时,我都在经历一种没有选择的痛苦.sql不完美,但长期代码让人无暇顾及完美,再痛苦的呐喊到最后都归于疲倦已 ...

  7. vs2005 未能完成操作。未指定的错误

    具体解决过程是这样的: 1.先把.vcproj 文件剪切到其他地方 2.打开.sln,报错->点“确定”->再点“确定” 3.把 .vcproj 文件 放回来,在vs2008右边的“解决方 ...

  8. windows server安装oracle

    倒腾windows server的时候一定要先整net framework然后再安装oracle不然连不上或者装一下client 也能连上但是为了不安装client一定要先装framework!

  9. linux 中mmap的用法

    函数:void *mmap(void *start,size_t length,int prot,int flags,int fd,off_t offsize); 参数start(dst):指向欲映射 ...

  10. bfs 邻接表(需要优化 可能会RE *【模板】)

    //---基于邻接表的bfs #include <stdio.h> #include <string.h> #include <iostream> #include ...