forked from Sysdeploy/idlers-agent
Compare commits
3 Commits
54f87b0314
...
df525d2908
Author | SHA1 | Date | |
---|---|---|---|
df525d2908 | |||
75744e0010 | |||
2c19f2eefc |
81
agent.py
81
agent.py
|
@ -30,6 +30,7 @@ class ServerData:
|
|||
self.public_ip = self.get_public_ip()
|
||||
self.dmidecode_data = self.parse_dmidecode_output()
|
||||
self.hdparm_data = self.parse_hdparm_output()
|
||||
self.nvme_data = self.parse_nvme_devices()
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
|
||||
def parse_dmidecode_output(self):
|
||||
|
@ -103,8 +104,8 @@ class ServerData:
|
|||
|
||||
# Get the list of devices
|
||||
try:
|
||||
device_list = subprocess.check_output(['lsblk', '-d', '-o', 'NAME'], stderr=subprocess.STDOUT, universal_newlines=True).split()[1:]
|
||||
except subprocess.CalledProcessError as e:
|
||||
device_list = [device for device in os.listdir('/sys/block') if device.startswith('sd')]
|
||||
except OSError as e:
|
||||
logging.error("Failed to get device list: {}".format(e))
|
||||
return devices
|
||||
|
||||
|
@ -135,6 +136,53 @@ class ServerData:
|
|||
|
||||
return devices
|
||||
|
||||
def parse_nvme_devices(self):
|
||||
'''
|
||||
Example nvme id-ctrl output:
|
||||
|
||||
NVME Identify Controller:
|
||||
vid : 0x144d
|
||||
ssvid : 0x144d
|
||||
sn : S3RVNA0K502408F
|
||||
mn : Samsung SSD 970 EVO Plus 1TB
|
||||
fr : 2B2QEXM7
|
||||
rab : 2
|
||||
....
|
||||
'''
|
||||
devices = {}
|
||||
|
||||
# Check if nvme exists
|
||||
if shutil.which('nvme') is None:
|
||||
print("nvme not found")
|
||||
return devices
|
||||
|
||||
# Get the list of nvme devices starting with 'nvme'
|
||||
nvme_devices = [device for device in os.listdir('/sys/block') if device.startswith('nvme')]
|
||||
|
||||
for device in nvme_devices:
|
||||
device_path = '/dev/' + device
|
||||
|
||||
try:
|
||||
# Get the output of nvme id-ctrl command
|
||||
output = subprocess.check_output(['nvme', 'id-ctrl', device_path], stderr=subprocess.STDOUT, universal_newlines=True)
|
||||
except subprocess.CalledProcessError as e:
|
||||
print(f"Failed to get nvme id-ctrl output for {device_path}: {e}")
|
||||
continue
|
||||
|
||||
# Split the output into lines
|
||||
lines = output.split('\n')
|
||||
|
||||
# Parse each line
|
||||
device_info = {}
|
||||
for line in lines:
|
||||
if ':' in line:
|
||||
key, value = line.split(':', 1)
|
||||
device_info[key.strip()] = value.strip()
|
||||
|
||||
devices[device_path] = device_info
|
||||
|
||||
return devices
|
||||
|
||||
def get_ram_and_disk(self):
|
||||
# RAM information
|
||||
with open('/proc/meminfo', 'r') as f:
|
||||
|
@ -186,7 +234,8 @@ class ServerData:
|
|||
return '127.0.0.1'
|
||||
|
||||
def get_os(self):
|
||||
os_id = 27
|
||||
server_manager = ServerManager(host, api_key)
|
||||
os_id = server_manager.get_os_id()
|
||||
logging.info("OS ID: {}".format(os_id))
|
||||
return os_id
|
||||
|
||||
|
@ -194,7 +243,7 @@ class ServerData:
|
|||
ram, disk = self.get_ram_and_disk()
|
||||
post_data = {
|
||||
"server_type": 1,
|
||||
"os_id": self.get_os(),
|
||||
"os_id": self.get_os(host, api_key),
|
||||
"provider_id": 10,
|
||||
"location_id": 15,
|
||||
"ssh_port": 22,
|
||||
|
@ -254,9 +303,15 @@ class ServerData:
|
|||
for device, details in self.hdparm_data.items():
|
||||
sata_details.append("Device: {}, Model: {}, Serial: {}, Checksum: {}, Buffer Size: {}, Form Factor: {}".format(
|
||||
device, details['model_number'], details['serial_number'], details['checksum'], details['buffer_size'], details['form_factor']))
|
||||
|
||||
# NVMe Storage media
|
||||
nvme_details = []
|
||||
for device, details in self.nvme_data.items():
|
||||
nvme_details.append("Device: {}, Model: {}, Serial: {}, Firmware: {}".format(
|
||||
device, details.get('mn', 'Unknown'), details.get('sn', 'Unknown'), details.get('fr', 'Unknown')))
|
||||
|
||||
note = "Chassis Model: {} | Serial Number: {} ||| Processor Model: {} | Count: {} ||| RAM Details: {} ||| SATA Details {}".format(
|
||||
chassis_model, chassis_serial, processor_model, processor_count, ' | '.join(ram_details), ' | '.join(sata_details))
|
||||
note = "Chassis Model: {} | Serial Number: {} ||| Processor Model: {} | Count: {} ||| RAM Details: {} ||| SATA Details {} ||| NVME Details {}".format(
|
||||
chassis_model, chassis_serial, processor_model, processor_count, ' | '.join(ram_details), ' | '.join(sata_details), ' | '.join(nvme_details))
|
||||
|
||||
note_data = {
|
||||
'note': note,
|
||||
|
@ -350,6 +405,20 @@ class ServerManager:
|
|||
return self.update_note(note_data, server_id)
|
||||
else:
|
||||
return self.create_note(note_data)
|
||||
|
||||
|
||||
def get_os_id(self):
|
||||
try:
|
||||
response = urllib.request.urlopen(f"{self.host}/api/os")
|
||||
os_list = json.loads(response.read().decode())
|
||||
current_os = os.uname().sysname.lower()
|
||||
for os_entry in os_list:
|
||||
if current_os in os_entry['name'].lower():
|
||||
return os_entry['id']
|
||||
return next(os['id'] for os in os_list if os['name'].lower() == "other")
|
||||
except Exception as e:
|
||||
logging.error("Failed to fetch OS IDs: {}".format(e))
|
||||
return 27 # Default to 'Other' if unable to fetch
|
||||
|
||||
def validate_env_vars():
|
||||
api_key = os.getenv('AGENT_API')
|
||||
|
|
Loading…
Reference in New Issue
Block a user