|
1 | 1 | #!/usr/bin/env python3
|
2 | 2 |
|
| 3 | +# redis-priority-queue -> queue monitor |
| 4 | +# Author: Gabriel Bordeaux (gabfl) |
| 5 | +# Github: https://github.com/gabfl/redis-priority-queue |
| 6 | +# Compatible with python 2 & 3 |
| 7 | + |
3 | 8 | import sys, getopt, redis, argparse
|
4 | 9 | from prettytable import PrettyTable
|
5 | 10 |
|
|
13 | 18 | help="Redis server authentification")
|
14 | 19 | parser.add_argument("-n", "--dbnum", type=int, default=0,
|
15 | 20 | help="Redis server database number")
|
| 21 | +parser.add_argument("-s", "--sort_groups", action='append', |
| 22 | + help="Sort groups") |
16 | 23 | args = parser.parse_args()
|
17 | 24 |
|
| 25 | +def setSortGroups(sortGroups = None): |
| 26 | + """Return the sorting groups, either user defined or from the default list""" |
| 27 | + if sortGroups is None: # Default groups |
| 28 | + return [('-inf', '+inf'), ('-inf', 100), (101, '+inf')]; |
| 29 | + else: |
| 30 | + groups = [('-inf', '+inf')]; # Default mandatory group (Total) |
| 31 | + groups.extend([tuple(map(int, sortGroup.split('->'))) for sortGroup in sortGroups]) |
| 32 | + return groups; |
| 33 | + |
| 34 | +def getCount(queueName, min, max): |
| 35 | + """Fetches set count from Redis""" |
| 36 | + return r.zcount(queueName, min, max); |
| 37 | + |
| 38 | +def getColumnTitle(min, max): |
| 39 | + """Human readable column titles""" |
| 40 | + if str(min) == '-inf' and str(max) == '+inf': |
| 41 | + return 'Total'; |
| 42 | + elif str(min) == '-inf': |
| 43 | + return 'Up to ' + '{0:,}'.format(max); |
| 44 | + elif str(max) == '+inf': |
| 45 | + return 'From ' + '{0:,}'.format(min); |
| 46 | + else: |
| 47 | + return '{0:,}'.format(min) + ' to ' + '{0:,}'.format(max); |
| 48 | + |
| 49 | +def setColumnAlign(titles): |
| 50 | + """Set PrettyTable column alignment""" |
| 51 | + global t; |
| 52 | + for i, title in enumerate(titles): |
| 53 | + if i == 0: # First column (title) |
| 54 | + t.align[title] = 'l'; # Left |
| 55 | + else: # Any other column |
| 56 | + t.align[title] = 'r'; # Right |
| 57 | + |
18 | 58 | # Redis connection
|
19 | 59 | r = redis.StrictRedis(host=args.host, port=args.port, db=args.dbnum, password=args.auth)
|
20 | 60 |
|
21 |
| -# Get queues |
22 |
| -queueNames = r.smembers('rpq|names') |
| 61 | +# Sort groups |
| 62 | +sortGroups = setSortGroups(args.sort_groups); |
23 | 63 |
|
24 |
| -# Table titles |
25 |
| -titles=['Queue Name', 'Total', 'Priority <= 100', 'Priority > 100'] |
| 64 | +# Column titles (queue name, then a column per sorting group) |
| 65 | +titles = ['Queue name']; |
| 66 | +titles.extend([getColumnTitle(sortGroup[0], sortGroup[1]) for sortGroup in sortGroups]); |
26 | 67 |
|
27 | 68 | # Create table
|
28 |
| -t = PrettyTable(titles) |
| 69 | +t = PrettyTable(titles); |
| 70 | +setColumnAlign(titles); |
29 | 71 |
|
30 |
| -# Column alignment |
31 |
| -t.align['Queue Name'] = "l" # Left align queue names |
32 |
| -t.align['Total'] = "r" # Right align numbers |
33 |
| -t.align['Priority <= 100'] = "r" # Right align numbers |
34 |
| -t.align['Priority > 100'] = "r" # Right align numbers |
| 72 | +# Get queues |
| 73 | +queueNames = r.smembers('rpq|names') |
35 | 74 |
|
36 | 75 | # Add a row par queue
|
37 | 76 | for queueName in sorted(queueNames):
|
38 |
| - # Get counts |
39 |
| - countA = r.zcount(queueName, '-inf', '+inf') |
40 |
| - countB = r.zcount(queueName, '-inf', 100) |
41 |
| - countC = r.zcount(queueName, 101, '+inf') |
| 77 | + # Get row |
| 78 | + row = [queueName.decode("utf-8")] |
| 79 | + row.extend(['{0:,}'.format(getCount(queueName, sortGroup[0], sortGroup[1])) for sortGroup in sortGroups]); |
42 | 80 |
|
43 | 81 | # Add row
|
44 |
| - t.add_row([queueName.decode("utf-8"), '{0:,}'.format(countA), '{0:,}'.format(countB), '{0:,}'.format(countC)]) |
| 82 | + t.add_row(row); |
45 | 83 |
|
46 | 84 | # Print table
|
47 | 85 | print (t);
|
0 commit comments