2025-10-14 11:18:01 +08:00
|
|
|
#ifndef SYSTEM_MONITOR_H
|
|
|
|
|
#define SYSTEM_MONITOR_H
|
|
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <vector>
|
|
|
|
|
#include <cstdint> // for standard integer types like uint64_t
|
|
|
|
|
|
|
|
|
|
namespace SystemMonitor {
|
|
|
|
|
|
|
|
|
|
// --- 1. 定义数据结构体 ---
|
|
|
|
|
|
|
|
|
|
struct SystemInfo {
|
|
|
|
|
std::string kernelVersion;
|
|
|
|
|
std::string kernelRelease;
|
|
|
|
|
std::string architecture;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct StorageDevice {
|
|
|
|
|
std::string filesystem;
|
|
|
|
|
std::string type;
|
|
|
|
|
std::string totalSize;
|
|
|
|
|
std::string usedSize;
|
|
|
|
|
std::string availableSize;
|
|
|
|
|
std::string usePercentage;
|
|
|
|
|
std::string mountPoint;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct MemoryInfo {
|
|
|
|
|
uint64_t total_kb = 0;
|
2025-10-15 14:27:21 +08:00
|
|
|
uint64_t available_kb = 0;
|
2025-10-14 11:18:01 +08:00
|
|
|
// 未来可以添加 free, available, cached 等
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct ThermalInfo {
|
|
|
|
|
std::string zoneName;
|
|
|
|
|
double temperatureCelsius = 0.0;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct CpuUtilization {
|
|
|
|
|
double totalUsagePercentage = 0.0;
|
|
|
|
|
// 未来可以添加每个核心的利用率
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// --- 2. 定义 SystemMonitor 类 ---
|
|
|
|
|
|
|
|
|
|
class SystemMonitor {
|
|
|
|
|
public:
|
|
|
|
|
SystemMonitor() = default;
|
|
|
|
|
|
|
|
|
|
// --- 数据采集接口 ---
|
|
|
|
|
SystemInfo getSystemInfo() const;
|
|
|
|
|
std::vector<StorageDevice> getStorageInfo() const;
|
|
|
|
|
MemoryInfo getMemoryInfo() const;
|
|
|
|
|
std::vector<ThermalInfo> getChipTemperature() const;
|
|
|
|
|
CpuUtilization getCpuUtilization(int interval_ms = 1000) const;
|
|
|
|
|
std::string getKernelLogs(int last_n_lines = 20) const;
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
std::string readFile(const std::string& filePath) const;
|
|
|
|
|
std::string execCommand(const char* cmd) const;
|
|
|
|
|
|
|
|
|
|
// 用于计算CPU利用率的内部状态
|
|
|
|
|
mutable uint64_t prev_idle_ = 0;
|
|
|
|
|
mutable uint64_t prev_total_ = 0;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace SystemMonitor
|
|
|
|
|
|
|
|
|
|
#endif // SYSTEM_MONITOR_H
|