forked from Sysdeploy/idlers-agent
Compare commits
3 Commits
c0d919ce2c
...
217e433f76
Author | SHA1 | Date | |
---|---|---|---|
217e433f76 | |||
bd9a848737 | |||
7ab3d18ae9 |
59
agent.py
59
agent.py
|
@ -28,19 +28,40 @@ class ServerData:
|
|||
logging.basicConfig(level=logging.INFO)
|
||||
|
||||
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'):
|
||||
try:
|
||||
with open(f'/sys/block/{device}/size', 'r') as f:
|
||||
size = int(f.read().strip())
|
||||
disk += size
|
||||
except Exception as e:
|
||||
logging.error(f"Failed to read disk size for {device}: {e}")
|
||||
|
||||
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):
|
||||
with open('/proc/cpuinfo', 'r') as f:
|
||||
cpuinfo = f.read()
|
||||
cpu_count = cpuinfo.count('processor')
|
||||
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
|
||||
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
|
||||
|
||||
|
@ -121,6 +142,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)
|
||||
|
@ -169,5 +201,18 @@ def main():
|
|||
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)
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
main()
|
||||
|
|
Loading…
Reference in New Issue
Block a user