Skip to content

Commit

Permalink
Added all code examples for Chapter 10.
Browse files Browse the repository at this point in the history
  • Loading branch information
williamsjj committed Aug 12, 2011
1 parent 38b5ffb commit 4470a1e
Show file tree
Hide file tree
Showing 7 changed files with 420 additions and 0 deletions.
39 changes: 39 additions & 0 deletions python/chapter-10/amqp_ping_check.py
@@ -0,0 +1,39 @@
###############################################
# RabbitMQ in Action
# Chapter 10 - RabbitMQ ping (AMQP) check.
###############################################
#
#
# Author: Jason J. W. Williams
# (C)2011
###############################################
import sys, pika, socket

#(nc.0) Nagios status codes
EXIT_OK = 0
EXIT_WARNING = 1
EXIT_CRITICAL = 2
EXIT_UNKNOWN = 3

#/(nc.1) Parse command line arguments
server, port = sys.argv[1].split(":")
vhost = sys.argv[2]
username = sys.argv[3]
password = sys.argv[4]

#/(nc.2) Establish connection to broker
creds_broker = pika.PlainCredentials(username, password)
conn_params = pika.ConnectionParameters(server,
virtual_host = vhost,
credentials = creds_broker)
try:
conn_broker = pika.BlockingConnection(conn_params)
channel = conn_broker.channel()
except socket.timeout:
#/(nc.3) Connection failed, return CRITICAL status
print "CRITICAL: Could not connect to %s:%s!" % (server, port)
exit(EXIT_CRITICAL)

#(nc.4) Connection OK, return OK status
print "OK: Connect to %s:%s successful." % (server, port)
exit(EXIT_OK)
58 changes: 58 additions & 0 deletions python/chapter-10/amqp_queue_count_check.py
@@ -0,0 +1,58 @@
###############################################
# RabbitMQ in Action
# Chapter 10 - Queue count (AMQP) check.
###############################################
#
#
# Author: Jason J. W. Williams
# (C)2011
###############################################
import sys, pika, socket

#(aqcc.0) Nagios status codes
EXIT_OK = 0
EXIT_WARNING = 1
EXIT_CRITICAL = 2
EXIT_UNKNOWN = 3

#/(aqcc.1) Parse command line arguments
server, port = sys.argv[1].split(":")
vhost = sys.argv[2]
username = sys.argv[3]
password = sys.argv[4]
queue_name = sys.argv[5]
max_critical = int(sys.argv[6])
max_warn = int(sys.argv[7])

#/(aqcc.2) Establish connection to broker
creds_broker = pika.PlainCredentials(username, password)
conn_params = pika.ConnectionParameters(server,
virtual_host = vhost,
credentials = creds_broker)
try:
conn_broker = pika.BlockingConnection(conn_params)
channel = conn_broker.channel()
except socket.timeout:
#/(aqcc.3) Connection failed, return unknown status
print "Unknown: Could not connect to %s:%s!" % (server, port)
exit(EXIT_UNKNOWN)

response = channel.queue_declare(queue=queue_name,
passive=True)

#(aqcc.4) Message count is above critical limit
if response.method.message_count >= max_critical:
print "CRITICAL: Queue %s message count: %d" % \
(queue_name, response.method.message_count)
exit(EXIT_CRITICAL)

#(aqcc.5) Message count is above critical limit
if response.method.message_count >= max_warn:
print "WARN: Queue %s message count: %d" % \
(queue_name, response.method.message_count)
exit(EXIT_WARNING)

#(aqcc.6) Connection OK, return OK status
print "OK: Queue %s message count: %d" % \
(queue_name, response.method.message_count)
exit(EXIT_OK)
55 changes: 55 additions & 0 deletions python/chapter-10/api_ping_check.py
@@ -0,0 +1,55 @@
###############################################
# RabbitMQ in Action
# Chapter 10 - RabbitMQ ping (HTTP API) check.
###############################################
#
#
# Author: Jason J. W. Williams
# (C)2011
###############################################

