1

voegt nu ook datum toe wanneer alle metrics zijn opgehaald

This commit is contained in:
Ventilaar 2021-10-27 15:05:55 +02:00
parent a3b34d69e3
commit e2ebb95c0b

View File

@ -3,6 +3,8 @@ import platform
import psutil
import time
import requests
import datetime
def load_config():
tree = ET.parse('config.xml') # parseer xml
@ -16,23 +18,29 @@ def load_config():
# return de settings in een dict
return {'master_address':a, 'master_port':b, 'agent_name_override':c}
def get_hostname(override):
if override == None:
return platform.node()
else:
return override
def get_os_release():
return platform.release()
def get_os_type():
return platform.system()
def get_cpu_usage():
return psutil.cpu_percent(1)
def get_ram_usage():
return {'total':psutil.virtual_memory()[0], 'used':psutil.virtual_memory()[3]}
return {'total': psutil.virtual_memory()[0], 'used': psutil.virtual_memory()[3]}
def get_network_troughput():
"""
@ -47,20 +55,22 @@ def get_network_troughput():
downs = post[1] - pre[1] # totaal bytes per seconde gekregen berekenen
return {'bytes_upload_per_second': ups, 'bytes_download_per_second': downs}
def post_json(data, ma, mp):
r = requests.post(f'http://{ma}:{mp}/api/v1/host/post', json=data)
def post_json(blob, ma, mp):
r = requests.post(f'http://{ma}:{mp}/api/v1/host/post', json=blob)
if r.status_code != 200:
print(f'Error posting, got HTTP status code {r.status_code}, exiting with code 1!')
exit(1)
else:
return
if __name__ == '__main__':
config = load_config() # sla de dict op in een variabele
data = {} # hoofd dict wat omgezet gaat worden naar json
static = {} # static dict waar waardes in opgeslagen worden die (meestal) statisch zijn
proc = {}
data = dict() # hoofd dict wat omgezet gaat worden naar json
static = dict() # static dict waar waardes in opgeslagen worden die (meestal) statisch zijn
proc = dict()
static['hostname'] = get_hostname(config['agent_name_override'])
static['os_release'] = get_os_release()
@ -72,6 +82,7 @@ if __name__ == '__main__':
data['static'] = static
data['proc'] = proc
data['time'] = datetime.datetime.utcnow().replace(microsecond=0).isoformat()
print(data)
post_json(data, config['master_address'], config['master_port'])
post_json(data, config['master_address'], config['master_port'])