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:
parent
839c36bbf7
commit
1e57d30597
1 changed files with 27 additions and 0 deletions
27
log.py
27
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)))
|
Loading…
Reference in a new issue