system_settings.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #include "sysinfo.h"
  2. #include "system_settings.h"
  3. #include <fstream>
  4. #include <iostream>
  5. #include <map>
  6. #include <net/if.h>
  7. #include <sstream>
  8. #include <string>
  9. #include <sys/ioctl.h>
  10. #include <sys/statfs.h>
  11. #include <sys/sysinfo.h>
  12. #include <sys/utsname.h>
  13. #include <utils/custom.h>
  14. #include <vector>
  15. namespace settings {
  16. void SystemSettings::UpdateNetDev()
  17. {
  18. std::ifstream file("/proc/net/dev");
  19. std::string line;
  20. netCards.clear();
  21. // 跳过前两行标题
  22. std::getline(file, line);
  23. std::getline(file, line);
  24. while (std::getline(file, line)) {
  25. std::istringstream iss(line);
  26. std::string interface;
  27. if (!(iss >> interface)) {
  28. continue;
  29. }
  30. interface = interface.substr(0, interface.length() - 1);
  31. if (interface == "lo") {
  32. continue;
  33. }
  34. // 跳过冒号后的空白字符
  35. char colon;
  36. iss >> std::ws >> colon;
  37. std::string stats_str;
  38. std::getline(iss, stats_str); // 读取剩余所有统计信息
  39. NetCard interface_stats;
  40. std::istringstream stats_iss(stats_str);
  41. std::string item;
  42. unsigned long long value;
  43. std::vector<int32_t *> keys = {&interface_stats.rxPackets, &interface_stats.rxBytes, &interface_stats.rxErrs,
  44. &interface_stats.rxDrop, &interface_stats.rxFifoErr, &interface_stats.rxFrameErr,
  45. &interface_stats.rxCompressed, &interface_stats.rxMulticast, &interface_stats.txPackets,
  46. &interface_stats.txBytes, &interface_stats.txErrs, &interface_stats.txDrop, &interface_stats.txFifoErr,
  47. &interface_stats.txCollisionErr, &interface_stats.txCarrierErr, &interface_stats.txCompressed};
  48. interface_stats.ifname = interface;
  49. for (const auto &key : keys) {
  50. if (stats_iss >> value) { // 尝试从流中读取一个值
  51. *key = value;
  52. // 如果需要,可以跳过分隔符(这里假设是空格或制表符,由std::istringstream自动处理)
  53. }
  54. }
  55. // 获取MAC, IP
  56. char ipaddr[64];
  57. char macaddr[64];
  58. char mask[64];
  59. char gateway[64];
  60. sysinfo::GetIfAddr(interface_stats.ifname.c_str(), ipaddr, mask, gateway);
  61. sysinfo::GetLocalMac(interface_stats.ifname.c_str(), macaddr);
  62. interface_stats.ipAddr = ipaddr;
  63. interface_stats.mask = mask;
  64. interface_stats.MAC = macaddr;
  65. interface_stats.gateway = gateway;
  66. netCards.push_back(interface_stats);
  67. }
  68. }
  69. bool SystemSettings::UpdateSysInfo()
  70. {
  71. // appVer
  72. appVer = DAS_VERSION;
  73. // sysver
  74. struct utsname un;
  75. uname(&un);
  76. sysVer = std::string(un.sysname) + " " + std::string(un.release) + " " + std::string(un.machine);
  77. // systime
  78. sysTime = utils::nowtostr();
  79. return true;
  80. }
  81. bool SystemSettings::UpdateDiskInfo()
  82. {
  83. struct statfs diskinfo;
  84. statfs("/", &diskinfo);
  85. double fused = diskinfo.f_bfree / 1024.0 * diskinfo.f_bsize / 1024.0;
  86. double ftotal = diskinfo.f_blocks / 1024.0 * diskinfo.f_bsize / 1024.0;
  87. totalDiskSpace = ftotal;
  88. freeDiskSpace = fused;
  89. return true;
  90. }
  91. bool SystemSettings::UpdateMemInfo()
  92. {
  93. sysinfo::MEM_OCCUPY mc;
  94. sysinfo::get_mem_occupy(&mc);
  95. totalMemory = mc.total / 1024.0;
  96. freeMemory = mc.free / 1024.0;
  97. return true;
  98. }
  99. } // namespace settings