Monitoring System 0.1.0
System resource monitoring with pluggable collectors and alerting
Loading...
Searching...
No Matches
example_plugin.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
24
25#include <chrono>
26#include <random>
27
28using namespace kcenon::monitoring;
29
35public:
36 example_plugin() : generator_(std::random_device{}()) {}
37
38 auto name() const -> std::string_view override {
39 return "example_plugin";
40 }
41
42 auto initialize(const config_map& config) -> bool override {
43 (void)config; // Unused
44 initialized_ = true;
45 return true;
46 }
47
48 auto shutdown() -> void override {
49 initialized_ = false;
50 }
51
52 auto collect() -> std::vector<metric_data> override {
53 std::vector<metric_data> metrics;
54
55 if (!initialized_) {
56 return metrics;
57 }
58
59 // Generate some dummy metrics
60 metric_data cpu_metric;
61 cpu_metric.name = "example.cpu_usage";
62 cpu_metric.value = generate_random_value(0.0, 100.0);
63 cpu_metric.unit = "%";
64 cpu_metric.timestamp = std::chrono::system_clock::now();
65 cpu_metric.labels["plugin"] = "example";
66 cpu_metric.labels["type"] = "cpu";
67 metrics.push_back(cpu_metric);
68
69 metric_data memory_metric;
70 memory_metric.name = "example.memory_usage";
71 memory_metric.value = generate_random_value(0.0, 1024.0);
72 memory_metric.unit = "MB";
73 memory_metric.timestamp = std::chrono::system_clock::now();
74 memory_metric.labels["plugin"] = "example";
75 memory_metric.labels["type"] = "memory";
76 metrics.push_back(memory_metric);
77
78 metric_data counter_metric;
79 counter_metric.name = "example.request_count";
80 counter_metric.value = static_cast<double>(++request_counter_);
81 counter_metric.unit = "requests";
82 counter_metric.timestamp = std::chrono::system_clock::now();
83 counter_metric.labels["plugin"] = "example";
84 counter_metric.labels["type"] = "counter";
85 metrics.push_back(counter_metric);
86
87 return metrics;
88 }
89
90 auto is_available() const -> bool override {
91 // This plugin is available on all platforms
92 return true;
93 }
94
95 auto get_metadata() const -> plugin_metadata_t override {
96 return plugin_metadata_t{
97 plugin_category::custom,
98 plugin_type::collector,
99 "Example Plugin",
100 "1.0.0",
101 "Demonstrates dynamic plugin loading"
102 };
103 }
104
105private:
106 auto generate_random_value(double min, double max) -> double {
107 std::uniform_real_distribution<double> dist(min, max);
108 return dist(generator_);
109 }
110
111 bool initialized_{false};
112 uint64_t request_counter_{0};
113 std::mt19937 generator_;
114};
115
116// Export plugin using the IMPLEMENT_PLUGIN macro
117IMPLEMENT_PLUGIN(
119 "example_plugin",
120 "1.0.0",
121 "Example dynamically loaded collector plugin",
122 "kcenon",
123 "custom"
124)
Example collector plugin that generates dummy metrics.
auto collect() -> std::vector< metric_data > override
Collect current metrics from this plugin.
auto initialize(const config_map &config) -> bool override
Initialize plugin with configuration.
uint64_t request_counter_
auto generate_random_value(double min, double max) -> double
auto shutdown() -> void override
Shutdown plugin and release resources.
std::mt19937 generator_
auto get_metadata() const -> plugin_metadata_t override
Get plugin metadata.
auto name() const -> std::string_view override
Get the unique name of this plugin.
auto is_available() const -> bool override
Check if this plugin is available on the current system.
Pure virtual interface for metric collector plugins.
Plugin interface for metric collectors.
std::unordered_map< std::string, std::string > config_map
Type alias for configuration map.
C API for dynamically loaded collector plugins.