Ejemplo n.º 1
0
def unsubscribe(address, userid, password, receiver_address):
    """
    Unsubscribes the Nagios server that the system won't receive
    the brocade Traps
    Returned values:
    '0' --> success
    '1' --> connection failed
    '2' --> invalid userid/password combination
    '5' --> after login the Brocade switch, the snmpConfig operation failed
    """
    _METHOD_ = "manage_brocade.unsubscribe"

    Timeout = 120
    logging.info("ENTER %s::address=%s userid=%s receiver=%s",
                 _METHOD_, address, userid, receiver_address)
    switch_admin = userid + "> "
    try:
        ssh_conn_child = pexpect.spawn(
            " ".join(["ssh -o StrictHostKeyChecking=false -l", userid, address]))
        ssh_conn_child.timeout = Timeout
        ssh_conn_index = ssh_conn_child.expect(
            ['(?i)password:'******'s " + ssh_conn_child.before.decode('ascii').split("\r\n"))
        # disable inform, we just use trap
        ssh_conn_child.sendline("f")
        # no need to do action in user config
        while True:
            expect_result = ssh_conn_child.expect(["User \(r.\): \[(.+)\]", "Auth Protocol", "New Auth Passwd", "Verify Auth Passwd",
                                                   "Priv Protocol", "New Priv Passwd", "Verify Priv Passwd", "SNMPv3 trap recipient configuration", pexpect.EOF, pexpect.TIMEOUT])
            if expect_result == 7:
                break
            ssh_conn_child.send("\r")

        while True:
            expect_result = ssh_conn_child.expect(["Trap Recipient's IP address :.*\[(.+)\]", "UserIndex",
                                                   "Trap recipient Severity level", "Trap recipient Port", switch_admin, pexpect.EOF, pexpect.TIMEOUT])
            if expect_result == 0 and receiver_address == ssh_conn_child.match.group(1).decode('ascii'):
                ssh_conn_child.send("0.0.0.0")
            if expect_result == 4:
                break
            ssh_conn_child.send("\r")

        (snmp_user, snmp_user_index, snmp_auth_password,
         snmp_priv_password) = _get_snmp_config()
        (rc, message) = ssh_utils.execute_cmd_on_local(" ".join(
            ["snmpget", "-v", "3", "-u", snmp_user, "-l", "authPriv", "-a", "MD5", "-A", snmp_auth_password, "-x", "DES", "-X", snmp_priv_password, address, "1.3.6.1.6.3.10.2.1.1.0"]))
        if rc != 0:
            raise BrocadeException(
                4, "failed to get engine id from switch with message = " + message)
        engine_id = ""
        for line in message:
            engine_id += line if line.rfind(":") == - \
                1 else line[line.rfind(":") + 1:]
        engine_id = "0x" + engine_id.replace(" ", "")

        (rc) = update_snmp_v3_conf(
            engine_id, snmp_user, "MD5", snmp_auth_password, "DES", snmp_priv_password, 2)
        logging.info("%s::The Trap subcribe has been done. address=%s userid=%s targe=%s",
                     _METHOD_, address, userid, receiver_address)
        if rc == 0:
            (rc, message) = ssh_utils.execute_cmd_on_local(
                "service snmptrapd restart")
            if rc != 0:
                logging.error(
                    "%s::restart snmptrap service failed , message = " + ''.join(message))
                return 3
        else:
            logging.error(
                "%s::update snmptrap conf failed, won't restart snmptrapd service")
            return 3
        logging.info(
            "%s:: has been completed successfully. Address=%s", _METHOD_, address)
        return 0
    except BrocadeException as e:
        if e.error_code == 1:
            logging.error(
                "%s::connection to device failed. address=%s userid=%s", _METHOD_, address, userid)
            return 1
        elif e.error_code == 2:
            logging.error(
                "%s::userid/password combination not valid. address=%s userid=%s", _METHOD_, address, userid)
            return 2
        elif e.error_code == 3:
            logging.error(
                "%s::The device is not a brocade switch. address=%s userid=%s", _METHOD_, address, userid)
            return 5
        elif e.error_code == 4:
            logging.error(
                "%s::failed to get engine id from switch. address=%s userid=%s", _METHOD_, address, userid)
            return 5
        else:
            logging.error(
                "%s:: exception error", _METHOD_ + "::" + str(e.error_message))
            return 3
    except Exception as e:
        #print("%s:: exception error", _METHOD_+"::"+str(e))
        logging.error("%s:: exception error", _METHOD_ + "::" + str(e))
        return 3

    finally:
        ssh_conn_child.close()
Ejemplo n.º 2
0
def subscribe(address, userid, password, receiver_address):
    """
    This function will call snmpConfig to configure the snmpv1 in the brocade switch
    to submit the Traps to the receiver nagios server
    The return values:
    '0' --> success
    '1' --> connection to the brocade switch failed
    '2' --> invalid userid/password
    '4' --> snmpConfig failed to add the target server in snmpv3
    """
    _METHOD_ = "manage_brocade.subscribe"
    logging.info("ENTER %s::address=%s userid=%s receiver=%s",
                 _METHOD_, address, userid, receiver_address)
    switch_admin = userid + "> "
    try:
        ssh_conn_child = pexpect.spawn(
            " ".join(["ssh -o StrictHostKeyChecking=false -l", userid, address]))
        ssh_conn_child.timeout = Timeout
        ssh_conn_index = ssh_conn_child.expect(
            ['(?i)password:'******'ascii').split("\r\n")
        user_num = -1
        is_recv_defined = False
        tmp_index = 0
        username_to_modify = None
        while tmp_index < len(output_lines):
            line = output_lines[tmp_index]
            if "User" in line and "Trap" not in line and (line.split()[3] == snmp_user or line.split()[1] == snmp_user_index):
                user_num = line.split()[1]
                username_to_modify = line.split()[3]
            if "Trap Entry" in line and len(line.split()) == 4 and line.split()[3] == receiver_address:
                is_recv_defined = True
                logging.info(
                    "%s::snmp already defined on %s for receiver %s", _METHOD_, address, receiver_address)
                break
            tmp_index += 1

        ssh_conn_child.send("snmpconfig --set snmpv3\r")
        expect_result = ssh_conn_child.expect("SNMP Informs Enabled")
        if expect_result != 0:
            raise BrocadeException(
                4, "Assume get message for inform setting but now it's " + ssh_conn_child.before.decode('ascii').split("\r\n"))
        # disable inform, we just use trap
        ssh_conn_child.sendline("f")
        go_to_password_change = False
        while True:
            expect_result = ssh_conn_child.expect(["User \(r.\): \[(.+)\]", "Auth Protocol", "New Auth Passwd", "Verify Auth Passwd",
                                                   "Priv Protocol", "New Priv Passwd", "Verify Priv Passwd", "SNMPv3 trap recipient configuration", pexpect.EOF, pexpect.TIMEOUT])
            # input name if it's the one to be modify
            if expect_result == 0 and username_to_modify == ssh_conn_child.match.group(1).decode('ascii'):
                ssh_conn_child.send(snmp_user)
                go_to_password_change = True
            if go_to_password_change and expect_result == 1:
                # chosse auth type, MD5 is 1 in default
                ssh_conn_child.send("1")
            if go_to_password_change and (expect_result == 2 or expect_result == 3):
                ssh_conn_child.send(snmp_auth_password)
            if go_to_password_change and (expect_result == 5 or expect_result == 6):
                ssh_conn_child.send(snmp_priv_password)
                go_to_password_change = False if expect_result == 6 else True
            if go_to_password_change and expect_result == 4:
                ssh_conn_child.send("1")
            if expect_result == 7:
                break
            ssh_conn_child.send("\r")

        go_to_recv_define = False
        # the user,level,port define message will appear for each defined trap,
        # so that we have to set flag to identify just pass or enter data to
        # modify
        while True:
            expect_result = ssh_conn_child.expect(["Trap Recipient's IP address :.*\[(.+)\]", "UserIndex",
                                                   "Trap recipient Severity level", "Trap recipient Port", switch_admin, pexpect.EOF, pexpect.TIMEOUT])
            if expect_result == 0 and ((is_recv_defined and receiver_address == ssh_conn_child.match.group(1).decode('ascii')) or (not is_recv_defined and not go_to_recv_define and "0.0.0.0" == ssh_conn_child.match.group(1).decode('ascii'))):
                ssh_conn_child.send(receiver_address)
                go_to_recv_define = True
            if expect_result == 1 and go_to_recv_define:
                ssh_conn_child.send(user_num)
            if expect_result == 3 and go_to_recv_define:
                # modify flag
                is_recv_defined = True
                go_to_recv_define = False
            if expect_result == 4:
                break
            ssh_conn_child.send("\r")
        # run snmpget command to get the engine_id
        (rc, message) = ssh_utils.execute_cmd_on_local(" ".join(
            ["snmpget", "-v", "3", "-u", snmp_user, "-l", "authPriv", "-a", "MD5", "-A", snmp_auth_password, "-x", "DES", "-X", snmp_priv_password, address, "1.3.6.1.6.3.10.2.1.1.0"]))
        if rc != 0:
            raise BrocadeException(
                4, "failed to get engine id from switch with message = " + message)
        engine_id = ""
        for line in message:
            engine_id += line if line.rfind(":") == - \
                1 else line[line.rfind(":") + 1:]
        engine_id = "0x" + engine_id.replace(" ", "")
        (rc) = update_snmp_v3_conf(
            engine_id, snmp_user, "MD5", snmp_auth_password, "DES", snmp_priv_password, 1)
        if rc == 0:
            (rc, message) = ssh_utils.execute_cmd_on_local(
                "service snmptrapd restart")
            if rc != 0:
                logging.error(
                    "%s::restart snmptrap service failed , message = " + message)
                return 3
        else:
            logging.error(
                "%s::update snmptrap conf failed, won't restart snmptrapd service")
            return 3
        logging.info("%s::The Trap subcribe has been done. address=%s userid=%s targe=%s",
                     _METHOD_, address, userid, receiver_address)
        return 0
    except BrocadeException as e:
        if e.error_code == 1:
            logging.error(
                "%s::connection to device failed. address=%s userid=%s", _METHOD_, address, userid)
            return 1
        elif e.error_code == 2:
            logging.error(
                "%s::userid/password combination not valid. address=%s userid=%s", _METHOD_, address, userid)
            return 2
        elif e.error_code == 3:
            logging.error(
                "%s::The device is not a brocade switch. address=%s userid=%s", _METHOD_, address, userid)
            return 4
        elif e.error_code == 4:
            logging.error(
                "%s::failed to get engine id from switch. address=%s userid=%s", _METHOD_, address, userid)
            return 4
        else:
            logging.error(
                "%s:: exception error", _METHOD_ + "::" + str(e.error_message))
            return 3
    except Exception as e:
        #print("%s:: exception error", _METHOD_+"::"+str(e))
        logging.error("%s:: exception error", _METHOD_ + "::" + str(e))
        return 3
    finally:
        ssh_conn_child.close()