131 lines
4.0 KiB
Python
131 lines
4.0 KiB
Python
import datetime
|
|
import os
|
|
import sqlite3
|
|
|
|
def register_success(name, db):
|
|
cur = db.cursor()
|
|
cur.execute('''
|
|
DELETE FROM alert
|
|
WHERE failure IN (
|
|
SELECT c.id
|
|
FROM failure c INNER JOIN service s
|
|
ON c.service=s.id
|
|
WHERE s.name=?
|
|
);
|
|
''', (name,))
|
|
cur.execute('''
|
|
DELETE FROM failure
|
|
WHERE service IN (
|
|
SELECT id FROM service WHERE name=?
|
|
);
|
|
''', (name,))
|
|
cur.execute('''
|
|
DELETE FROM service WHERE name=?;
|
|
''', (name,))
|
|
db.commit()
|
|
|
|
def register_failure(name, alert_method, db):
|
|
cur = db.cursor()
|
|
cur.execute('INSERT OR IGNORE INTO service(name) VALUES (?);', (name,))
|
|
cur.execute('SELECT id FROM service WHERE name=?;', (name,))
|
|
(service_id,) = cur.fetchone()
|
|
cur.execute(''' INSERT INTO failure
|
|
( service, service_result, exit_code, exit_status, invocation_id, timestamp)
|
|
VALUES
|
|
( ?, ?, ?, ?, ?, ?)
|
|
RETURNING id;
|
|
''', (
|
|
service_id,
|
|
os.environ['MONITOR_SERVICE_RESULT'],
|
|
os.environ['MONITOR_EXIT_CODE'],
|
|
os.environ['MONITOR_EXIT_STATUS'],
|
|
os.environ['MONITOR_INVOCATION_ID'],
|
|
datetime.datetime.now()
|
|
))
|
|
(failure_id,) = cur.fetchone()
|
|
if alert_method:
|
|
cur.execute(''' INSERT INTO alert
|
|
(failure, timestamp, method)
|
|
VALUES
|
|
(?, ?, ?);
|
|
''', (
|
|
failure_id,
|
|
datetime.datetime.now(),
|
|
alert_method
|
|
))
|
|
db.commit()
|
|
|
|
|
|
|
|
def get_failures(name, db):
|
|
cur = db.cursor()
|
|
cur.execute('''SELECT
|
|
failure.id, service_result, exit_code, exit_status, invocation_id, timestamp
|
|
FROM failure
|
|
INNER JOIN service ON
|
|
failure.service=service.id
|
|
WHERE service.name=?;
|
|
''', (name,))
|
|
|
|
failures=[]
|
|
for f in cur.fetchall():
|
|
cur.execute('SELECT method FROM alert where failure=?', (f[0],))
|
|
alert_method = cur.fetchone()
|
|
if alert_method:
|
|
alert_method = alert_method[0]
|
|
failures.append({
|
|
'service_result': f[1],
|
|
'exit_code': f[2],
|
|
'exit_status': f[3],
|
|
'invocation_id': f[4],
|
|
'timestamp': f[5],
|
|
'alert_method': alert_method
|
|
})
|
|
db.commit()
|
|
|
|
failures.sort(key=lambda x: x['timestamp'])
|
|
return failures
|
|
|
|
|
|
def init(path):
|
|
con = sqlite3.connect(path, detect_types=sqlite3.PARSE_DECLTYPES | sqlite3.PARSE_COLNAMES)
|
|
cur = con.cursor()
|
|
cur.execute('''
|
|
CREATE TABLE IF NOT EXISTS
|
|
service (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
name TEXT UNIQUE
|
|
);
|
|
''')
|
|
cur.execute('''
|
|
CREATE TABLE IF NOT EXISTS
|
|
failure (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
service INTEGER,
|
|
service_result TEXT,
|
|
exit_code TEXT,
|
|
exit_status INTEGER,
|
|
invocation_id TEXT,
|
|
timestamp timestamp,
|
|
|
|
FOREIGN KEY(service) REFERENCES service(id)
|
|
);
|
|
''')
|
|
cur.execute('''
|
|
CREATE TABLE IF NOT EXISTS
|
|
alert (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
failure INTEGER,
|
|
timestamp timestamp,
|
|
method TEXT,
|
|
|
|
FOREIGN KEY(failure) REFERENCES failure(id)
|
|
);
|
|
''')
|
|
con.commit();
|
|
return con
|
|
|
|
def close(con):
|
|
con.close()
|
|
|