运维天地-服务器04-X86-PCServer内存信息获取
- 2020 年 4 月 2 日
- 笔记
01
PART
知识回顾
本篇文章,我们重点介绍服务器"内存"信息的获取。
前期知识储备和回顾,请参考以下文章
01)运维天地-服务器01-X86-PCServer属性介绍
02)运维天地-服务器02-X86-PCServer资产信息获取
03)运维天地-服务器02-X86-PCServer CPU信息获取
特别说明:本文的服务器特指X86-PCServer硬件服务器


02
PART
X86-PCServer 内存信息获取
A
获取脚本
#!/usr/bin/python #-*- coding: utf-8 -*- #author: wangzhiguo #date: 2020.01.29 import commands class MemInfo(): def __init__(self): return def get_mem_type(self): status, output = commands.getstatusoutput("dmidecode -t memory | grep 'Type: DDR' | sort -u | awk '{print $2}'") mem_type = output.strip() return mem_type def get_mem_speed(self): status, output = commands.getstatusoutput("dmidecode -t memory | grep 'Configured Clock Speed:' | grep -v 'Configured Clock Speed: Unknown' | sort -u | awk '{print$4}'") mem_speed = output.strip() return mem_speed def get_mem_size(self): status, output = commands.getstatusoutput ("dmidecode -t memory | grep 'Size:' | grep -v 'Size: No Module Installed' | sort -u") output = output.split(":")[1].split() if output[1] == "MB": mem_size = int(output[0])/1024 return mem_size elif output[1] == "GB": mem_size = int(output[0]) return mem_size elif output[1] == "KB": mem_size = int(output[0])/1024/1024 return mem_size elif output[1] == "TB": mem_size = int(output[0])*1024 return mem_size else: mem_size = int(output[0]) return mem_size def get_mem_num(self): status, output = commands.getstatusoutput("dmidecode -t memory | grep 'Size:' | grep -v 'Size: No Module Installed' | wc -l") mem_num = output.strip() mem_num = int(mem_num) return mem_num def get_mem_slot_num(self): status, output = commands.getstatusoutput("dmidecode -t memory | grep 'Memory Device' | wc -l") mem_slot_num = output.strip() mem_slot_num = int(mem_slot_num) return mem_slot_num def get_all(self): mem_type = self.get_mem_type() mem_speed = self.get_mem_speed() mem_size = self.get_mem_size() mem_num = self.get_mem_num() mem_slot_num = self.get_mem_slot_num() return mem_type, mem_speed, mem_size, mem_num, mem_slot_num def main(): temp = MemInfo() print(temp.get_all()) if __name__ == '__main__': main()
B
获取方法概述


- 内存信息的获取,其实就利用了dmidecode命令
- dmidecode -t momory
- 接下来就是字符串的处理(过滤、分割、拼接)