Older/Server/SystemUsage.h
amass 1a84bdc1a3
Some checks failed
Deploy / PullDocker (push) Successful in 7s
Deploy / Build (push) Failing after 1m20s
Deploy Docker Images / Docusaurus build and Server deploy (push) Successful in 19s
add network minitor.
2024-11-09 19:21:15 +08:00

58 lines
1.6 KiB
C++

#ifndef __SYSTEMUSAGE_H__
#define __SYSTEMUSAGE_H__
#include <boost/asio/system_timer.hpp>
#include <list>
/**
* @brief 参考 https://github.com/improvess/cpp-linux-system-stats
*
*/
class SystemUsage {
public:
struct CpuStats { // see http://www.linuxhowtos.org/manpages/5/proc.htm
int user;
int nice;
int system;
int idle;
int iowait;
int irq;
int softirq;
int steal;
int guest;
int guestNice;
int totalIdle() const {
return idle + iowait;
}
int totalActive() const {
return user + nice + system + irq + softirq + steal + guest + guestNice;
}
};
struct NetworkStats {
std::string interface;
int64_t receiveBytes = 0;
int64_t transmitBytes = 0;
std::chrono::system_clock::time_point time = std::chrono::system_clock::now();
};
SystemUsage(boost::asio::io_context &ioContext, const std::string &accessToken);
void start();
protected:
void publish(const std::string_view &deviceName, float value, const std::string_view &unit,
const std::string_view &friendlyName);
CpuStats readCpuData();
float cpuUsage(const CpuStats &first, const CpuStats &second);
float diskUsage(const std::string &disk);
NetworkStats networkStats(const std::string &interface);
private:
boost::asio::io_context &m_ioContext;
std::shared_ptr<boost::asio::system_timer> m_timer;
std::string m_accessToken;
CpuStats m_lastCpuStats;
std::list<NetworkStats> m_networkStats;
};
#endif // __SYSTEMUSAGE_H__