Parse dmidecode output #14
76
agent.py
76
agent.py
|
@ -25,8 +25,70 @@ class ServerData:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.hostname = os.uname().nodename
|
self.hostname = os.uname().nodename
|
||||||
self.public_ip = self.get_public_ip()
|
self.public_ip = self.get_public_ip()
|
||||||
|
self.dmidecode_data = parse_dmidecode_output()
|
||||||
logging.basicConfig(level=logging.INFO)
|
logging.basicConfig(level=logging.INFO)
|
||||||
|
|
||||||
|
def parse_dmidecode_output():
|
||||||
|
'''
|
||||||
|
Example dmidecode output:
|
||||||
|
|
||||||
|
Handle 0x0069, DMI type 20, 35 bytes
|
||||||
|
Memory Device Mapped Address
|
||||||
|
Starting Address: 0x00600000000
|
||||||
|
Ending Address: 0x007FFFFFFFF
|
||||||
|
Range Size: 8 GB
|
||||||
|
Physical Device Handle: 0x0067
|
||||||
|
Memory Array Mapped Address Handle: 0x006A
|
||||||
|
Partition Row Position: Unknown
|
||||||
|
Interleave Position: 2
|
||||||
|
Interleaved Data Depth: 2
|
||||||
|
|
||||||
|
Handle 0x006A, DMI type 19, 31 bytes
|
||||||
|
Memory Array Mapped Address
|
||||||
|
Starting Address: 0x00000000000
|
||||||
|
Ending Address: 0x007FFFFFFFF
|
||||||
|
Range Size: 32 GB
|
||||||
|
Physical Array Handle: 0x005E
|
||||||
|
Partition Width: 4
|
||||||
|
'''
|
||||||
|
if os.path.isfile('/usr/sbin/dmidecode'):
|
||||||
|
try:
|
||||||
|
# ignore error messages produced by the command
|
||||||
|
output = subprocess.check_output(['sudo', '/usr/sbin/dmidecode'], stderr=subprocess.DEVNULL).decode('utf-8')
|
||||||
|
|
||||||
|
# each section is separated by two newlines
|
||||||
|
sections = output.split('\n\n')
|
||||||
|
parsed_sections = []
|
||||||
|
for section in sections:
|
||||||
|
|
||||||
|
# each line in a section is separated by a newline
|
||||||
|
lines = section.split('\n')
|
||||||
|
|
||||||
|
# first line contains the DMI type - only need number
|
||||||
|
match = re.search(r'DMI type (\d+)', lines[0])
|
||||||
|
if match:
|
||||||
|
dmi_type = int(match.group(1))
|
||||||
|
else: # skip if no DMI type in this section
|
||||||
|
continue
|
||||||
|
|
||||||
|
# each section is a dictionary with DMIType as key
|
||||||
|
section_dict = {'DMIType': dmi_type}
|
||||||
|
if len(lines) > 1:
|
||||||
|
section_dict['description'] = lines[1].strip()
|
||||||
|
|
||||||
|
for line in lines[2:]: # skip first two lines - already processed
|
||||||
|
|
||||||
|
# each line has tabs at the beginning and space(maybe optional) between key and value
|
||||||
|
match = re.match(r'\t(.+):\s*(.*)', line)
|
||||||
|
if match:
|
||||||
|
key, value = match.groups()
|
||||||
|
section_dict[key] = value
|
||||||
|
parsed_sections.append(section_dict)
|
||||||
|
return parsed_sections
|
||||||
|
except subprocess.CalledProcessError:
|
||||||
|
pass
|
||||||
|
return []
|
||||||
|
|
||||||
def get_ram_and_disk(self):
|
def get_ram_and_disk(self):
|
||||||
with open('/proc/meminfo', 'r') as f:
|
with open('/proc/meminfo', 'r') as f:
|
||||||
meminfo = f.read()
|
meminfo = f.read()
|
||||||
|
@ -39,15 +101,11 @@ class ServerData:
|
||||||
|
|
||||||
def get_cpu_count(self):
|
def get_cpu_count(self):
|
||||||
cpu_count = 0
|
cpu_count = 0
|
||||||
if os.path.isfile('/usr/sbin/dmidecode'):
|
for section in self.dmidecode_data:
|
||||||
try:
|
if section['DMIType'] == 4: # 4 corresponds to processor
|
||||||
output = subprocess.check_output(['sudo', '/usr/sbin/dmidecode', '-t', 'processor']).decode('utf-8')
|
core_count = int(section.get('Core Count', '0'))
|
||||||
core_match = re.search(r'Core Count: (\d+)', output)
|
thread_count = int(section.get('Thread Count', '0'))
|
||||||
thread_match = re.search(r'Thread Count: (\d+)', output)
|
cpu_count = core_count * thread_count
|
||||||
if core_match and thread_match:
|
|
||||||
cpu_count = int(core_match.group(1)) * int(thread_match.group(1))
|
|
||||||
except subprocess.CalledProcessError:
|
|
||||||
pass
|
|
||||||
if cpu_count == 0:
|
if cpu_count == 0:
|
||||||
with open('/proc/cpuinfo', 'r') as f:
|
with open('/proc/cpuinfo', 'r') as f:
|
||||||
cpuinfo = f.read()
|
cpuinfo = f.read()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user