]> git.uio.no Git - uio-zabbix.git/blame - zabbix_rabbitmq_stats.py
Explicit python shebang
[uio-zabbix.git] / zabbix_rabbitmq_stats.py
CommitLineData
ad51e17e 1#!/usr/bin/python2
29781f38
CMB
2
3from datetime import datetime
4
5import argparse
6import subprocess
7import time
8import json
9import sys
10import shlex
11
12
13
14# Generic function to run a command, removes top item from output as this is always either 'listing vhosts', 'listing queue' etc
15def get_command(command):
16
17 command = shlex.split(command)
18
19 proc = subprocess.Popen(command,stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=False)
20 output = proc.communicate()
21
22 # Output[0] comes as a tuple so items are first split at \n before first and last items are removed because first items descripes the output and last item is and empty item
23 out = output[0].split('\n')[1:-1]
24
25 return out
26
27
28
29# Returns a list of rabbitmq virtual hosts
30def get_vhosts():
31
32 command = '/usr/sbin/rabbitmqctl list_vhosts'
33 vhosts = get_command(command)
34
35 return vhosts
36
37
38
39# Generates autodiscovery json output for virtualhost and its queues
40def get_queues():
41
42 vhosts = get_vhosts()
43
44 json_list = []
45
46 for vhost in vhosts:
47
48 command = '/usr/sbin/rabbitmqctl list_queues -p ' + vhost + ' name'
49 queues = get_command(command)
50
51 # only if queues are not empty
52 if queues:
53
54 for queue in queues:
55
56 inst = {}
57 inst = {"{#VHOST}":vhost, "{#QUEUE}":queue}
58 json_list.append(inst)
59
60 result = {"data":json_list}
61 print json.dumps(result,sort_keys=True,indent=2)
62
63
64
65# Generates autodiscovery json output for virtualhost and its queues
66def get_exchanges():
67
68 vhosts = get_vhosts()
69
70 json_list = []
71
72 for vhost in vhosts:
73
74 command = '/usr/sbin/rabbitmqctl list_exchanges -p ' + vhost + ' name'
75
76 exchanges = get_command(command)
77
78 # default and empty exchanges should be ignored
79 default_and_empty_exchanges = ['amq.direct', 'amq.fanout', 'amq.match', 'amq.topic', 'amq.headers', 'amq.rabbitmq.trace', 'amq.rabbitmq.log', '']
80
81 # only if exchanges are not empty
82 if exchanges:
83
84 for exchange in exchanges:
85
86 # Ensures that non of the exchanges from default_and_empty_exchanges are included
87 if all(x != exchange for x in default_and_empty_exchanges):
88
89 inst = {}
90 inst = {"{#VHOST}":vhost, "{#EXCHANGE}":exchange}
91 json_list.append(inst)
92
93 result = {"data":json_list}
94 print json.dumps(result,sort_keys=True,indent=2)
95
96
97
98if __name__ == '__main__':
99
100 try:
101
102 parser= argparse.ArgumentParser()
103
104 parser.add_argument('-lh', '--list_vhosts', action='store_true', help='lists virtual hosts')
105 parser.add_argument('-lq', '--list_queues', action='store_true', help='lists virtual hosts and their queues')
106 parser.add_argument('-le', '--list_exchanges', action='store_true', help='lists virtual hosts and their exchanges')
107
108 args = parser.parse_args()
109
110 if args.list_vhosts:
111 vhosts = get_vhosts()
112 for vhost in vhosts:
113 print vhost
114
115 elif args.list_queues:
116 get_queues()
117
118 elif args.list_exchanges:
119 get_exchanges()
120
121 else:
122 parser.print_help()
123 sys.exit(1)
124
125 except Exception as e:
126 print e
127