Older/Server/SystemUsage.h

58 lines
1.6 KiB
C
Raw Normal View History

2024-05-03 22:30:46 +08:00
#ifndef __SYSTEMUSAGE_H__
#define __SYSTEMUSAGE_H__
#include <boost/asio/system_timer.hpp>
2024-11-09 19:21:15 +08:00
#include <list>
2024-05-03 22:30:46 +08:00
/**
* @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;
}
};
2024-11-09 19:21:15 +08:00
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();
};
2024-05-03 22:30:46 +08:00
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);
2024-11-09 19:21:15 +08:00
NetworkStats networkStats(const std::string &interface);
2024-05-03 22:30:46 +08:00
private:
boost::asio::io_context &m_ioContext;
std::shared_ptr<boost::asio::system_timer> m_timer;
std::string m_accessToken;
CpuStats m_lastCpuStats;
2024-11-09 19:21:15 +08:00
std::list<NetworkStats> m_networkStats;
2024-05-03 22:30:46 +08:00
};
#endif // __SYSTEMUSAGE_H__