27using namespace std::chrono_literals;
33 std::cout <<
"\n=== System Resource Metrics ===" << std::endl;
36 std::cout <<
"CPU:" << std::endl;
37 std::cout <<
" Usage: " << std::fixed << std::setprecision(2)
39 std::cout <<
" User: " << resources.
cpu.
user_percent <<
"%" << std::endl;
41 std::cout <<
" Idle: " << resources.
cpu.
idle_percent <<
"%" << std::endl;
42 std::cout <<
" Core Count: " << resources.
cpu.
count << std::endl;
43 std::cout <<
" Load Average: "
49 std::cout <<
"\nMemory:" << std::endl;
50 std::cout <<
" Total: " << (resources.
memory.
total_bytes / (1024.0 * 1024.0 * 1024.0))
51 <<
" GB" << std::endl;
52 std::cout <<
" Used: " << (resources.
memory.
used_bytes / (1024.0 * 1024.0 * 1024.0))
55 <<
" GB" << std::endl;
60 std::cout <<
"\nDisk:" << std::endl;
61 std::cout <<
" Total: " << (resources.
disk.
total_bytes / (1024.0 * 1024.0 * 1024.0))
62 <<
" GB" << std::endl;
63 std::cout <<
" Used: " << (resources.
disk.
used_bytes / (1024.0 * 1024.0 * 1024.0))
71 std::cout <<
"\nNetwork:" << std::endl;
82 std::cout <<
"\nProcess:" << std::endl;
83 std::cout <<
" Count: " << resources.
process.
count << std::endl;
89 std::cout <<
"\nContext Switches:" << std::endl;
100 std::cout <<
"\n=== Network Collector Metrics ===" << std::endl;
102 for (
const auto& m : metrics) {
103 std::cout <<
" " << m.name <<
": ";
104 std::visit([](
const auto& val) { std::cout << val; }, m.value);
105 auto unit_it = m.tags.find(
"unit");
106 if (unit_it != m.tags.end() && !unit_it->second.empty()) {
107 std::cout <<
" " << unit_it->second;
109 std::cout << std::endl;
117 std::cout <<
"\n=== Process Collector Metrics ===" << std::endl;
119 for (
const auto& m : metrics) {
120 std::cout <<
" " << m.name <<
": ";
121 std::visit([](
const auto& val) { std::cout << val; }, m.value);
122 auto unit_it = m.tags.find(
"unit");
123 if (unit_it != m.tags.end() && !unit_it->second.empty()) {
124 std::cout <<
" " << unit_it->second;
126 std::cout << std::endl;
131 std::cout <<
"=== System Collectors Example ===" << std::endl;
135 std::cout <<
"\n1. Creating system_resource_collector..." << std::endl;
145 sys_config.
interval = std::chrono::milliseconds(1000);
150 std::unordered_map<std::string, std::string> init_config;
152 std::cerr <<
"Failed to initialize system_resource_collector" << std::endl;
156 std::cout <<
" Initialized: " << sys_collector.
get_name() << std::endl;
157 std::cout <<
" Health: " << (sys_collector.
is_healthy() ?
"OK" :
"UNHEALTHY") << std::endl;
160 std::cout <<
"\n2. Creating network_metrics_collector..." << std::endl;
164 std::cerr <<
"Failed to initialize network_metrics_collector" << std::endl;
168 std::cout <<
" Initialized: " << net_collector.
name() << std::endl;
169 std::cout <<
" Health: " << (net_collector.
is_available() ?
"OK" :
"UNHEALTHY") << std::endl;
172 std::cout <<
"\n3. Creating process_metrics_collector..." << std::endl;
175 if (!proc_collector.
initialize(init_config)) {
176 std::cerr <<
"Failed to initialize process_metrics_collector" << std::endl;
180 std::cout <<
" Initialized: " << proc_collector.
name() << std::endl;
181 std::cout <<
" Health: " << (proc_collector.
is_available() ?
"OK" :
"UNHEALTHY") << std::endl;
184 std::cout <<
"\n4. Demonstrating collector lifecycle (3 iterations)..." << std::endl;
186 for (
int i = 0; i < 3; ++i) {
187 std::cout <<
"\n--- Iteration " << (i + 1) <<
"/3 ---" << std::endl;
190 auto sys_metrics = sys_collector.
collect();
191 std::cout <<
"System metrics collected: " << sys_metrics.size() << std::endl;
198 auto net_metrics = net_collector.
collect();
199 std::cout <<
"Network metrics collected: " << net_metrics.size() << std::endl;
203 auto proc_metrics = proc_collector.
collect();
204 std::cout <<
"Process metrics collected: " << proc_metrics.size() << std::endl;
209 std::cout <<
"\nWaiting 2 seconds before next collection..." << std::endl;
210 std::this_thread::sleep_for(2s);
215 std::cout <<
"\n5. Collector Statistics:" << std::endl;
218 std::cout <<
"\nSystem Resource Collector:" << std::endl;
219 for (
const auto& [key, value] : sys_stats) {
220 std::cout <<
" " << key <<
": " << value << std::endl;
224 std::cout <<
"\nNetwork Metrics Collector:" << std::endl;
225 for (
const auto& [key, value] : net_stats) {
226 std::cout <<
" " << key <<
": " << value << std::endl;
230 std::cout <<
"\nProcess Metrics Collector:" << std::endl;
231 for (
const auto& [key, value] : proc_stats) {
232 std::cout <<
" " << key <<
": " << value << std::endl;
237 std::cout <<
"\n6. Load Average History:" << std::endl;
240 std::cout <<
" Total samples: " << load_history.size() << std::endl;
242 if (!load_history.empty()) {
245 std::cout <<
" 5-min avg: " << load_stats.load_5m_stats.avg << std::endl;
246 std::cout <<
" 15-min avg: " << load_stats.load_15m_stats.avg << std::endl;
250 std::cout <<
"\n=== Example completed successfully ===" << std::endl;
252 }
catch (
const std::exception& e) {
253 std::cerr <<
"Exception: " << e.what() << std::endl;
Unified network metrics collector implementing collector_plugin interface.
auto collect() -> std::vector< metric > override
Collect current metrics from this plugin.
auto is_available() const -> bool override
Check if this plugin is available on the current system.
auto initialize(const config_map &config) -> bool override
auto get_statistics() const -> stats_map override
auto name() const -> std::string_view override
Get the unique name of this plugin.
Unified process-level metrics collector.
bool initialize(const config_map &config) override
Initialize plugin with configuration.
auto collect() -> std::vector< metric > override
Collect current metrics from this plugin.
auto get_statistics() const -> stats_map override
auto is_available() const -> bool override
Check if this plugin is available on the current system.
auto name() const -> std::string_view override
Get the unique name of this plugin.
bool initialize(const std::unordered_map< std::string, std::string > &config) override
std::unordered_map< std::string, double > get_statistics() const override
system_resources get_last_resources() const
bool is_healthy() const override
load_average_statistics get_all_load_statistics() const
std::string get_name() const override
std::vector< load_average_sample > get_all_load_history() const
std::vector< metric > collect() override
Unified network metrics collector for socket buffers and TCP states.
Unified process-level metrics collector.
time_series_statistics load_1m_stats
std::chrono::milliseconds interval
size_t load_history_max_samples
struct kcenon::monitoring::system_resources::cpu_metrics::load_average load
size_t write_bytes_per_sec
size_t read_bytes_per_sec
struct kcenon::monitoring::system_resources::disk_metrics::io_throughput io
struct kcenon::monitoring::system_resources::memory_metrics::swap_info swap
size_t tx_packets_per_sec
size_t rx_packets_per_sec
size_t open_file_descriptors
struct kcenon::monitoring::system_resources::memory_metrics memory
struct kcenon::monitoring::system_resources::cpu_metrics cpu
struct kcenon::monitoring::system_resources::process_metrics process
struct kcenon::monitoring::system_resources::disk_metrics disk
struct kcenon::monitoring::system_resources::network_metrics network
struct kcenon::monitoring::system_resources::context_switch_metrics context_switches
void display_network_collector_metrics(const std::vector< metric > &metrics)
void display_process_collector_metrics(const std::vector< metric > &metrics)
void display_system_metrics(const system_resources &resources)
System resource collector for CPU, memory, and disk metrics.