// SPDX-License-Identifier: MIT
// StarAgent Native C++ - platform abstraction.
#pragma once
#include "util.hpp"
#include <atomic>
#include <cstdint>
#include <string>
#include <vector>
namespace staragent::platform {
struct MemoryInfo
{
std::uint64_t Total = 0;
std::uint64_t Available = 0;
std::uint64_t Free = 0;
};
struct DiskInfo
{
std::uint64_t Total = 0;
std::uint64_t Free = 0;
};
struct DriveEntry
{
std::string Path;
std::string FileSystem;
std::uint64_t Total = 0;
std::uint64_t Free = 0;
};
struct NetTotals
{
bool Valid = false;
std::uint64_t Sent = 0;
std::uint64_t Received = 0;
};
// TCP 连接状æ€è®¡æ•°ï¼Œå¯¹åº”心跳ä¸çš„ TcpConnections/TcpTimeWait/TcpCloseWait。
struct TcpCounts
{
std::uint64_t Established = 0;
std::uint64_t TimeWait = 0;
std::uint64_t CloseWait = 0;
};
// ç£ç›˜ IO é‡‡æ ·ï¼Œå¯¹åº”å¿ƒè·³ä¸çš„ DiskIOPS 与 DiskActiveTime。
// ActivePercent 为 0~100 的百分比,上报å‰ç”± Agent æ¢ç®—æˆæœåŠ¡ç«¯ä½¿ç”¨çš„ 0~1 比例。
struct DiskIoSample
{
std::uint64_t Iops = 0;
double ActivePercent = 0;
};
// ICMP 探测结果,对应心跳ä¸çš„网络质é‡å—段。
struct ProbeResult
{
bool Valid = false;
int Latency = 0; // å¹³å‡å»¶è¿Ÿï¼ˆæ¯«ç§’)
double LossRate = 0; // 丢包率 0~1
double Score = 0; // 综åˆè¯„分 0~1
};
struct HardwareInfo
{
std::string UUID;
std::string MachineGuid;
std::string SerialNumber;
std::string Board;
std::string DiskID;
std::string Macs;
std::string Product;
std::string Vendor;
std::string Processor;
std::string GPU;
std::string Dpi;
std::string Resolution;
std::string CLibVersion;
};
struct SystemInfo
{
std::string OSName;
std::string OSVersion;
std::string HostName;
std::string UserName;
std::string TimeZone;
};
struct NetworkInfo
{
std::string IP;
std::string Gateway;
std::string Dns;
};
struct ShellResult
{
std::string Output;
int ExitCode = -1;
bool Truncated = false;
bool TimedOut = false;
bool Cancelled = false;
std::string Error;
};
double CpuPercent();
MemoryInfo Memory();
DiskInfo DiskUsage(const std::string& path);
std::vector<DriveEntry> Drives();
NetTotals NetTotalsSinceStart();
std::uint64_t ProcessCount();
std::vector<std::string> ProcessNames();
std::uint64_t UptimeSeconds();
// 系统负载。Linux/macOS 为 1 分钟平å‡è´Ÿè½½ï¼ŒWindows 为处ç†å™¨é˜Ÿåˆ—长度(与 .NET 客户端一致)。
double LoadAverage();
// 统计本机 TCP 连接状æ€åˆ†å¸ƒï¼›ä¸æ”¯æŒç»Ÿè®¡çš„å¹³å°è¿”回全 0。
TcpCounts TcpStates();
// 采集 CPU 温度(摄æ°åº¦ï¼‰ï¼›æ— æ¸©åº¦ä¼ æ„Ÿå™¨æ—¶è¿”å›ž 0。
double Temperature();
// é‡‡é›†ç”µæ± ç”µé‡ç™¾åˆ†æ¯”(0~100ï¼‰ï¼›æ— ç”µæ± æˆ–æœªçŸ¥æ—¶è¿”å›ž 0。
double BatteryPercent();
// é‡‡é›†æ— çº¿ä¿¡å·å¼ºåº¦ï¼ˆ0~100ï¼‰ï¼›æœ‰çº¿æˆ–æ— æ— çº¿ç½‘å¡æ—¶è¿”回 0。
int WirelessSignal();
// é‡‡æ ·ç£ç›˜ IO 速率;首次调用返回 0ï¼Œä¹‹åŽæŒ‰ä¸¤æ¬¡é‡‡æ ·çš„间隔计算。
DiskIoSample DiskIo();
// 当å‰è¿›ç¨‹å¯æ‰“å¼€æ–‡ä»¶å¥æŸ„上é™ï¼›å¹³å°æ— æ¤æ¦‚念时返回 0。
std::uint64_t MaxOpenFiles();
// å¯¹ç›®æ ‡ä¸»æœºåš ICMP 探测,用于评估网关/DNS/æœåŠ¡ç«¯ç½‘ç»œè´¨é‡ã€‚
ProbeResult PingProbe(const std::string& host, int timeout_ms, int count);
// é‡å¯æˆ–关机本机。reboot=true é‡å¯ï¼Œfalse å…³æœºï¼›è¿”å›žå‘½ä»¤æ˜¯å¦æˆåŠŸæäº¤ã€‚
bool PowerAction(bool reboot);
HardwareInfo Hardware();
SystemInfo System();
NetworkInfo Network();
std::string LocalIP();
std::string Gateway();
std::string Dns();
std::string Architecture();
std::string ExecutablePath();
std::string ExecutableDir();
int ProcessId();
bool IsRoot();
std::string HomeDir();
std::string DefaultDataDir();
std::string TimeZoneName();
bool SetSystemTime(TimePoint value);
// Runs a shell script with a hard timeout. The merged stdout/stderr output is
// capped at 64KB (head and tail are preserved).
ShellResult RunShell(const std::string& script, int timeout_ms, const std::atomic<bool>& cancelled);
// Starts a detached copy of the current executable with the supplied
// arguments. Used by node/restart.
bool RestartSelf(const std::vector<std::string>& arguments);
// Sleeps for the given duration, returning false when cancelled becomes true.
bool SleepCancelable(int milliseconds, const std::atomic<bool>& cancelled);
} // namespace staragent::platform
|