voeg post data toe aan database, host Nonetype afhandeling met try except
This commit is contained in:
parent
c1771b6d18
commit
fbff17fb82
|
@ -56,7 +56,7 @@ class Mydb:
|
|||
self.cur.execute(''' SELECT entryID FROM hosts WHERE hostname=('%s') ''' % hostname)
|
||||
return self.cur.fetchone()[0]
|
||||
|
||||
def set_hoststats(self, hostname, cpu_usage, memory_used, troughput_up, troughput_down):
|
||||
def set_hoststats(self, hostname, cpu_usage, memory_used, troughput_up, troughput_down, datetime):
|
||||
"""
|
||||
Voegt dynamische data entry toe in de stats tabel met de opgegeven variabelen. Vraagt trouwens ook de host PK op
|
||||
voordat de query wordt uitgevoerd.
|
||||
|
@ -65,11 +65,12 @@ class Mydb:
|
|||
:param memory_used: str
|
||||
:param troughput_up: str
|
||||
:param troughput_down: str
|
||||
:param datetime: str
|
||||
:return: None
|
||||
"""
|
||||
self.cur.execute(''' INSERT INTO stats (host, cpu_usage, memory_used, troughput_up, troughput_down) VALUES \
|
||||
('%s', '%s', '%s', '%s', '%s')''' %
|
||||
(self.get_hostdbid(hostname), cpu_usage, memory_used, troughput_up, troughput_down))
|
||||
self.cur.execute(''' INSERT INTO stats (host, cpu_usage, memory_used, troughput_up, troughput_down, datetime) \
|
||||
VALUES ('%s', '%s', '%s', '%s', '%s', '%s')''' %
|
||||
(self.get_hostdbid(hostname), cpu_usage, memory_used, troughput_up, troughput_down, datetime))
|
||||
self.conn.commit()
|
||||
|
||||
def get_latesthostdata(self, hostname):
|
||||
|
@ -135,7 +136,6 @@ def index():
|
|||
@app.route('/host/<hostname>')
|
||||
def host_info(hostname):
|
||||
data = db.get_latesthostdata(hostname)
|
||||
print(data)
|
||||
return render_template('host.html', hostname=hostname, data=data)
|
||||
|
||||
|
||||
|
@ -143,6 +143,18 @@ def host_info(hostname):
|
|||
@app.route('/api/v1/host/post', methods=['POST'])
|
||||
def host_post():
|
||||
data = request.get_json() # sla json data op in dict
|
||||
|
||||
try:
|
||||
db.get_hostdbid(data['static']['hostname']) # vraag hostname PK op, als host niet bestaat is dit None
|
||||
except TypeError:
|
||||
db.set_addhost(data['static']['hostname'], data['static']['os_release'], data['static']['os_type'],
|
||||
data['proc']['ram_usage']['total']) # voeg host toe aan hosts tabel
|
||||
|
||||
db.set_hoststats(data['static']['hostname'], data['proc']['cpu_usage'],
|
||||
data['proc']['ram_usage']['used'], data['proc']['network_troughput']['bytes_upload_per_second'],
|
||||
data['proc']['network_troughput']['bytes_download_per_second'],
|
||||
data['time'])
|
||||
|
||||
return '{"status": 200}', 200 # return een json dict met waarde status 200
|
||||
|
||||
|
||||
|
|
Reference in New Issue