From 622edc1369a93adaec9950feaf6644908ec5f801 Mon Sep 17 00:00:00 2001 From: Daniel Maya Date: Mon, 17 Apr 2017 12:50:46 +0200 Subject: [PATCH] new feature: added agent_alias_cmd in pandora_agent --- pandora_agents/unix/Linux/pandora_agent.conf | 6 +++ pandora_agents/unix/pandora_agent | 20 +++++++++- pandora_agents/win32/pandora_agent_conf.cc | 9 +++++ .../win32/pandora_windows_service.cc | 38 +++++++++++++++---- 4 files changed, 64 insertions(+), 9 deletions(-) diff --git a/pandora_agents/unix/Linux/pandora_agent.conf b/pandora_agents/unix/Linux/pandora_agent.conf index 4475226edf..37d9de9efc 100644 --- a/pandora_agents/unix/Linux/pandora_agent.conf +++ b/pandora_agents/unix/Linux/pandora_agent.conf @@ -46,6 +46,12 @@ agent_name_cmd __rand__ #Parent agent_name #parent_agent_name caprica +# By default, agent takes machine alias +#agent_alias + +# To define agent alias by specific command, define 'agent_alias_cmd'. +#agent_alias_cmd + # Agent description #description This is a demo agent for Linux diff --git a/pandora_agents/unix/pandora_agent b/pandora_agents/unix/pandora_agent index 0bdb5ea11e..14e5b08eba 100755 --- a/pandora_agents/unix/pandora_agent +++ b/pandora_agents/unix/pandora_agent @@ -135,9 +135,10 @@ my %DefaultConf = ( 'interval' => 300, 'debug' => 0, 'agent_name' => '', - 'agent_alias' => hostname(), + 'agent_alias' => '', 'ehorus_conf' => undef, 'agent_name_cmd' => '', + 'agent_alias_cmd' => '', 'description' => '', 'group' => '', 'group_id' => undef, @@ -802,6 +803,23 @@ sub read_config (;$) { # Module, plugin and collection definitions parse_conf_modules(\@file); + + # If agent_alias_cmd is defined, agent_alias is set by command result. + if ($Conf{'agent_alias'} eq '') { + if ($Conf{'agent_alias_cmd'} ne '') { + my $result = `$Conf{'agent_alias_cmd'}`; + # Use only the first line. + my ($temp_agent_alias, $remain2) = split(/\n/, $result); + chomp ($temp_agent_alias); + + # Remove white spaces of the first and last. + $temp_agent_alias =~ s/^ *(.*?) *$/$1/; + + $Conf{'agent_alias'} = $temp_agent_alias if ($temp_agent_alias ne ''); + } else { + $Conf{'agent_alias'} = hostname(); + } + } # If agent_name_cmd is defined, agent_name is set by command result. if ($Conf{'agent_name'} eq '') { diff --git a/pandora_agents/win32/pandora_agent_conf.cc b/pandora_agents/win32/pandora_agent_conf.cc index 83675cdde6..94617243d5 100644 --- a/pandora_agents/win32/pandora_agent_conf.cc +++ b/pandora_agents/win32/pandora_agent_conf.cc @@ -228,6 +228,15 @@ Pandora::Pandora_Agent_Conf::setFile (string *all_conf){ key_values->push_back (kv); continue; } + + /*Check if is a agent_alias_cmd"*/ + pos = buffer.find("agent_alias_cmd"); + if (pos != string::npos){ + Key_Value kv; + kv.parseLineByPosition(buffer, 15); + key_values->push_back (kv); + continue; + } /*Check if is a collection*/ pos = buffer.find("file_collection"); diff --git a/pandora_agents/win32/pandora_windows_service.cc b/pandora_agents/win32/pandora_windows_service.cc index 45c2189385..a7c1105975 100644 --- a/pandora_agents/win32/pandora_windows_service.cc +++ b/pandora_agents/win32/pandora_windows_service.cc @@ -257,7 +257,36 @@ Pandora_Windows_Service::pandora_init () { // Read modules this->modules = new Pandora_Module_List (conf_file); delete []all_conf; - + + // Get the agent alias. + agent_alias = conf->getValue ("agent_alias"); + if (agent_alias == "") { + agent_alias_cmd = conf->getValue ("agent_alias_cmd"); + if (agent_alias_cmd != "") { + agent_alias_cmd = "cmd.exe /c \"" + agent_alias_cmd + "\""; + static string temp_agent_alias = getAgentNameFromCmdExec(agent_alias_cmd); + + // Delete new line and carriage return. + pos = temp_agent_alias.find("\n"); + if(pos != string::npos) { + temp_agent_alias.erase(pos, temp_agent_alias.size () - pos); + } + pos = temp_agent_alias.find("\r"); + if(pos != string::npos) { + temp_agent_alias.erase(pos, temp_agent_alias.size () - pos); + } + + // Remove leading and trailing white spaces. + temp_agent_alias = trim(temp_agent_alias); + if (temp_agent_alias != "") { + agent_alias = temp_agent_alias; + } + } else { + agent_alias = Pandora_Windows_Info::getSystemName (); + } + } + this->conf->setValue("agent_alias", agent_alias); + // Get the agent name. agent_name = conf->getValue ("agent_name"); if (agent_name == "") { @@ -299,13 +328,6 @@ Pandora_Windows_Service::pandora_init () { this->conf->setValue("agent_name", agent_name); } - // Get the agent alias. - agent_alias = conf->getValue ("agent_alias"); - if (agent_alias == "") { - agent_alias = Pandora_Windows_Info::getSystemName (); - this->conf->setValue("agent_alias", agent_alias); - } - pandora_agent = "PANDORA_AGENT=" + agent_name; putenv(pandora_agent.c_str());