Skip to content

Commit

Permalink
feat: comments, fixes and conflict resolutions
Browse files Browse the repository at this point in the history
  • Loading branch information
LucioDonda committed Dec 10, 2024
1 parent e070931 commit 54e4f50
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 59 deletions.
2 changes: 1 addition & 1 deletion etc/config/wazuh-agent.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ agent:
retry_interval: 30s
events:
batch_interval: 10s
batch_size: 1000
batch_size: 100B
inventory:
enabled: true
interval: 1h
Expand Down
6 changes: 3 additions & 3 deletions src/agent/communicator/include/communicator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ namespace communicator
m_batchSize = getConfigValue.template operator()<size_t>("events", "batch_size")
.value_or(config::agent::DEFAULT_BATCH_SIZE);

if (m_batchSize < 1 || m_batchSize > 1'000'000)
if (m_batchSize < 1'000 || m_batchSize > 100'000'000)
{
LogWarn("batch_size must be between 1 and 1000000. Using default value.");
LogWarn("batch_size must be between 1000B and 100MB. Using default value.");
m_batchSize = config::agent::DEFAULT_BATCH_SIZE;
}
}
Expand Down Expand Up @@ -138,7 +138,7 @@ namespace communicator
/// @brief Time between batch requests
std::time_t m_batchInterval = config::agent::DEFAULT_BATCH_INTERVAL;

//TODO:
/// @brief Size for batch requests
size_t m_batchSize = config::agent::DEFAULT_BATCH_SIZE;

/// @brief The server URL
Expand Down
4 changes: 2 additions & 2 deletions src/agent/communicator/src/http_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,9 @@ namespace http_client
const auto messages = co_await messageGetter(batchSize);
messagesCount = std::get<0>(messages);

if (batchTimeoutTimer.expiry() <= std::chrono::steady_clock::now())
if (messagesCount || batchTimeoutTimer.expiry() <= std::chrono::steady_clock::now())
{
LogTrace("Messages count: {}", messagesCount);
LogInfo("Messages count: {}", messagesCount);
reqParams.Body = std::get<1>(messages);
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,13 @@ namespace configuration
/// @throws YAML::Exception If there is an error while loading or parsing a YAML file.
void LoadSharedConfig();

//TODO: fill doc
/// @brief Converts a size unit represented as a string to an size_t value (B).
/// @param option A string representing a size unit.
/// @return The corresponding size_t value.
/// @throws std::invalid_argument if the string does not represent a valid size unit.
/// @details This function parses a string representing a size unit and returns the equivalent size_t
/// value. The size unit can be expressed in Bytes (e.g. "1B"), Mega bytes (e.g. "1M" or "1MB"), kilo bytes (e.g.
/// "1K" or "1KB"). If no unit is specified, the value is assumed to be in Bytes
std::size_t ParseSizeUnit(const std::string& option) const;

public:
Expand Down
37 changes: 2 additions & 35 deletions src/agent/configuration_parser/src/configuration_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,39 +20,7 @@ namespace
constexpr unsigned int A_MB_IN_BYTES = 1000 * A_KB_IN_BYTES;
constexpr unsigned int A_GB_IN_BYTES = 1000 * A_MB_IN_BYTES;

#ifdef _WIN32
/// @brief Gets the path to the configuration file.
///
/// On Windows, this method queries the environment variable ProgramData and
/// constructs the path to the configuration file as
/// %ProgramData%\\wazuh-agent\\config\\wazuh-agent.yml. If the environment variable
/// is not set, it falls back to the default path
/// C:\\ProgramData\\wazuh-agent\\config\\wazuh-agent.yml.
///
/// @return The path to the configuration file.
std::string getConfigFilePath()
{
std::string configFilePath;
char* programData = nullptr;
std::size_t len = 0;
int error = _dupenv_s(&programData, &len, "ProgramData");

if (error || programData == nullptr)
{
configFilePath = "C:\\ProgramData\\wazuh-agent\\config\\wazuh-agent.yml";
}
else
{
configFilePath = std::string(programData) + "\\wazuh-agent\\config\\wazuh-agent.yml";
}
free(programData);
return configFilePath;
}

const std::string CONFIG_FILE_NAME = getConfigFilePath();
#else
const std::string CONFIG_FILE_NAME = "/etc/wazuh-agent/wazuh-agent.yml";
#endif
const std::filesystem::path CONFIG_FILE = std::filesystem::path(config::DEFAULT_CONFIG_PATH) / "wazuh-agent.yml";
} // namespace

namespace configuration
Expand Down Expand Up @@ -265,9 +233,8 @@ namespace configuration
}
else
{
// By default, assume KB
// By default, assume B
number = option;
multiplier = A_KB_IN_BYTES;
}

if (!std::all_of(number.begin(), number.end(), static_cast<int (*)(int)>(std::isdigit)))
Expand Down
9 changes: 7 additions & 2 deletions src/agent/multitype_queue/include/imultitype_queue.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class IMultiTypeQueue
* @brief Retrieves the next message from the queue asynchronously.
*
* @param type The type of the queue to use as the source.
* @param messageQuantity The quantity of messages to return.
* @param messageQuantity In bytes or in number of messages.
* @param moduleName The name of the module requesting the message.
* @param moduleType The type of the module requesting the messages.
* @return boost::asio::awaitable<std::vector<Message>> Awaitable object representing the next N messages.
Expand Down Expand Up @@ -134,6 +134,11 @@ class IMultiTypeQueue
*/
virtual int storedItems(MessageType type, const std::string moduleName = "") = 0;

