chore: read hostname in initialization

Signed-off-by: Adrián Arroyo Calle <adrian.arroyocalle@gmail.com>
This commit is contained in:
Adrián Arroyo Calle 2019-11-21 15:52:22 +00:00 committed by Lorenzo Fontana
parent 4d180cbc31
commit 1b05f0e6a7
3 changed files with 28 additions and 18 deletions

View File

@ -895,12 +895,31 @@ int falco_init(int argc, char **argv)
printf("%s\n", support.dump().c_str()); printf("%s\n", support.dump().c_str());
goto exit; goto exit;
} }
// read hostname
string hostname;
if(char* env_hostname = getenv("FALCO_GRPC_HOSTNAME"))
{
hostname = env_hostname;
}
else
{
char c_hostname[256];
int err = gethostname(c_hostname, 256);
if(err != 0)
{
throw falco_exception("Failed to get hostname");
}
hostname = c_hostname;
}
outputs->init(config.m_json_output, outputs->init(config.m_json_output,
config.m_json_include_output_property, config.m_json_include_output_property,
config.m_notifications_rate, config.m_notifications_max_burst, config.m_notifications_rate, config.m_notifications_max_burst,
config.m_buffered_outputs, config.m_buffered_outputs,
config.m_time_format_iso_8601); config.m_time_format_iso_8601,
hostname);
if(!all_events) if(!all_events)
{ {

View File

@ -39,7 +39,8 @@ falco_outputs::falco_outputs(falco_engine *engine)
m_initialized(false), m_initialized(false),
m_buffered(true), m_buffered(true),
m_json_output(false), m_json_output(false),
m_time_format_iso_8601(false) m_time_format_iso_8601(false),
m_hostname("")
{ {
} }
@ -72,7 +73,7 @@ falco_outputs::~falco_outputs()
void falco_outputs::init(bool json_output, void falco_outputs::init(bool json_output,
bool json_include_output_property, bool json_include_output_property,
uint32_t rate, uint32_t max_burst, bool buffered, uint32_t rate, uint32_t max_burst, bool buffered,
bool time_format_iso_8601) bool time_format_iso_8601, string hostname)
{ {
// The engine must have been given an inspector by now. // The engine must have been given an inspector by now.
if(! m_inspector) if(! m_inspector)
@ -97,6 +98,7 @@ void falco_outputs::init(bool json_output,
m_buffered = buffered; m_buffered = buffered;
m_time_format_iso_8601 = time_format_iso_8601; m_time_format_iso_8601 = time_format_iso_8601;
m_hostname = hostname;
m_initialized = true; m_initialized = true;
} }
@ -146,19 +148,7 @@ void falco_outputs::handle_event(gen_event *ev, string &rule, string &source,
std::lock_guard<std::mutex> guard(m_ls_semaphore); std::lock_guard<std::mutex> guard(m_ls_semaphore);
lua_getglobal(m_ls, m_lua_output_event.c_str()); lua_getglobal(m_ls, m_lua_output_event.c_str());
std::string hostname;
char* env_hostname = getenv("FALCO_GRPC_HOSTNAME");
if(env_hostname == NULL){
char c_hostname[256];
int err = gethostname(c_hostname, 256);
if(err != 0){
string err = "Failed to get hostname";
throw falco_exception(err);
}
hostname = c_hostname;
}else{
hostname = env_hostname;
}
if(lua_isfunction(m_ls, -1)) if(lua_isfunction(m_ls, -1))
{ {
lua_pushlightuserdata(m_ls, ev); lua_pushlightuserdata(m_ls, ev);
@ -167,7 +157,7 @@ void falco_outputs::handle_event(gen_event *ev, string &rule, string &source,
lua_pushstring(m_ls, falco_common::priority_names[priority].c_str()); lua_pushstring(m_ls, falco_common::priority_names[priority].c_str());
lua_pushnumber(m_ls, priority); lua_pushnumber(m_ls, priority);
lua_pushstring(m_ls, format.c_str()); lua_pushstring(m_ls, format.c_str());
lua_pushstring(m_ls, hostname.c_str()); lua_pushstring(m_ls, m_hostname.c_str());
if(lua_pcall(m_ls, 7, 0, 0) != 0) if(lua_pcall(m_ls, 7, 0, 0) != 0)
{ {

View File

@ -54,7 +54,7 @@ public:
void init(bool json_output, void init(bool json_output,
bool json_include_output_property, bool json_include_output_property,
uint32_t rate, uint32_t max_burst, bool buffered, uint32_t rate, uint32_t max_burst, bool buffered,
bool time_format_iso_8601); bool time_format_iso_8601, std::string hostname);
void add_output(output_config oc); void add_output(output_config oc);
@ -89,6 +89,7 @@ private:
bool m_buffered; bool m_buffered;
bool m_json_output; bool m_json_output;
bool m_time_format_iso_8601; bool m_time_format_iso_8601;
std::string m_hostname;
std::string m_lua_add_output = "add_output"; std::string m_lua_add_output = "add_output";
std::string m_lua_output_event = "output_event"; std::string m_lua_output_event = "output_event";