forked from Sysdeploy/idlers-agent
Compare commits
12 Commits
de3d5fb4ab
...
8f79919659
Author | SHA1 | Date | |
---|---|---|---|
8f79919659 | |||
61909258a0 | |||
c5474b1f3c | |||
f10d0b53b0 | |||
3940cbdbde | |||
fa67103b20 | |||
064d781607 | |||
52ecd2dc53 | |||
1b2769996a | |||
bd9a848737 | |||
7ab3d18ae9 | |||
f73823cd07 |
171
agent.py
171
agent.py
|
@ -3,6 +3,8 @@ import urllib.request
|
|||
import logging
|
||||
import json
|
||||
import http.client
|
||||
import subprocess
|
||||
import re
|
||||
|
||||
NON_UPDATABLE_KEYS = [
|
||||
'server_type',
|
||||
|
@ -13,7 +15,6 @@ NON_UPDATABLE_KEYS = [
|
|||
'bandwidth',
|
||||
'was_promo',
|
||||
'active',
|
||||
'show_public',
|
||||
'owned_since',
|
||||
'currency',
|
||||
'price',
|
||||
|
@ -26,8 +27,70 @@ 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):
|
||||
with open('/proc/meminfo', 'r') as f:
|
||||
meminfo = f.read()
|
||||
|
@ -39,9 +102,16 @@ class ServerData:
|
|||
return ram, disk
|
||||
|
||||
def get_cpu_count(self):
|
||||
with open('/proc/cpuinfo', 'r') as f:
|
||||
cpuinfo = f.read()
|
||||
cpu_count = cpuinfo.count('processor')
|
||||
cpu_count = 0
|
||||
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()
|
||||
cpu_count = cpuinfo.count('processor')
|
||||
logging.info(f"CPU Count: {cpu_count}")
|
||||
return cpu_count
|
||||
|
||||
|
@ -93,6 +163,43 @@ 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')
|
||||
configured_speed = ram.get('Configured Memory Speed', 'Unknown')
|
||||
total_width = int(ram.get('Total Width', "0").split()[0])
|
||||
data_width = int(ram.get('Data Width', "0").split()[0])
|
||||
ecc = 'Yes' if total_width > data_width 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, configured_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):
|
||||
|
@ -122,6 +229,17 @@ class ServerManager:
|
|||
def get_existing_servers(self):
|
||||
return self.send_request('GET', '/api/servers')
|
||||
|
||||
def get_note(self, service_id):
|
||||
return self.send_request('GET', '/api/notes/' + str(service_id))
|
||||
|
||||
def create_note(self, post_data):
|
||||
logging.info("Creating note...")
|
||||
return self.send_request('POST', '/api/notes', post_data)
|
||||
|
||||
def update_note(self, post_data, service_id):
|
||||
logging.info(f"Updating note with id {service_id}...")
|
||||
return self.send_request('PUT', '/api/notes/' + str(service_id), post_data)
|
||||
|
||||
def create_server(self, post_data):
|
||||
logging.info("Creating server...")
|
||||
return self.send_request('POST', '/api/servers', post_data)
|
||||
|
@ -140,6 +258,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')
|
||||
|
@ -159,16 +307,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 = server_data.create_note_data()
|
||||
server_manager.upsert_note(note_data, server_id)
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
main()
|
||||
|
|
Loading…
Reference in New Issue
Block a user