monutil/rabbitmq.py

107 lines
4.1 KiB
Python
Raw Normal View History

#!/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
2024-07-06 12:51:00 +00:00
import base64
2024-07-06 13:40:04 +00:00
import config
import log
import datetime
2024-07-08 13:23:16 +00:00
from retrying import retry
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):
2024-07-08 13:23:16 +00:00
@retry(stop_max_attempt_number=3)
def publishMessage(conn):
ch = conn.channel()
ch.basic_publish(exchange='', routing_key=self.routingKey, body=message, properties=pika.BasicProperties(delivery_mode=pika.spec.PERSISTENT_DELIVERY_MODE))
2024-07-06 12:51:00 +00:00
message = base64.b64encode(message.encode('utf-8'))
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:
2024-07-08 13:23:16 +00:00
publishMessage(conn)
2024-07-06 13:40:04 +00:00
2024-07-08 13:23:16 +00:00
@retry(stop_max_attempt_number=3)
2024-07-06 13:40:04 +00:00
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()