48 lines
1.3 KiB
C++
48 lines
1.3 KiB
C++
#ifndef __SYSTEMUSAGE_H__
|
|
#define __SYSTEMUSAGE_H__
|
|
|
|
#include <boost/asio/system_timer.hpp>
|
|
|
|
/**
|
|
* @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;
|
|
}
|
|
};
|
|
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);
|
|
|
|
private:
|
|
boost::asio::io_context &m_ioContext;
|
|
std::shared_ptr<boost::asio::system_timer> m_timer;
|
|
std::string m_accessToken;
|
|
CpuStats m_lastCpuStats;
|
|
};
|
|
#endif // __SYSTEMUSAGE_H__
|