Monitoring System 0.1.0
System resource monitoring with pluggable collectors and alerting
Loading...
Searching...
No Matches
plugin_loader.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
36#include <memory>
37#include <mutex>
38#include <string>
39#include <string_view>
40#include <unordered_map>
41
42#include "collector_plugin.h"
43#include "plugin_api.h"
44
45namespace kcenon {
46namespace monitoring {
47
64
68auto to_string(plugin_load_error error) -> std::string;
69
78public:
79 virtual ~plugin_loader() = default;
80
95 virtual auto load_plugin(std::string_view path)
96 -> std::unique_ptr<collector_plugin> = 0;
97
110 virtual auto unload_plugin(std::string_view plugin_name) -> bool = 0;
111
116 virtual auto get_last_error() const -> plugin_load_error = 0;
117
122 virtual auto get_last_error_message() const -> std::string = 0;
123
129 virtual auto is_plugin_loaded(std::string_view plugin_name) const -> bool = 0;
130
135 virtual auto get_loaded_plugins() const -> std::vector<std::string> = 0;
136};
137
151public:
154
155 // Disable copy
158
159 // Enable move
162
163 auto load_plugin(std::string_view path)
164 -> std::unique_ptr<collector_plugin> override;
165
166 auto unload_plugin(std::string_view plugin_name) -> bool override;
167
168 auto get_last_error() const -> plugin_load_error override;
169 auto get_last_error_message() const -> std::string override;
170 auto is_plugin_loaded(std::string_view plugin_name) const -> bool override;
171 auto get_loaded_plugins() const -> std::vector<std::string> override;
172
173private:
182 struct library_handle;
183
189 std::string name;
190 std::string path;
191 std::unique_ptr<library_handle> handle;
194 };
195
201 auto load_library(std::string_view path) -> std::unique_ptr<library_handle>;
202
207 void unload_library(std::unique_ptr<library_handle> handle);
208
216 template <typename T>
217 auto resolve_symbol(library_handle* handle, const char* symbol_name) -> T;
218
224 auto verify_api_version(const plugin_api_metadata& metadata) const -> bool;
225
231 void set_error(plugin_load_error error, std::string message);
232
233 // Loaded plugins tracking
234 std::unordered_map<std::string, plugin_entry> loaded_plugins_;
235
236 // Error state
239
240 // Thread safety
241 mutable std::mutex mutex_;
242};
243
244} // namespace monitoring
245} // namespace kcenon
Pure virtual interface for metric collector plugins.
Concrete implementation of plugin_loader using OS dynamic loading APIs.
std::unordered_map< std::string, plugin_entry > loaded_plugins_
dynamic_plugin_loader(dynamic_plugin_loader &&) noexcept
void set_error(plugin_load_error error, std::string message)
Set error state.
auto load_library(std::string_view path) -> std::unique_ptr< library_handle >
Load a library file.
dynamic_plugin_loader & operator=(const dynamic_plugin_loader &)=delete
void unload_library(std::unique_ptr< library_handle > handle)
Unload a library.
dynamic_plugin_loader(const dynamic_plugin_loader &)=delete
auto resolve_symbol(library_handle *handle, const char *symbol_name) -> T
Resolve a symbol from a library.
auto verify_api_version(const plugin_api_metadata &metadata) const -> bool
Verify plugin API version compatibility.
Abstract interface for plugin loading.
virtual auto get_loaded_plugins() const -> std::vector< std::string >=0
Get list of loaded plugin names.
virtual auto get_last_error() const -> plugin_load_error=0
Get the last error that occurred.
virtual auto is_plugin_loaded(std::string_view plugin_name) const -> bool=0
Check if a plugin is currently loaded.
virtual auto unload_plugin(std::string_view plugin_name) -> bool=0
Unload a previously loaded plugin.
virtual auto get_last_error_message() const -> std::string=0
Get detailed error message for the last error.
virtual auto load_plugin(std::string_view path) -> std::unique_ptr< collector_plugin >=0
Load a plugin from a shared library.
Plugin interface for metric collectors.
@ none
Not in a cgroup or not Linux.
auto to_string(plugin_load_error error) -> std::string
Convert plugin_load_error to string.
plugin_load_error
Error codes for plugin loading operations.
C API for dynamically loaded collector plugins.
void(* destroy_plugin_fn)(kcenon::monitoring::collector_plugin *plugin)
Destroy a plugin instance.
Definition plugin_api.h:123
Information about a loaded plugin.
std::string path
std::string name
std::unique_ptr< library_handle > handle
plugin_api_metadata metadata
destroy_plugin_fn destroy_fn
Opaque handle to a loaded library.
Plugin API metadata information.
Definition plugin_api.h:79