Skip to content

Commit

Permalink
feat: test
Browse files Browse the repository at this point in the history
  • Loading branch information
lchico committed Dec 4, 2024
1 parent 554b973 commit d0089ef
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/agent/include/agent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
19 changes: 19 additions & 0 deletions src/agent/src/agent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,22 @@ void Agent::Run()
m_moduleManager.Stop();
m_communicator.Stop();
}

void Agent::Stop()
{
std::cout << "Stopping Agent..." << std::endl;

// Stop accepting new tasks and cancel ongoing ones
m_taskManager.Stop();

// Stop modules
m_moduleManager.Stop();

// Stop communicator
m_communicator.Stop();

// If signal handler is blocking, unblocking it is necessary
m_signalHandler->Stop();

std::cout << "Agent stopped successfully." << std::endl;
}
8 changes: 7 additions & 1 deletion src/agent/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"};
Expand All @@ -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<std::string>(), "Path to the Wazuh configuration file (optional)")(
OPT_REGISTER_AGENT, "Use this option to register as a new agent")(
Expand Down Expand Up @@ -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<std::string>() : "");
}
else
{
StartAgent(validOptions.count(OPT_CONFIG_FILE) ? validOptions[OPT_CONFIG_FILE].as<std::string>() : "");
Expand All @@ -98,4 +104,4 @@ int main(int argc, char* argv[])
LogCritical("An error occurred: {}.", e.what());
return 1;
}
}
}
4 changes: 4 additions & 0 deletions src/agent/src/process_options.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
19 changes: 19 additions & 0 deletions src/agent/src/process_options_unix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

0 comments on commit d0089ef

Please sign in to comment.