EquipmentMonitor.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * @Description:
  3. * @Version: 1.0
  4. * @Autor: lishengyin
  5. * @Date: 2022-03-24 14:16:49
  6. * @LastEditors: lishengyin
  7. * @LastEditTime: 2022-07-06 15:40:54
  8. */
  9. #include "EquipmentMonitor.h"
  10. #include "Shell.h"
  11. /**
  12. * @description: 获取ptr
  13. * @param {*}
  14. * @return {*}
  15. */
  16. shared_ptr<EquipmentMonitor> EquipmentMonitor::getPtr(){
  17. static shared_ptr<EquipmentMonitor> m_equipmentMonitor = nullptr;
  18. if(m_equipmentMonitor == nullptr) m_equipmentMonitor = std::shared_ptr<EquipmentMonitor>(new EquipmentMonitor);
  19. return m_equipmentMonitor;
  20. }
  21. /**
  22. * @description: 获取数据
  23. * @param {*}
  24. * @return {*}
  25. */
  26. std::string EquipmentMonitor::get_cur_executable_path_()
  27. {
  28. char *p = NULL;
  29. const int len = 256;
  30. /// to keep the absolute path of executable's path
  31. char arr_tmp[len] = {0};
  32. int n = readlink("/proc/self/exe", arr_tmp, len);
  33. if (NULL != (p = strrchr(arr_tmp,'/')))
  34. *p = '\0';
  35. else
  36. {
  37. printf("wrong process path");
  38. std::string("");
  39. }
  40. return std::string(arr_tmp);
  41. }
  42. /**
  43. * @description: 获取硬盘使用率
  44. * @param {*}
  45. * @return {*}
  46. */
  47. double EquipmentMonitor::getHardDiskUsage(){
  48. double HardDiskUsage = 0;
  49. // 读取executable所在绝对路径
  50. std::string exec_str = get_cur_executable_path_();
  51. struct statfs diskInfo;
  52. statfs(exec_str.c_str(), &diskInfo);
  53. unsigned long long blocksize = diskInfo.f_bsize; //每个block里包含的字节数
  54. unsigned long long totalsize = blocksize * diskInfo.f_blocks; //总的字节数,f_blocks为block的数目
  55. unsigned long long availableDisk = diskInfo.f_bavail * blocksize; //可用空间大小
  56. // MB
  57. availableDisk = availableDisk>>20;
  58. totalsize = totalsize>>20;
  59. HardDiskUsage = (double)(((double)totalsize - (double)availableDisk) / (double)totalsize);
  60. return HardDiskUsage;
  61. }
  62. /**
  63. * @description: 获取Cpu温度
  64. * @param {*}
  65. * @return {*}
  66. */
  67. int EquipmentMonitor::getCpuTemp(){
  68. int32_t temp = 0;
  69. string shell = "cat /sys/devices/virtual/thermal/thermal_zone1/temp";
  70. vector<string> results;
  71. CShell::exeShellCmd(shell, results);
  72. for(auto iter = results.begin(); iter != results.end(); iter++){
  73. temp = std::atoi(iter->c_str());
  74. }
  75. return temp;
  76. }