From 2d478918e717c54fe1ef50a1bc478e7a180c423b Mon Sep 17 00:00:00 2001 From: Pavel Odintsov Date: Sat, 13 Jul 2024 13:41:53 +0300 Subject: [PATCH] Migrates Graphtie metrics plugin to new configuration struicture and adjusted it to new total counters format --- src/fastnetmon.cpp | 19 +++------- src/fastnetmon_configuration_scheme.hpp | 19 ++++++++++ src/metrics/graphite.cpp | 49 ++++++++++++------------- 3 files changed, 47 insertions(+), 40 deletions(-) diff --git a/src/fastnetmon.cpp b/src/fastnetmon.cpp index 7c0c6bc..687e121 100644 --- a/src/fastnetmon.cpp +++ b/src/fastnetmon.cpp @@ -446,15 +446,6 @@ std::string exabgp_community_host = ""; std::string exabgp_next_hop = ""; -// Graphite monitoring -bool graphite_enabled = false; -std::string graphite_host = "127.0.0.1"; -unsigned short int graphite_port = 2003; -unsigned int graphite_push_period = 1; - -// Default graphite namespace -std::string graphite_prefix = "fastnetmon"; - std::string influxdb_writes_total_desc = "Total number of InfluxDB writes"; uint64_t influxdb_writes_total = 0; @@ -687,7 +678,7 @@ bool load_configuration_file() { } if (configuration_map.count("graphite_prefix") != 0) { - graphite_prefix = configuration_map["graphite_prefix"]; + fastnetmon_global_configuration.graphite_prefix = configuration_map["graphite_prefix"]; } if (configuration_map.count("average_calculation_time") != 0) { @@ -813,19 +804,19 @@ bool load_configuration_file() { // Graphite if (configuration_map.count("graphite") != 0) { - graphite_enabled = configuration_map["graphite"] == "on" ? true : false; + fastnetmon_global_configuration.graphite = configuration_map["graphite"] == "on" ? true : false; } if (configuration_map.count("graphite_host") != 0) { - graphite_host = configuration_map["graphite_host"]; + fastnetmon_global_configuration.graphite_host = configuration_map["graphite_host"]; } if (configuration_map.count("graphite_port") != 0) { - graphite_port = convert_string_to_integer(configuration_map["graphite_port"]); + fastnetmon_global_configuration.graphite_port = convert_string_to_integer(configuration_map["graphite_port"]); } if (configuration_map.count("graphite_push_period") != 0) { - graphite_push_period = convert_string_to_integer(configuration_map["graphite_push_period"]); + fastnetmon_global_configuration.graphite_push_period = convert_string_to_integer(configuration_map["graphite_push_period"]); } // InfluxDB diff --git a/src/fastnetmon_configuration_scheme.hpp b/src/fastnetmon_configuration_scheme.hpp index 3f1c5ae..755fa6f 100644 --- a/src/fastnetmon_configuration_scheme.hpp +++ b/src/fastnetmon_configuration_scheme.hpp @@ -9,6 +9,7 @@ class fastnetmon_configuration_t { public: + // Clickhouse metrics bool clickhouse_metrics{ false }; std::string clickhouse_metrics_database{ "fastnetmon" }; std::string clickhouse_metrics_username{ "default" }; @@ -16,5 +17,23 @@ class fastnetmon_configuration_t { std::string clickhouse_metrics_host{ "127.0.0.1" }; unsigned int clickhouse_metrics_port{ 9000 }; unsigned int clickhouse_metrics_push_period{ 1 }; + + // InfluxDB metrics + bool influxdb{ false }; + std::string influxdb_database{ "fastnetmon" }; + std::string influxdb_host{ "127.0.0.1" }; + unsigned int influxdb_port{ 8086 }; + bool influxdb_skip_host_counters{ false }; + std::string influxdb_user{ "fastnetmon" }; + std::string influxdb_password{ "fastnetmon" }; + bool influxdb_auth{ false }; + unsigned int influxdb_push_period{ 1 }; + + // Graphtie metrics + bool graphite{ false }; + std::string graphite_host{ "127.0.0.1" }; + unsigned int graphite_port{ 2003 }; + std::string graphite_prefix{ "fastnetmon" }; + unsigned int graphite_push_period{ 1 }; }; diff --git a/src/metrics/graphite.cpp b/src/metrics/graphite.cpp index b35e2ab..c9eef1a 100644 --- a/src/metrics/graphite.cpp +++ b/src/metrics/graphite.cpp @@ -3,6 +3,7 @@ #include "../fast_library.hpp" #include "../fastnetmon_types.hpp" +#include "../fastnetmon_configuration_scheme.hpp" #include @@ -11,17 +12,8 @@ #include "../abstract_subnet_counters.hpp" extern log4cpp::Category& logger; -extern uint64_t incoming_total_flows_speed; -extern uint64_t outgoing_total_flows_speed; -extern abstract_subnet_counters_t ipv4_network_counters; -extern total_speed_counters_t total_counters_ipv4; -extern total_speed_counters_t total_counters_ipv6; -extern bool graphite_enabled; -extern std::string graphite_host; -extern unsigned short int graphite_port; -extern std::string graphite_prefix; -extern unsigned int graphite_push_period; +extern fastnetmon_configuration_t fastnetmon_global_configuration; // Push host traffic to Graphite bool push_hosts_traffic_counters_to_graphite() { @@ -57,7 +49,7 @@ bool push_hosts_traffic_counters_to_graphite() { direction_as_string = "outgoing"; } - std::string graphite_current_prefix = graphite_prefix + ".hosts." + + std::string graphite_current_prefix = fastnetmon_global_configuration.graphite_prefix + ".hosts." + ip_as_string_with_dash_delimiters + "." + direction_as_string; @@ -96,11 +88,11 @@ bool push_hosts_traffic_counters_to_graphite() { } } - bool graphite_put_result = store_data_to_graphite(graphite_port, graphite_host, graphite_data); + bool graphite_put_result = store_data_to_graphite(fastnetmon_global_configuration.graphite_port, fastnetmon_global_configuration.graphite_host, graphite_data); if (!graphite_put_result) { logger << log4cpp::Priority::ERROR << "Can't store host load data to Graphite server " - << graphite_host << " port: " << graphite_port; + << fastnetmon_global_configuration.graphite_host << " port: " << fastnetmon_global_configuration.graphite_port; return false; } @@ -110,11 +102,15 @@ bool push_hosts_traffic_counters_to_graphite() { // Push total counters to graphite bool push_total_traffic_counters_to_graphite() { + extern total_speed_counters_t total_counters_ipv4; + extern uint64_t incoming_total_flows_speed; + extern uint64_t outgoing_total_flows_speed; + std::vector directions = { INCOMING, OUTGOING, INTERNAL, OTHER }; for (auto packet_direction : directions) { - uint64_t speed_in_pps = total_counters_ipv4.total_speed_average_counters[packet_direction].packets; - uint64_t speed_in_bps = total_counters_ipv4.total_speed_average_counters[packet_direction].bytes; + uint64_t speed_in_pps = total_counters_ipv4.total_speed_average_counters[packet_direction].total.packets; + uint64_t speed_in_bps = total_counters_ipv4.total_speed_average_counters[packet_direction].total.bytes; graphite_data_t graphite_data; @@ -130,17 +126,17 @@ bool push_total_traffic_counters_to_graphite() { flow_counter_for_this_direction = outgoing_total_flows_speed; } - graphite_data[graphite_prefix + ".total." + direction_as_string + ".flows"] = flow_counter_for_this_direction; + graphite_data[fastnetmon_global_configuration.graphite_prefix + ".total." + direction_as_string + ".flows"] = flow_counter_for_this_direction; } - graphite_data[graphite_prefix + ".total." + direction_as_string + ".pps"] = speed_in_pps; - graphite_data[graphite_prefix + ".total." + direction_as_string + ".bps"] = speed_in_bps * 8; + graphite_data[fastnetmon_global_configuration.graphite_prefix + ".total." + direction_as_string + ".pps"] = speed_in_pps; + graphite_data[fastnetmon_global_configuration.graphite_prefix + ".total." + direction_as_string + ".bps"] = speed_in_bps * 8; - bool graphite_put_result = store_data_to_graphite(graphite_port, graphite_host, graphite_data); + bool graphite_put_result = store_data_to_graphite(fastnetmon_global_configuration.graphite_port, fastnetmon_global_configuration.graphite_host, graphite_data); if (!graphite_put_result) { - logger << log4cpp::Priority::ERROR << "Can't store total load data to Graphite server " << graphite_host - << " port: " << graphite_port; + logger << log4cpp::Priority::ERROR << "Can't store total load data to Graphite server " << fastnetmon_global_configuration.graphite_host + << " port: " << fastnetmon_global_configuration.graphite_port; ; return false; } @@ -151,6 +147,7 @@ bool push_total_traffic_counters_to_graphite() { // Push per subnet traffic counters to graphite bool push_network_traffic_counters_to_graphite() { + extern abstract_subnet_counters_t ipv4_network_counters; graphite_data_t graphite_data; std::vector> speed_elements; @@ -166,7 +163,7 @@ bool push_network_traffic_counters_to_graphite() { // Replace / by dashes too std::replace(subnet_as_string_as_dash_delimiters.begin(), subnet_as_string_as_dash_delimiters.end(), '/', '_'); - std::string current_prefix = graphite_prefix + ".networks." + subnet_as_string_as_dash_delimiters + "."; + std::string current_prefix = fastnetmon_global_configuration.graphite_prefix + ".networks." + subnet_as_string_as_dash_delimiters + "."; graphite_data[current_prefix + "incoming.pps"] = speed->total.in_packets; graphite_data[current_prefix + "outgoing.pps"] = speed->total.out_packets; @@ -175,11 +172,11 @@ bool push_network_traffic_counters_to_graphite() { } - bool graphite_put_result = store_data_to_graphite(graphite_port, graphite_host, graphite_data); + bool graphite_put_result = store_data_to_graphite(fastnetmon_global_configuration.graphite_port, fastnetmon_global_configuration.graphite_host, graphite_data); if (!graphite_put_result) { - logger << log4cpp::Priority::ERROR << "Can't store network load data to Graphite server " << graphite_host - << " port: " << graphite_port; + logger << log4cpp::Priority::ERROR << "Can't store network load data to Graphite server " << fastnetmon_global_configuration.graphite_host + << " port: " << fastnetmon_global_configuration.graphite_port; return false; } @@ -194,7 +191,7 @@ void graphite_push_thread() { boost::this_thread::sleep(boost::posix_time::milliseconds(500)); while (true) { - boost::this_thread::sleep(boost::posix_time::seconds(graphite_push_period)); + boost::this_thread::sleep(boost::posix_time::seconds(fastnetmon_global_configuration.graphite_push_period)); std::chrono::steady_clock::time_point start_time = std::chrono::steady_clock::now();