python 获取服务器基本信息

方法1:通过系统文件或者系统命令来获取;

import os
import commands
import string
import socket
import json
def base_info():
    """
    :return HOSTNAME, IPADDR, CPU, CPUIDLE, MEM, AVAI_MEM, VERSION(6/7), MACHINE(64/32)
    """
    ifname='eth0'
    osinfo = os.uname()
    HOSTNAME = osinfo[1]
    VERSION  = osinfo[2].split('.')[-2]
    MACHINE  = osinfo[-1]
    mem_cmd = "awk '/MemTotal/{printf(\"%0.2f\",$2/1000/1000)}' /proc/meminfo"
    MEM = int(round(float(commands.getoutput(mem_cmd))))

    cpu_cmd = "awk '/processor/{cpu+=1}END{print cpu}' /proc/cpuinfo"
    CPU = commands.getoutput(cpu_cmd)
    if VERSION == 'el7':
        avai_mem_cmd = "awk '/MemAvailable/ {printf(\"%0.2f\",$2/1000/1000)}' /proc/meminfo"
        AVAI_MEM =  commands.getoutput(avai_mem_cmd)
        cpuidle_cmd = "top -bcn 1 | head |awk '/Cpu/ {print $8}'"
        CPUIDLE = commands.getoutput(cpuidle_cmd)
    if VERSION == 'el6':
        ##centos6 可用内存计算方法:MemFree+Buffers+Cached
        avai_mem_cmd = "sed -n '2,4p' /proc/meminfo |awk '{sum+=$2}END{printf(\"%.2f\",sum/1000/1000)}'"
        AVAI_MEM =  commands.getoutput(avai_mem_cmd)
        cpuidle_cmd = "top -bcn 1 | sed -n 's/ //g;3p' | awk -F\"%|,\" '{print $7}'"
        CPUIDLE = commands.getoutput(cpuidle_cmd)
    unix_socker = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    IPADDR = socket.inet_ntoa(fcntl.ioctl(
        unix_socker.fileno(),
        0x8915,  # SIOCGIFADDR
        struct.pack('256s', ifname[:15])
    )[20:24])

    return HOSTNAME, IPADDR, CPU, CPUIDLE, MEM, AVAI_MEM, VERSION, MACHINE

方法2:通过 psutil 模块来获取


import os
import commands
import string
import socket
import json
import psutil
def base_info():
    """
    :return HOSTNAME, IPADDR, CPU, CPUIDLE, MEM, AVAI_MEM, VERSION(6/7), MACHINE(64/32)
    """
    ifname='eth0'
    osinfo = psutil.os.uname()
    HOSTNAME = osinfo[1]
    VERSION = osinfo[2].split('.')[-2]
    MACHINE = osinfo[-1]
    CPUIDLE = 100-psutil.cpu_percent(interval=1)
    CPU = psutil.cpu_count()
    mem_info = psutil.virtual_memory()
    MEM = mem_info.total/1024/1024/1000
    AVAI_MEM = mem_info.available/1024/1024/1000
    net_info = psutil.net_if_addrs()
    IPADDR = net_info['eth0'][0].address
 
    return HOSTNAME, IPADDR, CPU, CPUIDLE, MEM, AVAI_MEM, VERSION, MACHINE



上一篇:信核史上最全能的新品:OSNSolution 6.3正式发布!


下一篇:Python3 数据库操作基本类