Beispiel #1
0
def set_read_write(group, node_ip, connection, logger):
    node = Node(group, node_ip, connection)

    try:
        if inner_logic.set_read_write(node, connection):
            logger.info('Node: "{}" defined as "read_write" '
                        'at group_name: "{}".'.format(node.ip, node.group))
            return True
    except NodeIsDownError:
        print_error('--set read_write failed. Connection could not '
                    'established with the node_ip {}'.format(node_ip))
        logger.error('--set read_write failed. Connection could not '
                     'established with the node_ip {}'.format(node_ip))
    except ReplicationNotRunningError:
        print_warning('The replication is stopped at node "{}" and '
                      'cannot be set as read_write.'.format(node_ip))
        logger.error('The replication is stopped at node "{}" and '
                     'cannot be set as read_write.'.format(node_ip))
    except NodeWithDelayError:
        print_warning(
            'The node "{}" is {} seconds delayed and cannot be set as '
            'read_write.'.format(node_ip, node.seconds_behind))
        logger.error(
            'The node "{}" is {} seconds delayed and cannot be set as '
            'read_write.'.format(node_ip, node.seconds_behind))
    return False
Beispiel #2
0
def set_read_only(group, node_ip, connection, logger):
    node = Node(group, node_ip, connection)

    try:
        if inner_logic.set_read_only(node, connection):
            logger.info('Node: "{}" defined as "read_only" '
                        'at group_name: "{}"'.format(node.ip, node.group))
            return True
    except NodeIsDownError:
        print_error('--set read_only failed. Connection could not be '
                    'established with the node_ip {}'.format(node_ip))
        logger.error('--set read_only failed. Connection could not be '
                     'established with the node_ip {}'.format(node_ip))
    return False
Beispiel #3
0
def check_write(group, connection, logger):
    try:
        return inner_logic.get_write_node(group, connection)
    except ManyWriteNodesError:
        print_error("Something is wrong! You could not have more than one "
                    "node with read_write mode. Check your configuration!")
        logger.critical(
            "Something is wrong! You could not have more than one node with "
            "read_write mode. Check your configuration!")
        exit(99)
    except NoWriteNodeError:
        logger.info("There isn't any node with the 'read_write' "
                    "mode at group_name: '{}'.".format(group))
    return False
Beispiel #4
0
def failover(group, connection, logger):
    try:
        inner_logic.failover(group, connection)
    except NoWriteNodeError:
        print_warning('There isn\'t any node with the "read_write" mode. '
                      'Check your configuration!')
    except IsNotMasterMasterEnvironmentError:
        print_warning(
            'You have more than two nodes and we\'re not prepared to that.')
        logger.warning(
            'You have more than two nodes or it`s not a master<->master and '
            'we\'re not prepared to that')
    except NodeIsDownError:
        print_error('ERROR -> The only candidate you have is unavailable!')
        logger.error('ERROR -> The only candidate you have is unavailable!')
Beispiel #5
0
def start(group_name, connection, logger):
    try:
        inner_logic.start(group_name, connection)
    except NoWriteNodeError:
        print_warning('There isn\'t any node with the "read_write" mode. '
                      'Check your configuration!')
        logger.error('FoxHa version cannot be initiated for '
                     'group_name: "{}"'.format(group_name))
    except ManyWriteNodesError:
        print_warning('There is many nodes with the "read_write" mode. '
                      'Check your configuration!')
        logger.error('FoxHa version cannot be initiated for '
                     'group_name: "{}"'.format(group_name))
    except NodeIsDownError:
        print_error('Connection could not established with write node')
        logger.error('Connection could not established with write node')
    else:
        logger.info('FoxHa initiated for group_name: {}'.format(group_name))
Beispiel #6
0
def status_nodes(group, logger, connection):
    status_table = PrettyTable([
        "Group_name", "Node", "Node_Status", "Mode", "Repl_Status",
        "Sec_Behind", "Master_Host"
    ])
    status_table.header_style = "upper"
    status_table.align = "c"  # Center align

    logger.info('Health of group_name: "{}"'.format(group))

    messages = []
    for node in inner_logic.get_nodes(group, connection):
        if node.is_failed():
            messages.append({
                'text':
                "Critical -> Node: {} has failed status at "
                "repository.".format(node.address),
                'Err_Type':
                'Critical'
            })
            logger.critical("{} has failed status at repository.".format(
                node.address))

        if node.is_mysql_status_up():

            if node.seconds_behind > 0:
                messages.append({
                    'text':
                    "Warning -> Node: {} - Heartbeat is delayed {} "
                    "seconds.".format(node.get_name_ip_port(),
                                      str(node.seconds_behind)),
                    'Err_Type':
                    "Warning"
                })
                logger.warning(
                    "Node: {} - Heartbeat is delayed {} seconds.".format(
                        node.get_name_ip_port(), node.seconds_behind))

            if node.is_replication_running():
                status_table.add_row([
                    node.group, node.address, node.mysql_status, node.mode,
                    node.replication_status, node.seconds_behind, node.master
                ])
                logger.info(
                    "Node: {}, Mode: {}, Node_status: {}, Replication running,"
                    " Seconds_Behind: {}, Master_Host: {}".format(
                        node.address, node.mode, node.mysql_status,
                        node.seconds_behind, node.master))
            else:
                status_table.add_row([
                    node.group, node.address, node.mysql_status, node.mode,
                    fore_red(node.replication_status), " - ", " - "
                ])
                logger.critical("Node: {}, Mode: {}, Node_status: {}, "
                                "Replication stopped".format(
                                    node.address, node.mode,
                                    node.mysql_status))

            if node.mode != node.fox_mode:
                messages.append({
                    'text':
                    'Critical -> Node: {} - Database mode differs '
                    'from repository! Should be {} but is {}.'.format(
                        node.get_name_ip_port(), node.fox_mode, node.mode),
                    'Err_Type':
                    'Critical'
                })
                logger.critical(
                    'Database mode on node {} differs from repository! '
                    'Should be {} but is {}.'.format(node.get_name_ip_port(),
                                                     node.fox_mode, node.mode))
        else:
            status_table.add_row([
                node.group, node.address,
                fore_red(node.mysql_status, True), node.fox_mode, '-', '-', '-'
            ])
            messages.append({
                'text':
                'Critical -> Node: {} - {}'.format(
                    node.get_name_ip_port(), fore_red(node.mysql_status,
                                                      True)),
                'Err_Type':
                "Critical"
            })
            logger.error("Node: {}, Mode: {}, Node_status: {}".format(
                node.get_name_ip_port(), node.fox_mode, node.mysql_status))

    print status_table
    messages.sort(reverse=True)
    for message in messages:
        if message['Err_Type'] == "Critical":
            print_error("{text}".format(**message))
        else:
            print_warning("{text}".format(**message))