Exemple #1
0
def test_get_daemons_stats_from_socket_ko():
    """Check if get_daemons_stats_from_socket() raises expected exceptions."""
    with pytest.raises(WazuhError, match=r'\b1307\b'):
        stats.get_daemons_stats_from_socket(None, None)

    with pytest.raises(WazuhError, match=r'\b1310\b'):
        stats.get_daemons_stats_from_socket('000', 'agent')

    with pytest.raises(WazuhInternalError, match=r'\b1121\b'):
        stats.get_daemons_stats_from_socket('000', 'logcollector')

    with patch('wazuh.core.stats.WazuhSocket.__init__', return_value=None):
        with patch('wazuh.core.stats.WazuhSocket.send', side_effect=None):
            with patch('wazuh.core.configuration.WazuhSocket.receive',
                       side_effect=ValueError):
                with pytest.raises(WazuhInternalError, match=r'\b1118\b'):
                    stats.get_daemons_stats_from_socket('000', 'logcollector')

            with patch('wazuh.core.configuration.WazuhSocket.receive',
                       return_value="err Error message test".encode()):
                with patch('wazuh.core.stats.WazuhSocket.close',
                           side_effect=None):
                    with pytest.raises(WazuhError, match=r'\b1117\b'):
                        stats.get_daemons_stats_from_socket(
                            '000', 'logcollector')
Exemple #2
0
    def get_stats(self, component):
        """Read the agent's component stats.

        Parameters
        ----------
        component : string
            Name of the component to get stats from.

        Returns
        -------
        Dict
            Object with component's stats.
        """
        # Check if agent version is compatible with this feature
        self.load_info_from_db()
        if self.version is None:
            raise WazuhInternalError(1015)
        agent_version = WazuhVersion(self.version.split(" ")[1])
        required_version = WazuhVersion(
            AGENT_COMPONENT_STATS_REQUIRED_VERSION.get(component))
        if agent_version < required_version:
            raise WazuhInternalError(
                1735,
                extra_message="Minimum required version is " +
                str(required_version))

        return stats.get_daemons_stats_from_socket(self.id, component)
Exemple #3
0
def test_get_daemons_stats_from_socket(agent_id, daemon, response):
    """Check that get_daemons_stats_from_socket function uses the expected params and returns expected result.

    Parameters
    ----------
    agent_id : string
        Id of the agent to get stats from.
    daemon : string
        Name of the service to get stats from.
    response : string
        Response to be returned by the socket.
    """
    with patch('wazuh.core.stats.OssecSocket.__init__',
               return_value=None) as mock_socket:
        with patch('wazuh.core.stats.OssecSocket.send',
                   side_effect=None) as mock_send:
            with patch('wazuh.core.stats.OssecSocket.receive',
                       return_value=response.encode()):
                with patch('wazuh.core.stats.OssecSocket.close',
                           side_effect=None):
                    result = stats.get_daemons_stats_from_socket(
                        agent_id, daemon)

        if agent_id == '000':
            mock_socket.assert_called_once_with(
                os.path.join(common.ossec_path, "queue", "sockets",
                             "logcollector"))
            mock_send.assert_called_once_with(b'getstate')
        else:
            mock_socket.assert_called_once_with(
                os.path.join(common.ossec_path, "queue", "sockets", "request"))
            mock_send.assert_called_once_with(
                f"{str(agent_id).zfill(3)} {daemon} getstate".encode())
Exemple #4
0
def test_get_daemons_stats_from_socket(agent_id, daemon, response):
    """Check that get_daemons_stats_from_socket() function uses the expected params and returns expected result"""
    with patch('wazuh.core.stats.WazuhSocket.__init__',
               return_value=None) as mock_socket:
        with patch('wazuh.core.stats.WazuhSocket.send',
                   side_effect=None) as mock_send:
            with patch('wazuh.core.stats.WazuhSocket.receive',
                       return_value=response.encode()):
                with patch('wazuh.core.stats.WazuhSocket.close',
                           side_effect=None):
                    stats.get_daemons_stats_from_socket(agent_id, daemon)

        if agent_id == '000':
            mock_socket.assert_called_once_with(
                os.path.join(common.wazuh_path, "queue", "sockets",
                             "logcollector"))
            mock_send.assert_called_once_with(b'getstate')
        else:
            mock_socket.assert_called_once_with(
                os.path.join(common.wazuh_path, "queue", "sockets", "request"))
            mock_send.assert_called_once_with(
                f"{str(agent_id).zfill(3)} {daemon} getstate".encode())