Monitoring System 0.1.0
System resource monitoring with pluggable collectors and alerting
Loading...
Searching...
No Matches
plugin_collector_example.cpp
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2021-2025, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
17#include <chrono>
18#include <iostream>
19#include <thread>
20
25
26using namespace kcenon::monitoring;
27
28int main() {
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}
Metric collector for logger_system log statistics.
Plugin-based metric collector with dynamic discovery and registration.
Basic metric structure for interface compatibility.
std::variant< double, int64_t, std::string > value
System resource collector for CPU, memory, and disk metrics.
Metric collector for thread_system pool and queue statistics.