Search code examples
pythonpython-2.7cpucpu-architecturesunos

How do I get CPU information by using python on SunOs systems?


I want to get CPU information programatically. I already coded a solution for Linux, but I need to build a similar solutions for SunOS. Does anyone have any idea?

def getCpusInfo():
    cpuinfos = []
    cpuinfo = {}

    for line in open('/proc/cpuinfo').readlines():
        line = line.strip()
        dual = line.split(':')
        key = dual[0].replace('\t', '')
        if (key == 'processor'):
            cpuinfo = {}
            cpuinfos.append(cpuinfo)
        elif (len(dual)>1):
            cpuinfo[key] = trim(dual[1])
    return cpuinfos



cpusinfo = getCpusInfo();

print "Model = " + cpusinfo[0]['model name']
print "Quantity = " + str(len(cpusinfo))
print "Cores = " + cpusinfo[0]['cpu cores']
print "Threads = " + cpusinfo[0]['siblings']

Solution

  • I could find a solution in bash so it is easly portable for python.

    Thanks for the help in the comments

    CORES_F=`/usr/sbin/psrinfo -p`
    PROCESSOR=`/usr/sbin/psrinfo -vp | head -1 | awk '{print $5}'`
    MODEL=`/usr/sbin/psrinfo -pv | tail -1 | awk '{print $1}'`
    echo "Model = " $MODEL
    echo "Cores = " $CORES_F
    echo "Threads = " $PROCESSOR
    

    It is still missing cpu quantity, but in my case I don't need to save this information anymore.