Further changes for rabbitmq

This commit is contained in:
Thomas Williams 2024-07-06 14:40:04 +01:00
parent 33ede80613
commit 5990a88b26
Signed by: thomas
GPG key ID: EB8F975CF60BCBFF
4 changed files with 76 additions and 23 deletions

View file

@ -1,21 +0,0 @@
#!/usr/bin/python3
# See README before changing any of these options.
hostMonitoringPeriod = 1
urlMonitoringPeriod = 60
urls = ["https://one.one.one.one"]
urlTimeout = 10
maxWorkers = 4
forceNonPOSIXCPU = True
loggingMode = ''
sqlServer = ''
sqlDatabase = ''
sqlUsername = ''
sqlPassword = ''
logRetentionDays = 90
maximumSQLAttempts = 3
hostMonitorStartTime = "00:00:00"
hostMonitorEndTime = "23:59:59"
urlMonitorStartTime = "00:00:00"
urlMonitorEndTime = "23:59:59"

View file

@ -184,7 +184,7 @@ def logHostLog(hostname, logTime, cpu, memory):
if config.loggingMode == 'rabbitmq': if config.loggingMode == 'rabbitmq':
rabbitmq.publish(hostname + '|' + str(logTime) + '|' + 'cpumem' + '|' + str(cpu) + '|' + str(memory)) rabbitmq.publish(hostname + '|' + socket.gethostbyname(socket.gethostname()) + '|' + str(logTime) + '|' + 'cpumem' + '|' + str(cpu) + '|' + str(memory))
def logURLLog(hostname, logTime, url, responseTime): def logURLLog(hostname, logTime, url, responseTime):
@ -195,7 +195,7 @@ def logURLLog(hostname, logTime, url, responseTime):
if config.loggingMode == 'rabbitmq': if config.loggingMode == 'rabbitmq':
rabbitmq.publish(hostname + '|' + str(logTime) + '|' + 'url' + '|' + url + '|' + str(responseTime)) rabbitmq.publish(hostname + '|' + socket.gethostbyname(socket.gethostname()) + '|' + str(logTime) + '|' + 'url' + '|' + url + '|' + str(responseTime))
def main(): def main():

32
rabbitmq-consumer.py Executable file
View file

@ -0,0 +1,32 @@
#!/usr/bin/python3
# MIT License
# Copyright (c) 2024 Thomas Williams - https://git.server.wales/thomas
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import config
if config.loggingMode == 'rabbitmq':
import rabbitmq
rabbitmq = rabbitmq.rabbitMQClient(config.rabbitmqca,config.rabbitmqcacert,config.rabbitmqcakey,config.rabbitmqHost,config.rabbitmqPort,config.rabbitmqRoutingKey)
rabbitmq.retrieve()

View file

@ -25,6 +25,9 @@
import pika import pika
import ssl import ssl
import base64 import base64
import config
import log
import datetime
class rabbitMQClient: class rabbitMQClient:
@ -55,3 +58,42 @@ class rabbitMQClient:
ch = conn.channel() ch = conn.channel()
ch.basic_publish(exchange='', routing_key=self.routingKey, body=message, properties=pika.BasicProperties(delivery_mode=pika.spec.PERSISTENT_DELIVERY_MODE)) ch.basic_publish(exchange='', routing_key=self.routingKey, body=message, properties=pika.BasicProperties(delivery_mode=pika.spec.PERSISTENT_DELIVERY_MODE))
def retrieve(self):
def callback(ch, method, properties, body):
body = base64.b64decode(body)
body = body.decode('utf-8')
result = body.split('|')
if result[3] == 'cpumem':
result[2] = datetime.datetime.strptime(result[2], "%Y-%m-%d %H:%M:%S.%f")
manager = log.logsManager(config.sqlServer, config.sqlDatabase, config.sqlUsername, config.sqlPassword)
manager.insertHostLog(result[0], result[1], result[2], result[4], result[5])
ch.basic_ack(delivery_tag=method.delivery_tag)
if result[3] == 'url':
result[2] = datetime.datetime.strptime(result[2], "%Y-%m-%d %H:%M:%S.%f")
manager = log.logsManager(config.sqlServer, config.sqlDatabase, config.sqlUsername, config.sqlPassword)
manager.insertURLLog(result[0], result[1], result[2], result[4], result[5])
ch.basic_ack(delivery_tag=method.delivery_tag)
context = ssl.create_default_context(
cafile=self.ca)
context.verify_mode = ssl.CERT_REQUIRED
context.load_cert_chain(self.cacert, self.cakey)
sslOptions = pika.SSLOptions(context, self.rabbitmqHost)
connParams = pika.ConnectionParameters(host=self.rabbitmqHost,port=self.rabbitmqPort, ssl_options=sslOptions, credentials=pika.credentials.ExternalCredentials())
connection = pika.BlockingConnection(connParams)
channel = connection.channel()
channel.basic_consume(queue='monutil', on_message_callback=callback, auto_ack=False)
channel.start_consuming()