Monitoring System 0.1.0
System resource monitoring with pluggable collectors and alerting
Loading...
Searching...
No Matches
plugin_example/example_plugin.cpp

This file demonstrates how to create a collector plugin that can be loaded as a shared library at runtime.

Build: g++ -shared -fPIC -o libexample_plugin.so example_plugin.cpp \ -I../../include -std=c++20

Usage: auto& registry = collector_registry::instance(); registry.load_plugin("./libexample_plugin.so");

// BSD 3-Clause License
// Copyright (c) 2021-2025, 🍀☀🌕🌥 🌊
// See the LICENSE file in the project root for full license information.
#include <chrono>
#include <random>
using namespace kcenon::monitoring;
public:
example_plugin() : generator_(std::random_device{}()) {}
auto name() const -> std::string_view override {
return "example_plugin";
}
auto initialize(const config_map& config) -> bool override {
(void)config; // Unused
initialized_ = true;
return true;
}
auto shutdown() -> void override {
initialized_ = false;
}
auto collect() -> std::vector<metric_data> override {
std::vector<metric_data> metrics;
if (!initialized_) {
return metrics;
}
// Generate some dummy metrics
metric_data cpu_metric;
cpu_metric.name = "example.cpu_usage";
cpu_metric.value = generate_random_value(0.0, 100.0);
cpu_metric.unit = "%";
cpu_metric.timestamp = std::chrono::system_clock::now();
cpu_metric.labels["plugin"] = "example";
cpu_metric.labels["type"] = "cpu";
metrics.push_back(cpu_metric);
metric_data memory_metric;
memory_metric.name = "example.memory_usage";
memory_metric.value = generate_random_value(0.0, 1024.0);
memory_metric.unit = "MB";
memory_metric.timestamp = std::chrono::system_clock::now();
memory_metric.labels["plugin"] = "example";
memory_metric.labels["type"] = "memory";
metrics.push_back(memory_metric);
metric_data counter_metric;
counter_metric.name = "example.request_count";
counter_metric.value = static_cast<double>(++request_counter_);
counter_metric.unit = "requests";
counter_metric.timestamp = std::chrono::system_clock::now();
counter_metric.labels["plugin"] = "example";
counter_metric.labels["type"] = "counter";
metrics.push_back(counter_metric);
return metrics;
}
auto is_available() const -> bool override {
// This plugin is available on all platforms
return true;
}
auto get_metadata() const -> plugin_metadata_t override {
return plugin_metadata_t{
plugin_category::custom,
plugin_type::collector,
"Example Plugin",
"1.0.0",
"Demonstrates dynamic plugin loading"
};
}
private:
auto generate_random_value(double min, double max) -> double {
std::uniform_real_distribution<double> dist(min, max);
return dist(generator_);
}
bool initialized_{false};
uint64_t request_counter_{0};
std::mt19937 generator_;
};
// Export plugin using the IMPLEMENT_PLUGIN macro
IMPLEMENT_PLUGIN(
"example_plugin",
"1.0.0",
"Example dynamically loaded collector plugin",
"kcenon",
"custom"
)
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.