Compare commits

...

10 Commits

Author SHA1 Message Date
56d64c65b5
Update README.md
All checks were successful
buildbot/multibuild_parent Build done.
buildbot/travis_bionic Build done.
2024-12-09 16:28:35 +08:00
ef9e8e33d6
Fix missing drive form factor
All checks were successful
buildbot/travis_bionic Build done.
buildbot/multibuild_parent Build done.
2024-12-09 16:14:24 +08:00
43e019615e Update README.md
All checks were successful
buildbot/multibuild_parent Build done.
buildbot/travis_bionic Build done.
2024-07-07 08:46:42 +00:00
7209cea893
Code quality and missing variables
All checks were successful
buildbot/multibuild_parent Build done.
buildbot/travis_bionic Build done.
2024-07-07 15:38:33 +08:00
8253e5fc51
getting os details with /api/os
All checks were successful
buildbot/multibuild_parent Build done.
buildbot/travis_bionic Build done.
2024-07-05 23:37:55 +05:30
a07225a0eb
F-string removal
All checks were successful
buildbot/multibuild_parent Build done.
buildbot/travis_bionic Build done.
2024-07-05 23:24:00 +08:00
bee43980da
Fix core/thread count calculation
All checks were successful
buildbot/multibuild_parent Build done.
buildbot/travis_bionic Build done.
2024-07-05 23:07:33 +08:00
ca77ce4c45
Docu typo
All checks were successful
buildbot/multibuild_parent Build done.
buildbot/travis_bionic Build done.
2024-07-05 22:57:34 +08:00
9a9a98edf7
Fix "Unknown" RAM size
- "Unknown" isn't an int
2024-07-05 22:56:08 +08:00
601bed9a34
skip device which doesn't have model number
All checks were successful
buildbot/multibuild_parent Build done.
buildbot/travis_bionic Build done.
2024-07-05 18:42:18 +05:30
2 changed files with 110 additions and 18 deletions

View File

