Closses issue #19
This commit is contained in:
parent
a677ce90ba
commit
dca95f5780
4 changed files with 122 additions and 69 deletions
25
README.md
25
README.md
|
@ -5,15 +5,16 @@ Both CPU/RAM monitoring and URL monitoring can be set on their own monitoring pe
|
|||
|
||||
**Configuration options:**
|
||||
|
||||
- hostMonitoringPeriod - the delay in between the CPU and RAM usage being probed (defined in seconds).
|
||||
- urlMonitoringPeriod - the delay in between monitoring all of the URLs (defined in seconds).
|
||||
- urls - the list of URLs to monitor (e.g. ["url1", "url2"]).
|
||||
- urlTimeout - the delay before considering a URL to have timed out.
|
||||
- maxWorkers - the amount of threads to use when pulling URL resources. Do not set above the maximum number of threads on the host.
|
||||
- forceNonPOSIXCPU - For POSIX compatible systems, psutil.getloadavg() is executed which relies on os.getloadavg(). For Windows, this seemingly returns 0 (at least on the version executed during development). For Windows, a custom function has been built to obtain running CPU averages, but you can choose to use this function on POSIX systems by setting this variable to True.
|
||||
- loggingMode - Valid options: mssql, mariadb, rabbitmq, none.
|
||||
- sqlServer - the address of the SQL server which to write the data.
|
||||
- sqlDatabase - the database to write the data.
|
||||
- sqlUsername - the username used to authenticate to the SQL server.
|
||||
- sqlPassword - the password used to authenticate to the SQL server.
|
||||
- logRetentionDays - the maximum age logs should be kept.
|
||||
- **hostMonitoringPeriod** - the delay in between the CPU and RAM usage being probed (defined in seconds).
|
||||
- **urlMonitoringPeriod** - the delay in between monitoring all of the URLs (defined in seconds).
|
||||
- **urls** - the list of URLs to monitor (e.g. ["url1", "url2"]).
|
||||
- **urlTimeout** - the delay before considering a URL to have timed out.
|
||||
- **maxWorkers** - the amount of threads to use when pulling URL resources. Do not set above the maximum number of threads on the host.
|
||||
- **forceNonPOSIXCPU** - For POSIX compatible systems, psutil.getloadavg() is executed which relies on os.getloadavg(). For Windows, this seemingly returns 0 (at least on the version executed during development). For Windows, a custom function has been built to obtain running CPU averages, but you can choose to use this function on POSIX systems by setting this variable to True.
|
||||
- **loggingMode** - Valid options: mssql, mariadb, rabbitmq, none.
|
||||
- **sqlServer** - the address of the SQL server which to write the data.
|
||||
- **sqlDatabase** - the database to write the data.
|
||||
- **sqlUsername** - the username used to authenticate to the SQL server.
|
||||
- **sqlPassword** - the password used to authenticate to the SQL server.
|
||||
- **logRetentionDays** - the maximum age logs should be kept.
|
||||
- **maximumSQLAttempts** - the maximum number of attempts to try certain SQL operations
|
||||
|
|
|
@ -2,8 +2,8 @@
|
|||
|
||||
# See README before changing any of these options.
|
||||
|
||||
hostMonitoringPeriod = 1
|
||||
urlMonitoringPeriod = 10
|
||||
hostMonitoringPeriod = 15
|
||||
urlMonitoringPeriod = 60
|
||||
urls = ["https://1.1.1.1"]
|
||||
urlTimeout = 10
|
||||
maxWorkers = 4
|
||||
|
@ -13,4 +13,5 @@ sqlServer = ''
|
|||
sqlDatabase = ''
|
||||
sqlUsername = ''
|
||||
sqlPassword = ''
|
||||
logRetentionDays = '90'
|
||||
logRetentionDays = 90
|
||||
maximumSQLAttempts = 3
|
||||
|
|
64
log.py
64
log.py
|
@ -23,7 +23,8 @@
|
|||
# SOFTWARE.
|
||||
|
||||
import pyodbc
|
||||
from config import logRetentionDays
|
||||
import time
|
||||
from config import logRetentionDays, maximumSQLAttempts
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
class logsManager:
|
||||
|
@ -33,6 +34,10 @@ class logsManager:
|
|||
|
||||
def insertHost(self, hostname, ipAddress):
|
||||
|
||||
currentAttempts = 1
|
||||
|
||||
while currentAttempts <= maximumSQLAttempts:
|
||||
|
||||
try:
|
||||
|
||||
conn = pyodbc.connect(self.conn_str)
|
||||
|
@ -43,21 +48,36 @@ class logsManager:
|
|||
|
||||
cursor.execute("INSERT INTO monutil_hosts (hostname, ipAddress) VALUES (?, ?)", hostname, ipAddress)
|
||||
conn.commit()
|
||||
conn.close()
|
||||
break
|
||||
|
||||
else:
|
||||
|
||||
cursor.execute("UPDATE monutil_hosts SET ipAddress = ? WHERE hostname = ?", ipAddress, hostname)
|
||||
conn.close()
|
||||
break
|
||||
|
||||
except pyodbc.Error as ex:
|
||||
|
||||
print("Error inserting data: {}".format(str(ex)))
|
||||
currentAttempts += 1
|
||||
print("SQL Error: {}".format(str(ex)))
|
||||
|
||||
if not currentAttempts <= maximumSQLAttempts:
|
||||
raise
|
||||
|
||||
time.sleep(1)
|
||||
|
||||
|
||||
def insertHostLog(self, hostname, ipAddress, log_time, cpu, memory):
|
||||
|
||||
try:
|
||||
currentAttempts = 1
|
||||
|
||||
self.insertHost(hostname, ipAddress)
|
||||
self.deleteOldLogs("monutil_hostLogs", "logTime")
|
||||
self.deleteOldLogs("monutil_hostlogs", "logTime")
|
||||
|
||||
while currentAttempts <= maximumSQLAttempts:
|
||||
|
||||
try:
|
||||
|
||||
conn = pyodbc.connect(self.conn_str)
|
||||
cursor = conn.cursor()
|
||||
|
@ -65,32 +85,53 @@ class logsManager:
|
|||
cursor.execute("INSERT INTO monutil_hostlogs (hostname, logTime, cpu, memory) VALUES (?, ?, ?, ?)", hostname, log_time, cpu, memory)
|
||||
conn.commit()
|
||||
conn.close()
|
||||
break
|
||||
|
||||
except pyodbc.Error as ex:
|
||||
|
||||
currentAttempts += 1
|
||||
print("Error inserting data: {}".format(str(ex)))
|
||||
|
||||
if not currentAttempts <= maximumSQLAttempts:
|
||||
raise
|
||||
|
||||
time.sleep(1)
|
||||
|
||||
def insertURLLog(self, hostname, ipAddress, log_time, url, responseTime):
|
||||
|
||||
try:
|
||||
currentAttempts = 1
|
||||
|
||||
self.insertHost(hostname, ipAddress)
|
||||
self.deleteOldLogs("monutil_urlLogs", "logTime")
|
||||
self.deleteOldLogs("monutil_urllogs", "logTime")
|
||||
|
||||
while currentAttempts <= maximumSQLAttempts:
|
||||
|
||||
try:
|
||||
|
||||
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()
|
||||
break
|
||||
|
||||
except pyodbc.Error as ex:
|
||||
|
||||
currentAttempts += 1
|
||||
print("Error inserting data into monutil_urlLogs:", ex)
|
||||
|
||||
if not currentAttempts <= maximumSQLAttempts:
|
||||
raise
|
||||
|
||||
time.sleep(1)
|
||||
|
||||
def deleteOldLogs(self, tableName, logTimeColumn):
|
||||
|
||||
currentAttempts = 1
|
||||
|
||||
while currentAttempts <= maximumSQLAttempts:
|
||||
|
||||
try:
|
||||
|
||||
conn = pyodbc.connect(self.conn_str)
|
||||
|
@ -105,6 +146,15 @@ class logsManager:
|
|||
deleteQuery = f"DELETE FROM {tableName} WHERE {logTimeColumn} < ?"
|
||||
cursor.execute(deleteQuery, datetime.now() - timedelta(days=int(logRetentionDays)))
|
||||
conn.commit()
|
||||
|
||||
break
|
||||
|
||||
except pyodbc.Error as ex:
|
||||
|
||||
currentAttempts += 1
|
||||
print("Error deleting old logs: {}".format(str(ex)))
|
||||
|
||||
if not currentAttempts <= maximumSQLAttempts:
|
||||
raise
|
||||
|
||||
time.sleep(1)
|
||||
|
|
1
main.py
1
main.py
|
@ -160,6 +160,7 @@ def monitorUrls(stop_event):
|
|||
def logHostLog(hostname, logTime, cpu, memory):
|
||||
|
||||
if not config.loggingMode == 'none':
|
||||
|
||||
manager = logsManager(config.sqlServer, config.sqlDatabase, config.sqlUsername, config.sqlPassword)
|
||||
manager.insertHostLog(hostname, socket.gethostbyname(socket.gethostname()), logTime, cpu, memory)
|
||||
|
||||
|
|
Loading…
Reference in a new issue