From 7a463d541984ad2659846ba9c063ee05a842c5d8 Mon Sep 17 00:00:00 2001 From: Luis Enrique Chico Capistrano Date: Tue, 3 Dec 2024 17:37:23 -0300 Subject: [PATCH] feat: test --- src/agent/include/agent.hpp | 5 +++++ src/agent/src/agent.cpp | 10 ++++++++++ src/agent/src/main.cpp | 6 ++++++ src/agent/src/process_options.hpp | 4 ++++ src/agent/src/process_options_unix.cpp | 19 +++++++++++++++++++ 5 files changed, 44 insertions(+) diff --git a/src/agent/include/agent.hpp b/src/agent/include/agent.hpp index 835f9b3eee..f7f0c652f4 100644 --- a/src/agent/include/agent.hpp +++ b/src/agent/include/agent.hpp @@ -37,6 +37,11 @@ class Agent /// This method sets up the agent and starts the task manager. void Run(); + /// @brief Stop the agent + /// + /// This method stops the agent by stopping the task manager, modules, and communicator. + void Stop(); + private: /// @brief Task manager TaskManager m_taskManager; diff --git a/src/agent/src/agent.cpp b/src/agent/src/agent.cpp index d77eecb354..1e31f94615 100644 --- a/src/agent/src/agent.cpp +++ b/src/agent/src/agent.cpp @@ -110,3 +110,13 @@ void Agent::Run() m_moduleManager.Stop(); m_communicator.Stop(); } + +void Agent::Stop() +{ + m_taskManager.Stop(); + + m_moduleManager.Stop(); + + m_communicator.Stop(); + +} diff --git a/src/agent/src/main.cpp b/src/agent/src/main.cpp index cbaed1ae13..3166f655a9 100644 --- a/src/agent/src/main.cpp +++ b/src/agent/src/main.cpp @@ -12,6 +12,7 @@ namespace program_options = boost::program_options; static const auto OPT_HELP {"help"}; static const auto OPT_RUN {"run"}; static const auto OPT_STATUS {"status"}; +static const auto OPT_STOP {"stop"}; static const auto OPT_CONFIG_FILE {"config-file"}; static const auto OPT_REGISTER_AGENT {"register-agent"}; static const auto OPT_URL {"url"}; @@ -34,6 +35,7 @@ int main(int argc, char* argv[]) program_options::options_description cmdParser("Allowed options"); cmdParser.add_options()(OPT_HELP, "Display this help menu")( OPT_RUN, "Run agent in foreground (this is the default behavior)")( + OPT_STOP, "Stop agent")( OPT_STATUS, "Check if the agent is running (running or stopped)")( OPT_CONFIG_FILE, program_options::value(), "Path to the Wazuh configuration file (optional)")( OPT_REGISTER_AGENT, "Use this option to register as a new agent")( @@ -86,6 +88,10 @@ int main(int argc, char* argv[]) { std::cout << cmdParser << '\n'; } + else if (validOptions.count(OPT_STOP) > 0) + { + StopAgent(validOptions.count(OPT_CONFIG_FILE) ? validOptions[OPT_CONFIG_FILE].as() : ""); + } else { StartAgent(validOptions.count(OPT_CONFIG_FILE) ? validOptions[OPT_CONFIG_FILE].as() : ""); diff --git a/src/agent/src/process_options.hpp b/src/agent/src/process_options.hpp index 13df711573..894195b957 100644 --- a/src/agent/src/process_options.hpp +++ b/src/agent/src/process_options.hpp @@ -25,6 +25,10 @@ void StartAgent(const std::string& configFilePath); /// @param configFilePath The file path to the configuration file used to get the status of the agent. void StatusAgent(const std::string& configFilePath); +/// @brief Stop the agent using the specified configuration file. +/// @param configFilePath The file path to the configuration file used for stop the agent. +void StopAgent(const std::string& configFilePath); + #ifdef _WIN32 /// @brief Installs the agent as a service. /// @return True if the installation is successful, false otherwise. diff --git a/src/agent/src/process_options_unix.cpp b/src/agent/src/process_options_unix.cpp index c58874dc91..a1b6dd73c1 100644 --- a/src/agent/src/process_options_unix.cpp +++ b/src/agent/src/process_options_unix.cpp @@ -32,3 +32,22 @@ void StatusAgent(const std::string& configFilePath) { std::cout << fmt::format("wazuh-agent is {}\n", unix_daemon::GetDaemonStatus(configFilePath)); } + +void StopAgent(const std::string& configFilePath) +{ + unix_daemon::LockFileHandler lockFileHandler = unix_daemon::GenerateLockFile(configFilePath); + + if (!lockFileHandler.isLockFileCreated()) + { + std::cout << "wazuh-agent is not running\n"; + return; + } + + LogInfo("Stopping wazuh-agent"); + Agent agent(configFilePath); + agent.Stop(); + + lockFileHandler.removeLockFile(); + + LogInfo("wazuh-agent stopped successfully"); +}