diff --git a/config.py b/config.py index 3fc66e9..18c5922 100644 --- a/config.py +++ b/config.py @@ -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 = '' diff --git a/log.py b/log.py index 925af71..2f8af2b 100644 --- a/log.py +++ b/log.py @@ -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) - 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 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) diff --git a/main.py b/main.py index 6318a92..342b266 100755 --- a/main.py +++ b/main.py @@ -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() @@ -102,6 +102,8 @@ def monitorUrls(stop_event): timeDiff = endTime - startTime 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():