From 4e959749fc399c3c88284cb86d8e19dc8b09539e Mon Sep 17 00:00:00 2001 From: Thomas Williams Date: Sat, 6 Jul 2024 13:31:26 +0100 Subject: [PATCH] Framework in place but lots more to do for this issue. --- main.py | 13 +++++++++++-- rabbitmq.py | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 rabbitmq.py diff --git a/main.py b/main.py index 3380873..aaeceb0 100755 --- a/main.py +++ b/main.py @@ -37,6 +37,11 @@ from bs4 import BeautifulSoup from log import logsManager from datetime import datetime +if config.loggingMode == 'rabbitmq': + + import rabbitmq + rabbitmq = rabbitmq.rabbitMQClient('sampleca.crt','sample.crt','sample.key','','5671','monutil') + stop_event = threading.Event() nonPOSIXCPULoads = [] lock = threading.Lock() @@ -173,14 +178,18 @@ def monitorUrls(stop_event): def logHostLog(hostname, logTime, cpu, memory): - if not config.loggingMode == 'none': + if not config.loggingMode == 'none' and not config.loggingMode == 'rabbitmq': manager = logsManager(config.sqlServer, config.sqlDatabase, config.sqlUsername, config.sqlPassword) manager.insertHostLog(hostname, socket.gethostbyname(socket.gethostname()), logTime, cpu, memory) + if config.loggingMode == 'rabbitmq': + + rabbitmq.publish(hostname + '|' + str(logTime) + '|' + 'cpumem' + '|' + str(cpu) + '|' + str(memory)) + def logURLLog(hostname, logTime, url, responseTime): - if not config.loggingMode == 'none': + if not config.loggingMode == 'none' and not config.loggingMode == 'rabbitmq': manager = logsManager(config.sqlServer, config.sqlDatabase, config.sqlUsername, config.sqlPassword) manager.insertURLLog(hostname, socket.gethostbyname(socket.gethostname()), logTime, url, responseTime) diff --git a/rabbitmq.py b/rabbitmq.py new file mode 100644 index 0000000..a74cc2d --- /dev/null +++ b/rabbitmq.py @@ -0,0 +1,54 @@ +#!/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 pika +import ssl + +class rabbitMQClient: + + def __init__(self, ca, cacert, cakey, rabbitmqHost, rabbitmqPort, routingKey): + + self.ca = ca + self.cacert = cacert + self.cakey = cakey + self.rabbitmqHost = rabbitmqHost + self.rabbitmqPort = rabbitmqPort + self.routingKey = routingKey + + def publish(self, message): + + 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()) + + with pika.BlockingConnection(connParams) as conn: + + ch = conn.channel() + + ch.basic_publish(exchange='', routing_key=self.routingKey, body=message, properties=pika.BasicProperties(delivery_mode=pika.spec.PERSISTENT_DELIVERY_MODE))