Compare commits

...

9 Commits

186
agent.py
View File

@ -3,6 +3,8 @@ import urllib.request
import logging
import json
import http.client
import subprocess
import re
NON_UPDATABLE_KEYS = [
'server_type',
@ -25,29 +27,99 @@ class ServerData:
def __init__(self):
self.hostname = os.uname().nodename
self.public_ip = self.get_public_ip()
self.dmidecode_data = self.parse_dmidecode_output()
logging.basicConfig(level=logging.INFO)
def parse_dmidecode_output(self):
'''
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):
# RAM information
with open('/proc/meminfo', 'r') as f:
meminfo = f.read()
ram = int([x for x in meminfo.split('\n') if 'MemTotal' in x][0].split()[1]) // 1024
with open('/proc/diskstats', 'r') as f:
diskstats = f.read()
disk = sum(int(x.split()[9]) for x in diskstats.split('\n') if x) * 512 // 10**9
# Disk space information
disk = 0
for device in os.listdir('/sys/block'):
size_path = f'/sys/block/{device}/size'
if os.path.exists(size_path) and os.access(size_path, os.R_OK):
try:
with open(size_path, 'r') as f:
size = int(f.read().strip())
disk += size
except Exception:
pass # Skip the device if any exception occurs
disk = disk * 512 // (1024**3) # convert to GB
logging.info(f"RAM: {ram}MB, Disk: {disk}GB")
return ram, disk
def get_cpu_count(self):
cpu_count = 0
if os.path.isfile('/usr/sbin/dmidecode'):
try:
output = subprocess.check_output(['sudo', '/usr/sbin/dmidecode', '-t', 'processor']).decode('utf-8')
core_match = re.search(r'Core Count: (\d+)', output)
thread_match = re.search(r'Thread Count: (\d+)', output)
if core_match and thread_match:
cpu_count = int(core_match.group(1)) * int(thread_match.group(1))
except subprocess.CalledProcessError:
pass
for section in self.dmidecode_data:
if section['DMIType'] == 4: # 4 corresponds to processor
core_count = int(section.get('Core Count', '0'))
thread_count = int(section.get('Thread Count', '0'))
cpu_count = core_count * thread_count
if cpu_count == 0:
with open('/proc/cpuinfo', 'r') as f:
cpuinfo = f.read()
@ -103,6 +175,40 @@ class ServerData:
logging.info("Post data created")
return post_data
def create_note_data(self):
chassis_info = None
for section in self.dmidecode_data:
if section['DMIType'] == 1:
chassis_info = section
break
if chassis_info:
chassis_model = chassis_info.get('Product Name', 'Unknown')
chassis_serial = chassis_info.get('Serial Number', 'Unknown')
else:
chassis_model = chassis_serial = 'Unknown'
processor_info = [section for section in self.dmidecode_data if section['DMIType'] == 4]
processor_model = processor_info[0].get('Version', 'Unknown') if processor_info else 'Unknown'
processor_count = len(processor_info)
ram_info = [section for section in self.dmidecode_data if section['DMIType'] == 17]
ram_details = []
for ram in ram_info:
size = ram.get('Size', 'Unknown')
speed = ram.get('Speed', 'Unknown')
ecc = 'Yes' if ram.get('Total Width') == '72 bits' and ram.get('Data Width') == '64 bits' else 'No'
serial_number = ram.get('Serial Number', 'Unknown')
ram_type = ram.get('Type', 'Unknown')
ram_details.append("Size: {}, Speed: {}, ECC: {}, Serial Number: {}, Type: {}".format(size, speed, ecc, serial_number, ram_type))
note = "Chassis Model: {} | Serial Number: {} ||| Processor Model: {} | Count: {} ||| RAM Details: {}".format(
chassis_model, chassis_serial, processor_model, processor_count, ' | '.join(ram_details))
note_data = {
'note': note,
}
return note_data
class ServerManager:
def __init__(self, host, api_key):
@ -161,6 +267,36 @@ class ServerManager:
return server['id']
return None
def upsert_server(self, post_data):
server_id = self.existing_server_id(post_data)
if server_id:
logging.info('Server already exists with id: {}, Updating...'.format(server_id))
response = self.update_server(post_data, server_id)
else:
logging.info('Server does not exist, Creating...')
response = self.create_server(post_data)
# Extract the server_id from the response
server_id = json.loads(response).get('server_id', None)
if server_id is None:
logging.error('Failed to get server_id from response: {}'.format(response))
raise ValueError('Failed to get server_id from response')
return server_id
def upsert_note(self, note_data, server_id):
note_data['service_id'] = server_id
try:
note = self.get_note(server_id)
except urllib.error.HTTPError:
note = None
if note:
return self.update_note(note_data, server_id)
else:
return self.create_note(note_data)
def validate_env_vars():
api_key = os.getenv('AGENT_API')
host = os.getenv('HOST')
@ -180,29 +316,11 @@ def main():
server_manager = ServerManager(host, api_key)
# Check if the server already exists
server_id = server_manager.existing_server_id(post_data)
server_id = server_manager.upsert_server(post_data)
logging.info('Server id: {}'.format(server_id))
# If the server exists, update it
if server_id:
logging.info('Server already exists with id: {}, Updating...'.format(server_id))
logging.info(server_manager.update_server(post_data, server_id))
else:
logging.info('Server does not exist, Creating...')
logging.info(server_manager.create_server(post_data))
note_data = {
'service_id': server_id,
'note': 'Bla bla bla'
}
try:
note = server_manager.get_note(server_id)
except urllib.error.HTTPError:
note = None
if note:
server_manager.update_note(note_data, server_id)
else:
server_manager.create_note(note_data)
note_data = server_data.create_note_data()
server_manager.upsert_note(note_data, server_id)
if __name__ == '__main__':
main()