From 67a5015be7b9ba9b64d8188526d6152117f13f6c Mon Sep 17 00:00:00 2001 From: Melissa Kilby Date: Thu, 9 May 2024 16:48:11 +0000 Subject: [PATCH] cleanup(metrics): use filesystem lib to derive file names + build fix Signed-off-by: Melissa Kilby --- userspace/engine/falco_utils.cpp | 5 ++++- userspace/engine/falco_utils.h | 2 ++ userspace/falco/app/actions/load_rules_files.cpp | 2 ++ userspace/falco/configuration.cpp | 2 ++ userspace/falco/falco_metrics.cpp | 13 ++++++++----- userspace/falco/stats_writer.cpp | 14 ++++++++------ 6 files changed, 26 insertions(+), 12 deletions(-) diff --git a/userspace/engine/falco_utils.cpp b/userspace/engine/falco_utils.cpp index 593d1688..3804b094 100644 --- a/userspace/engine/falco_utils.cpp +++ b/userspace/engine/falco_utils.cpp @@ -22,8 +22,9 @@ limitations under the License. #include #include +#if defined(__linux__) #include - +#endif #include #include #include @@ -118,6 +119,7 @@ uint64_t parse_prometheus_interval(std::string interval_str) return interval; } +#if defined(__linux__) std::string calculate_file_sha256sum(const std::string& filename) { std::ifstream file(filename, std::ios::binary); @@ -147,6 +149,7 @@ std::string calculate_file_sha256sum(const std::string& filename) } return ss.str(); } +#endif std::string wrap_text(const std::string& in, uint32_t indent, uint32_t line_len) { diff --git a/userspace/engine/falco_utils.h b/userspace/engine/falco_utils.h index d46c81f2..7b08a89a 100644 --- a/userspace/engine/falco_utils.h +++ b/userspace/engine/falco_utils.h @@ -27,7 +27,9 @@ namespace falco::utils { uint64_t parse_prometheus_interval(std::string interval_str); +#if defined(__linux__) std::string calculate_file_sha256sum(const std::string& filename); +#endif std::string wrap_text(const std::string& in, uint32_t indent, uint32_t linelen); diff --git a/userspace/falco/app/actions/load_rules_files.cpp b/userspace/falco/app/actions/load_rules_files.cpp index a1ec164b..902ac3b5 100644 --- a/userspace/falco/app/actions/load_rules_files.cpp +++ b/userspace/falco/app/actions/load_rules_files.cpp @@ -84,7 +84,9 @@ 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__) s.config->m_loaded_rules_filenames_sha256sum.push_back(falco::utils::calculate_file_sha256sum(filename)); +#endif } // note: we have an egg-and-chicken problem here. We would like to check diff --git a/userspace/falco/configuration.cpp b/userspace/falco/configuration.cpp index b8c987d8..3bc34722 100644 --- a/userspace/falco/configuration.cpp +++ b/userspace/falco/configuration.cpp @@ -161,10 +161,12 @@ void falco_configuration::merge_configs_files(const std::string& config_name, st } } +#if defined(__linux__) for(auto &filename : m_loaded_configs_filenames) { m_loaded_configs_filenames_sha256sum.push_back(falco::utils::calculate_file_sha256sum(filename)); } +#endif } void falco_configuration::init_logger() diff --git a/userspace/falco/falco_metrics.cpp b/userspace/falco/falco_metrics.cpp index 72d72fa5..e5348f39 100644 --- a/userspace/falco/falco_metrics.cpp +++ b/userspace/falco/falco_metrics.cpp @@ -20,7 +20,8 @@ limitations under the License. #include "app/state.h" #include -#include + +namespace fs = std::filesystem; /*! \class falco_metrics @@ -83,12 +84,13 @@ std::string falco_metrics::to_text(const falco::app::state& state) prometheus_text += prometheus_metrics_converter.convert_metric_to_text_prometheus("kernel_release", "falcosecurity", "falco", {{"kernel_release", agent_info->uname_r}}); prometheus_text += prometheus_metrics_converter.convert_metric_to_text_prometheus("hostname", "falcosecurity", "evt", {{"hostname", machine_info->hostname}}); +#if defined(__linux__) 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()) { - std::string metric_name_file_sha256 = *it_filename; - RE2::GlobalReplace(&metric_name_file_sha256, R"([.\\/]|yaml|yml)", ""); + fs::path fs_path = *it_filename; + 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; @@ -99,13 +101,14 @@ std::string falco_metrics::to_text(const falco::app::state& state) 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()) { - std::string metric_name_file_sha256 = *it_filename; - RE2::GlobalReplace(&metric_name_file_sha256, R"([.\\/]|yaml|yml)", ""); + fs::path fs_path = *it_filename; + 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; } +#endif for (const std::string& source: inspector->event_sources()) { diff --git a/userspace/falco/stats_writer.cpp b/userspace/falco/stats_writer.cpp index ae3d5950..0bb37a2b 100644 --- a/userspace/falco/stats_writer.cpp +++ b/userspace/falco/stats_writer.cpp @@ -23,7 +23,6 @@ limitations under the License. #include #include -#include #include "falco_common.h" #include "stats_writer.h" @@ -32,6 +31,8 @@ limitations under the License. #include #include +namespace fs = std::filesystem; + // note: ticker_t is an uint16_t, which is enough because we don't care about // overflows here. Threads calling stats_writer::handle() will just // check that this value changed since their last observation. @@ -329,12 +330,13 @@ void stats_writer::collector::get_metrics_output_fields_wrapper( output_fields["falco.host_num_cpus"] = machine_info->num_cpus; output_fields["falco.outputs_queue_num_drops"] = m_writer->m_outputs->get_outputs_queue_num_drops(); +#if defined(__linux__) 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()) { - std::string metric_name_file_sha256 = *it_filename; - RE2::GlobalReplace(&metric_name_file_sha256, R"([.\\/]|yaml|yml)", ""); + fs::path fs_path = *it_filename; + 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; @@ -345,14 +347,14 @@ void stats_writer::collector::get_metrics_output_fields_wrapper( 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()) { - std::string metric_name_file_sha256 = *it_filename; - RE2::GlobalReplace(&metric_name_file_sha256, R"([.\\/]|yaml|yml)", ""); + fs::path fs_path = *it_filename; + 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; } - +#endif output_fields["evt.source"] = src; for (size_t i = 0; i < sizeof(all_driver_engines) / sizeof(const char*); i++) {