Now submits URL logs and hostlogs

This commit is contained in:
Thomas Williams 2024-07-03 21:23:36 +01:00
parent 6644051c7b
commit 5bf9beb1bc
Signed by: thomas
GPG key ID: EB8F975CF60BCBFF
3 changed files with 59 additions and 11 deletions

View file

@ -5,3 +5,8 @@ urlMonitoringPeriod = 30
urls = ["https://www.bootlesshacker.com"]
urlTimeout = 10
maxWorkers = 4
forceNonPOSIXCPU = False # this will force the custom cpu monintor to run instead if set to True
sqlServer = ''
sqlDatabase = ''
sqlUsername = ''
sqlPassword = ''

44
log.py
View file

@ -6,22 +6,58 @@ 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):
def insertHost(self, hostname, ipAddress):
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_hosts (hostname, ipAddress) VALUES (?, ?)", hostname, ipAddress)
conn.commit()
else:
cursor.execute("UPDATE monutil_hosts SET ipAddress = ? WHERE hostname = ?", ipAddress, hostname)
except pyodbc.Error as ex:
print("Error inserting data: {}".format(str(ex)))
def insertHostLog(self, hostname, ipAddress, log_time, cpu, memory):
try:
self.insertHost(hostname, ipAddress)
conn = pyodbc.connect(self.conn_str)
cursor = conn.cursor()
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)))
def insertURLLog(self, hostname, ipAddress, log_time, url, responseTime):
try:
self.insertHost(hostname, ipAddress)
conn = pyodbc.connect(self.conn_str)
cursor = conn.cursor()
cursor.execute("INSERT INTO monutil_urlLogs (hostname, url, logTime, responseTime) VALUES (?, ?, ?, ?)", hostname, url, log_time, responseTime)
conn.commit()
conn.close()
except pyodbc.Error as ex:
print("Error inserting data into monutil_urlLogs:", ex)

13
main.py
View file

@ -34,7 +34,7 @@ import socket
from functools import partial
from concurrent.futures import ThreadPoolExecutor, as_completed
from bs4 import BeautifulSoup
from log import hostLogsManager
from log import logsManager
from datetime import datetime
stop_event = threading.Event()
@ -103,6 +103,8 @@ def monitorUrls(stop_event):
print(timeDiff)
logURLLog(socket.gethostname(), datetime.now(), baseUrl, timeDiff)
else:
urlFail = True
@ -110,8 +112,13 @@ def monitorUrls(stop_event):
def logHostLog(hostname, logTime, cpu, memory):
manager = hostLogsManager(config.sqlServer, config.sqlDatabase, config.sqlUsername, config.sqlPassword)
manager.insert_host_log(hostname, logTime, cpu, memory)
manager = logsManager(config.sqlServer, config.sqlDatabase, config.sqlUsername, config.sqlPassword)
manager.insertHostLog(hostname, socket.gethostbyname(socket.gethostname()), logTime, cpu, memory)
def logURLLog(hostname, logTime, url, responseTime):
manager = logsManager(config.sqlServer, config.sqlDatabase, config.sqlUsername, config.sqlPassword)
manager.insertURLLog(hostname, socket.gethostbyname(socket.gethostname()), logTime, url, responseTime)
def main():