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...

This commit is contained in:
Thomas Williams 2024-07-02 20:42:34 +01:00
parent 839c36bbf7
commit 1e57d30597
Signed by: thomas
GPG key ID: EB8F975CF60BCBFF

27
log.py
View file

@ -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)))