Merge branch 'issue8'
This commit is contained in:
commit
40f7d772fb
3 changed files with 82 additions and 0 deletions
|
@ -6,3 +6,7 @@ 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 = ''
|
||||
|
|
63
log.py
63
log.py
|
@ -0,0 +1,63 @@
|
|||
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 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 (?, ?)", 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)
|
15
main.py
15
main.py
|
@ -30,9 +30,12 @@ import log
|
|||
import requests
|
||||
import threading
|
||||
import signal
|
||||
import socket
|
||||
from functools import partial
|
||||
from concurrent.futures import ThreadPoolExecutor, as_completed
|
||||
from bs4 import BeautifulSoup
|
||||
from log import logsManager
|
||||
from datetime import datetime
|
||||
|
||||
stop_event = threading.Event()
|
||||
nonPOSIXCPULoads = []
|
||||
|
@ -107,6 +110,7 @@ def monitorHost(stop_event):
|
|||
loadavg = round((load1/os.cpu_count()) * 100, 2)
|
||||
|
||||
memory = psutil.virtual_memory().percent
|
||||
logHostLog(socket.gethostname(), datetime.now(), loadavg, memory)
|
||||
|
||||
print("CPU %: " + str(loadavg))
|
||||
print("Memory %: " + str(memory))
|
||||
|
@ -146,12 +150,23 @@ def monitorUrls(stop_event):
|
|||
|
||||
print(baseUrl + " response time: " + str(timeDiff))
|
||||
print() # new line
|
||||
logURLLog(socket.gethostname(), datetime.now(), baseUrl, timeDiff)
|
||||
|
||||
else:
|
||||
urlFail = True
|
||||
|
||||
time.sleep(config.urlMonitoringPeriod)
|
||||
|
||||
def logHostLog(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():
|
||||
|
||||
signal.signal(signal.SIGTERM, signal_handler)
|
||||
|
|
Loading…
Reference in a new issue