首页 > 编程语言 > 详细

Python 获取linux资源使用情况

时间:2020-01-01 21:03:59      阅读:111      评论:0      收藏:0      [点我收藏+]
#!/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)

  

Python 获取linux资源使用情况

原文:https://www.cnblogs.com/wanglj/p/12129829.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!