#!/usr/local/bin/python3.4 # Copyright (c) 2015, IBM Corp. # # This module is free software; you can redistribute it and/or modify it # under the terms of GNU General Public License (GPL) version 2. import pexpect import logging import argparse import device_mgr Timeout = 120 ROOT_PROMPT = '#' Timeout_CmdRep = 15 SWITCH_STATUS_SHOW_CMD = "switchstatusshow" SWITCH_RUNNING_STATUS = None TEMPERATURE_STATUS = None FLASH_MEMORY_STATUS = None SWITCH_STATUS_LABEL = "SwitchState:" TEMPERATURE_LABEL = "Temperatures monitor" FLASH_MEMORY_LABEL = "Flash monitor" MARGINAL_PORTS_LABEL = "Marginal ports monitor" FAULTY_PORTS_LABEL = "Faulty ports monitor" brocade_switch_status = {} brocade_switch_status["switch_running_status"] = None brocade_switch_status["temperature_status"] = None brocade_switch_status["flash_memory_status"] = None brocade_switch_status["marginal_ports_status"] = None brocade_switch_status["faulty_ports_status"] = None brocade_switch_status["power_supply_status"] = None POWER_SUPPLY_LABEL = "Power supplies monitor" def check_switch_status(): """ Checking the brocade_switch_status["switch_running_status"], if its None, calling retrieve switchstatusshow result. """ _METHOD_ ="check_brocade.check_switch_status" #logging.info("ENTER %s:: SWITCH_RUNNING_STATUS=%s",_METHOD_, brocade_switch_status["switch_running_status"]) if brocade_switch_status["switch_running_status"] == None: retrieve_switch_status_show_results() if brocade_switch_status["switch_running_status"] == "HEALTHY": rc = 0 msg = "Switch status is healthy." elif brocade_switch_status["switch_running_status"] == "MARGINAL": rc = 1 msg = "There is a warning state on the Brocade switch." elif brocade_switch_status["switch_running_status"] == "DOWN": rc = 2 msg = "The Brocade switch is reporting a critical state." elif brocade_switch_status["switch_running_status"] == None: rc = 3 msg = "There was a failure getting status from the Brocade switch." #logging.info("::%s:: Switch Running Status %s",_METHOD_, brocade_switch_status["switch_running_status"]) return (rc, msg) def check_temperature(): """ Checking the brocade_switch_status["temperature_status"], if its None, calling retrieve switchstatusshow result. """ _METHOD_ ="check_brocade.check_temperature" #logging.info("ENTER %s:: TEMPERATURE_STATUS=%s",_METHOD_, brocade_switch_status["temperature_status"]) if brocade_switch_status["switch_running_status"] == None: retrieve_switch_status_show_results() if brocade_switch_status["temperature_status"] == "HEALTHY": rc = 0 msg = "Temperature is OK." elif brocade_switch_status["temperature_status"] == "MARGINAL": rc = 1 msg = "There is a warning state on the Brocade switch temperature." elif brocade_switch_status["temperature_status"] == "DOWN": rc = 2 msg = "The Brocade switch is reporting a critical temperature state." elif brocade_switch_status["temperature_status"] == None: rc = 3 msg = "There was a failure getting status from the Brocade switch." #logging.info("::%s:: Temperature Status %s",_METHOD_, brocade_switch_status["temperature_status"]) return (rc, msg) def check_flash(): """ Checking the brocade_switch_status["flash_memory_status"], if its None, calling retrieve switchstatusshow result. """ _METHOD_ ="check_brocade.check_flash" #logging.info("ENTER %s:: FLASH_MEMORY_STATUS=%s",_METHOD_, brocade_switch_status["flash_memory_status"]) if brocade_switch_status["flash_memory_status"] == None: retrieve_switch_status_show_results() if brocade_switch_status["flash_memory_status"] == "HEALTHY": rc = 0 msg = "Flash memory is OK." elif brocade_switch_status["flash_memory_status"] == "MARGINAL": rc = 1 msg = "There is a warning state on the Brocade switch flash memory." elif brocade_switch_status["flash_memory_status"] == "DOWN": rc = 2 msg = "The Brocade switch is reporting a critical flash memory state." elif brocade_switch_status["flash_memory_status"] == None: rc = 3 msg = "There was a failure getting status from the Brocade switch." #logging.info("::%s:: Flash Memory Status %s",_METHOD_, brocade_switch_status["flash_memory_status"]) return (rc, msg) def check_marginal_ports(): """ Checking the brocade_switch_status["marginal_ports_status"], if its None, calling retrieve switchstatusshow result. """ _METHOD_ ="check_brocade.check_marginal_ports" #logging.info("ENTER %s:: MARGINAL_PORTS_STATUS=%s",_METHOD_, brocade_switch_status["marginal_ports_status"]) if brocade_switch_status["marginal_ports_status"] == None: retrieve_switch_status_show_results() if brocade_switch_status["marginal_ports_status"] == "HEALTHY": rc = 0 msg = "Switch ports are OK." elif brocade_switch_status["marginal_ports_status"] == "MARGINAL": rc = 1 msg = "There is a warning state on the switch ports." elif brocade_switch_status["marginal_ports_status"] == "DOWN": rc = 2 msg = "The switch is reporting several warning states on switch ports." elif brocade_switch_status["marginal_ports_status"] == None: rc = 3 msg = "There was a failure getting status from the Brocade switch." #logging.info("::%s:: Marginal ports Status %s",_METHOD_, brocade_switch_status["marginal_ports_status"]) return (rc, msg) def check_faulty_ports(): """ Checking the brocade_switch_status["faulty_ports_status"], if its None, calling retrieve switchstatusshow result. """ _METHOD_ ="check_brocade.check_faulty_ports" #logging.info("ENTER %s:: FAULTY_PORTS_STATUS=%s",_METHOD_, brocade_switch_status["faulty_ports_status"]) if brocade_switch_status["faulty_ports_status"] == None: retrieve_switch_status_show_results() if brocade_switch_status["faulty_ports_status"] == "HEALTHY": rc = 0 msg = "Switch ports are OK." elif brocade_switch_status["faulty_ports_status"] == "MARGINAL": rc = 1 msg = "There is a critical state on the switch ports." elif brocade_switch_status["faulty_ports_status"] == "DOWN": rc = 2 msg = "The switch is reporting several critical states on switch ports." elif brocade_switch_status["faulty_ports_status"] == None: rc = 3 msg = "There was a failure getting status from the Brocade switch." #logging.info("::%s:: Faulty ports Status %s",_METHOD_, brocade_switch_status["faulty_ports_status"]) return (rc, msg) def check_power_supply(): """ Checking the brocade_switch_status["power_supply_status"], if its None, calling retrieve switchstatusshow result. """ _METHOD_ ="check_brocade.check_power_supply" #logging.info("ENTER %s:: POWER_SUPPLY_STATUS=%s",_METHOD_, brocade_switch_status["power_supply_status"]) if brocade_switch_status["power_supply_status"] == None: retrieve_switch_status_show_results() if brocade_switch_status["power_supply_status"] == "HEALTHY": rc = 0 msg = "Power supply is OK." elif brocade_switch_status["power_supply_status"] == "MARGINAL": rc = 1 msg = "There is a warning state on the Brocade switch power supply." elif brocade_switch_status["power_supply_status"] == "DOWN": rc = 2 msg = "The Brocade switch is reporting a critical power supply state." elif brocade_switch_status["power_supply_status"] == None: rc = 0 msg = "The power supply check is not available." #logging.info("::%s:: Power Supply Status %s",_METHOD_, brocade_switch_status["power_supply_status"]) return (rc, msg) def retrieve_switch_status_show_results(): """ check_switch_status will return: return code '0' --> HEALTHY '1' --> MARGINAL '2' --> DOWN '3' --> Failed to retrieve the monitor data """ _METHOD_ = "check_brocade.retrieve_switch_status_show_results" #logging.info("ENTER %s::address=%s userid=%s",_METHOD_,address,userid) ssh_conn_cmd = "ssh -o StrictHostKeyChecking=false -l " + userid + " " + address try: ssh_conn_child = pexpect.spawn(ssh_conn_cmd) ssh_conn_child.timeout = Timeout ssh_conn_index = ssh_conn_child.expect(["(?i)password:", pexpect.EOF, pexpect.TIMEOUT]) if ssh_conn_index == 0: ssh_conn_child.sendline(password) #if shell prompts for password again, the password / user combination wrong switch_admin = userid + "> " ssh_conn_index = ssh_conn_child.expect([switch_admin, "(?i)password:", ROOT_PROMPT, pexpect.TIMEOUT], timeout=Timeout_CmdRep) if ssh_conn_index == 0: ssh_conn_child.sendline(SWITCH_STATUS_SHOW_CMD) ssh_conn_child.expect(switch_admin) ssh_output = ssh_conn_child.before.decode('ascii') lines = ssh_output.split("\r\n") for line in lines: if (line.strip().find(SWITCH_STATUS_LABEL)) >= 0: #logging.info("%s::SwitchStatus: found", line) index = line.strip().find(SWITCH_STATUS_LABEL) """ Retrieve the switch status """ SWITCH_RUNNING_STATUS = line[len(SWITCH_STATUS_LABEL)+index:].strip() brocade_switch_status["switch_running_status"] = SWITCH_RUNNING_STATUS #logging.info ("The switch running status::%s ", SWITCH_RUNNING_STATUS) elif (line.strip().find(TEMPERATURE_LABEL)) >= 0: #logging.info("%s::Temperatures monitor found", line) index = line.strip().find(TEMPERATURE_LABEL) """ Retrieve the temperature sensor status """ TEMPERATURE_STATUS = line[len(TEMPERATURE_LABEL)+index:].strip() brocade_switch_status["temperature_status"] = TEMPERATURE_STATUS #logging.info ("The switch temperature status::%s ", TEMPERATURE_STATUS) elif (line.strip().find(FLASH_MEMORY_LABEL)) >= 0: #logging.info("%s::Flash monitor found", line) index = line.strip().find(FLASH_MEMORY_LABEL) """ Retrieve the flash memory status """ FLASH_MEMORY_STATUS = line[len(FLASH_MEMORY_LABEL)+index:].strip() brocade_switch_status["flash_memory_status"] = FLASH_MEMORY_STATUS #logging.info ("The flash memory status::%s ", FLASH_MEMORY_STATUS) elif (line.strip().find(MARGINAL_PORTS_LABEL)) >= 0: #logging.info("%s::Marginal ports found", line) index = line.strip().find(MARGINAL_PORTS_LABEL) """ Retrieve the marginal ports status """ MARGINAL_PORTS_STATUS = line[len(MARGINAL_PORTS_LABEL)+index:].strip() brocade_switch_status["marginal_ports_status"] = MARGINAL_PORTS_STATUS #logging.info ("The marginal ports status::%s ", MARGINAL_PORTS_STATUS) elif (line.strip().find(FAULTY_PORTS_LABEL)) >= 0: #logging.info("%s::Faulty ports found", line) index = line.strip().find(FAULTY_PORTS_LABEL) """ Retrieve the marginal ports status """ FAULTY_PORTS_STATUS = line[len(FAULTY_PORTS_LABEL)+index:].strip() brocade_switch_status["faulty_ports_status"] = FAULTY_PORTS_STATUS #logging.info ("The faulty ports status::%s ", FAULTY_PORTS_STATUS) elif (line.strip().find(POWER_SUPPLY_LABEL)) >= 0: #logging.info("%s::Power supply available in this switch:%s ", _METHOD_, line) index = line.strip().find(POWER_SUPPLY_LABEL) """ Retrieve the power supply status """ POWER_SUPPLY_STATUS = line[len(POWER_SUPPLY_LABEL)+index:].strip() brocade_switch_status["power_supply_status"] = POWER_SUPPLY_STATUS #logging.info ("%s::The power supply status::%s ", _METHOD_, POWER_SUPPLY_STATUS) elif ssh_conn_index == 1: #logging.error("%s::userid/password combination not valid. address=%s userid=%s", _METHOD_, address, userid) rc = 3 elif ssh_conn_index == 2: #logging.warning("%s::Device is not a brocade switch. address=%s userid=%s", _METHOD_, address, userid) re = 3 else: #logging.error("%s::Connection timed out. Address=%s",_METHOD_,address) rc = 3 except Exception as e: #logging.error("%s:: exception error", _METHOD_+"::"+str(e)) rc = 3 finally: ssh_conn_child.close() if __name__ == '__main__': logging.basicConfig(format="%(asctime)s %(levelname)s %(message)s",filename="/tmp/puremgr.log", level=logging.INFO) CMD = "check_brocade" parser = argparse.ArgumentParser() parser.add_argument("operation") parser.add_argument('-H', '--Host', required=True) args = parser.parse_args() operation = args.operation address = args.Host try: (userid,password) = device_mgr.retrieveAccessInfoForDevice(address) #logging.info("%s:: userid=%s password=%s", CMD+"::", userid, password) if operation == "SWITCH_STATUS": (rc,msg) = check_switch_status() elif operation == "TEMPERATURE": (rc,msg) = check_temperature() elif operation == "FLASH": (rc,msg) = check_flash() elif operation == "MARGINAL_PORTS": (rc, msg) = check_marginal_ports() elif operation == "FAULTY_PORTS": (rc, msg) = check_faulty_ports() elif operation == "ERROR_PORTS": (rc, msg) = check_error_ports() elif operation == "POWER_SUPPLY": (rc, msg) = check_power_supply() else: msg = "Unknown operation: " + operation rc = 3 except Exception as e: msg = "userid/password combination not valid" rc = 3 msg = str(e) #logging.error("%s:: exception error", CMD+"::"+str(e)) finally: print(msg) exit(rc)