/* * @Description: * @Version: 1.0 * @Autor: lishengyin * @Date: 2022-03-24 14:16:49 * @LastEditors: lishengyin * @LastEditTime: 2022-07-06 15:40:54 */ #include "EquipmentMonitor.h" #include "Shell.h" /** * @description: 获取ptr * @param {*} * @return {*} */ shared_ptr EquipmentMonitor::getPtr(){ static shared_ptr m_equipmentMonitor = nullptr; if(m_equipmentMonitor == nullptr) m_equipmentMonitor = std::shared_ptr(new EquipmentMonitor); return m_equipmentMonitor; } /** * @description: 获取数据 * @param {*} * @return {*} */ std::string EquipmentMonitor::get_cur_executable_path_() { char *p = NULL; const int len = 256; /// to keep the absolute path of executable's path char arr_tmp[len] = {0}; int n = readlink("/proc/self/exe", arr_tmp, len); if (NULL != (p = strrchr(arr_tmp,'/'))) *p = '\0'; else { printf("wrong process path"); std::string(""); } return std::string(arr_tmp); } /** * @description: 获取硬盘使用率 * @param {*} * @return {*} */ double EquipmentMonitor::getHardDiskUsage(){ double HardDiskUsage = 0; // 读取executable所在绝对路径 std::string exec_str = get_cur_executable_path_(); struct statfs diskInfo; statfs(exec_str.c_str(), &diskInfo); unsigned long long blocksize = diskInfo.f_bsize; //每个block里包含的字节数 unsigned long long totalsize = blocksize * diskInfo.f_blocks; //总的字节数,f_blocks为block的数目 unsigned long long availableDisk = diskInfo.f_bavail * blocksize; //可用空间大小 // MB availableDisk = availableDisk>>20; totalsize = totalsize>>20; HardDiskUsage = (double)(((double)totalsize - (double)availableDisk) / (double)totalsize); return HardDiskUsage; } /** * @description: 获取Cpu温度 * @param {*} * @return {*} */ int EquipmentMonitor::getCpuTemp(){ int32_t temp = 0; string shell = "cat /sys/devices/virtual/thermal/thermal_zone1/temp"; vector results; CShell::exeShellCmd(shell, results); for(auto iter = results.begin(); iter != results.end(); iter++){ temp = std::atoi(iter->c_str()); } return temp; }