proc_net_ip_vs_stats.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "plugin_proc.h"
  3. #define RRD_TYPE_NET_IPVS "ipvs"
  4. #define PLUGIN_PROC_MODULE_NET_IPVS_NAME "/proc/net/ip_vs_stats"
  5. #define CONFIG_SECTION_PLUGIN_PROC_NET_IPVS "plugin:" PLUGIN_PROC_CONFIG_NAME ":" PLUGIN_PROC_MODULE_NET_IPVS_NAME
  6. int do_proc_net_ip_vs_stats(int update_every, usec_t dt) {
  7. (void)dt;
  8. static int do_bandwidth = -1, do_sockets = -1, do_packets = -1;
  9. static procfile *ff = NULL;
  10. if(do_bandwidth == -1) do_bandwidth = config_get_boolean(CONFIG_SECTION_PLUGIN_PROC_NET_IPVS, "IPVS bandwidth", 1);
  11. if(do_sockets == -1) do_sockets = config_get_boolean(CONFIG_SECTION_PLUGIN_PROC_NET_IPVS, "IPVS connections", 1);
  12. if(do_packets == -1) do_packets = config_get_boolean(CONFIG_SECTION_PLUGIN_PROC_NET_IPVS, "IPVS packets", 1);
  13. if(!ff) {
  14. char filename[FILENAME_MAX + 1];
  15. snprintfz(filename, FILENAME_MAX, "%s%s", netdata_configured_host_prefix, "/proc/net/ip_vs_stats");
  16. ff = procfile_open(config_get(CONFIG_SECTION_PLUGIN_PROC_NET_IPVS, "filename to monitor", filename), " \t,:|", PROCFILE_FLAG_DEFAULT);
  17. }
  18. if(!ff) return 1;
  19. ff = procfile_readall(ff);
  20. if(!ff) return 0; // we return 0, so that we will retry to open it next time
  21. // make sure we have 3 lines
  22. if(procfile_lines(ff) < 3) return 1;
  23. // make sure we have 5 words on the 3rd line
  24. if(procfile_linewords(ff, 2) < 5) return 1;
  25. unsigned long long entries, InPackets, OutPackets, InBytes, OutBytes;
  26. entries = strtoull(procfile_lineword(ff, 2, 0), NULL, 16);
  27. InPackets = strtoull(procfile_lineword(ff, 2, 1), NULL, 16);
  28. OutPackets = strtoull(procfile_lineword(ff, 2, 2), NULL, 16);
  29. InBytes = strtoull(procfile_lineword(ff, 2, 3), NULL, 16);
  30. OutBytes = strtoull(procfile_lineword(ff, 2, 4), NULL, 16);
  31. if(do_sockets) {
  32. static RRDSET *st = NULL;
  33. if(unlikely(!st)) {
  34. st = rrdset_create_localhost(
  35. RRD_TYPE_NET_IPVS
  36. , "sockets"
  37. , NULL
  38. , RRD_TYPE_NET_IPVS
  39. , NULL
  40. , "IPVS New Connections"
  41. , "connections/s"
  42. , PLUGIN_PROC_NAME
  43. , PLUGIN_PROC_MODULE_NET_IPVS_NAME
  44. , NETDATA_CHART_PRIO_IPVS_SOCKETS
  45. , update_every
  46. , RRDSET_TYPE_LINE
  47. );
  48. rrddim_add(st, "connections", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
  49. }
  50. rrddim_set(st, "connections", entries);
  51. rrdset_done(st);
  52. }
  53. if(do_packets) {
  54. static RRDSET *st = NULL;
  55. if(unlikely(!st)) {
  56. st = rrdset_create_localhost(
  57. RRD_TYPE_NET_IPVS
  58. , "packets"
  59. , NULL
  60. , RRD_TYPE_NET_IPVS
  61. , NULL
  62. , "IPVS Packets"
  63. , "packets/s"
  64. , PLUGIN_PROC_NAME
  65. , PLUGIN_PROC_MODULE_NET_IPVS_NAME
  66. , NETDATA_CHART_PRIO_IPVS_PACKETS
  67. , update_every
  68. , RRDSET_TYPE_LINE
  69. );
  70. rrddim_add(st, "received", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
  71. rrddim_add(st, "sent", NULL, -1, 1, RRD_ALGORITHM_INCREMENTAL);
  72. }
  73. rrddim_set(st, "received", InPackets);
  74. rrddim_set(st, "sent", OutPackets);
  75. rrdset_done(st);
  76. }
  77. if(do_bandwidth) {
  78. static RRDSET *st = NULL;
  79. if(unlikely(!st)) {
  80. st = rrdset_create_localhost(
  81. RRD_TYPE_NET_IPVS
  82. , "net"
  83. , NULL
  84. , RRD_TYPE_NET_IPVS
  85. , NULL
  86. , "IPVS Bandwidth"
  87. , "kilobits/s"
  88. , PLUGIN_PROC_NAME
  89. , PLUGIN_PROC_MODULE_NET_IPVS_NAME
  90. , NETDATA_CHART_PRIO_IPVS_NET
  91. , update_every
  92. , RRDSET_TYPE_AREA
  93. );
  94. rrddim_add(st, "received", NULL, 8, BITS_IN_A_KILOBIT, RRD_ALGORITHM_INCREMENTAL);
  95. rrddim_add(st, "sent", NULL, -8, BITS_IN_A_KILOBIT, RRD_ALGORITHM_INCREMENTAL);
  96. }
  97. rrddim_set(st, "received", InBytes);
  98. rrddim_set(st, "sent", OutBytes);
  99. rrdset_done(st);
  100. }
  101. return 0;
  102. }