some cosmethics and save daily totals to db
This commit is contained in:
pare
10fdf11c32
commit
d46ede3bd5
S'han modificat 2 arxius amb 44 adicions i 26 eliminacions
|
@ -6,7 +6,6 @@ import sys
|
||||||
import time
|
import time
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
import requests
|
import requests
|
||||||
import threading
|
|
||||||
import json
|
import json
|
||||||
import psycopg2
|
import psycopg2
|
||||||
import pdb
|
import pdb
|
||||||
|
@ -25,25 +24,19 @@ def get_totals():
|
||||||
|
|
||||||
row = cur.fetchone()
|
row = cur.fetchone()
|
||||||
|
|
||||||
if row != None:
|
total_peers = row[0] if row != None else 0
|
||||||
|
|
||||||
total_peers = row[0]
|
|
||||||
|
|
||||||
cur.execute('select count(*) from peers where not alive')
|
cur.execute('select count(*) from peers where not alive')
|
||||||
|
|
||||||
row = cur.fetchone()
|
row = cur.fetchone()
|
||||||
|
|
||||||
if row != None:
|
not_alive_peers = row[0] if row != None else 0
|
||||||
|
|
||||||
not_alive_peers = row[0]
|
|
||||||
|
|
||||||
cur.execute('select count(*) from peers where alive')
|
cur.execute('select count(*) from peers where alive')
|
||||||
|
|
||||||
row = cur.fetchone()
|
row = cur.fetchone()
|
||||||
|
|
||||||
if row != None:
|
alive_peers = row[0] if row != None else 0
|
||||||
|
|
||||||
alive_peers = row[0]
|
|
||||||
|
|
||||||
cur.close()
|
cur.close()
|
||||||
|
|
||||||
|
@ -123,21 +116,9 @@ def write_server(server, alive, checked):
|
||||||
|
|
||||||
row = cur.fetchone()
|
row = cur.fetchone()
|
||||||
|
|
||||||
if row != None:
|
downs_before = row[0] if row != None else 0
|
||||||
|
|
||||||
downs_before = row[0]
|
downs_now = downs_before + 1 if not alive else 0
|
||||||
|
|
||||||
else:
|
|
||||||
|
|
||||||
downs_before = 0
|
|
||||||
|
|
||||||
if not alive:
|
|
||||||
|
|
||||||
downs_now = downs_before + 1
|
|
||||||
|
|
||||||
else:
|
|
||||||
|
|
||||||
downs_now = downs_before
|
|
||||||
|
|
||||||
if alive:
|
if alive:
|
||||||
|
|
||||||
|
@ -146,7 +127,37 @@ def write_server(server, alive, checked):
|
||||||
else:
|
else:
|
||||||
|
|
||||||
cur.execute(insert_sql, (server, updated_at, alive, checked, downs_now))
|
cur.execute(insert_sql, (server, updated_at, alive, checked, downs_now))
|
||||||
|
|
||||||
|
conn.commit()
|
||||||
|
|
||||||
|
cur.close()
|
||||||
|
|
||||||
|
except (Exception, psycopg2.DatabaseError) as error:
|
||||||
|
|
||||||
|
print(error)
|
||||||
|
|
||||||
|
finally:
|
||||||
|
|
||||||
|
if conn is not None:
|
||||||
|
|
||||||
|
conn.close()
|
||||||
|
|
||||||
|
def write_totals(alive, not_alive, total):
|
||||||
|
|
||||||
|
now = datetime.now()
|
||||||
|
|
||||||
|
insert_sql = "INSERT INTO totals(datetime, alive, dead, total) VALUES(%s,%s,%s,%s) ON CONFLICT DO NOTHING"
|
||||||
|
|
||||||
|
conn = None
|
||||||
|
|
||||||
|
try:
|
||||||
|
|
||||||
|
conn = psycopg2.connect(database = cleanserver_db, user = cleanserver_db_user, password = "", host = "/var/run/postgresql", port = "5432")
|
||||||
|
|
||||||
|
cur = conn.cursor()
|
||||||
|
|
||||||
|
cur.execute(insert_sql, (now, alive, not_alive, total))
|
||||||
|
|
||||||
conn.commit()
|
conn.commit()
|
||||||
|
|
||||||
cur.close()
|
cur.close()
|
||||||
|
@ -235,8 +246,8 @@ def dbconfig():
|
||||||
|
|
||||||
return (cleanserver_db, cleanserver_db_user)
|
return (cleanserver_db, cleanserver_db_user)
|
||||||
|
|
||||||
|
|
||||||
def get_parameter( parameter, file_path ):
|
def get_parameter( parameter, file_path ):
|
||||||
|
|
||||||
# Check if secrets file exists
|
# Check if secrets file exists
|
||||||
if not os.path.isfile(file_path):
|
if not os.path.isfile(file_path):
|
||||||
print("File %s not found, exiting."%file_path)
|
print("File %s not found, exiting."%file_path)
|
||||||
|
@ -309,5 +320,7 @@ if __name__ == '__main__':
|
||||||
print(f'Alive servers: {alive_peers}')
|
print(f'Alive servers: {alive_peers}')
|
||||||
print(f'Not Alive servers: {not_alive_peers}')
|
print(f'Not Alive servers: {not_alive_peers}')
|
||||||
|
|
||||||
|
write_totals(alive_peers, not_alive_peers, total_peers)
|
||||||
|
|
||||||
exec_time = str(round((time.time() - start_time), 2))
|
exec_time = str(round((time.time() - start_time), 2))
|
||||||
print(f'Execution time: {exec_time} seconds')
|
print(f'Execution time: {exec_time} seconds')
|
||||||
|
|
|
@ -195,4 +195,9 @@ if __name__ == '__main__':
|
||||||
sql = "create table "+table+" (server varchar(200) PRIMARY KEY, updated_at timestamptz, saved_at timestamptz, alive boolean, checked boolean, downs int)"
|
sql = "create table "+table+" (server varchar(200) PRIMARY KEY, updated_at timestamptz, saved_at timestamptz, alive boolean, checked boolean, downs int)"
|
||||||
create_table(db, db_user, table, sql)
|
create_table(db, db_user, table, sql)
|
||||||
|
|
||||||
|
table = "totals"
|
||||||
|
sql = "create table "+table+" (datetime timestamptz PRIMARY KEY, alive int, dead int, total int)"
|
||||||
|
create_table(db, db_user, table, sql)
|
||||||
|
|
||||||
|
|
||||||
print(f'Done!\nNow you can run setup.py\n')
|
print(f'Done!\nNow you can run setup.py\n')
|
||||||
|
|
Loading…
Referencia en una nova incidència