import sys, json, httplib, urllib, base64, socket

#(apic.0) Nagios status codes
EXIT_OK = 0
EXIT_WARNING = 1
EXIT_CRITICAL = 2
EXIT_UNKNOWN = 3

#/(apic.1) Parse arguments
server, port = sys.argv[1].split(":")
vhost = sys.argv[2]
username = sys.argv[3]
password = sys.argv[4]

#/(apic.2) Connect to server
conn = httplib.HTTPConnection(server, port)

#/(apic.3) Build API path
path = "/api/aliveness-test/%s" % urllib.quote(vhost, safe="")
method = "GET"

#/(apic.4) Issue API request
credentials = base64.b64encode("%s:%s" % (username, password))

try:
conn.request(method, path, "",
{"Content-Type" : "application/json",
"Authorization" : "Basic " + credentials})

#/(apic.5) Could not connect to API server, return critical status
except socket.error:
print "CRITICAL: Could not connect to %s:%s" % (server, port)
exit(EXIT_CRITICAL)

response = conn.getresponse()

#/(apic.6) RabbitMQ not responding/alive, return critical status
if response.status > 299:
print "CRITICAL: Broker not alive: %s" % response.read()
exit(EXIT_CRITICAL)

#/(apic.7) RabbitMQ alive, return OK status
resp_payload = json.loads(response.read())

print repr(resp_payload)
88 changes: 88 additions & 0 deletions python/chapter-10/api_queue_count_check.py
@@ -0,0 +1,88 @@
###############################################
# RabbitMQ in Action
# Chapter 10 - Queue count (HTTP API) check.
###############################################
#
#
# Author: Jason J. W. Williams
# (C)2011
###############################################

import sys, json, httplib, urllib, base64, socket

#(aqcc.0) Nagios status codes
EXIT_OK = 0
EXIT_WARNING = 1
EXIT_CRITICAL = 2
EXIT_UNKNOWN = 3

#/(aqcc.1) Parse arguments
server, port = sys.argv[1].split(":")
vhost = sys.argv[2]
username = sys.argv[3]
password = sys.argv[4]
queue_name = sys.argv[5]
max_unack_critical = int(sys.argv[6])
max_unack_warn = int(sys.argv[7])
max_ready_critical = int(sys.argv[8])
max_ready_warn = int(sys.argv[9])


#/(aqcc.2) Connect to server
conn = httplib.HTTPConnection(server, port)

#/(aqcc.3) Build API path
path = "/api/queues/%s/%s" % (urllib.quote(vhost, safe=""),
queue_name)
method = "GET"

#/(aqcc.4) Issue API request
credentials = base64.b64encode("%s:%s" % (username, password))

try:
conn.request(method, path, "",
{"Content-Type" : "application/json",
"Authorization" : "Basic " + credentials})

#/(aqcc.5) Could not connect to API server, return unknown status
except socket.error:
print "UNKNOWN: Could not connect to %s:%s" % (server, port)
exit(EXIT_UNKNOWN)

response = conn.getresponse()

#/(aqcc.6) RabbitMQ not responding/alive, return critical status
if response.status > 299:
print "UNKNOWN: Unexpected API error: %s" % response.read()
exit(EXIT_UNKNOWN)

#/(aqcc.7) RabbitMQ alive, return OK status
resp_payload = json.loads(response.read())
msg_cnt_unack = resp_payload["messages_unacknowledged"]
msg_cnt_ready = resp_payload["messages_ready"]
msg_cnt_total = resp_payload["messages"]

#/(aqcc.8) Consumed but unackowledged message count above limit
if msg_cnt_unack >= max_unack_critical:
print "CRITICAL: %s - %d unack'd messages." % (queue_name,
msg_cnt_unack)
exit(EXIT_CRITICAL)
elif msg_cnt_unack >= max_unack_warn:
print "WARN: %s - %d unack'd messages." % (queue_name,
msg_cnt_unack)
exit(EXIT_WARNING)

