Esempio n. 1
0
def amp_sim(exit_event, amp_id, lb):
    seq = 0
    sender = health_sender.UDPStatusSender()
    while not exit_event.is_set():
        sender.dosend(build_heartbeat_msg(amp_id, lb, seq))
        seq += 1
        time.sleep(CONF.health_manager.heartbeat_interval)
Esempio n. 2
0
def run_sender(cmd_queue):
    LOG.info('Health Manager Sender starting.')
    sender = health_sender.UDPStatusSender()

    keepalived_cfg_path = util.keepalived_cfg_path()
    keepalived_pid_path = util.keepalived_pid_path()

    while True:

        try:
            # If the keepalived config file is present check
            # that it is running, otherwise don't send the health
            # heartbeat
            if os.path.isfile(keepalived_cfg_path):
                # Is there a pid file for keepalived?
                with open(keepalived_pid_path, 'r') as pid_file:
                    pid = int(pid_file.readline())
                os.kill(pid, 0)

            message = build_stats_message()
            sender.dosend(message)

        except IOError as e:
            # Missing PID file, skip health heartbeat
            if e.errno == errno.ENOENT:
                LOG.error(
                    'Missing keepalived PID file %s, skipping health '
                    'heartbeat.', keepalived_pid_path)
            else:
                LOG.error(
                    'Failed to check keepalived and haproxy status due '
                    'to exception %s, skipping health heartbeat.', e)
        except OSError as e:
            # Keepalived is not running, skip health heartbeat
            if e.errno == errno.ESRCH:
                LOG.error('Keepalived is configured but not running, '
                          'skipping health heartbeat.')
            else:
                LOG.error(
                    'Failed to check keepalived and haproxy status due '
                    'to exception %s, skipping health heartbeat.', e)
        except Exception as e:
            LOG.error(
                'Failed to check keepalived and haproxy status due to '
                'exception %s, skipping health heartbeat.', e)

        try:
            cmd = cmd_queue.get_nowait()
            if cmd == 'reload':
                LOG.info('Reloading configuration')
                CONF.reload_config_files()
            elif cmd == 'shutdown':
                LOG.info('Health Manager Sender shutting down.')
                break
        except queue.Empty:
            pass
        time.sleep(CONF.health_manager.heartbeat_interval)
Esempio n. 3
0
def run_sender(cmd_queue):
    LOG.info(_LI('Health Manager Sender starting.'))
    sender = health_sender.UDPStatusSender()
    while True:
        message = build_stats_message()
        sender.dosend(message)
        try:
            cmd = cmd_queue.get_nowait()
            if cmd is 'reload':
                LOG.info(_LI('Reloading configuration'))
                CONF.reload_config_files()
            elif cmd is 'shutdown':
                LOG.info(_LI('Health Manager Sender shutting down.'))
                break
        except queue.Empty:
            pass
        time.sleep(CONF.health_manager.heartbeat_interval)
Esempio n. 4
0
    def test_sender(self, mock_socket, mock_getaddrinfo):
        socket_mock = mock.MagicMock()
        mock_socket.return_value = socket_mock
        sendto_mock = mock.MagicMock()
        socket_mock.sendto = sendto_mock

        # Test when no addresses are returned
        mock_getaddrinfo.return_value = []
        sender = health_sender.UDPStatusSender()
        sender.dosend(SAMPLE_MSG)

        # Test IPv4 path
        mock_getaddrinfo.return_value = [
            (socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP, '',
             ('192.0.2.20', 80))
        ]
        sendto_mock.reset_mock()

        sender = health_sender.UDPStatusSender()
        sender.dosend(SAMPLE_MSG)

        sendto_mock.assert_called_once_with(SAMPLE_MSG_BIN, ('192.0.2.20', 80))

        sendto_mock.reset_mock()

        # Test IPv6 path
        mock_getaddrinfo.return_value = [
            (socket.AF_INET6, socket.SOCK_DGRAM, socket.IPPROTO_UDP, '',
             ('2001:0DB8::F00D', 80))
        ]

        sender = health_sender.UDPStatusSender()

        sender.dosend(SAMPLE_MSG)

        sendto_mock.assert_called_once_with(SAMPLE_MSG_BIN,
                                            ('2001:0DB8::F00D', 80))

        sendto_mock.reset_mock()

        # Test invalid address family

        mock_getaddrinfo.return_value = [
            (socket.AF_UNIX, socket.SOCK_DGRAM, socket.IPPROTO_UDP, '',
             ('2001:0DB8::F00D', 80))
        ]

        sender = health_sender.UDPStatusSender()

        sender.dosend(SAMPLE_MSG)

        self.assertFalse(sendto_mock.called)

        sendto_mock.reset_mock()

        # Test socket error
        socket_mock.sendto.side_effect = socket.error

        mock_getaddrinfo.return_value = [
            (socket.AF_INET6, socket.SOCK_DGRAM, socket.IPPROTO_UDP, '',
             ('2001:0DB8::F00D', 80))
        ]

        sender = health_sender.UDPStatusSender()

        # Should not raise an exception
        sender.dosend(SAMPLE_MSG)
