首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >psutil网络监控脚本

psutil网络监控脚本
EN

Stack Overflow用户
提问于 2020-05-26 18:34:25
回答 2查看 1.3K关注 0票数 0

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

欢迎任何使其更优化的建议。

代码语言:javascript
复制
#!/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()
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-05-26 19:16:03

您的函数只返回0的原因是,当您在time.sleep(1)命令之后计算差值时,您不会轮询接口以获得新的统计数据。你基本上是在计算1000-1000

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

代码语言:javascript
复制
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")

输出:

代码语言:javascript
复制
Current net-usage:
IN: 24.263 MB/s, OUT: 0.421 MB/s
票数 1
EN

Stack Overflow用户

发布于 2020-05-26 19:22:21

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

代码语言:javascript
复制
  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

我必须在睡眠前收集一次数据,然后在睡眠之后再收集一次。非常简单的基本错误。但是,如果有人可以帮助进一步优化代码,我将不胜感激。再次感谢。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62020140

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档