//TODO: doc
/**
* @brief Returns the number of bytes stored in the queue.
*
* @param type The type of the queue.
* @return size_t The number of bytes in the queue.
*/
virtual size_t sizePerType(MessageType type) = 0;
};
16 changes: 10 additions & 6 deletions src/agent/multitype_queue/include/multitype_queue.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,14 @@ class MultiTypeQueue : public IMultiTypeQueue
Message getNext(MessageType type, const std::string moduleName = "", const std::string moduleType = "") override;

/**
* TODO
* @copydoc IMultiTypeQueue::getNextNAwaitable(MessageType type, std::variant<const int, const size_t>
* messageQuantity, const std::string moduleName, const std::string moduleType)
*/
boost::asio::awaitable<std::vector<Message>> getNextNAwaitable(MessageType type,
std::variant<const int, const size_t> messageQuantity,
const std::string moduleName = "",
const std::string moduleType = "") override;
boost::asio::awaitable<std::vector<Message>>
getNextNAwaitable(MessageType type,
std::variant<const int, const size_t> messageQuantity,
const std::string moduleName = "",
const std::string moduleType = "") override;

/**
* @copydoc IMultiTypeQueue::getNextN(MessageType, int, const std::string, const std::string)
Expand Down Expand Up @@ -136,6 +138,8 @@ class MultiTypeQueue : public IMultiTypeQueue
*/
int storedItems(MessageType type, const std::string moduleName = "") override;

//TODO: doc
/**
* @copydoc IMultiTypeQueue::sizePerType(MessageType type)
*/
size_t sizePerType(MessageType type) override;
};
21 changes: 17 additions & 4 deletions src/agent/multitype_queue/include/persistence.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,25 @@ class Persistence
virtual int GetElementCount(const std::string& queueName, const std::string& moduleName = "") = 0;


//TODO:doc
virtual size_t GetElementsStoredSize(const std::string& tableName) = 0;
/**
* @brief Get the bytes occupied by elements stored in the specified queue.
*
* @param queueName The name of the queue.
* @return size_t The bytes occupied by elements stored in the specified queue.
*/
virtual size_t GetElementsStoredSize(const std::string& queueName) = 0;

//TODO: doc
/**
* @brief Retrieve multiple JSON messages based on size from the specified queue.
*
* @param n size occupied by the messages to be retrieved.
* @param queueName The name of the queue.
* @param moduleName The name of the module.
* @param moduleType The type of the module.
* @return nlohmann::json The retrieved JSON messages.
*/
virtual nlohmann::json RetrieveBySize(size_t n,
const std::string& tableName,
const std::string& queueName,
const std::string& moduleName = "",
const std::string& moduleType = "") = 0;
};
2 changes: 1 addition & 1 deletion src/agent/multitype_queue/src/multitype_queue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ std::vector<Message> MultiTypeQueue::getNextN(MessageType type,
{
sizeRequested = availableSize;
}

LogInfo("Requesting {}B of {}B", sizeRequested, availableSize);
arrayData = m_persistenceDest->RetrieveBySize(
sizeRequested, m_mapMessageTypeName.at(type), moduleName, moduleType);
}
Expand Down
21 changes: 18 additions & 3 deletions src/agent/multitype_queue/src/sqlitestorage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,23 @@ class SQLiteStorage : public Persistence
*/
int GetElementCount(const std::string& tableName, const std::string& moduleName = "") override;

// TODO: doc
/**
* @brief Get the bytes occupied by elements stored in the specified queue.
*
* @param tableName The name of the table.
* @return size_t The bytes occupied by elements stored in the specified queue.
*/
size_t GetElementsStoredSize(const std::string& tableName) override;

// TODO: doc
/**
* @brief Retrieve multiple JSON messages based on size from the specified queue.
*
* @param n size occupied by the messages to be retrieved.
* @param tableName The name of the table to retrieve the message from.
* @param moduleName The name of the module.
* @param moduleType The type of the module.
* @return nlohmann::json The retrieved JSON messages.
*/
nlohmann::json RetrieveBySize(size_t n,
const std::string& tableName,
const std::string& moduleName = "",
Expand All @@ -149,7 +162,9 @@ class SQLiteStorage : public Persistence
*/
void ReleaseDatabaseAccess();

// TODO: fill doc
/**
* @brief Intermediate function for processing Retrieve queries.
*/
nlohmann::json ProcessRequest(SQLite::Statement& sqlStatementQuery, size_t maxSize = 0);

/**
Expand Down
2 changes: 1 addition & 1 deletion src/agent/src/message_queue_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class IMultiTypeQueue;
/// @brief Gets messages from a queue and returns them as a JSON string
/// @param multiTypeQueue The queue to get messages from
/// @param messageType The type of messages to get from the queue
/// @param messagesSize TODO
/// @param messagesSize Size of messages to get from the queue
/// @param getMetadataInfo Function to get the agent metadata
/// @return A string containing the messages from the queue
boost::asio::awaitable<std::tuple<int, std::string>>
Expand Down

0 comments on commit 54e4f50

Please sign in to comment.