code cleaning

This commit is contained in:
Benjamin Renard 2021-05-21 13:04:32 +02:00
parent 9693e0d22b
commit c3f1fd1e04

View file

@ -188,58 +188,61 @@ ceph_cmd.append('--format=json')
p = subprocess.Popen(ceph_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) p = subprocess.Popen(ceph_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output, err = p.communicate() output, err = p.communicate()
if output: if not output:
data = json.loads(output.decode(sys.getdefaultencoding())) print("UNKNOWN : fail to execute ceph status command")
sys.exit(STATUS['UNKNOWN'])
status = 'OK' data = json.loads(output.decode(sys.getdefaultencoding()))
health = data['health'].get('status', data['health'].get('overall_status')) status = 'OK'
if not health:
health = data['health'].get('status', data['health'].get('overall_status'))
if not health:
print("UNKNOWN : fail to retreive health status") print("UNKNOWN : fail to retreive health status")
sys.exit(STATUS['UNKNOWN']) sys.exit(STATUS['UNKNOWN'])
if health == 'HEALTH_WARN': if health == 'HEALTH_WARN':
status = 'WARNING' status = 'WARNING'
elif health == 'HEALTH_CRIT': elif health == 'HEALTH_CRIT':
status = 'CRITICAL' status = 'CRITICAL'
total_mon = data['monmap'].get('num_mons', len(data['monmap'].get('mons', []))) total_mon = data['monmap'].get('num_mons', len(data['monmap'].get('mons', [])))
if not total_mon: if not total_mon:
print("UNKNOWN : fail to retreive total number of monitors") print("UNKNOWN : fail to retreive total number of monitors")
sys.exit(STATUS['UNKNOWN']) sys.exit(STATUS['UNKNOWN'])
total_mon_up = len(data.get('quorum', data['health'].get('timechecks', dict()).get('mons', []))) total_mon_up = len(data.get('quorum', data['health'].get('timechecks', dict()).get('mons', [])))
if not total_mon_up: if not total_mon_up:
print("UNKNOWN : fail to retreive total number of UP monitors") print("UNKNOWN : fail to retreive total number of UP monitors")
sys.exit(STATUS['UNKNOWN']) sys.exit(STATUS['UNKNOWN'])
num_lost_mon = total_mon-total_mon_up num_lost_mon = total_mon-total_mon_up
if num_lost_mon == 0: if num_lost_mon == 0:
monstate = "(MONs UP : %s/%s)" % (total_mon_up, total_mon) monstate = "(MONs UP : %s/%s)" % (total_mon_up, total_mon)
else: else:
monstate = "%s MONs down (MONs UP : %s/%s)" % (num_lost_mon, total_mon_up, total_mon) monstate = "%s MONs down (MONs UP : %s/%s)" % (num_lost_mon, total_mon_up, total_mon)
if num_lost_mon >= options.critlostmon: if num_lost_mon >= options.critlostmon:
status = 'CRITICAL' status = 'CRITICAL'
elif num_lost_mon >= options.warnlostmon and status != 'CRITICAL': elif num_lost_mon >= options.warnlostmon and status != 'CRITICAL':
status = 'WARNING' status = 'WARNING'
total_osd = data['osdmap'].get('osdmap', data['osdmap']).get('num_osds') total_osd = data['osdmap'].get('osdmap', data['osdmap']).get('num_osds')
if total_osd is None: if total_osd is None:
print("UNKNOWN : fail to retreive total number of OSD") print("UNKNOWN : fail to retreive total number of OSD")
sys.exit(STATUS['UNKNOWN']) sys.exit(STATUS['UNKNOWN'])
total_osd_up = data['osdmap'].get('osdmap', data['osdmap']).get('num_up_osds') total_osd_up = data['osdmap'].get('osdmap', data['osdmap']).get('num_up_osds')
if total_osd_up is None: if total_osd_up is None:
print("UNKNOWN : fail to retreive total number of UP OSD") print("UNKNOWN : fail to retreive total number of UP OSD")
sys.exit(STATUS['UNKNOWN']) sys.exit(STATUS['UNKNOWN'])
num_lost_osd = total_osd - total_osd_up num_lost_osd = total_osd - total_osd_up
if num_lost_osd >= options.critlostosd: if num_lost_osd >= options.critlostosd:
status = 'CRITICAL' status = 'CRITICAL'
elif num_lost_osd >= options.warnlostosd and status != 'CRITICAL': elif num_lost_osd >= options.warnlostosd and status != 'CRITICAL':
status = 'WARNING' status = 'WARNING'
total_pg = data['pgmap']['num_pgs'] total_pg = data['pgmap']['num_pgs']
pgstate = "" pgstate = ""
for st in data['pgmap']['pgs_by_state']: for st in data['pgmap']['pgs_by_state']:
if re.search('(down|inconsistent|imcomplete|stale)', st['state_name'], re.IGNORECASE): if re.search('(down|inconsistent|imcomplete|stale)', st['state_name'], re.IGNORECASE):
status = 'CRITICAL' status = 'CRITICAL'
pgstate = "%s / %s PGs %s" % (pgstate, st['count'], st['state_name']) pgstate = "%s / %s PGs %s" % (pgstate, st['count'], st['state_name'])
@ -250,14 +253,11 @@ if output:
elif st['state_name'] == "active+clean": elif st['state_name'] == "active+clean":
pgstate = "%s / %s/%s PGs active+clean" % (pgstate, st['count'], total_pg) pgstate = "%s / %s/%s PGs active+clean" % (pgstate, st['count'], total_pg)
msg = "%s : %s%s %s" % (status, health, pgstate, monstate) msg = "%s : %s%s %s" % (status, health, pgstate, monstate)
if num_lost_osd == 0: if num_lost_osd == 0:
print("%s (OSDs UP : %s/%s)" % (msg, total_osd_up, total_osd)) print("%s (OSDs UP : %s/%s)" % (msg, total_osd_up, total_osd))
else:
print("%s / %s OSDs down (OSDs UP : %s/%s)" % (msg, num_lost_osd, total_osd_up, total_osd))
sys.exit(STATUS[status])
else: else:
print("UNKNOWN : fail to execute ceph status command") print("%s / %s OSDs down (OSDs UP : %s/%s)" % (msg, num_lost_osd, total_osd_up, total_osd))
sys.exit(STATUS['UNKNOWN']) sys.exit(STATUS[status])