From 1e57d3059736af24185c9616c4e5ed49634ca97d Mon Sep 17 00:00:00 2001 From: Thomas Williams Date: Tue, 2 Jul 2024 20:42:34 +0100 Subject: [PATCH] Some changes done to log CPU/RAM to MS SQL. More changes to be done to log URL monitors to MS SQL too, along with making a compatible MySQL/MariaDB equiv... --- log.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/log.py b/log.py index e69de29..925af71 100644 --- a/log.py +++ b/log.py @@ -0,0 +1,27 @@ +import pyodbc +from datetime import datetime + +class logsManager: + + def __init__(self, server, database, username, password): + self.conn_str = 'DRIVER={ODBC Driver 17 for SQL Server};SERVER=' + server + ';DATABASE=' + database + ';UID=' + username + ';PWD=' + password + + def insert_host_log(self, hostname, log_time, cpu, memory): + try: + conn = pyodbc.connect(self.conn_str) + cursor = conn.cursor() + + cursor.execute("SELECT COUNT(*) FROM monutil_hosts WHERE hostname = ?", hostname) + + if cursor.fetchone()[0] == 0: + + cursor.execute("INSERT INTO monutil_hosts (hostname, ipAddress) VALUES (?, '127.0.0.1')", hostname) + + cursor.execute("INSERT INTO monutil_hostlogs (hostname, logTime, cpu, memory) VALUES (?, ?, ?, ?)", hostname, log_time, cpu, memory) + conn.commit() + + conn.close() + + except pyodbc.Error as ex: + + print("Error inserting data: {}".format(str(ex)))