Python psutil网络监视脚本

Python psutil网络监视脚本,python,psutil,Python,Psutil,我正在尝试创建一个脚本来监视基本服务器资源:-CPU、RAM、磁盘和网络进出(以兆字节/秒为单位)。 不幸的是,当cpu、内存和磁盘部件工作时,网络功能始终显示0.0字节的输入或输出,即使存在实际流量(yum update-y)。 请提供帮助,并记住我是Python的一个完全的noob,这只是一个实践项目。提前谢谢 欢迎提出任何使其更加优化的建议 #!/usr/bin/env python3 #Module psutil needs to be installed via pip3 first.

我正在尝试创建一个脚本来监视基本服务器资源:-CPU、RAM、磁盘和网络进出(以兆字节/秒为单位)。 不幸的是,当cpu、内存和磁盘部件工作时,网络功能始终显示0.0字节的输入或输出,即使存在实际流量(yum update-y)。 请提供帮助,并记住我是Python的一个完全的noob,这只是一个实践项目。提前谢谢

欢迎提出任何使其更加优化的建议

#!/usr/bin/env python3
#Module psutil needs to be installed via pip3 first.
#Python script to Monitor Server Resources.

import time
import psutil

cpu_thresh = 50.0
cpu_pct = psutil.cpu_percent(interval=1)

if cpu_pct >= cpu_thresh:
    print("CPU Warning, CPU at ",cpu_pct, "percent")

mem = psutil.virtual_memory()
mem_thresh = 1024 * 1024 * 1024 #500MB

if mem_thresh >= mem.available:
    print("Memory Usage Warning only", mem.available /1024 /1024, "MB available")

partition1 = '/'
disk1 = psutil.disk_usage(partition1)
disk_thresh = 85.0

if disk_thresh <= disk1[3]:
    print("Root volume usage warning", disk1[3], "% used")

def net_in(inf = "eth0"):   #change the inf variable according to the interface
  net_in_ps = psutil.net_io_counters(pernic=True, nowrap=True)[inf]
  net_in_1 = net_in_ps.bytes_recv
  time.sleep(1)
  net_in_2 = net_in_ps.bytes_recv
  net_in_result = net_in_2 - net_in_1
  net_in_result_mbps = net_in_result /1024 /1024
  print(net_in_result_mbps)
  net_in_thresh = 1.5
  if net_in_result_mbps <= net_in_thresh:
      print("Network in Warning, NetSpeed at:", net_in_result_mbps, "mbps")

def net_out(inf = "eth0"):   #change the inf variable according to the interface
  net_out_ps = psutil.net_io_counters(pernic=True, nowrap=True)[inf]
  net_out_1 = net_out_ps.bytes_sent
  time.sleep(1)
  net_out_2 = net_out_ps.bytes_sent
  net_out_result = net_out_2 - net_out_1
  net_out_result_mbps = net_out_result /1024 /1024
  print(net_out_result_mbps)
  net_out_thresh = 1.5
  if net_out_result_mbps <= net_out_thresh:
      print("Network out Warning, NetSpeed at:", net_out_result_mbps, "mbps")
net_in()
net_out()
#/usr/bin/env蟒蛇3
#模块psutil需要首先通过pip3安装。
#用于监视服务器资源的Python脚本。
导入时间
导入psutil
cpu_阈值=50.0
cpu\u pct=psutil.cpu\u百分比(间隔=1)
如果cpu\u pct>=cpu\u阈值:
打印(“CPU警告,CPU处于”,CPU\U pct,“百分比”)
mem=psutil.virtual_memory()
内存阈值=1024*1024*1024#500MB
如果mem_thresh>=mem.available:
打印(“仅限内存使用警告”,mem.available/1024/1024,“MB available”)
分区1='/'
disk1=psutil.disk\u使用情况(分区1)
圆盘_阈值=85.0

如果disk_thresh函数仅返回
0
的原因是,在执行
time.sleep(1)
命令后计算差值时,您没有轮询接口以获取新的统计信息。您基本上是在计算
1000-1000

我已更正了您的代码,并将输出更改为MB而不是MB,因为
psutil
返回的是字节,而不是位:

def net_usage(inf = "eth0"):   #change the inf variable according to the interface
    net_stat = psutil.net_io_counters(pernic=True, nowrap=True)[inf]
    net_in_1 = net_stat.bytes_recv
    net_out_1 = net_stat.bytes_sent
    time.sleep(1)
    net_stat = psutil.net_io_counters(pernic=True, nowrap=True)[inf]
    net_in_2 = net_stat.bytes_recv
    net_out_2 = net_stat.bytes_sent

    net_in = round((net_in_2 - net_in_1) / 1024 / 1024, 3)
    net_out = round((net_out_2 - net_out_1) / 1024 / 1024, 3)

    print(f"Current net-usage:\nIN: {net_in} MB/s, OUT: {net_out} MB/s")
输出:

Current net-usage:
IN: 24.263 MB/s, OUT: 0.421 MB/s

通过添加一个非常简单的步骤来解决此问题:-

  net_out_ps1 = psutil.net_io_counters(pernic=True, nowrap=True)[inf]
  net_out_1 = net_out_ps1.bytes_sent
  time.sleep(1)
  net_out_ps2 = psutil.net_io_counters(pernic=True, nowrap=True)[inf]
  net_out_2 = net_out_ps2.bytes_sent
我必须在睡觉前收集一次数据,然后在睡觉后再收集一次。 非常简单的基本错误。 然而,如果有人能帮助进一步优化代码,我将不胜感激。
再次感谢。

打印(psutil.net io_计数器(pernic=True,nowrap=True))时的输出是什么?
在函数中只轮询一次以太网端口。所以你的计算总是=0,因为
1000-1000==0
。而且,它是语义,但你写的是“Mbps”,而它应该是“Mb/s”,因为你在计算字节,而不是比特。你也应该考虑在同一个函数中同时做输入/输出,这样你就不必连续做两次完全相同的轮询。谢谢Hampus Larsson!我也知道了。真是基本的错误。