#/(aqcc.9) Ready to be consumed message count about limit
if msg_cnt_ready >= max_ready_critical:
print "CRITICAL: %s - %d unconsumed messages." % (queue_name,
msg_cnt_ready)
exit(EXIT_CRITICAL)
elif msg_cnt_ready >= max_ready_warn:
print "WARN: %s - %d unconsumed messages." % (queue_name,
msg_cnt_ready)
exit(EXIT_WARNING)


print "OK: %s - %d in-flight messages. %dB used memory." % \
(queue_name, msg_cnt_total, resp_payload["memory"])
exit(EXIT_OK)
78 changes: 78 additions & 0 deletions python/chapter-10/cluster_health_check.py
@@ -0,0 +1,78 @@
###############################################
# RabbitMQ in Action
# Chapter 10 - Cluster health check.
###############################################
#
#
# Author: Jason J. W. Williams
# (C)2011
###############################################

import sys, json, httplib, base64, socket

#(chc.0) Nagios status codes
EXIT_OK = 0
EXIT_WARNING = 1
EXIT_CRITICAL = 2
EXIT_UNKNOWN = 3

#/(chc.1) Parse arguments
server, port = sys.argv[1].split(":")
username = sys.argv[2]
password = sys.argv[3]
node_list = sys.argv[4].split(",")
mem_critical = int(sys.argv[5])
mem_warning = int(sys.argv[6])

#/(chc.2) Connect to server
conn = httplib.HTTPConnection(server, port)

#/(chc.3) Build API path
path = "/api/nodes"
method = "GET"

#/(chc.4) Issue API request
credentials = base64.b64encode("%s:%s" % (username, password))
try:
conn.request(method, path, "",
{"Content-Type" : "application/json",
"Authorization" : "Basic " + credentials})
#/(chc.5) Could not connect to API server, return unknown status
except socket.error:
print "UNKNOWN: Could not connect to %s:%s" % (server, port)
exit(EXIT_UNKNOWN)

response = conn.getresponse()

#/(chc.6) Unexpected API error, return unknown status
if response.status > 299:
print "UNKNOWN: Unexpected API error: %s" % response.read()
exit(EXIT_UNKNOWN)

#/(chc.7) Parse API response
response = json.loads(response.read())

#/(chc.8) Cluster is missing nodes, return warning status
for node in response:
if node["name"] in node_list:
node_list.remove(node["name"])

if len(node_list):
print "WARNING: Cluster missing nodes: %s" % str(node_list)
exit(EXIT_WARNING)

#/(chc.9) Node used memory is over limit
for node in response:
if node["mem_used"] > mem_critical:
print "CRITICAL: Node %s memory usage is %d." % \
(node["name"], node["mem_used"])
exit(EXIT_CRITICAL)
elif node["mem_used"] > mem_warning:
print "WARNING: Node %s memory usage is %d." % \
(node["name"], node["mem_used"])
exit(EXIT_WARNING)

#/(chc.10) All nodes present and used memory below limit
print "OK: %d nodes. All memory usage below %d." % (len(response),
mem_warning)
exit(EXIT_OK)
26 changes: 26 additions & 0 deletions python/chapter-10/nagios_check.py
@@ -0,0 +1,26 @@
###############################################
# RabbitMQ in Action
# Chapter 10 - Basic Nagios check.
###############################################
#
#
# Author: Jason J. W. Williams
# (C)2011
###############################################
import sys, json, httplib, base64

#/(nc.1) Return requested Nagios status code
status = sys.argv[1]

if status.lower() == "warning":
print "Status is WARN"
exit(1)
elif status.lower() == "critical":
print "Status is CRITICAL"
exit(2)
elif status.lower() == "unknown":
print "Status is UNKNOWN"
exit(3)
else:
print "Status is OK"
exit(0)

0 comments on commit 4470a1e

Please sign in to comment.