Monitoring System 0.1.0
System resource monitoring with pluggable collectors and alerting
Loading...
Searching...
No Matches
network_metrics_collector.h
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
5#pragma once
6
26#include <atomic>
27#include <chrono>
28#include <cstdint>
29#include <memory>
30#include <string>
31#include <unordered_map>
32#include <vector>
33
36
37namespace kcenon {
38namespace monitoring {
39
44enum class tcp_state {
45 ESTABLISHED = 1,
46 SYN_SENT = 2,
47 SYN_RECV = 3,
48 FIN_WAIT1 = 4,
49 FIN_WAIT2 = 5,
50 TIME_WAIT = 6,
51 CLOSE = 7,
52 CLOSE_WAIT = 8,
53 LAST_ACK = 9,
54 LISTEN = 10,
55 CLOSING = 11,
56 UNKNOWN = 0
57};
58
64inline std::string tcp_state_to_string(tcp_state state) {
65 switch (state) {
66 case tcp_state::ESTABLISHED: return "ESTABLISHED";
67 case tcp_state::SYN_SENT: return "SYN_SENT";
68 case tcp_state::SYN_RECV: return "SYN_RECV";
69 case tcp_state::FIN_WAIT1: return "FIN_WAIT1";
70 case tcp_state::FIN_WAIT2: return "FIN_WAIT2";
71 case tcp_state::TIME_WAIT: return "TIME_WAIT";
72 case tcp_state::CLOSE: return "CLOSE";
73 case tcp_state::CLOSE_WAIT: return "CLOSE_WAIT";
74 case tcp_state::LAST_ACK: return "LAST_ACK";
75 case tcp_state::LISTEN: return "LISTEN";
76 case tcp_state::CLOSING: return "CLOSING";
77 default: return "UNKNOWN";
78 }
79}
80
86 uint64_t established{0};
87 uint64_t syn_sent{0};
88 uint64_t syn_recv{0};
89 uint64_t fin_wait1{0};
90 uint64_t fin_wait2{0};
91 uint64_t time_wait{0};
92 uint64_t close{0};
93 uint64_t close_wait{0};
94 uint64_t last_ack{0};
95 uint64_t listen{0};
96 uint64_t closing{0};
97 uint64_t unknown{0};
98
104 uint64_t get_count(tcp_state state) const {
105 switch (state) {
107 case tcp_state::SYN_SENT: return syn_sent;
108 case tcp_state::SYN_RECV: return syn_recv;
109 case tcp_state::FIN_WAIT1: return fin_wait1;
110 case tcp_state::FIN_WAIT2: return fin_wait2;
111 case tcp_state::TIME_WAIT: return time_wait;
112 case tcp_state::CLOSE: return close;
114 case tcp_state::LAST_ACK: return last_ack;
115 case tcp_state::LISTEN: return listen;
116 case tcp_state::CLOSING: return closing;
117 default: return unknown;
118 }
119 }
120
125 void increment(tcp_state state) {
126 switch (state) {
127 case tcp_state::ESTABLISHED: ++established; break;
128 case tcp_state::SYN_SENT: ++syn_sent; break;
129 case tcp_state::SYN_RECV: ++syn_recv; break;
130 case tcp_state::FIN_WAIT1: ++fin_wait1; break;
131 case tcp_state::FIN_WAIT2: ++fin_wait2; break;
132 case tcp_state::TIME_WAIT: ++time_wait; break;
133 case tcp_state::CLOSE: ++close; break;
134 case tcp_state::CLOSE_WAIT: ++close_wait; break;
135 case tcp_state::LAST_ACK: ++last_ack; break;
136 case tcp_state::LISTEN: ++listen; break;
137 case tcp_state::CLOSING: ++closing; break;
138 default: ++unknown; break;
139 }
140 }
141
146 uint64_t total() const {
149 }
150};
151
164
170 // Socket buffer metrics
171 uint64_t recv_buffer_bytes{0};
172 uint64_t send_buffer_bytes{0};
174 uint64_t socket_count{0};
175 uint64_t tcp_socket_count{0};
176 uint64_t udp_socket_count{0};
178
179 // TCP state metrics
181 uint64_t total_connections{0};
183
184 std::chrono::system_clock::time_point timestamp;
185};
186
187// Forward declaration
188namespace platform {
189class metrics_provider;
190} // namespace platform
191
200 public:
203
204 // Non-copyable, non-moveable due to internal state
209
215
221
228
229 private:
230 std::unique_ptr<platform::metrics_provider> provider_;
231};
232
241 public:
243 ~network_metrics_collector() override = default;
244
245 // Non-copyable, non-moveable due to internal state
250
251 // collector_plugin implementation
252 auto name() const -> std::string_view override { return "network_metrics_collector"; }
253 auto collect() -> std::vector<metric> override;
254 auto interval() const -> std::chrono::milliseconds override { return std::chrono::seconds(10); }
255 auto is_available() const -> bool override;
256 auto get_metric_types() const -> std::vector<std::string> override;
257
258 auto get_metadata() const -> plugin_metadata override {
259 return plugin_metadata{
260 .name = name(),
261 .description = "Network metrics (socket buffers, TCP states)",
262 .category = plugin_category::network,
263 .version = "1.0.0",
264 .dependencies = {},
265 .requires_platform_support = true
266 };
267 }
268
280 auto initialize(const config_map& config) -> bool override;
281
286 auto get_statistics() const -> stats_map override;
287
293
299
305
306 private:
308
309 // Configuration
310 bool enabled_{true};
312
313 // Last metrics cache
315
316 // Statistics
317 std::atomic<size_t> collection_count_{0};
318 std::atomic<size_t> collection_errors_{0};
319
320 // Helper methods
321 void add_socket_buffer_metrics(std::vector<metric>& metrics,
322 const network_metrics& data);
323 void add_tcp_state_metrics(std::vector<metric>& metrics,
324 const network_metrics& data);
325};
326
327} // namespace monitoring
328} // namespace kcenon
Pure virtual interface for metric collector plugins.
Internal network data collector using platform abstraction layer.
network_metrics collect_metrics(const network_metrics_config &config)
network_info_collector(network_info_collector &&)=delete
network_info_collector & operator=(network_info_collector &&)=delete
network_info_collector(const network_info_collector &)=delete
std::unique_ptr< platform::metrics_provider > provider_
network_info_collector & operator=(const network_info_collector &)=delete
Unified network metrics collector implementing collector_plugin interface.
auto collect() -> std::vector< metric > override
Collect current metrics from this plugin.
auto get_metadata() const -> plugin_metadata override
Get plugin metadata.
auto is_available() const -> bool override
Check if this plugin is available on the current system.
void add_tcp_state_metrics(std::vector< metric > &metrics, const network_metrics &data)
auto initialize(const config_map &config) -> bool override
auto get_statistics() const -> stats_map override
void add_socket_buffer_metrics(std::vector< metric > &metrics, const network_metrics &data)
auto name() const -> std::string_view override
Get the unique name of this plugin.
network_metrics_collector(network_metrics_collector &&)=delete
std::unique_ptr< network_info_collector > collector_
auto interval() const -> std::chrono::milliseconds override
Get the collection interval for this plugin.
network_metrics_collector & operator=(network_metrics_collector &&)=delete
network_metrics_collector & operator=(const network_metrics_collector &)=delete
network_metrics_collector(const network_metrics_collector &)=delete
auto get_metric_types() const -> std::vector< std::string > override
Get supported metric types.
Plugin interface for metric collectors.
Adapter for metric types to support interface definitions.
std::unordered_map< std::string, double > stats_map
Type alias for statistics map.
std::string tcp_state_to_string(tcp_state state)
Convert tcp_state to string representation.
tcp_state
TCP connection states as defined in RFC 793.
@ FIN_WAIT1
FIN sent, waiting for ACK or FIN.
@ SYN_SENT
SYN sent, waiting for SYN-ACK.
@ TIME_WAIT
Waiting for enough time to pass (2MSL)
@ FIN_WAIT2
FIN-ACK received, waiting for FIN.
@ UNKNOWN
Unknown or invalid state.
@ SYN_RECV
SYN received, SYN-ACK sent.
@ CLOSE
Connection closed.
@ ESTABLISHED
Connection established.
@ CLOSE_WAIT
Remote side has closed, waiting for local close.
@ LAST_ACK
FIN sent after CLOSE_WAIT, waiting for ACK.
@ CLOSING
Both sides sent FIN simultaneously.
@ LISTEN
Listening for incoming connections.
@ network
Network metrics (connectivity, bandwidth)
std::unordered_map< std::string, std::string > config_map
Type alias for configuration map.
@ platform
Platform/system power domain.
Configuration for network metrics collector.
bool collect_socket_buffers
Enable socket buffer collection.
uint64_t close_wait_warning_threshold
CLOSE_WAIT warning threshold.
uint64_t time_wait_warning_threshold
TIME_WAIT warning threshold.
uint64_t queue_full_threshold_bytes
Socket queue full threshold.
uint64_t memory_warning_threshold_bytes
Socket memory warning (100MB)
bool collect_tcp_states
Enable TCP state collection.
Aggregated network metrics from all sources.
uint64_t send_buffer_bytes
Total bytes in send buffers.
tcp_state_counts tcp_counts
TCP state counts.
std::chrono::system_clock::time_point timestamp
Reading timestamp.
bool socket_buffer_available
Socket buffer metrics availability.
bool tcp_state_available
TCP state metrics availability.
uint64_t socket_count
Total number of sockets.
uint64_t recv_buffer_bytes
Total bytes in receive buffers.
uint64_t udp_socket_count
Number of UDP sockets.
uint64_t tcp_socket_count
Number of TCP sockets.
uint64_t socket_memory_bytes
Total socket buffer memory used.
uint64_t total_connections
Total TCP connections.
Metadata describing a collector plugin.
Counts of connections in each TCP state.
uint64_t close_wait
CLOSE_WAIT connections (leak indicator)
uint64_t established
ESTABLISHED connections.
uint64_t get_count(tcp_state state) const
uint64_t unknown
Unknown state connections.