Skip to content

Commit

Permalink
cleanup(metrics): use map for config and rules filenames sha256 tracking
Browse files Browse the repository at this point in the history
Co-authored-by: Federico Di Pierro <[email protected]>
Signed-off-by: Melissa Kilby <[email protected]>
  • Loading branch information
2 people authored and poiana committed May 14, 2024
1 parent 91b58c4 commit 60e6798
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 36 deletions.
2 changes: 1 addition & 1 deletion userspace/falco/app/actions/load_rules_files.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ falco::app::run_result falco::app::actions::load_rules_files(falco::app::state&
falco_logger::log(falco_logger::level::WARNING,res->as_string(true, rc) + "\n");
}
#if defined(__linux__) and !defined(MINIMAL_BUILD) and !defined(__EMSCRIPTEN__)
s.config->m_loaded_rules_filenames_sha256sum.push_back(falco::utils::calculate_file_sha256sum(filename));
s.config->m_loaded_rules_filenames_sha256sum.insert({filename, falco::utils::calculate_file_sha256sum(filename)});
#endif
}

Expand Down
2 changes: 1 addition & 1 deletion userspace/falco/configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ void falco_configuration::merge_configs_files(const std::string& config_name, st
#if defined(__linux__) and !defined(MINIMAL_BUILD) and !defined(__EMSCRIPTEN__)
for(auto &filename : m_loaded_configs_filenames)
{
m_loaded_configs_filenames_sha256sum.push_back(falco::utils::calculate_file_sha256sum(filename));
m_loaded_configs_filenames_sha256sum.insert({filename, falco::utils::calculate_file_sha256sum(filename)});
}
#endif
}
Expand Down
8 changes: 4 additions & 4 deletions userspace/falco/configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,17 +105,17 @@ class falco_configuration

// Config list as passed by the user. Filenames.
std::list<std::string> m_loaded_configs_filenames;
// sha256 of the loaded configs files
std::list<std::string> m_loaded_configs_filenames_sha256sum;
// Map with filenames and their sha256 of the loaded configs files
std::unordered_map<std::string, std::string> m_loaded_configs_filenames_sha256sum;
// Config list as passed by the user. Folders.
std::list<std::string> m_loaded_configs_folders;

// Rules list as passed by the user
std::list<std::string> m_rules_filenames;
// Actually loaded rules, with folders inspected
std::list<std::string> m_loaded_rules_filenames;
// sha256 of the loaded rules files
std::list<std::string> m_loaded_rules_filenames_sha256sum;
// Map with filenames and their sha256 of the loaded rules files
std::unordered_map<std::string, std::string> m_loaded_rules_filenames_sha256sum;
// List of loaded rule folders
std::list<std::string> m_loaded_rules_folders;
bool m_json_output;
Expand Down
24 changes: 8 additions & 16 deletions userspace/falco/falco_metrics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,28 +85,20 @@ std::string falco_metrics::to_text(const falco::app::state& state)
prometheus_text += prometheus_metrics_converter.convert_metric_to_text_prometheus("hostname", "falcosecurity", "evt", {{"hostname", machine_info->hostname}});

#if defined(__linux__) and !defined(MINIMAL_BUILD) and !defined(__EMSCRIPTEN__)
auto it_filename = state.config.get()->m_loaded_rules_filenames.begin();
auto it_sha256 = state.config.get()->m_loaded_rules_filenames_sha256sum.begin();
while (it_filename != state.config.get()->m_loaded_rules_filenames.end() && it_sha256 != state.config.get()->m_loaded_rules_filenames_sha256sum.end())
for (const auto& item : state.config.get()->m_loaded_rules_filenames_sha256sum)
{
fs::path fs_path = *it_filename;
fs::path fs_path = item.first;
std::string metric_name_file_sha256 = fs_path.filename().stem();
metric_name_file_sha256 = "sha256_rule_file_" + metric_name_file_sha256;
prometheus_text += prometheus_metrics_converter.convert_metric_to_text_prometheus(metric_name_file_sha256, "falcosecurity", "falco", {{metric_name_file_sha256, *it_sha256}});
++it_filename;
++it_sha256;
metric_name_file_sha256 = "falco.sha256_rule_file." + metric_name_file_sha256;
prometheus_text += prometheus_metrics_converter.convert_metric_to_text_prometheus(metric_name_file_sha256, "falcosecurity", "falco", {{metric_name_file_sha256, item.second}});
}

