Monitoring System 0.1.0
System resource monitoring with pluggable collectors and alerting
Loading...
Searching...
No Matches
system_collectors_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 <iostream>
18#include <thread>
19#include <chrono>
20#include <iomanip>
21
25
26using namespace kcenon::monitoring;
27using namespace std::chrono_literals;
28
33 std::cout << "\n=== System Resource Metrics ===" << std::endl;
34
35 // CPU metrics
36 std::cout << "CPU:" << std::endl;
37 std::cout << " Usage: " << std::fixed << std::setprecision(2)
38 << resources.cpu.usage_percent << "%" << std::endl;
39 std::cout << " User: " << resources.cpu.user_percent << "%" << std::endl;
40 std::cout << " System: " << resources.cpu.system_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: "
44 << resources.cpu.load.one_min << " (1m), "
45 << resources.cpu.load.five_min << " (5m), "
46 << resources.cpu.load.fifteen_min << " (15m)" << std::endl;
47
48 // Memory metrics
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))
53 << " GB (" << resources.memory.usage_percent << "%)" << std::endl;
54 std::cout << " Available: " << (resources.memory.available_bytes / (1024.0 * 1024.0 * 1024.0))
55 << " GB" << std::endl;
56 std::cout << " Swap Used: " << (resources.memory.swap.used_bytes / (1024.0 * 1024.0 * 1024.0))
57 << " GB (" << resources.memory.swap.usage_percent << "%)" << std::endl;
58
59 // Disk metrics
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))
64 << " GB (" << resources.disk.usage_percent << "%)" << std::endl;
65 std::cout << " I/O Read: " << (resources.disk.io.read_bytes_per_sec / (1024.0 * 1024.0))
66 << " MB/s (" << resources.disk.io.read_ops_per_sec << " ops/s)" << std::endl;
67 std::cout << " I/O Write: " << (resources.disk.io.write_bytes_per_sec / (1024.0 * 1024.0))
68 << " MB/s (" << resources.disk.io.write_ops_per_sec << " ops/s)" << std::endl;
69
70 // Network metrics
71 std::cout << "\nNetwork:" << std::endl;
72 std::cout << " RX: " << (resources.network.rx_bytes_per_sec / (1024.0 * 1024.0))
73 << " MB/s (" << resources.network.rx_packets_per_sec << " packets/s)" << std::endl;
74 std::cout << " TX: " << (resources.network.tx_bytes_per_sec / (1024.0 * 1024.0))
75 << " MB/s (" << resources.network.tx_packets_per_sec << " packets/s)" << std::endl;
76 std::cout << " Errors: RX=" << resources.network.rx_errors
77 << ", TX=" << resources.network.tx_errors << std::endl;
78 std::cout << " Dropped: RX=" << resources.network.rx_dropped
79 << ", TX=" << resources.network.tx_dropped << std::endl;
80
81 // Process metrics
82 std::cout << "\nProcess:" << std::endl;
83 std::cout << " Count: " << resources.process.count << std::endl;
84 std::cout << " Threads: " << resources.process.thread_count << std::endl;
85 std::cout << " Handles: " << resources.process.handle_count << std::endl;
86 std::cout << " Open FDs: " << resources.process.open_file_descriptors << std::endl;
87
88 // Context switches
89 std::cout << "\nContext Switches:" << std::endl;
90 std::cout << " Total: " << resources.context_switches.total << std::endl;
91 std::cout << " Per Second: " << resources.context_switches.per_sec << std::endl;
92 std::cout << " Voluntary: " << resources.context_switches.voluntary << std::endl;
93 std::cout << " Non-voluntary: " << resources.context_switches.nonvoluntary << std::endl;
94}
95
99void display_network_collector_metrics(const std::vector<metric>& metrics) {
100 std::cout << "\n=== Network Collector Metrics ===" << std::endl;
101
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;
108 }
109 std::cout << std::endl;
110 }
111}
112
116void display_process_collector_metrics(const std::vector<metric>& metrics) {
117 std::cout << "\n=== Process Collector Metrics ===" << std::endl;
118
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;
125 }
126 std::cout << std::endl;
127 }
128}
129
130int main() {
131 std::cout << "=== System Collectors Example ===" << std::endl;
132
133 try {
134 // Step 1: Create and configure system_resource_collector
135 std::cout << "\n1. Creating system_resource_collector..." << std::endl;
136
137 system_metrics_config sys_config;
138 sys_config.collect_cpu = true;
139 sys_config.collect_memory = true;
140 sys_config.collect_disk = true;
141 sys_config.collect_network = true;
142 sys_config.collect_process = true;
143 sys_config.enable_load_history = true;
144 sys_config.load_history_max_samples = 100;
145 sys_config.interval = std::chrono::milliseconds(1000);
146
147 system_resource_collector sys_collector(sys_config);
148
149 // Initialize the collector
150 std::unordered_map<std::string, std::string> init_config;
151 if (!sys_collector.initialize(init_config)) {
152 std::cerr << "Failed to initialize system_resource_collector" << std::endl;
153 return 1;
154 }
155
156 std::cout << " Initialized: " << sys_collector.get_name() << std::endl;
157 std::cout << " Health: " << (sys_collector.is_healthy() ? "OK" : "UNHEALTHY") << std::endl;
158
159 // Step 2: Create network_metrics_collector
160 std::cout << "\n2. Creating network_metrics_collector..." << std::endl;
161
162 network_metrics_collector net_collector;
163 if (!net_collector.initialize(init_config)) {
164 std::cerr << "Failed to initialize network_metrics_collector" << std::endl;
165 return 1;
166 }
167
168 std::cout << " Initialized: " << net_collector.name() << std::endl;
169 std::cout << " Health: " << (net_collector.is_available() ? "OK" : "UNHEALTHY") << std::endl;
170
171 // Step 3: Create process_metrics_collector
172 std::cout << "\n3. Creating process_metrics_collector..." << std::endl;
173
174 process_metrics_collector proc_collector;
175 if (!proc_collector.initialize(init_config)) {
176 std::cerr << "Failed to initialize process_metrics_collector" << std::endl;
177 return 1;
178 }
179
180 std::cout << " Initialized: " << proc_collector.name() << std::endl;
181 std::cout << " Health: " << (proc_collector.is_available() ? "OK" : "UNHEALTHY") << std::endl;
182
183 // Step 4: Collector lifecycle demonstration
184 std::cout << "\n4. Demonstrating collector lifecycle (3 iterations)..." << std::endl;
185
186 for (int i = 0; i < 3; ++i) {
187 std::cout << "\n--- Iteration " << (i + 1) << "/3 ---" << std::endl;
188
189 // Collect system resources
190 auto sys_metrics = sys_collector.collect();
191 std::cout << "System metrics collected: " << sys_metrics.size() << std::endl;
192
193 // Get last collected resources for detailed display
194 auto resources = sys_collector.get_last_resources();
195 display_system_metrics(resources);
196
197 // Collect network metrics
198 auto net_metrics = net_collector.collect();
199 std::cout << "Network metrics collected: " << net_metrics.size() << std::endl;
201
202 // Collect process metrics
203 auto proc_metrics = proc_collector.collect();
204 std::cout << "Process metrics collected: " << proc_metrics.size() << std::endl;
206
207 // Wait before next collection
208 if (i < 2) {
209 std::cout << "\nWaiting 2 seconds before next collection..." << std::endl;
210 std::this_thread::sleep_for(2s);
211 }
212 }
213
214 // Step 5: Display collector statistics
215 std::cout << "\n5. Collector Statistics:" << std::endl;
216
217 auto sys_stats = sys_collector.get_statistics();
218 std::cout << "\nSystem Resource Collector:" << std::endl;
219 for (const auto& [key, value] : sys_stats) {
220 std::cout << " " << key << ": " << value << std::endl;
221 }
222
223 auto net_stats = net_collector.get_statistics();
224 std::cout << "\nNetwork Metrics Collector:" << std::endl;
225 for (const auto& [key, value] : net_stats) {
226 std::cout << " " << key << ": " << value << std::endl;
227 }
228
229 auto proc_stats = proc_collector.get_statistics();
230 std::cout << "\nProcess Metrics Collector:" << std::endl;
231 for (const auto& [key, value] : proc_stats) {
232 std::cout << " " << key << ": " << value << std::endl;
233 }
234
235 // Step 6: Load history demonstration (if enabled)
236 if (sys_config.enable_load_history) {
237 std::cout << "\n6. Load Average History:" << std::endl;
238
239 auto load_history = sys_collector.get_all_load_history();
240 std::cout << " Total samples: " << load_history.size() << std::endl;
241
242 if (!load_history.empty()) {
243 auto load_stats = sys_collector.get_all_load_statistics();
244 std::cout << " 1-min avg: " << load_stats.load_1m_stats.avg << std::endl;
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;
247 }
248 }
249
250 std::cout << "\n=== Example completed successfully ===" << std::endl;
251
252 } catch (const std::exception& e) {
253 std::cerr << "Exception: " << e.what() << std::endl;
254 return 1;
255 }
256
257 return 0;
258}
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
load_average_statistics get_all_load_statistics() const
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.
struct kcenon::monitoring::system_resources::cpu_metrics::load_average load
struct kcenon::monitoring::system_resources::disk_metrics::io_throughput io
struct kcenon::monitoring::system_resources::memory_metrics::swap_info swap
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.