psutil获取操作系统负载信息

 

[root@host134 scripts]# more os.py 
# -*- coding: utf-8 -*-
import datetime
import time
import sys
import os
import platform
import psutil

while True:
    ##cpu
    cpu_percent = psutil.cpu_percent(interval=1)
    print(f'CPU usage: {cpu_percent}%')
    
    ##memory
    mem_info = psutil.virtual_memory()
    ##print(f'Total memory: {mem_info.total / (1024**3):.2f} GB')
    ##print(f'Used memory: {mem_info.used / (1024**3):.2f} GB')
    print(f'Memory usage: {mem_info.percent}%')

    ##disk
    ##disk_usage = psutil.disk_usage('/')
    ##print(f'Total disk space: {disk_usage.total / (1024**3):.2f} GB')
    ##print(f'Used disk space: {disk_usage.used / (1024**3):.2f} GB')
    ##print(f'Disk usage: {disk_usage.percent}%')

    ##print(psutil.cpu_times_percent())
    ##print(os.getloadavg())
    os_loadavg = psutil.getloadavg()
    print("loadavg="+str(os_loadavg))
    os_idle = psutil.cpu_times_percent(interval=2).idle
    print("idle=" + str(os_idle))
    print(psutil.net_io_counters(pernic=False, nowrap=True))
    time.sleep(1)

 

执行输出:

[root@host134 scripts]# python os.py 
CPU usage: 0.5%
Memory usage: 81.6%
loadavg=(0.0, 0.02, 0.05)
idle=98.5
snetio(bytes_sent=1211973285718, bytes_recv=2139221613101, packets_sent=1536001712, packets_recv=2780453045, errin=0, errout=0, dropin=16261939, dropout=0)

 

posted @ 2024-05-24 17:06  slnngk  阅读(11)  评论(0编辑  收藏  举报