Create a gist now

Instantly share code, notes, and snippets.

@andyhky /rabbit.cfg Secret
Created Sep 2, 2014

What would you like to do?
[environment_name]
url: http://<rabbitMQ_host>:55672
user: management user
pass: management password
vhost: nova
#!/usr/bin/env python
import httplib2
import json
import os
import re
import socket
import sys
import ConfigParser
EXIT_CODES = { 0: "OK",
1: "Warning",
2: "Critical",
3: "Unknown" }
THOLD = 25
def main(host):
env = None
cfiles = []
config = ConfigParser.RawConfigParser()
f = "%s/rabbit.cfg" % (os.path.dirname(__file__))
cdir = os.path.join(os.path.split(__file__)[0], 'rabbit.d/')
if os.path.isdir(cdir):
for dirname, dirnames, filenames in os.walk(cdir):
for filename in filenames:
fullpath = os.path.join(dirname, filename)
cfiles.append(fullpath)
else:
cfiles.append(f)
for cfile in cfiles:
config.read(cfile)
if "prod" in host:
if config.has_section(host):
env = host
else:
for section in config.sections():
if config.has_option(section, "url") and host in config.get(section, "url"):
env = section
if env is None:
status = 3
sys.exit(status)
url = config.get(env, "url")
user = config.get(env, "user")
password = config.get(env, "pass")
vhost = config.get(env, "vhost")
full_url = "%s/api/queues/%s" % (url, vhost)
msg = "No compute queues > %s" % THOLD
dict = {}
status = 0
headers = {'Content-Type': 'application/json'}
keys = ("messages", "messages_ready", "messages_unacknowledged")
h = httplib2.Http()
h.add_credentials(user, password)
try:
resp, content = h.request(full_url, "GET", headers=headers)
if resp["status"] == "200":
data = json.loads(content)
for queue in data:
if re.match("compute", queue["name"]) and queue["messages"] > THOLD:
dict[queue["name"]] = queue["messages"]
if dict:
if len(dict) < 5:
status = 1
else:
status = 2
msg = str(dict)
else:
status = 3
msg = "%s => %s" % (resp["content-location"], resp["status"])
except socket.error, errmsg:
status = 3
msg = errmsg
# let's truncate the msg if it's too large
msg = '{0:.40}'.format(msg)
print("%s: %s" % (msg, EXIT_CODES[status]))
sys.exit(status)
if __name__ == "__main__":
if len(sys.argv) == 2:
main(sys.argv[1])
else:
status = 3
print("Env. invalid or not specified: %s" % (EXIT_CODES[status]))
sys.exit(status)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment