diff --git a/config.py b/config.py deleted file mode 100644 index b1a65d7..0000000 --- a/config.py +++ /dev/null @@ -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" diff --git a/main.py b/main.py index 15ce8f6..8d036d7 100755 --- a/main.py +++ b/main.py @@ -184,7 +184,7 @@ def logHostLog(hostname, logTime, cpu, memory): 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): @@ -195,7 +195,7 @@ def logURLLog(hostname, logTime, url, responseTime): 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(): diff --git a/rabbitmq-consumer.py b/rabbitmq-consumer.py new file mode 100755 index 0000000..f5c7961 --- /dev/null +++ b/rabbitmq-consumer.py @@ -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() diff --git a/rabbitmq.py b/rabbitmq.py index f8fe63a..792c36c 100644 --- a/rabbitmq.py +++ b/rabbitmq.py @@ -25,6 +25,9 @@ import pika import ssl import base64 +import config +import log +import datetime class rabbitMQClient: @@ -55,3 +58,42 @@ class rabbitMQClient: ch = conn.channel() 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()