DAS_2024_1/minhasapov_ruslan_lab_4/labwork/publisher.py
2024-11-18 01:14:26 +04:00

28 lines
876 B
Python

import pika
import time
import random
def generage_message_metrics():
return f" | CPU: {random.randint(0, 100)}% | RAM: {random.randint(0, 100)}% | CD: {random.randint(0, 100)}% |"
def produce():
connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost', port=5672))
channel = connection.channel()
channel.exchange_declare(exchange='server_monitoring', exchange_type='fanout')
messages = [
"Server | ID: 1",
"Server | ID: 2",
"Server | ID: 3",
]
while True:
for message in messages:
message_with_metric = message + generage_message_metrics()
channel.basic_publish(exchange='server_monitoring', routing_key='', body=message_with_metric)
print(f"[x] Sent: {message_with_metric}")
time.sleep(1)
if __name__ == "__main__":
produce()