Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100755 175 lines (149 sloc) 5.932 kb
947af5f @Kami Add script for monitoring RabbitMQ.
Kami authored
1 #!/usr/bin/env python
2 #
3 # License: MIT
4 #
5 # Permission is hereby granted, free of charge, to any person obtaining a copy
6 # of this software and associated documentation files (the 'Software'), to deal
7 # in the Software without restriction, including without limitation the rights
8 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 # copies of the Software, and to permit persons to whom the Software is
10 # furnished to do so, subject to the following conditions:
11 #
12 # The above copyright notice and this permission notice shall be included in
13 # all copies or substantial portions of the Software.
14 #
15 # THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 # THE SOFTWARE.
22 #
23 # Cloudkick plugin for monitoring a RabbitMQ stats.
ca7d4e9 @Kami Add some docs.
Kami authored
24 #
25 # Example usage (arguments which you pass in to the plugin the Cloudkick
26 # dashboard):
27 #
28 # Monitor queue "bg_jobs" memory usage, number of consumers and number of
29 # messages:
30 #
31 # --action list_queues --queue bg_jobs --parameters memory,consumers,messages
32 #
33 # Monitor exchange "amqp.direct" type, durability and auto_delete value
34 #
35 # --action list_exchanges --exchange amqp.direct --parameters type,durable,auto_delete
947af5f @Kami Add script for monitoring RabbitMQ.
Kami authored
36
37 import re
38 import sys
39 import subprocess
40 import optparse
41
42 METRIC_TYPES = {
43 'list_queues': {
44 'name': 'string',
45 'durable': 'string',
46 'auto_delete': 'string',
47 'arguments': 'string',
48 'pid': 'int',
49 'owner_pid': 'int',
50 'messages_ready': 'int',
51 'messages_unacknowledged': 'int',
52 'messages': 'int',
53 'consumers': 'int',
54 'memory': 'int'
55 },
56
57 'list_exchanges': {
58 'name': 'string',
59 'type': 'string',
60 'durable': 'string',
61 'auto_delete': 'string',
62 'internal': 'string',
63 'argument': 'string'
64 }
65 }
66
67 def retrieve_stats(vhost, action, queue, exchange, parameters,
68 rabbitmqctl_path):
69 value = queue or exchange
70 command = [ rabbitmqctl_path, action, '-p', vhost ]
71 parameters = parameters.split(',')
72
73 parameters = [ p.lower() for p in parameters \
74 if p.lower() in METRIC_TYPES[action].keys() ]
75
76 command.extend( [ 'name' ] + parameters)
77 process1 = subprocess.Popen(command, stdout=subprocess.PIPE,
78 stderr=subprocess.STDOUT)
79 process2 = subprocess.Popen([ 'grep', value ], stdin=process1.stdout,
80 stdout=subprocess.PIPE,
81 stderr=subprocess.PIPE)
82 process1.stdout.close()
83 stdout, stderr = process2.communicate()
84
85 if stderr:
86 return None, stderr
87
88 stdout = stdout.split('\n')
89 stdout = stdout[0]
90
91 if not stdout:
92 return None, 'Empty output'
93
94 return parse_stats( [ 'name' ] + parameters, stdout), None
95
96 def parse_stats(parameters, data):
97 values = re.split('\s+', data)
98
99 stats = {}
100 for index, parameter in enumerate(parameters):
101 stats[parameter] = values[index]
102
103 return stats
104
105 def print_metrics(action, metrics):
106 for key, value in metrics.iteritems():
107 metric_type = METRIC_TYPES[action].get(key, None)
108
109 if not metric_type:
110 continue
111
112 print 'metric %s %s %s' % (key, metric_type, value)
113
114 if __name__ == '__main__':
115 parser = optparse.OptionParser()
116 parser.add_option('--path', action='store', dest='rabbitmqctl_path',
117 default='rabbitmqctl',
118 help='Path to the rabbitmqctl binary (optional)')
119 parser.add_option('--action', action='store', dest='action',
120 help='Action (list_queues or list_exchanges)')
121 parser.add_option('--vhost', action='store', dest='vhost', default='/',
122 help='Vhost (optional)')
123 parser.add_option('--queue', action='store', dest='queue',
124 help='Queue name')
125 parser.add_option('--exchange', action='store', dest='exchange',
126 help='Exchange name')
127 parser.add_option('--parameters', action='store', dest='parameters',
128 default='messages',
129 help='Comma separated list of parameters to retrieve (default = messages)')
43729b7 @ryandub Made --queue-length optional.
ryandub authored
130 parser.add_option('--queue-length', type='int', action='store', dest='length',
85c040f @ryandub Added ability to define max messages in queue and alert if exceeded.
ryandub authored
131 help='Max messages in the queue before alert')
947af5f @Kami Add script for monitoring RabbitMQ.
Kami authored
132
133 (options, args) = parser.parse_args(sys.argv)
134
135 rabbitmqctl_path = options.rabbitmqctl_path
136 action = getattr(options, 'action', None)
137 vhost = options.vhost
138 queue = getattr(options, 'queue', None)
139 exchange = getattr(options, 'exchange', None)
140 parameters = options.parameters
85c040f @ryandub Added ability to define max messages in queue and alert if exceeded.
ryandub authored
141 length = getattr(options, 'length', None)
947af5f @Kami Add script for monitoring RabbitMQ.
Kami authored
142
143 if not action:
144 print 'status err Missing required argument: action'
145 sys.exit(1)
146
147 if action == 'list_queues' and not queue:
148 print 'status err Missing required argument: queue'
149 sys.exit(1)
150 elif action == 'list_exchanges' and not exchange:
151 print 'status err Missing required argument: exchange'
152 sys.exit(1)
153
154 if action not in METRIC_TYPES.keys():
155 print 'status err Invalid action: %s' % (action)
156 sys.exit(1)
157
158 if not parameters:
159 print 'status err Missing required argument: parameters'
160 sys.exit(1)
161
162 metrics, error = retrieve_stats(vhost, action, queue, exchange,
163 parameters, rabbitmqctl_path)
164
165 if error:
166 print 'status err %s' % (error)
167 sys.exit(1)
43729b7 @ryandub Made --queue-length optional.
ryandub authored
168 if length:
169 if int(metrics['messages']) > length:
170 print 'status err Message queue %s at %d and above threshold of %d' % (
171 queue, int(metrics['messages']), length)
172 sys.exit(1)
947af5f @Kami Add script for monitoring RabbitMQ.
Kami authored
173 print 'status ok metrics successfully retrieved'
174 print_metrics(action, metrics)
Something went wrong with that request. Please try again.