Monitoring System 0.1.0
System resource monitoring with pluggable collectors and alerting
Loading...
Searching...
No Matches
plugin_collector_example.cpp File Reference

Demonstrates plugin-based metric collector setup and usage. More...

Include dependency graph for plugin_collector_example.cpp:

Go to the source code of this file.

Functions

int main ()
 

Detailed Description

Demonstrates plugin-based metric collector setup and usage.

Definition in file plugin_collector_example.cpp.

Function Documentation

◆ main()

int main ( )

Definition at line 28 of file plugin_collector_example.cpp.

28 {
29 std::cout << "=== Plugin-based Metric Collector Example ===" << std::endl;
30
31 // Create plugin collector with configuration
33 config.collection_interval = std::chrono::milliseconds(1000);
34 config.enable_caching = true;
35 config.enable_streaming = false;
36 config.worker_threads = 2;
37
38 auto collector = std::make_unique<plugin_metric_collector>(config);
39
40 // Create and register system resource collector
41 auto sys_collector = std::make_unique<system_resource_collector>();
42 if (sys_collector->initialize({})) {
43 std::cout << "System resource collector initialized" << std::endl;
44 collector->register_plugin(std::move(sys_collector));
45 }
46
47 // Create and register thread system collector
48 auto thread_collector = std::make_unique<thread_system_collector>();
49 if (thread_collector->initialize({})) {
50 std::cout << "Thread system collector initialized" << std::endl;
51 collector->register_plugin(std::move(thread_collector));
52 }
53
54 // Create and register logger system collector
55 auto logger_collector = std::make_unique<logger_system_collector>();
56 if (logger_collector->initialize({})) {
57 std::cout << "Logger system collector initialized" << std::endl;
58 collector->register_plugin(std::move(logger_collector));
59 }
60
61 // List registered plugins
62 std::cout << "\nRegistered plugins:" << std::endl;
63 for (const auto& plugin_name : collector->get_registered_plugins()) {
64 std::cout << " - " << plugin_name << std::endl;
65 }
66
67 // Start collection
68 if (collector->start()) {
69 std::cout << "\nCollection started successfully" << std::endl;
70 } else {
71 std::cerr << "Failed to start collection" << std::endl;
72 return 1;
73 }
74
75 // Run for a few seconds and collect metrics
76 std::cout << "\nCollecting metrics for 5 seconds..." << std::endl;
77 for (int i = 0; i < 5; ++i) {
78 std::this_thread::sleep_for(std::chrono::seconds(1));
79
80 // Force immediate collection
81 auto metrics = collector->force_collect();
82 std::cout << "Collected " << metrics.size() << " metrics" << std::endl;
83
84 // Display some metrics
85 for (const auto& metric : metrics) {
86 if (i == 0) { // Only show first iteration to avoid clutter
87 std::cout << " " << metric.name << ": " << metric.value << " " << metric.unit << std::endl;
88 }
89 }
90 }
91
92 // Get cached metrics
93 auto cached = collector->get_cached_metrics();
94 std::cout << "\nTotal cached metrics: " << cached.size() << std::endl;
95
96 // Stop collection
97 collector->stop();
98 std::cout << "Collection stopped" << std::endl;
99
100 return 0;
101}
Basic metric structure for interface compatibility.
std::variant< double, int64_t, std::string > value

References kcenon::monitoring::plugin_collector_config::collection_interval, kcenon::monitoring::plugin_collector_config::enable_caching, kcenon::monitoring::plugin_collector_config::enable_streaming, kcenon::monitoring::metric::name, kcenon::monitoring::metric::value, and kcenon::monitoring::plugin_collector_config::worker_threads.