From 60e6798f9be85ca12ea54118c4f932ca6a386bd6 Mon Sep 17 00:00:00 2001 From: Melissa Kilby Date: Fri, 10 May 2024 08:35:07 +0000 Subject: [PATCH] cleanup(metrics): use map for config and rules filenames sha256 tracking Co-authored-by: Federico Di Pierro Signed-off-by: Melissa Kilby --- .../falco/app/actions/load_rules_files.cpp | 2 +- userspace/falco/configuration.cpp | 2 +- userspace/falco/configuration.h | 8 +++---- userspace/falco/falco_metrics.cpp | 24 +++++++------------ userspace/falco/stats_writer.cpp | 20 +++++----------- 5 files changed, 20 insertions(+), 36 deletions(-) diff --git a/userspace/falco/app/actions/load_rules_files.cpp b/userspace/falco/app/actions/load_rules_files.cpp index 21c28b76ab9..590cc81ae52 100644 --- a/userspace/falco/app/actions/load_rules_files.cpp +++ b/userspace/falco/app/actions/load_rules_files.cpp @@ -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 } diff --git a/userspace/falco/configuration.cpp b/userspace/falco/configuration.cpp index b4f0dc767da..72eaf01f4ae 100644 --- a/userspace/falco/configuration.cpp +++ b/userspace/falco/configuration.cpp @@ -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 } diff --git a/userspace/falco/configuration.h b/userspace/falco/configuration.h index e124cf316a3..b43bae31b04 100644 --- a/userspace/falco/configuration.h +++ b/userspace/falco/configuration.h @@ -105,8 +105,8 @@ class falco_configuration // Config list as passed by the user. Filenames. std::list m_loaded_configs_filenames; - // sha256 of the loaded configs files - std::list m_loaded_configs_filenames_sha256sum; + // Map with filenames and their sha256 of the loaded configs files + std::unordered_map m_loaded_configs_filenames_sha256sum; // Config list as passed by the user. Folders. std::list m_loaded_configs_folders; @@ -114,8 +114,8 @@ class falco_configuration std::list m_rules_filenames; // Actually loaded rules, with folders inspected std::list m_loaded_rules_filenames; - // sha256 of the loaded rules files - std::list m_loaded_rules_filenames_sha256sum; + // Map with filenames and their sha256 of the loaded rules files + std::unordered_map m_loaded_rules_filenames_sha256sum; // List of loaded rule folders std::list m_loaded_rules_folders; bool m_json_output; diff --git a/userspace/falco/falco_metrics.cpp b/userspace/falco/falco_metrics.cpp index 54cc7e1f25a..58f83b5cce6 100644 --- a/userspace/falco/falco_metrics.cpp +++ b/userspace/falco/falco_metrics.cpp @@ -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 diff --git a/userspace/falco/stats_writer.cpp b/userspace/falco/stats_writer.cpp index 9117a68a524..22a33dbc4a5 100644 --- a/userspace/falco/stats_writer.cpp +++ b/userspace/falco/stats_writer.cpp @@ -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;