简体   繁体   English

Python系统资源枚举

[英]Python system resources enumeration

I'm trying to get system information. 我正在尝试获取系统信息。 I have to avoid installing third-party packages and libraries or modules that require the latest version of python to run. 我必须避免安装需要运行最新版本python的第三方程序包和库或模块。 This is still in early dev and currently only tested on Ubuntu 10.04 but I will play with that later. 这仍处于早期开发阶段,目前仅在Ubuntu 10.04上进行了测试,但稍后将进行介绍。 I'm having trouble with getting the proper information for memory usage. 我在获取有关内存使用情况的正确信息时遇到了麻烦。

So far what I have is this: 到目前为止,我所拥有的是:

#! /usr/bin/python

import platform
import multiprocessing
import os

print "\nEnumerating Available System Resources..."

print "\n++++++++++ OS Name and version ++++++++++"

print "Platform:", platform.system()
print "Kernel:", platform.release()
print "Distro:", platform.linux_distribution()
print "Architecture:", platform.architecture()

print "\n++++++++++ CPU Cores ++++++++++"
p = os.popen("ps aux|awk 'NR > 0{s +=$3};END{print s}'").read()
print "Cores:", multiprocessing.cpu_count(), '\nCPU Load:', p

print "\n++++++++++ System Memory ++++++++++\n"

def meminfo():
    meminfo=dict()

    with os.popen('cat /proc/meminfo') as f:
        for line in f:
            meminfo[line.split(':')[0]] = line.split(':')[1].strip()
    return meminfo

if __name__=='__main__':
    meminfo = meminfo()
    print('Total Memory: {0}'.format(meminfo['MemTotal']))
    print('Free Memory: {0}'.format(meminfo['MemFree']))

The problem is that the last portion I get the proper memory stats but I also need to add a "usage" portion in percentage format. 问题是最后一部分我获得了正确的内存统计信息,但是我还需要以百分比格式添加“使用”部分。 I can't figure out how to get the math right, though. 不过,我不知道如何正确计算数学。

I need this because some of the systems that I work on may be tapped out on ram and I can't allow the script to crash the machine so I will need to also add something in here to evaluate the RAM percentage and CPU percentage and not continue if certain conditions exist such as CPU load over 75% and RAM less than 5% remaining. 我需要这样做是因为我正在使用的某些系统可能是在ram上开发的,并且我不允许脚本使计算机崩溃,因此我还需要在此处添加一些内容来评估RAM百分比和CPU百分比,而不是如果存在某些情况,例如CPU负载超过75%,而RAM剩余少于5%,则继续操作。 I would like some insight to those functions, also but the primary concern right now is getting the memory usage percentage. 我想对这些功能有一些了解,但是现在最主要的问题是获取内存使用百分比。 I've been at this for hours and could use some fresh eyes. 我已经在这里待了几个小时,可能会用一些新鲜的眼睛。 Thanks! 谢谢!

How about using the free command instead? 改用free命令怎么样?

import os
import re
def meminfo():

    with os.popen('free -m') as f:
        output = f.read()

    fields = re.search(r'Mem\:\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)',
            output,re.M).group
    return dict(total=fields(1),used=fields(2),free=fields(3),
            shared=fields(4),buffers=fields(5),cached=fields(6))

print meminfo()

Output: 输出:

{'used': '3292', 'buffers': '105', 'cached': '2063', 'shared': '0', 'total': '3550', 'free': '257'}

Note that the -m outputs everything in megabytes. 请注意, -m输出所有内容(以兆字节为单位)。 You can use -k for kilobytes, -g for gigabytes, or just free by itself to output in bytes. 您可以使用-k千字节, -g千兆字节,或只是free字节本身输出。

free should be available pretty much anywhere /proc/meminfo is. free应该在/ proc / meminfo所在的任何地方都可以使用。

As to your original question, to get the percentage in use you would use: 关于原始问题,要获得使用百分比,您可以使用:

used/total

or 要么

(total-free)/total

But note that this can be misleading on linux as the used value includes any cached files. 但是请注意,这在Linux上可能会产生误导,因为使用的值包括所有缓存的文件。 So for a really accurate count of memory that is locked so to speak you would use: 因此,要获得锁定的真正准确的内存计数,可以使用:

(used-cached)/total

or 要么

(total-free-cached)/total

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM