打印一小时的cpu使用情况,获取平均cpu值,并打印该小时中的最高峰值

时间:2018-12-20 19:42:16

标签: python python-3.x

# Importing socket library 
import socket
import psutil
import time
import sys


# Function to display hostname and
# IP address 
def get_cpu():
    try:
        host_name = socket.gethostname()
        host_ip = socket.gethostbyname(host_name)
        current_cpu = psutil.cpu_percent()

        starttime = time.time()
        while True:
            print("get_cpu:", current_cpu, "host:", host_name, "IP: ", host_ip)
            time.sleep(60.0 - ((time.time() - starttime) % 60.0))
    except:

        print("Unable to get Hostname and IP and Current CPU")

    # Driver code


get_cpu()  # Function call

# gives a single float value
# psutil.cpu_percent()
# gives an object with many fields
# psutil.virtual_memory()`
# you can convert that object to a dictionary
# dict(psutil.virtual_memo

我当前的脚本每60秒打印一次CPU使用情况,

预期输出:获取一个小时的平均CPU使用率,并在该小时内显示最高峰值。

谢谢

0 个答案:

没有答案