Esempio n. 5
0
    def test_sender(self, mock_socket, mock_getaddrinfo):
        socket_mock = mock.MagicMock()
        mock_socket.return_value = socket_mock
        sendto_mock = mock.MagicMock()
        socket_mock.sendto = sendto_mock

        # Test when no addresses are returned
        self.conf.config(group="health_manager", controller_ip_port_list='')
        sender = health_sender.UDPStatusSender()
        sender.dosend(SAMPLE_MSG)
        sendto_mock.reset_mock()

        # Test IPv4 path
        self.conf.config(group="health_manager",
                         controller_ip_port_list=['192.0.2.20:80'])
        mock_getaddrinfo.return_value = [
            (socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP, '',
             ('192.0.2.20', 80))
        ]

        sender = health_sender.UDPStatusSender()
        sender.dosend(SAMPLE_MSG)

        sendto_mock.assert_called_once_with(SAMPLE_MSG_BIN, ('192.0.2.20', 80))
        sendto_mock.reset_mock()

        # Test IPv6 path
        self.conf.config(group="health_manager",
                         controller_ip_port_list=['2001:0db8::f00d:80'])
        mock_getaddrinfo.return_value = [
            (socket.AF_INET6, socket.SOCK_DGRAM, socket.IPPROTO_UDP, '',
             ('2001:db8::f00d', 80, 0, 0))
        ]

        sender = health_sender.UDPStatusSender()

        sender.dosend(SAMPLE_MSG)

        sendto_mock.assert_called_once_with(SAMPLE_MSG_BIN,
                                            ('2001:db8::f00d', 80, 0, 0))

        sendto_mock.reset_mock()

        # Test IPv6 link-local address path
        self.conf.config(
            group="health_manager",
            controller_ip_port_list=['fe80::00ff:fe00:cafe%eth0:80'])
        mock_getaddrinfo.return_value = [
            (socket.AF_INET6, socket.SOCK_DGRAM, socket.IPPROTO_UDP, '',
             ('fe80::ff:fe00:cafe', 80, 0, 2))
        ]

        sender = health_sender.UDPStatusSender()

        sender.dosend(SAMPLE_MSG)

        sendto_mock.assert_called_once_with(SAMPLE_MSG_BIN,
                                            ('fe80::ff:fe00:cafe', 80, 0, 2))

        sendto_mock.reset_mock()

        # Test socket error
        self.conf.config(group="health_manager",
                         controller_ip_port_list=['2001:0db8::f00d:80'])
        mock_getaddrinfo.return_value = [
            (socket.AF_INET6, socket.SOCK_DGRAM, socket.IPPROTO_UDP, '',
             ('2001:db8::f00d', 80, 0, 0))
        ]
        socket_mock.sendto.side_effect = socket.error

        sender = health_sender.UDPStatusSender()

        # Should not raise an exception
        sender.dosend(SAMPLE_MSG)

        # Test an controller_ip_port_list update
        sendto_mock.reset_mock()
        mock_getaddrinfo.reset_mock()
        self.conf.config(group="health_manager",
                         controller_ip_port_list=['192.0.2.20:80'])
        mock_getaddrinfo.return_value = [
            (socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP, '',
             ('192.0.2.20', 80))
        ]
        sender = health_sender.UDPStatusSender()
        sender.dosend(SAMPLE_MSG)
        sendto_mock.assert_called_once_with(SAMPLE_MSG_BIN, ('192.0.2.20', 80))
        mock_getaddrinfo.assert_called_once_with('192.0.2.20', '80', 0,
                                                 socket.SOCK_DGRAM)
        sendto_mock.reset_mock()
        mock_getaddrinfo.reset_mock()

        self.conf.config(group="health_manager",
                         controller_ip_port_list=['192.0.2.21:81'])
        mock_getaddrinfo.return_value = [
            (socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP, '',
             ('192.0.2.21', 81))
        ]
        sender.dosend(SAMPLE_MSG)
        mock_getaddrinfo.assert_called_once_with('192.0.2.21', '81', 0,
                                                 socket.SOCK_DGRAM)
        sendto_mock.assert_called_once_with(SAMPLE_MSG_BIN, ('192.0.2.21', 81))
        sendto_mock.reset_mock()
        mock_getaddrinfo.reset_mock()