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. hibernate的注解装配

    1.多对多,(中间表不用映射) @ManyToMany @JoinTable(name = "中间表名", joinColumns = { @JoinColumn(name = & ...

  2. linux查看磁盘信息

    linux查看磁盘挂载信息:df -hlinux查看文件夹大小:sudo du -h --max-depth=1

  3. 小贝_redis hash类型学习

    Redis  Hash类型 一.查看hash类型的命令 二.操作hash命令具体解释 一.查看hash类型的命令 1.输入 help@hash 127.0.0.1:6379>help @hash ...

  4. Logistic Regression 笔记与理解

    Logistic Regression 笔记与理解 Logistic Regression Hypothesis 记为 H(theta) H(theta)=g(z) 当中g(z),是一个叫做Logis ...

  5. 数据库中表的复杂查询&分页

    一.数据库中表的复杂查询 1)连接查询 1.0连接的基本的语法格式: from TABLE1 join_type TABLE2 [on (join_condition)][where (query_c ...

  6. POJ 2263 Heavy Cargo(ZOJ 1952)

    最短路变形或最大生成树变形. 问 目标两地之间能通过的小重量. 用最短路把初始赋为INF.其它为0.然后找 dis[v]=min(dis[u], d); 生成树就是把最大生成树找出来.直到出发和终点能 ...

  7. ZooKeeper学习笔记(一)

    引导 刚开始学习ZooKeepter,看的书是[O'Reilly Media] ZooKeeper.下面的内容基本上是该书的翻译,很多语句也存在问题,大致读还是没问题的,自己在学习中记录. 第一章 Z ...

  8. [Phoenix] 一、快速入门

    Phoenix是一个开源的HBASE SQL层.Phoeinx可以用标准的JDBC API替代HBASE client API来创建表,插入和查询HBASE中的数据. Phoenix作为应用层和HBA ...

  9. arm处理器的历史及现状

    1 arm处理器的发展历史 arm1 arm2 arm3 arm6 arm7 arm9 arm11 arm cortex 2 arm处理器现状 arm cortex A a即application,即 ...

  10. 清理yum 缓存

    两条命令 yum clean all 以及 rm -rf /var/cache/yum/* 如何有效的清理yum缓存 - CSDN博客 https://blog.csdn.net/nsrainbow/ ...