prometheus rabbitmq monitoring

#!/bin/python

# THIS PYTHON FLASK SCRIPT GENERATE HASH, SEND IT TO QUEUE AND CHECK IT

import puka,time,random,time,re,requests,json,sys
from random import randint
from flask import Flask
from datetime import datetime
from requests.auth import HTTPBasicAuth

app = Flask(__name__)

server_address = "amqp://rabbit.alexlinux.lan/"
queue_name = 'monitoring-prometheus'

randnum_string = "empty"
status = "1"

def send_msg():
    client = puka.Client(server_address)

    promise = client.connect()
    client.wait(promise)

    promise = client.queue_declare(queue=queue_name)
    client.wait(promise)

    randnum = randint(1000000, 9000000)
    global randnum_string
    randnum_string = str(randnum)

    promise = client.basic_publish(exchange='', routing_key=queue_name, body=randnum_string)
    client.wait(promise)

    promise = client.queue_declare(queue=queue_name, passive=True)

    promise = client.close()
    client.wait(promise)

def rec_msg():
    client = puka.Client(server_address)
    promise = client.connect()
    client.wait(promise)

    promise = client.queue_declare(queue=queue_name)
    client.wait(promise)

    consume_promise = client.basic_consume(queue=queue_name, prefetch_count=1)
    try:
        result = client.wait(consume_promise,timeout=1, raise_errors=False)
        global randnum_string
        global status
        if (result['body'] == randnum_string):
            status = "0"
        else:
            status = "1"
        client.basic_ack(result)
    except:
        pass

    promise = client.close()
    client.wait(promise)

@app.route('/')
def send_and_receive():
    global status
    send_msg()
    time.sleep(1)
    rec_msg()
    if (status != "0"):
        rec_msg()
    return "status " + status + "\n"

if __name__ == '__main__':
    app.run(port=30900)

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>