Monitoring System 0.1.0
System resource monitoring with pluggable collectors and alerting
Loading...
Searching...
No Matches
vm_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
16#include <atomic>
17#include <chrono>
18#include <cstdint>
19#include <memory>
20#include <mutex>
21#include <string>
22#include <unordered_map>
23#include <vector>
24
27
28namespace kcenon {
29namespace monitoring {
30
35enum class vm_type {
36 none = 0,
37 kvm = 1,
38 hyperv = 2,
39 vmware = 3,
40 virtualbox = 4,
41 xen = 5,
42 docker = 6,
43 other = 7
44};
45
51inline std::string vm_type_to_string(vm_type type) {
52 switch (type) {
53 case vm_type::none: return "NONE";
54 case vm_type::kvm: return "KVM";
55 case vm_type::hyperv: return "HYPER-V";
56 case vm_type::vmware: return "VMWARE";
57 case vm_type::virtualbox: return "VIRTUALBOX";
58 case vm_type::xen: return "XEN";
59 case vm_type::docker: return "DOCKER";
60 case vm_type::other: return "OTHER";
61 default: return "UNKNOWN";
62 }
63}
64
69struct vm_metrics {
70 bool is_virtualized{false};
73 std::string hypervisor_vendor;
74
75 // Additional possibilities:
76 // - host_physical_cpu_count (if exposed)
77 // - ballooned_memory (if relevant)
78};
79
85 public:
88
89 // Mac-specific / Platform-specific detection
91
92 private:
93 // Caching static info since VM type doesn't change at runtime usually
94 bool info_cached_{false};
96
99};
100
108 public:
110 ~vm_collector() = default;
111
112 // Non-copyable, non-moveable
113 vm_collector(const vm_collector&) = delete;
117
118 // collector_plugin interface implementation
119 auto name() const -> std::string_view override { return "vm_collector"; }
120 auto collect() -> std::vector<metric> override;
121 auto interval() const -> std::chrono::milliseconds override { return collection_interval_; }
122 auto is_available() const -> bool override;
127 bool is_healthy() const;
128 auto get_metric_types() const -> std::vector<std::string> override;
129
135 bool initialize(const std::unordered_map<std::string, std::string>& config) override;
136
137
142 std::unordered_map<std::string, double> get_statistics() const override;
143
144 private:
146
147 // Configuration
148 bool enabled_{true};
149 std::chrono::milliseconds collection_interval_{std::chrono::seconds(30)};
150
151 // Statistics
152 mutable std::mutex stats_mutex_;
153 std::atomic<size_t> collection_count_{0};
154 std::atomic<size_t> collection_errors_{0};
155
156 // Helper methods
157 metric create_metric(const std::string& name, double value,
158 const std::unordered_map<std::string, std::string>& tags = {},
159 const std::string& unit = "") const;
160};
161
162} // namespace monitoring
163} // namespace kcenon
Pure virtual interface for metric collector plugins.
Virtualization metrics monitoring collector.
std::unique_ptr< vm_info_collector > collector_
auto collect() -> std::vector< metric > override
Collect current metrics from this plugin.
std::chrono::milliseconds collection_interval_
vm_collector(const vm_collector &)=delete
bool initialize(const std::unordered_map< std::string, std::string > &config) override
auto get_metric_types() const -> std::vector< std::string > override
Get supported metric types.
std::unordered_map< std::string, double > get_statistics() const override
auto is_available() const -> bool override
Check if this plugin is available on the current system.
vm_collector(vm_collector &&)=delete
std::atomic< size_t > collection_count_
std::atomic< size_t > collection_errors_
vm_collector & operator=(vm_collector &&)=delete
vm_collector & operator=(const vm_collector &)=delete
metric create_metric(const std::string &name, double value, const std::unordered_map< std::string, std::string > &tags={}, const std::string &unit="") const
auto name() const -> std::string_view override
Get the unique name of this plugin.
auto interval() const -> std::chrono::milliseconds override
Get the collection interval for this plugin.
Platform-specific virtualization data collector implementation.
Plugin interface for metric collectors.
Adapter for metric types to support interface definitions.
@ none
Not in a cgroup or not Linux.
vm_type
Detected virtualization platform.
@ docker
Docker Container (if distinguishable)
@ none
Bare metal (or undetected)
@ virtualbox
Oracle VirtualBox.
@ other
Other detected virtualization.
@ hyperv
Microsoft Hyper-V.
std::string vm_type_to_string(vm_type type)
Convert vm_type to string representation.
Basic metric structure for interface compatibility.
Virtualization specific metrics.
bool is_virtualized
True if running in a VM.
double guest_cpu_steal_time
% CPU time stolen by hypervisor (if available)
std::string hypervisor_vendor
Vendor string (e.g., "KVMKVMKVM" or "Microsoft Hv")
vm_type type
Detected hypervisor type.