#!/usr/bin/env python # # Authors: # rafael@E-MC2.NET / https://e-mc2.net/ # # Copyright (c) 2018 USIT-University of Oslo # # zabbix_rabbitmq_autodiscovery.py is free software: you can # redistribute it and/or modify it under the terms of the GNU General # Public License as published by the Free Software Foundation, either # version 3 of the License, or (at your option) any later version. # # zabbix_rabbitmq_autodiscovery.py is distributed in the hope that it # will be useful, but WITHOUT ANY WARRANTY; without even the implied # warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. # See the GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with sms_send. If not, see . # import requests import json import sys import os import socket domain = ".uio.no" auth_file = "/var/lib/zabbix/.rabbitmq_auth" # ############################################ # get_auth_data() # ############################################ def get_auth_data(): try: username = "none" password = "none" if os.path.isfile(auth_file): with open(auth_file, 'r') as f: for line in f: (username, password) = line.split('::') password = password.replace('\n', '') return username,password except Exception as e: raise Exception("[ERROR]: %s\n" % e) # ############################################ # get_rabbitmq_queues_data() # ############################################ def get_rabbitmq_data(component): """ Get rabbitmq queues data """ try: (username,password) = get_auth_data() request_data = requests.get("http://127.0.0.1:15672/api/" + component ,auth=(username,password)) if request_data.status_code != 200: raise Exception("[ERROR]: Problems connecting to RabbiMQ API\n") data = request_data.json() return data except Exception as e: raise Exception("[ERROR]: %s\n" % e) # ############################################ # generate_queues_zabbix_autodiscovery() # ############################################ def generate_queues_zabbix_autodiscovery(data): try: queues_list =[] hostname = socket.gethostname().replace(domain,'') for queues in data: if hostname in queues['node']: queue = {"{#QUEUE}": queues['name'],"{#VHOST}": queues['vhost']} queues_list.append(queue) result = {"data":queues_list} print json.dumps(result,sort_keys=True,indent=2) except Exception as e: raise Exception("[ERROR]: %s\n" % e) if __name__ == '__main__': try: if len(sys.argv) ==2: component = sys.argv[1].lower() else: raise Exception("[ERROR]: No parameters defined. Valid parameters [queues]\n") if component == "queues": data = get_rabbitmq_data(component) generate_queues_zabbix_autodiscovery(data) else: raise Exception("[ERROR]: Wrong parameter. Valid parameters [queues]\n") except Exception, e: print e sys.exit(1)