python123系统基本信息获取_python获取linux系统信息的三种方法

5f21b83631127c88fef2639d476b9cf3.png

方法一:psutil模块

#!usr/bin/env 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_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}

方法二:proc

#!usr/bin/env python

# -*- coding: utf-8 -*-

import time

import os

from multiprocessing import cpu_count

class NodeResource(object):

def usage_percent(self,use, total):

# 返回百分占比

try:

ret = int(float(use)/ total * 100)

except ZeroDivisionError:

raise Exception("ERROR - zero division error")

return "%s%%"%ret

@property

def cpu_stat(self,interv

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值