it_filename = state.config.get()->m_loaded_configs_filenames.begin();
it_sha256 = state.config.get()->m_loaded_configs_filenames_sha256sum.begin();
while (it_filename != state.config.get()->m_loaded_configs_filenames.end() && it_sha256 != state.config.get()->m_loaded_configs_filenames_sha256sum.end())
for (const auto& item : state.config.get()->m_loaded_configs_filenames_sha256sum)
{
fs::path fs_path = *it_filename;
fs::path fs_path = item.first;
std::string metric_name_file_sha256 = fs_path.filename().stem();
metric_name_file_sha256 = "sha256_config_file_" + metric_name_file_sha256;
prometheus_text += prometheus_metrics_converter.convert_metric_to_text_prometheus(metric_name_file_sha256, "falcosecurity", "falco", {{metric_name_file_sha256, *it_sha256}});
++it_filename;
++it_sha256;
metric_name_file_sha256 = "falco.sha256_config_file." + metric_name_file_sha256;
prometheus_text += prometheus_metrics_converter.convert_metric_to_text_prometheus(metric_name_file_sha256, "falcosecurity", "falco", {{metric_name_file_sha256, item.second}});
}
#endif

Expand Down
20 changes: 6 additions & 14 deletions userspace/falco/stats_writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -331,28 +331,20 @@ void stats_writer::collector::get_metrics_output_fields_wrapper(
output_fields["falco.outputs_queue_num_drops"] = m_writer->m_outputs->get_outputs_queue_num_drops();

#if defined(__linux__) and !defined(MINIMAL_BUILD) and !defined(__EMSCRIPTEN__)
auto it_filename = m_writer->m_config->m_loaded_rules_filenames.begin();
auto it_sha256 = m_writer->m_config->m_loaded_rules_filenames_sha256sum.begin();
while (it_filename != m_writer->m_config->m_loaded_rules_filenames.end() && it_sha256 != m_writer->m_config->m_loaded_rules_filenames_sha256sum.end())
for (const auto& item : m_writer->m_config->m_loaded_rules_filenames_sha256sum)
{
fs::path fs_path = *it_filename;
fs::path fs_path = item.first;
std::string metric_name_file_sha256 = fs_path.filename().stem();
metric_name_file_sha256 = "falco.sha256_rule_file." + metric_name_file_sha256;
output_fields[metric_name_file_sha256] = *it_sha256;
++it_filename;
++it_sha256;
output_fields[metric_name_file_sha256] = item.second;
}

it_filename = m_writer->m_config->m_loaded_configs_filenames.begin();
it_sha256 = m_writer->m_config->m_loaded_configs_filenames_sha256sum.begin();
while (it_filename != m_writer->m_config->m_loaded_configs_filenames.end() && it_sha256 != m_writer->m_config->m_loaded_configs_filenames_sha256sum.end())
for (const auto& item : m_writer->m_config->m_loaded_configs_filenames_sha256sum)
{
fs::path fs_path = *it_filename;
fs::path fs_path = item.first;
std::string metric_name_file_sha256 = fs_path.filename().stem();
metric_name_file_sha256 = "falco.sha256_config_file." + metric_name_file_sha256;
output_fields[metric_name_file_sha256] = *it_sha256;
++it_filename;
++it_sha256;
output_fields[metric_name_file_sha256] = item.second;
}
#endif
output_fields["evt.source"] = src;
Expand Down

0 comments on commit 60e6798

Please sign in to comment.