获取服务器型号、内存、CPU信息

#!/usr/bin/python
#2013-02-18 by larry
import os
import sys
import re
from collections import Counter

def getSN():
    s=os.popen('dmidecode  -s  system-serial-number')
    n=os.popen('dmidecode  -s system-product-name')
    sn=s.readline().strip()
    name=n.readline().strip()
    s.close()
    if ( len(sn) > 0 ):
        print  "Product Name:",name
        print  "Serial Number:",sn,"n"
    else:
        return -1

def getCPU():
    opfile=file('/proc/cpuinfo','r')
    cpucount=0
    phcpucount=0
    cpumodel=[]
    L1=[]
    for line in opfile.readlines():
            i=line.strip('n')
            if re.match(r'^processor',i):
                cpucount=cpucount+1    
            elif re.match(r'^physical id',i):
                L1.append(i)
            elif re.findall('model name',i):
                cpumodel.append(i.split(':')[1])
            else:
                pass
    opfile.close()
    for num in Counter(L1).keys():
        phcpucount=phcpucount+1
    print "Processor number:",cpucount
    print "Physical CPU number:",phcpucount
    print "CPU Model:",cpumodel[0].strip(),"n"

def getMem():
    m=os.popen('grep MemTotal  /proc/meminfo')
    max=os.popen('dmidecode  -t memory')
    swm=os.popen('grep SwapTotal /proc/meminfo')
    L=[]
    for line in max.readlines():
        R1=line.strip("n")
        if re.findall('Maximum',R1):
            sw,se=R1.split(":")
            L.append(se)
    max.close()
    for line1 in swm.readlines():
        sw1,sw2,sw3=line1.split()
        L.append(sw2)
    for line2 in m.readlines():
        m1,m2,m3=line2.split()
        L.append(m2)
    print "Memory Total:",int(L[2])/1000/1000,"G"
    print "Swap Total:",int(L[1])/1024,"M"
    print "Max support Memory:",L[0]
 
if __name__ == '__main__':
    print "x1b[0;34m+++++++++++Device SN+++++++++x1b[0m"
    getSN()
    print "x1b[0;34m+++++++++++Cpu Info++++++++++x1b[0m"
    getCPU()
    print "x1b[0;34m+++++++++++Memory +++++++++++x1b[0m"
    getMem()

执行脚本结果:

+++++++++++Device SN+++++++++
Product Name: PowerEdge 860
Serial Number: BBNRF2X

+++++++++++Cpu Info++++++++++
Processor number: 4
Physical CPU number: 1
CPU Model: Intel(R) Xeon(R) CPU           X3210  @ 2.13GHz

+++++++++++Memory +++++++++++
Memory Total: 4 G
Swap Total: 8189 M
Max support Memory:  8 GB

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

  

  

  

Category

Archives