Completed
Pull Request — master (#342)
by Tomaz
02:50
created

list_queues()   C

Complexity

Conditions 7

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 7
dl 0
loc 32
rs 5.5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A MmonitEventsSensor.cleanup() 0 3 1
A MmonitEventsSensor._clear_datastore() 0 12 4
1
#!/usr/bin/env python
2
3
import json
4
5
from lib.shell import run_command
6
7
8
def to_bool(value):
9
    if not value:
10
        return False
11
12
    return value.lower() == 'true'
13
14
# See https://github.com/michaelklishin/rabbit-hole/blob/master/queues.go#L10
15
# for description
16
QUEUE_ATTRIBUTES = [
17
    # general properties
18
    ('vhost', str),
19
    ('name', str),
20
    ('node', str),
21
    ('auto_delete', to_bool),
22
    ('durable', to_bool),
23
24
    # queue info
25
    ('messages', int),
26
    ('messages_ready', int),
27
    ('messages_unacknowledged', int),
28
    ('consumers', int),
29
    ('memory', int),
30
    ('state', str),
31
32
    # backing store info
33
    ('backing_queue_status.len', int),
34
    ('backing_queue_status.pending_acks', int),
35
    ('backing_queue_status.ram_msg_count', int),
36
    ('backing_queue_status.ram_ack_count', int),
37
    ('backing_queue_status.persistent_count', int),
38
    ('backing_queue_status.avg_ingress_rate', float),
39
    ('backing_queue_status.avg_egress_rate', float),
40
    ('backing_queue_status.avg_ack_ingress_rate', float),
41
    ('backing_queue_status.avg_ack_egress_rate', float)
42
]
43
44
45
def list_queues():
46
    cmd = ['rabbitmqadmin', 'list', 'queues']
47
48
    for attr_name, attr_type in QUEUE_ATTRIBUTES:
49
        cmd.append(attr_name)
50
51
    exit_code, stdout, stderr = run_command(cmd=cmd, shell=False)
52
53
    if exit_code != 0:
54
        msg = 'Command failed: %s' % (stderr)
55
        raise Exception(msg)
56
57
    result = []
58
    lines = stdout.split('\n')
59
    lines = [line for line in lines if line.strip()]
60
61
    # Ignore header and footer
62
    lines = lines[3:-1]
63
64
    for line in lines:
65
        split = line.split('|')
66
        split = split[1:-1]
67
68
        item = {}
69
        for index, (attr_name, attr_cast) in enumerate(QUEUE_ATTRIBUTES):
70
            value = split[index].strip()
71
            value = attr_cast(value)
72
            item[attr_name] = value
73
74
        result.append(item)
75
76
    return result
77
78
79
if __name__ == '__main__':
80
    result = list_queues()
81
    print(json.dumps(result))
82