parser = argparse.ArgumentParser(description="This plugin will try to connect to a Minecraft server.");
parser.add_argument('-H', '--hostname', dest='hostname', metavar='ADDRESS', required=True, help="host name or IP address")
parser.add_argument('-p', '--port', dest='port', type=int, default=25565, metavar='INTEGER', help="port number (default: 25565)")
parser.add_argument('-n', '--number-of-checks', dest='num_checks', type=int, default=5, metavar='INTEGER', help="number of checks to get stable average response time (default: 5)")
parser.add_argument('-m', '--motd', dest='motd', default='A Minecraft Server', metavar='STRING', help="expected motd in server response (default: A Minecraft Server)")
parser.add_argument('-f', '--warn-on-full', dest='full', action='store_true', help="generate warning if server is full")
parser.add_argument('-w', '--warning', dest='warning', type=float, default=0.0, metavar='DOUBLE', help="response time to result in warning status (seconds)")
parser.add_argument('-c', '--critical', dest='critical', type=float, default=0.0, metavar='DOUBLE', help="response time to result in critical status (seconds)")
parser.add_argument('-t', '--timeout', dest='timeout', type=float, default=10.0, metavar='DOUBLE', help="seconds before connection times out (default: 10)")
parser.add_argument('-v', '--verbose', dest='verbose', action='store_true', help="show details for command-line debugging (Nagios may truncate output)")
# Parse the arguments. If it failes, exit overriding exit code.
try:
args = parser.parse_args()
except SystemExit:
print(OUTPUT_UNKNOWN)
sys.exit(STATE_UNKNOWN)
try:
info = get_server_info(args.hostname, args.port, args.num_checks, args.timeout, args.verbose)
if string.find(info['motd'], args.motd) > -1:
# Check if response time is above critical level.
if args.critical and info['response_time'] > args.critical:
print(OUTPUT_CRITICAL.format("{0} second response time".format(info['response_time']), info['byte_count'], info['response_time'], args.warning, args.critical, args.timeout))
sys.exit(STATE_CRITICAL)
# Check if response time is above warning level.
if args.warning and info['response_time'] > args.warning:
print(OUTPUT_WARNING.format("{0} second response time".format(info['response_time']), info['byte_count'], info['response_time'], args.warning, args.critical, args.timeout))
sys.exit(STATE_WARNING)
# Check if server is full.
if args.full and info['players'] == info['max_players']: