#!/usr/bin/python
# -*- coding:utf-8 -*-
import socket
import psutil
class NodeResource(object):
def get_host_info(self):
host_name = socket.gethostname()
return {‘host_name‘: host_name}
def get_cpu_state(self):
‘‘‘获取CPU使用情况‘‘‘
cpu_count = psutil.cpu_count(logical=False)
cpu_percent = (str)(psutil.cpu_percent(1)) + ‘%‘
return {‘cpu_count‘: cpu_count, ‘cpu_percent‘: cpu_percent}
def get_memory_state(self):
‘‘‘获取内存使用情况‘‘‘
mem = psutil.virtual_memory()
mem_total = mem.total / 1024 / 1024
mem_free = mem.available / 1024 / 1024
mem_percent = ‘%s%%‘ % mem.percent
return {‘mem_toal‘: mem_total, ‘mem_free‘: mem_free, ‘mem_percent‘: mem_percent}
def get_disk_state(self):
‘‘‘获取磁盘使用情况‘‘‘
disk_stat = psutil.disk_usage(‘/‘)
disk_total = disk_stat.total
disk_free = disk_stat.free
disk_percent = ‘%s%%‘ % disk_stat.percent
return {‘mem_toal‘: disk_total, ‘mem_free‘: disk_free, ‘mem_percent‘: disk_percent}
obj = NodeResource()
print(obj.get_host_info)
print(obj.get_cpu_state())
print(obj.get_memory_state)
print(obj.get_disk_state)
原文:https://www.cnblogs.com/wanglj/p/12129829.html