@ -2,6 +2,12 @@
Agent for my-idlers
The design is supposed to be minimise dependencies. It doesn't have python
dependencies and should work with python from 3.4 on (so that you can use it on
older OSes without having to upgrade python)
Application dependencies are hdparm, nvme and dmidecode
```
export AGENT_API=<API_KEY> HOST=https://idlers.test2.sysdeploy.org;python3 agent.py
```
export API_KEY=<API_KEY> HOST=https://idlers.test2.sysdeploy.org/api/servers;python3 main.py
```

118
agent.py
View File

@ -1,8 +1,8 @@
import os
import urllib.error
import urllib.request
import logging
import json
import http.client
import subprocess
import re
import shutil
@ -124,13 +124,21 @@ class ServerData:
# Parse the output
details = {}
details['model_number'] = re.search(r'Model Number:\s*(.*)', output).group(1)
model_number_match = re.search(r'Model Number:\s*(.*)', output)
if model_number_match is None:
logging.warning("Skipping device {} as it does not have a model number".format(device))
continue
details['model_number'] = model_number_match.group(1)
details['serial_number'] = re.search(r'Serial Number:\s*(.*)', output).group(1)
details['firmware_revision'] = re.search(r'Firmware Revision:\s*(.*)', output).group(1)
details['transport'] = re.search(r'Transport:\s*(.*)', output).group(1)
details['checksum'] = re.search(r'Checksum:\s*(.*)', output).group(1)
details['buffer_size'] = re.search(r'cache/buffer size\s*=\s*(.*)', output).group(1)
details['form_factor'] = re.search(r'Form Factor:\s*(.*)', output).group(1)
try:
details['form_factor'] = re.search(r'Form Factor:\s*(.*)', output).group(1)
except AttributeError:
details['form_factor'] = "N/A"
devices[device] = details
@ -166,7 +174,7 @@ class ServerData:
# 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}")
print("Failed to get nvme id-ctrl output for {}: {}".format(device_path, e))
continue
# Split the output into lines
@ -212,7 +220,10 @@ class ServerData:
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 thread_count:
cpu_count += thread_count
else:
cpu_count += core_count
if cpu_count == 0:
with open('/proc/cpuinfo', 'r') as f:
cpuinfo = f.read()
@ -233,16 +244,77 @@ class ServerData:
logging.error("Failed to get public IP: {}".format(e))
return '127.0.0.1'
def get_os(self):
os_id = 27
logging.info("OS ID: {}".format(os_id))
return os_id
def get_os_release_info(self):
os_release_info = {}
# reading from /etc/os-release
try:
with open('/etc/os-release') as f:
lines = f.read().splitlines()
os_release_info = {line.split('=')[0]: line.split('=')[1].strip('"') for line in lines if '=' in line}
except FileNotFoundError:
logging.warning("/etc/os-release not found, trying /etc/VERSION")
except Exception as e:
logging.error("Failed to read /etc/os-release: {}".format(e))
return {}
def create_post_data(self):
# If /etc/os-release is not found or empty, fallback to /etc/VERSION
if not os_release_info:
try:
with open('/etc/VERSION') as f:
lines = f.read().splitlines()
for line in lines:
if '=' in line:
key, value = line.split('=', 1)
os_release_info[key.strip()] = value.strip()
except Exception as e:
logging.error("Failed to read /etc/VERSION: {}".format(e))
return {}
return os_release_info
def get_os_id(self, os_list):
os_info = self.get_os_release_info()
if not os_info:
logging.error("No OS release info found.")
for os_entry in os_list:
if os_entry['name'].lower() in ["other", "custom"]:
return os_entry['id']
return 1
if 'ID' in os_info and 'VERSION_ID' in os_info:
current_os = (os_info.get('ID', 'Unknown') + " " + os_info.get('VERSION_ID', '').strip()).strip()
else:
current_os = (os_info.get('os_name', 'Unknown') +" "+ os_info.get('productversion', '').strip()).strip().lower()
for os_entry in os_list:
if current_os in os_entry['name'].lower():
return os_entry['id']
# Fallback checks for common OS names if full name doesn't match
for os_entry in os_list:
if 'ubuntu' in current_os and 'ubuntu' in os_entry['name'].lower():
return os_entry['id']
elif 'centos' in current_os and 'centos' in os_entry['name'].lower():
return os_entry['id']
elif 'fedora' in current_os and 'fedora' in os_entry['name'].lower():
return os_entry['id']
# Default to 'other' or 'custom' if no match found
for os_entry in os_list:
if os_entry['name'].lower() in ["other", "custom"]:
return os_entry['id']
return 1
def create_post_data(self, os_list):
ram, disk = self.get_ram_and_disk()
post_data = {
"server_type": 1,
"os_id": self.get_os(),
"os_id": self.get_os_id(os_list),
"provider_id": 10,
"location_id": 15,
"ssh_port": 22,
@ -290,8 +362,14 @@ class ServerData:
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])
try:
total_width = int(ram.get('Total Width', "0").split()[0])
except ValueError:
total_width = 0
try:
data_width = int(ram.get('Data Width', "0").split()[0])
except ValueError:
data_width = 0
ecc = 'Yes' if total_width > data_width else 'No'
serial_number = ram.get('Serial Number', 'Unknown')
ram_type = ram.get('Type', 'Unknown')
@ -404,6 +482,11 @@ class ServerManager:
return self.update_note(note_data, server_id)
else:
return self.create_note(note_data)
def get_os_list(self):
os_list = self.send_request('GET', '/api/os')
logging.info("OS list fetched successfully") if os_list else logging.error("Failed to fetch OS list")
return os_list or []
def validate_env_vars():
api_key = os.getenv('AGENT_API')
@ -419,14 +502,17 @@ def main():
host, api_key = validate_env_vars()
server_data = ServerData()
post_data = server_data.create_post_data()
server_manager = ServerManager(host, api_key)
server_data = ServerData()
os_list = server_manager.get_os_list()
post_data = server_data.create_post_data(os_list)
server_id = server_manager.upsert_server(post_data)
logging.info('Server id: {}'.format(server_id))
note_data = server_data.create_note_data()
server_manager.upsert_note(note_data, server_id)