#ifndef __SYSTEMUSAGE_H__ #define __SYSTEMUSAGE_H__ #include #include /** * @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 m_timer; std::string m_accessToken; CpuStats m_lastCpuStats; std::list m_networkStats; }; #endif // __SYSTEMUSAGE_H__