Monitoring System 0.1.0
System resource monitoring with pluggable collectors and alerting
Loading...
Searching...
No Matches
smart_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
18#include <atomic>
19#include <chrono>
20#include <cstdint>
21#include <memory>
22#include <mutex>
23#include <string>
24#include <unordered_map>
25#include <vector>
26
29
30namespace kcenon {
31namespace monitoring {
32
37 // Disk identification
38 std::string device_path;
39 std::string model_name;
40 std::string serial_number;
41 std::string firmware_version;
42
43 // Health status
44 bool smart_supported{false};
45 bool smart_enabled{false};
46 bool health_ok{true};
47
48 // SMART attributes
49 double temperature_celsius{0.0};
50 uint64_t reallocated_sectors{0};
51 uint64_t power_on_hours{0};
52 uint64_t power_cycle_count{0};
53 uint64_t pending_sectors{0};
55 uint64_t read_error_rate{0};
56 uint64_t write_error_rate{0};
57
58 // Timestamp
59 std::chrono::system_clock::time_point timestamp;
60};
61
65struct disk_info {
66 std::string device_path;
67 std::string device_type;
68 bool smart_available{false};
69};
70
75 public:
78
79 // Non-copyable, non-moveable due to internal state
84
90
95 std::vector<disk_info> enumerate_disks();
96
103
104 private:
105 mutable std::mutex mutex_;
106 mutable bool smartctl_checked_{false};
107 mutable bool smartctl_available_{false};
108
109 // Helper methods
110 std::string execute_command(const std::string& command) const;
111 smart_disk_metrics parse_smartctl_json(const std::string& json_output,
112 const disk_info& info) const;
113};
114
122 public:
124 ~smart_collector() = default;
125
126 // Non-copyable, non-moveable due to internal state
131
132 // collector_plugin interface implementation
133 auto name() const -> std::string_view override { return "smart_collector"; }
134 auto collect() -> std::vector<metric> override;
135 auto interval() const -> std::chrono::milliseconds override { return collection_interval_; }
136 auto is_available() const -> bool override;
141 bool is_healthy() const;
142 auto get_metric_types() const -> std::vector<std::string> override;
143
149 bool initialize(const std::unordered_map<std::string, std::string>& config) override;
150
151
156 std::unordered_map<std::string, double> get_statistics() const override;
157
163
168 bool is_smart_available() const;
169
170 private:
172
173 // Configuration
174 bool enabled_{true};
177 std::chrono::milliseconds collection_interval_{std::chrono::seconds(300)};
178
179 // Statistics
180 mutable std::mutex stats_mutex_;
181 std::atomic<size_t> collection_count_{0};
182 std::atomic<size_t> collection_errors_{0};
183 std::atomic<size_t> disks_found_{0};
184 std::vector<smart_disk_metrics> last_metrics_;
185
186 // Helper methods
187 metric create_metric(const std::string& name, double value, const smart_disk_metrics& disk,
188 const std::string& unit = "") const;
189 void add_disk_metrics(std::vector<metric>& metrics, const smart_disk_metrics& disk);
190};
191
192} // namespace monitoring
193} // namespace kcenon
Pure virtual interface for metric collector plugins.
auto is_available() const -> bool override
Check if this plugin is available on the current system.
std::atomic< size_t > collection_errors_
auto name() const -> std::string_view override
Get the unique name of this plugin.
smart_collector(const smart_collector &)=delete
metric create_metric(const std::string &name, double value, const smart_disk_metrics &disk, const std::string &unit="") const
auto collect() -> std::vector< metric > override
Collect current metrics from this plugin.
std::vector< smart_disk_metrics > get_last_metrics() const
std::vector< smart_disk_metrics > last_metrics_
smart_collector(smart_collector &&)=delete
smart_collector & operator=(smart_collector &&)=delete
std::chrono::milliseconds collection_interval_
std::unique_ptr< smart_info_collector > collector_
auto get_metric_types() const -> std::vector< std::string > override
Get supported metric types.
bool initialize(const std::unordered_map< std::string, std::string > &config) override
std::atomic< size_t > collection_count_
auto interval() const -> std::chrono::milliseconds override
Get the collection interval for this plugin.
std::unordered_map< std::string, double > get_statistics() const override
smart_collector & operator=(const smart_collector &)=delete
void add_disk_metrics(std::vector< metric > &metrics, const smart_disk_metrics &disk)
smart_info_collector(smart_info_collector &&)=delete
smart_info_collector & operator=(const smart_info_collector &)=delete
smart_disk_metrics parse_smartctl_json(const std::string &json_output, const disk_info &info) const
smart_info_collector(const smart_info_collector &)=delete
std::string execute_command(const std::string &command) const
std::vector< disk_info > enumerate_disks()
smart_info_collector & operator=(smart_info_collector &&)=delete
smart_disk_metrics collect_smart_metrics(const disk_info &info)
Plugin interface for metric collectors.
Adapter for metric types to support interface definitions.
@ info
Informational, no action required.
bool smart_available
Whether SMART data might be available.
std::string device_path
Device path.
std::string device_type
Device type (e.g., ata, nvme, scsi)
Basic metric structure for interface compatibility.
uint64_t power_on_hours
Total power-on hours.
uint64_t power_cycle_count
Number of power cycles.
std::string firmware_version
Firmware version.
std::string model_name
Disk model name.
uint64_t uncorrectable_errors
Uncorrectable error count.
bool smart_supported
Whether SMART is supported.
double temperature_celsius
Current temperature in Celsius.
std::chrono::system_clock::time_point timestamp
uint64_t pending_sectors
Sectors pending reallocation.
uint64_t write_error_rate
Write error rate (raw value)
uint64_t read_error_rate
Read error rate (raw value)
std::string device_path
Device path (e.g., /dev/sda, /dev/disk0)
std::string serial_number
Disk serial number.
bool health_ok
Overall health status (PASSED = true)
uint64_t reallocated_sectors
Reallocated sector count.
bool smart_enabled
Whether SMART is enabled.