Monitoring System 0.1.0
System resource monitoring with pluggable collectors and alerting
Loading...
Searching...
No Matches
plugin_loader_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
26
27#include <iostream>
28#include <thread>
29#include <chrono>
30
31using namespace kcenon::monitoring;
32
33int main(int argc, char* argv[]) {
34 if (argc < 2) {
35 std::cerr << "Usage: " << argv[0] << " <plugin_path>\n";
36 std::cerr << "Example: " << argv[0] << " ./libexample_plugin.so\n";
37 return 1;
38 }
39
40 std::string plugin_path = argv[1];
41
42 std::cout << "=== Dynamic Plugin Loading Example ===\n\n";
43
44 // Get the collector registry instance
45 auto& registry = collector_registry::instance();
46
47 // Load the plugin from shared library
48 std::cout << "Loading plugin from: " << plugin_path << "\n";
49 if (!registry.load_plugin(plugin_path)) {
50 std::cerr << "Failed to load plugin: "
51 << registry.get_plugin_loader_error() << "\n";
52 return 1;
53 }
54 std::cout << "Plugin loaded successfully\n\n";
55
56 // Get the loaded plugin
57 auto* plugin = registry.get_plugin("example_plugin");
58 if (!plugin) {
59 std::cerr << "Plugin not found in registry\n";
60 return 1;
61 }
62
63 // Display plugin metadata
64 auto metadata = plugin->get_metadata();
65 std::cout << "Plugin Metadata:\n";
66 std::cout << " Name: " << plugin->name() << "\n";
67 std::cout << " Description: " << metadata.description << "\n";
68 std::cout << " Version: " << metadata.version << "\n";
69 std::cout << " Available: " << (plugin->is_available() ? "yes" : "no") << "\n\n";
70
71 // Initialize the plugin
72 std::cout << "Initializing plugin...\n";
73 config_map config; // Empty config for this example
74 if (!plugin->initialize(config)) {
75 std::cerr << "Failed to initialize plugin\n";
76 return 1;
77 }
78 std::cout << "Plugin initialized\n\n";
79
80 // Collect metrics from the plugin
81 std::cout << "Collecting metrics (5 iterations)...\n";
82 for (int i = 0; i < 5; ++i) {
83 std::cout << "\nIteration " << (i + 1) << ":\n";
84
85 auto metrics = plugin->collect();
86 for (const auto& metric : metrics) {
87 std::cout << " " << metric.name << ": " << metric.value
88 << " " << metric.unit;
89
90 if (!metric.labels.empty()) {
91 std::cout << " [";
92 bool first = true;
93 for (const auto& [key, value] : metric.labels) {
94 if (!first) std::cout << ", ";
95 std::cout << key << "=" << value;
96 first = false;
97 }
98 std::cout << "]";
99 }
100 std::cout << "\n";
101 }
102
103 // Wait before next collection
104 if (i < 4) {
105 std::this_thread::sleep_for(std::chrono::seconds(1));
106 }
107 }
108
109 // Shutdown and unload the plugin
110 std::cout << "\nShutting down plugin...\n";
111 plugin->shutdown();
112
113 std::cout << "Unloading plugin...\n";
114 if (!registry.unload_plugin("example_plugin")) {
115 std::cerr << "Failed to unload plugin\n";
116 return 1;
117 }
118 std::cout << "Plugin unloaded successfully\n";
119
120 std::cout << "\n=== Example Complete ===\n";
121 return 0;
122}
static auto instance() -> collector_registry &
Get the singleton instance.
Registry for managing collector plugin lifecycle.
std::unordered_map< std::string, std::string > config_map
Type alias for configuration map.
Dynamic plugin loading from shared libraries.
Basic metric structure for interface compatibility.
std::variant< double, int64_t, std::string > value