Skip to content

Instantly share code, notes, and snippets.

@williamsjj
Created June 19, 2010 00:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save williamsjj/444401 to your computer and use it in GitHub Desktop.
Save williamsjj/444401 to your computer and use it in GitHub Desktop.
import sys, os, socket, struct
from amqplib import client_0_8 as amqp
from optparse import OptionParser
### Constants
EXIT_NAGIOS_OK = 0
EXIT_NAGIOS_WARN = 1
EXIT_NAGIOS_CRITICAL = 2
### Validate Commandline Arguments
opt_parser = OptionParser()
opt_parser.add_option("-s", "--server", dest="server",
help="AMQP server to connect to.")
opt_parser.add_option("-v", "--vhost", dest="vhost",
help="Virtual host to log on to.")
opt_parser.add_option("-u", "--user", dest="user",
help="Username to use when authenticating with server.")
opt_parser.add_option("-p", "--password", dest="password",
help="Password to use when authenticating with server.")
opt_parser.add_option("-q", "--queue", dest="queue_name",
help="Queue name to check.")
opt_parser.add_option("-d", "--durable", action="store_true", dest="durable",
default="False", help="Declare queue as durable. (Default: False)")
opt_parser.add_option("-a", "--auto-delete", action="store_true", dest="auto_delete",
default="False", help="Declare queue as auto-delete. (Default: False)")
opt_parser.add_option("-w", "--warn", dest="warn_threshold",
help="Number of unacknowledged messages that triggers a warning status.")
opt_parser.add_option("-c", "--critical", dest="critical_threshold",
help="Number of unacknowledged messages that triggers a critical status.")
args = opt_parser.parse_args()[0]
if args.server == None:
print "An AMQP server (--server) must be supplied. Please see --help for more details."
sys.exit(-1)
if args.vhost == None:
print "A virtual host (--vhost) must be supplied. Please see --help for more details."
sys.exit(-1)
if args.user == None:
print "A username (--user) to use when authenticating with AMQP server must be supplied. " \
"Please see --help for more details."
sys.exit(-1)
if args.password == None:
print "A password (--password) to use when authenticating with AMQP server must be supplied. " \
"Please see --help for more details."
sys.exit(-1)
if args.queue_name == None:
print "A queue name (--queue) must be supplied. Please see --help for more details."
sys.exit(-1)
if args.warn_threshold == None:
print "A warning threshold (--warn) must be supplied. Please see --help for more details."
sys.exit(-1)
try:
warn_threshold = int(args.warn_threshold)
if warn_threshold < 0:
raise ValueError
except ValueError, e:
print "Warning threshold (--warn) must be a positive integer. Please see --help for more details."
sys.exit(-1)
if args.critical_threshold == None:
print "A critical threshold (--critical) must be supplied. Please see --help for more details."
sys.exit(-1)
try:
critical_threshold = int(args.critical_threshold)
if critical_threshold < 0:
raise ValueError
except ValueError, e:
print "Critical threshold (--critical) must be a positive integer. Please see --help for more details."
sys.exit(-1)
### Check Queue Count
try:
mq_conn = amqp.Connection(host=args.server,
userid=args.user,
password=args.password,
virtual_host=args.vhost,
connect_timeout=5)
mq_chan = mq_conn.channel()
queue_stats = mq_chan.queue_declare(queue=args.queue_name,
durable=args.durable,
auto_delete=args.auto_delete)
except (socket.error,
amqp.AMQPConnectionException,
amqp.AMQPChannelException), e:
print "CRITICAL: Problem establishing MQ connection to server %s: %s " \
% (str(args.server), str(repr(e)))
sys.exit(EXIT_NAGIOS_CRITICAL)
except struct.error, e:
print "CRITICAL: Authentication error connecting to vhost '%s' on server '%s'." % \
(str(args.vhost), str(args.server))
sys.exit(EXIT_NAGIOS_CRITICAL)
mq_chan.close()
mq_conn.close()
if int(queue_stats[1]) >= critical_threshold:
print "CRITICAL: %s unacknowledged messages in queue '%s' on vhost '%s', server '%s'." % \
(str(queue_stats[1]), args.queue_name, str(args.vhost), str(args.server))
sys.exit(EXIT_NAGIOS_CRITICAL)
elif int(queue_stats[1]) >= warn_threshold:
print "WARN: %s unacknowledged messages in queue '%s' on vhost '%s', server '%s'." % \
(str(queue_stats[1]), args.queue_name, str(args.vhost), str(args.server))
sys.exit(EXIT_NAGIOS_WARN)
else:
print "OK: %s unacknowledged messages in queue '%s' on vhost '%s', server '%s'." % \
(str(queue_stats[1]), args.queue_name, str(args.vhost), str(args.server))
sys.exit(EXIT_NAGIOS_OK)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment