统计服务器的相关配置信息

该脚本主要用来统计CentOS,RedHat 6~7系统版本的服务器基础信息,方便自己检查新服的配置。

#!/usr/bin/python
# coding: utf8

from subprocess import Popen, PIPE
import time
import re
import os


# 获取主机名
def host_name():
    hostname = Popen(["hostname"], stdout=PIPE)
    hostname = hostname.stdout.read()
    return hostname

# 获取操作系统版本
def os_version():
    with open("/etc/redhat-release") as f:
        os_version = f.read()
    return os_version

# 获取操作系统内核版本
def os_core_version():
    os_core_version = Popen(["uname", "-r"], stdout=PIPE)
    os_core_version = os_core_version.stdout.read()
    return os_core_version

# 获取CPU相关信息
def cpu_info():
    core_number = []
    with open("/proc/cpuinfo") as cpu_info:
        for i in cpu_info:
            if i.startswith("processor"):
                core_number.append(i)
            if i.startswith("model name"):
                cpu_mode = i.split(":")[1]
    return len(core_number), cpu_mode

# 获取内存相关信息
def mem_info():
    with open("/proc/meminfo") as mem_info:
        for i in mem_info:
            if i.startswith("MemTotal"):
                mentotal = i.split(" ")[-2:]
                mentotal = ' '.join(mentotal)
    return mentotal

# 获取服务器硬件相关信息
def bios_info():
    bios_info = Popen(["dmidecode", "-t", "system"], stdout=PIPE)
    bios_info = bios_info.stdout.readlines()

    for i in bios_info:
        if "Manufacturer" in i:
            manufacturer = i.split(":")[1]
        if "Serial Number" in i:
            serialnumber = i.split(":")[1]
    return manufacturer, serialnumber

# 获取服务器的时间信息
def Time_info():
    time_info = time.strftime('%Y-%m-%d %A %X', time.localtime())
    # time_info = Popen(["date", "-R"], stdout=PIPE)
    # time_info = time_info.stdout.read()
    return time_info

# 使用ifconfig获取网卡信息
def getIfconfig():
    p = Popen(['ifconfig'], stdout=PIPE)
    data = p.stdout.read().split('\n\n')
    return [i for i in data if i and not i.startswith('lo')]

# 对ifconfig的输出信息进行筛选
def parseIfconfig(data):
    dic = {}
    for line in data:
        re_devname = re.compile(r'(\w+).*Link encap', re.M)
        re_macaddr = re.compile(r'HWaddr\s([0-9A-F:]{17})', re.M)
        # re_ipaddr = re.compile(r'inet addr: ([\d\.]{7, 15})', re.M)
        re_ipaddr = re.compile(r'inet addr:([\d\.]{7,15})', re.M)
        devname = re_devname.search(line)
        mac = re_macaddr.search(line)
        ip = re_ipaddr.search(line)

        if devname:
            devname = devname.group(1)
        else:
            devname = ''

        if mac:
            mac = mac.group(1)
        else:
            mac = ''

        if ip:
            ip = ip.group(1)
        else:
            ip = ''
        dic[devname] = [mac, ip]
    return dic

# 获取磁盘相关信息
def disk_info():
    disk_info = Popen(["df", "-h"], stdout=PIPE)
    disk_info = disk_info.stdout.read()
    return disk_info

# 格式对齐函数
def myAlign(string, length=0):
    if length == 0:
        return string
    slen = len(string)
    re = string
    if isinstance(string, str):
        placeholder = ' '
    else:
        placeholder = u' '
    while slen < length:
        re += placeholder
        slen += 1
    return re

if __name__ == '__main__':
    # 初始化相关对象
    hostname = host_name()
    osversion = os_version()
    oscoreversion = os_core_version()
    totalmem = mem_info()
    cpunumber, cpumode = cpu_info()
    manufacturer, serialnumber = bios_info()
    timeinfo = Time_info()
    data = getIfconfig()
    ipinfo = parseIfconfig(data)
    diskinfo = disk_info()

    width = 60
    print '%s' %("".center(width, '='))
    print myAlign(u"服务器时间及时区", 10) + ":" + myAlign(str(timeinfo))
    print myAlign(u"主机名", 10) + ":" + myAlign(str(hostname)),
    print myAlign(u"系统版本", 10) + ":" + myAlign(str(osversion)),
    print myAlign(u"系统内核版本", 10) + ":" + myAlign(str(oscoreversion)),
    print myAlign(u"总内存", 10) + ":" + myAlign(str(totalmem)),
    print myAlign(u"CPU生产商", 10) + "   :" + myAlign(cpumode),
    print myAlign(u"CPU总核心数", 10) + "   :" + myAlign(str(cpunumber))
    print myAlign(u"服务器生产商", 10) + ":" + myAlign(str(manufacturer)),
    print myAlign(u"服务器序列号", 10) + ":" + myAlign(str(serialnumber)),
    print ""
    print '%s' %("".center(width, '='))
    print '%s' %("网卡信息".center(width, '-'))
    for x in ipinfo:
        print '%s' %("".center(width, '-'))
        print myAlign(u"网卡名", 10) + ":" + myAlign(str(x))
        print myAlign(u"物理地址", 10) + ":" + myAlign(str(ipinfo[x][0]))
        print myAlign(u"IP地址", 10) + "  :" + myAlign(str(ipinfo[x][1]))
    print ""
    print '%s' %("".center(width, '='))
    print '%s' %("磁盘分区信息".center(width, '-'))
    print diskinfo
    print '%s' %("".center(width, '='))
  • 输出结果显示


    脚本输出结果
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,736评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,167评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,442评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,902评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,302评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,573评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,847评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,562评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,260评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,531评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,021评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,367评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,016评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,068评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,827评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,610评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,514评论 2 269

推荐阅读更多精彩内容

  • linux资料总章2.1 1.0写的不好抱歉 但是2.0已经改了很多 但是错误还是无法避免 以后资料会慢慢更新 大...
    数据革命阅读 12,016评论 2 34
  • 国家电网公司企业标准(Q/GDW)- 面向对象的用电信息数据交换协议 - 报批稿:20170802 前言: 排版 ...
    庭说阅读 10,503评论 6 13
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,358评论 6 343
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,099评论 18 139
  • 特立独行是什么? 特:独特;立:立身。形容人的志行高洁,不同流俗。 朋友问我说:“太阳太大了,你说我上班要不要买一...
    请叫我徐老师x阅读 740评论 16 8