Created
December 18, 2017 17:42
-
-
Save cruftyoldsysadmin/e3be4014e250832d3b917a21c8bbe168 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python2.7 | |
""" | |
Usage: {prog} --user username \ | |
--password password \ # Either password or Passfile is required | |
--passfile /etc/rabbitmq/secret \ | |
--url http://localhost:15672/api/nodes/rabbitmq@dev-rmq-01 \ | |
--metric partitions \ | |
--metricname displayname.of.metric (optional)\ | |
--debug (optional) | |
Returns count of partitions in rabbitmq. | |
""" | |
import argparse | |
import json | |
import os | |
import requests | |
import logging | |
# Logging configuration | |
try: | |
import http.client as http_client | |
except ImportError: | |
# Python 2 | |
import httplib as http_client | |
# Returns count of metric list from the RabbitMQ management API | |
# | |
def getJsonArray(password, user, url, metric, metricname,debug): | |
http_client.HTTPConnection.debuglevel = debug | |
logging.basicConfig() | |
logging.getLogger().setLevel(logging.ERROR) | |
requests_log = logging.getLogger("requests.packages.urllib3") | |
requests_log.setLevel(logging.ERROR) | |
requests_log.propagate = True | |
try: | |
request = requests.get(url, auth=(user, password)) | |
node = json.loads(request.text) | |
if isinstance(node[metric], list): | |
hash = { metricname: len(node[metric])} | |
return json.dumps(hash) | |
except: | |
raise | |
def parseArgs(): | |
p = argparse.ArgumentParser( | |
description='Test local RabbitMQ cluster for Network partitions') | |
for arg in ['metric', 'user', 'url']: | |
p.add_argument('--{0}'.format(arg), required=True, type=str) | |
for arg in ['password', 'passfile','metricname','debug']: | |
p.add_argument('--{0}'.format(arg), required=False, type=str) | |
[options, foo] = p.parse_known_args() | |
return vars(options) | |
opts = parseArgs() | |
metric = opts['metric'] | |
user = opts['user'] | |
url = opts['url'] | |
if opts['debug']: | |
debug = opts['debug'] | |
else: | |
debug = 0 | |
if opts['metricname']: | |
metricname=opts['metricname'] | |
else: | |
metricname=metric | |
if opts['password']: | |
password = opts['password'] | |
elif opts['passfile']: | |
try: | |
passfile = opts['passfile'] | |
password = open(passfile).read().rstrip('\n') | |
except (IOError, os.error) as why: | |
raise IOError(why) | |
partitions = getJsonArray(password,user,url,metric,metricname,debug) | |
print partitions |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment