def main(): usage = "%s [-H HOST] [-p PORT] -c check_type\n\n" "For options, run '%s --help'" % (sys.argv[0], sys.argv[0]) parser = OptionParser(usage) parser.add_option( "-H", "--hosts", type="string", nargs=1, dest="hosts", help="Comma separated list of hosts to check - defaults to 'localhost'", ) parser.add_option( "-p", "--ports", type="string", nargs=1, dest="ports", help="Comma separated list of ports to check - defaults to 11211", ) parser.add_option( "-c", "--check-type", type="string", nargs=1, dest="check_type", help="The type of check - Mandatory!" ) (options, args) = parser.parse_args() if not options.hosts: options.hosts = "localhost" if not options.ports: options.ports = "11211" hosts = options.hosts.split(",") ports = options.ports.split(",") if len(hosts) != len(ports): parser.error("You haven't specified the same number of hosts and ports") else: hosts_ports = [] for num in range(len(hosts)): hosts_ports.append("%s:%s" % (hosts[num], ports[num])) if not options.check_type: parser.error("You must specify a type of check to perform") else: if options.check_type == "version": nagios_plugin.try_check(check_version, hosts_ports) else: # Add to this list as other check types are implemented parser.error("Check types currently accepted: version")
'--ok-text', default='OK', help='String indicating OK status') parser.add_argument('-u', '--unknown-text', default='UNKNOWN', help='String indicating unknown status') return parser.parse_args() def check_status(args): nagios_plugin.check_file_freshness(args.status_file, 43200) with open(args.status_file, "r") as f: content = [l.strip() for l in f.readlines()] for line in content: if re.search(args.critical_text, line): raise nagios_plugin.CriticalError(line) elif re.search(args.warning_text, line): raise nagios_plugin.WarnError(line) elif re.search(args.unknown_text, line): raise nagios_plugin.UnknownError(line) else: print line if __name__ == '__main__': args = parse_args() nagios_plugin.try_check(check_status, args)
nagios_plugin.check_file_freshness(args.status_file, 3600) with open(args.status_file, "r") as f: lines = f.readlines() status_data = dict(l.strip().split(' ', 1) for l in lines if len(l) > 1) else: lines = subprocess.check_output(["ceph", "status"]).split('\n') status_data = dict(l.strip().split(' ', 1) for l in lines if len(l) > 1) if ('health' not in status_data or 'monmap' not in status_data or 'osdmap'not in status_data): raise nagios_plugin.UnknownError('UNKNOWN: status data is incomplete') if status_data['health'] != 'HEALTH_OK': msg = 'CRITICAL: ceph health status: "{}"'.format(status_data['health']) raise nagios_plugin.CriticalError(msg) osds = re.search("^.*: (\d+) osds: (\d+) up, (\d+) in", status_data['osdmap']) if osds.group(1) > osds.group(2): # not all OSDs are "up" msg = 'CRITICAL: Some OSDs are not up. Total: {}, up: {}'.format( osds.group(1), osds.group(2)) raise nagios_plugin.CriticalError(msg) print "All OK" if __name__ == '__main__': parser = argparse.ArgumentParser(description='Check ceph status') parser.add_argument('-f', '--file', dest='status_file', default=False, help='Optional file with "ceph status" output') args = parser.parse_args() nagios_plugin.try_check(check_ceph_status, args)