Now submits URL logs and hostlogs
This commit is contained in:
parent
6644051c7b
commit
5bf9beb1bc
3 changed files with 59 additions and 11 deletions
|
@ -5,3 +5,8 @@ urlMonitoringPeriod = 30
|
||||||
urls = ["https://www.bootlesshacker.com"]
|
urls = ["https://www.bootlesshacker.com"]
|
||||||
urlTimeout = 10
|
urlTimeout = 10
|
||||||
maxWorkers = 4
|
maxWorkers = 4
|
||||||
|
forceNonPOSIXCPU = False # this will force the custom cpu monintor to run instead if set to True
|
||||||
|
sqlServer = ''
|
||||||
|
sqlDatabase = ''
|
||||||
|
sqlUsername = ''
|
||||||
|
sqlPassword = ''
|
||||||
|
|
52
log.py
52
log.py
|
@ -6,22 +6,58 @@ class logsManager:
|
||||||
def __init__(self, server, database, username, password):
|
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
|
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:
|
try:
|
||||||
|
|
||||||
conn = pyodbc.connect(self.conn_str)
|
conn = pyodbc.connect(self.conn_str)
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
|
|
||||||
cursor.execute("SELECT COUNT(*) FROM monutil_hosts WHERE hostname = ?", hostname)
|
cursor.execute("SELECT COUNT(*) FROM monutil_hosts WHERE hostname = ?", hostname)
|
||||||
|
|
||||||
if cursor.fetchone()[0] == 0:
|
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:
|
except pyodbc.Error as ex:
|
||||||
|
|
||||||
print("Error inserting data: {}".format(str(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
13
main.py
|
@ -34,7 +34,7 @@ import socket
|
||||||
from functools import partial
|
from functools import partial
|
||||||
from concurrent.futures import ThreadPoolExecutor, as_completed
|
from concurrent.futures import ThreadPoolExecutor, as_completed
|
||||||
from bs4 import BeautifulSoup
|
from bs4 import BeautifulSoup
|
||||||
from log import hostLogsManager
|
from log import logsManager
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
stop_event = threading.Event()
|
stop_event = threading.Event()
|
||||||
|
@ -102,6 +102,8 @@ def monitorUrls(stop_event):
|
||||||
timeDiff = endTime - startTime
|
timeDiff = endTime - startTime
|
||||||
|
|
||||||
print(timeDiff)
|
print(timeDiff)
|
||||||
|
|
||||||
|
logURLLog(socket.gethostname(), datetime.now(), baseUrl, timeDiff)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
urlFail = True
|
urlFail = True
|
||||||
|
@ -110,8 +112,13 @@ def monitorUrls(stop_event):
|
||||||
|
|
||||||
def logHostLog(hostname, logTime, cpu, memory):
|
def logHostLog(hostname, logTime, cpu, memory):
|
||||||
|
|
||||||
manager = hostLogsManager(config.sqlServer, config.sqlDatabase, config.sqlUsername, config.sqlPassword)
|
manager = logsManager(config.sqlServer, config.sqlDatabase, config.sqlUsername, config.sqlPassword)
|
||||||
manager.insert_host_log(hostname, logTime, cpu, memory)
|
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():
|
def main():
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue