gfanet.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #include <stdio.h>
  2. #include <stdint.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. #include <fcntl.h>
  8. #include <regex>
  9. #include <string>
  10. #include <sstream>
  11. #include <iostream>
  12. #include "util.h"
  13. #include "gfanet.h"
  14. /////////////////////////////////////////////////////////////////////////////
  15. #define _PROC_NET_DEV "/proc/net/dev"
  16. /////////////////////////////////////////////////////////////////////////////
  17. int GetAvailableInterfaces(NETINTERFACE_LIST &nil)
  18. {
  19. static const std::string strRegExLine = "^\\s*([^:]+):\\s*([0-9]+)\\s+([0-9]+)\\s+([0-9]+)\\s+([0-9]+)\\s+([0-9]+)\\s+([0-9]+)\\s+([0-9]+)\\s+([0-9]+)\\s+([0-9]+)\\s+([0-9]+)\\s+([0-9]+)\\s+([0-9]+)\\s+([0-9]+)\\s+([0-9]+)\\s+([0-9]+)\\s+([0-9]+)$";
  20. static std::regex regl(strRegExLine, std::regex_constants::ECMAScript | std::regex_constants::optimize);
  21. std::string buf;
  22. int nCntItf = 0;
  23. if(ReadFile(_PROC_NET_DEV, buf) > 0)
  24. {
  25. std::string line;
  26. std::istringstream iss(buf);
  27. std::istream stm(iss.rdbuf());
  28. while(std::getline(stm, line))
  29. {
  30. std::smatch resl;
  31. if( line.size() > 0 &&
  32. regex_search(line, resl, regl))
  33. {
  34. if(resl.size() == 18)
  35. {
  36. NETINTERFACE ni;
  37. memset(&ni, 0, sizeof(ni));
  38. resl[1].str().copy(ni.name, sizeof(ni.name) - 1);
  39. ni.stats.rx_bytes = std::stoul(resl[2].str());
  40. ni.stats.rx_packets = std::stoul(resl[3].str());
  41. ni.stats.rx_errors = std::stoul(resl[4].str());
  42. ni.stats.rx_dropped = std::stoul(resl[5].str());
  43. ni.stats.rx_fifo_errors = std::stoul(resl[6].str());
  44. ni.stats.rx_frame_errors = std::stoul(resl[7].str());
  45. ni.stats.rx_compressed = std::stoul(resl[8].str());
  46. ni.stats.rx_multicast = std::stoul(resl[9].str());
  47. ni.stats.tx_bytes = std::stoul(resl[10].str());
  48. ni.stats.tx_packets = std::stoul(resl[11].str());
  49. ni.stats.tx_errors = std::stoul(resl[12].str());
  50. ni.stats.tx_dropped = std::stoul(resl[13].str());
  51. ni.stats.tx_fifo_errors = std::stoul(resl[14].str());
  52. ni.stats.tx_collisions = std::stoul(resl[15].str());
  53. ni.stats.tx_carrier_errors = std::stoul(resl[16].str());
  54. ni.stats.tx_compressed = std::stoul(resl[17].str());
  55. nil.push_back(ni);
  56. nCntItf++;
  57. }
  58. }
  59. }
  60. }
  61. return nCntItf;
  62. }