proc_net_sockstat6.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "plugin_proc.h"
  3. #define PLUGIN_PROC_MODULE_NET_SOCKSTAT6_NAME "/proc/net/sockstat6"
  4. static struct proc_net_sockstat6 {
  5. kernel_uint_t tcp6_inuse;
  6. kernel_uint_t udp6_inuse;
  7. kernel_uint_t udplite6_inuse;
  8. kernel_uint_t raw6_inuse;
  9. kernel_uint_t frag6_inuse;
  10. } sockstat6_root = { 0 };
  11. int do_proc_net_sockstat6(int update_every, usec_t dt) {
  12. (void)dt;
  13. static procfile *ff = NULL;
  14. static uint32_t hash_raw = 0,
  15. hash_frag = 0,
  16. hash_tcp = 0,
  17. hash_udp = 0,
  18. hash_udplite = 0;
  19. static ARL_BASE *arl_tcp = NULL;
  20. static ARL_BASE *arl_udp = NULL;
  21. static ARL_BASE *arl_udplite = NULL;
  22. static ARL_BASE *arl_raw = NULL;
  23. static ARL_BASE *arl_frag = NULL;
  24. static int do_tcp_sockets = -1, do_udp_sockets = -1, do_udplite_sockets = -1, do_raw_sockets = -1, do_frag_sockets = -1;
  25. static char *keys[6] = { NULL };
  26. static uint32_t hashes[6] = { 0 };
  27. static ARL_BASE *bases[6] = { NULL };
  28. if(unlikely(!arl_tcp)) {
  29. do_tcp_sockets = config_get_boolean_ondemand("plugin:proc:/proc/net/sockstat6", "ipv6 TCP sockets", CONFIG_BOOLEAN_AUTO);
  30. do_udp_sockets = config_get_boolean_ondemand("plugin:proc:/proc/net/sockstat6", "ipv6 UDP sockets", CONFIG_BOOLEAN_AUTO);
  31. do_udplite_sockets = config_get_boolean_ondemand("plugin:proc:/proc/net/sockstat6", "ipv6 UDPLITE sockets", CONFIG_BOOLEAN_AUTO);
  32. do_raw_sockets = config_get_boolean_ondemand("plugin:proc:/proc/net/sockstat6", "ipv6 RAW sockets", CONFIG_BOOLEAN_AUTO);
  33. do_frag_sockets = config_get_boolean_ondemand("plugin:proc:/proc/net/sockstat6", "ipv6 FRAG sockets", CONFIG_BOOLEAN_AUTO);
  34. arl_tcp = arl_create("sockstat6/TCP6", arl_callback_str2kernel_uint_t, 60);
  35. arl_expect(arl_tcp, "inuse", &sockstat6_root.tcp6_inuse);
  36. arl_udp = arl_create("sockstat6/UDP6", arl_callback_str2kernel_uint_t, 60);
  37. arl_expect(arl_udp, "inuse", &sockstat6_root.udp6_inuse);
  38. arl_udplite = arl_create("sockstat6/UDPLITE6", arl_callback_str2kernel_uint_t, 60);
  39. arl_expect(arl_udplite, "inuse", &sockstat6_root.udplite6_inuse);
  40. arl_raw = arl_create("sockstat6/RAW6", arl_callback_str2kernel_uint_t, 60);
  41. arl_expect(arl_raw, "inuse", &sockstat6_root.raw6_inuse);
  42. arl_frag = arl_create("sockstat6/FRAG6", arl_callback_str2kernel_uint_t, 60);
  43. arl_expect(arl_frag, "inuse", &sockstat6_root.frag6_inuse);
  44. hash_tcp = simple_hash("TCP6");
  45. hash_udp = simple_hash("UDP6");
  46. hash_udplite = simple_hash("UDPLITE6");
  47. hash_raw = simple_hash("RAW6");
  48. hash_frag = simple_hash("FRAG6");
  49. keys[0] = "TCP6"; hashes[0] = hash_tcp; bases[0] = arl_tcp;
  50. keys[1] = "UDP6"; hashes[1] = hash_udp; bases[1] = arl_udp;
  51. keys[2] = "UDPLITE6"; hashes[2] = hash_udplite; bases[2] = arl_udplite;
  52. keys[3] = "RAW6"; hashes[3] = hash_raw; bases[3] = arl_raw;
  53. keys[4] = "FRAG6"; hashes[4] = hash_frag; bases[4] = arl_frag;
  54. keys[5] = NULL; // terminator
  55. }
  56. if(unlikely(!ff)) {
  57. char filename[FILENAME_MAX + 1];
  58. snprintfz(filename, FILENAME_MAX, "%s%s", netdata_configured_host_prefix, "/proc/net/sockstat6");
  59. ff = procfile_open(config_get("plugin:proc:/proc/net/sockstat6", "filename to monitor", filename), " \t:", PROCFILE_FLAG_DEFAULT);
  60. if(unlikely(!ff)) return 1;
  61. }
  62. ff = procfile_readall(ff);
  63. if(unlikely(!ff)) return 0; // we return 0, so that we will retry to open it next time
  64. size_t lines = procfile_lines(ff), l;
  65. for(l = 0; l < lines ;l++) {
  66. size_t words = procfile_linewords(ff, l);
  67. char *key = procfile_lineword(ff, l, 0);
  68. uint32_t hash = simple_hash(key);
  69. int k;
  70. for(k = 0; keys[k] ; k++) {
  71. if(unlikely(hash == hashes[k] && strcmp(key, keys[k]) == 0)) {
  72. // fprintf(stderr, "KEY: '%s', l=%zu, w=1, words=%zu\n", key, l, words);
  73. ARL_BASE *arl = bases[k];
  74. arl_begin(arl);
  75. size_t w = 1;
  76. while(w + 1 < words) {
  77. char *name = procfile_lineword(ff, l, w); w++;
  78. char *value = procfile_lineword(ff, l, w); w++;
  79. // fprintf(stderr, " > NAME '%s', VALUE '%s', l=%zu, w=%zu, words=%zu\n", name, value, l, w, words);
  80. if(unlikely(arl_check(arl, name, value) != 0))
  81. break;
  82. }
  83. break;
  84. }
  85. }
  86. }
  87. // ------------------------------------------------------------------------
  88. if(do_tcp_sockets == CONFIG_BOOLEAN_YES || (do_tcp_sockets == CONFIG_BOOLEAN_AUTO && (sockstat6_root.tcp6_inuse))) {
  89. do_tcp_sockets = CONFIG_BOOLEAN_YES;
  90. static RRDSET *st = NULL;
  91. static RRDDIM *rd_inuse = NULL;
  92. if(unlikely(!st)) {
  93. st = rrdset_create_localhost(
  94. "ipv6"
  95. , "sockstat6_tcp_sockets"
  96. , NULL
  97. , "tcp6"
  98. , NULL
  99. , "IPv6 TCP Sockets"
  100. , "sockets"
  101. , PLUGIN_PROC_NAME
  102. , PLUGIN_PROC_MODULE_NET_SOCKSTAT6_NAME
  103. , NETDATA_CHART_PRIO_IPV6_TCP
  104. , update_every
  105. , RRDSET_TYPE_LINE
  106. );
  107. rd_inuse = rrddim_add(st, "inuse", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
  108. }
  109. else rrdset_next(st);
  110. rrddim_set_by_pointer(st, rd_inuse, (collected_number)sockstat6_root.tcp6_inuse);
  111. rrdset_done(st);
  112. }
  113. // ------------------------------------------------------------------------
  114. if(do_udp_sockets == CONFIG_BOOLEAN_YES || (do_udp_sockets == CONFIG_BOOLEAN_AUTO && sockstat6_root.udp6_inuse)) {
  115. do_udp_sockets = CONFIG_BOOLEAN_YES;
  116. static RRDSET *st = NULL;
  117. static RRDDIM *rd_inuse = NULL;
  118. if(unlikely(!st)) {
  119. st = rrdset_create_localhost(
  120. "ipv6"
  121. , "sockstat6_udp_sockets"
  122. , NULL
  123. , "udp6"
  124. , NULL
  125. , "IPv6 UDP Sockets"
  126. , "sockets"
  127. , PLUGIN_PROC_NAME
  128. , PLUGIN_PROC_MODULE_NET_SOCKSTAT6_NAME
  129. , NETDATA_CHART_PRIO_IPV6_UDP
  130. , update_every
  131. , RRDSET_TYPE_LINE
  132. );
  133. rd_inuse = rrddim_add(st, "inuse", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
  134. }
  135. else rrdset_next(st);
  136. rrddim_set_by_pointer(st, rd_inuse, (collected_number)sockstat6_root.udp6_inuse);
  137. rrdset_done(st);
  138. }
  139. // ------------------------------------------------------------------------
  140. if(do_udplite_sockets == CONFIG_BOOLEAN_YES || (do_udplite_sockets == CONFIG_BOOLEAN_AUTO && sockstat6_root.udplite6_inuse)) {
  141. do_udplite_sockets = CONFIG_BOOLEAN_YES;
  142. static RRDSET *st = NULL;
  143. static RRDDIM *rd_inuse = NULL;
  144. if(unlikely(!st)) {
  145. st = rrdset_create_localhost(
  146. "ipv6"
  147. , "sockstat6_udplite_sockets"
  148. , NULL
  149. , "udplite6"
  150. , NULL
  151. , "IPv6 UDPLITE Sockets"
  152. , "sockets"
  153. , PLUGIN_PROC_NAME
  154. , PLUGIN_PROC_MODULE_NET_SOCKSTAT6_NAME
  155. , NETDATA_CHART_PRIO_IPV6_UDPLITE
  156. , update_every
  157. , RRDSET_TYPE_LINE
  158. );
  159. rd_inuse = rrddim_add(st, "inuse", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
  160. }
  161. else rrdset_next(st);
  162. rrddim_set_by_pointer(st, rd_inuse, (collected_number)sockstat6_root.udplite6_inuse);
  163. rrdset_done(st);
  164. }
  165. // ------------------------------------------------------------------------
  166. if(do_raw_sockets == CONFIG_BOOLEAN_YES || (do_raw_sockets == CONFIG_BOOLEAN_AUTO && sockstat6_root.raw6_inuse)) {
  167. do_raw_sockets = CONFIG_BOOLEAN_YES;
  168. static RRDSET *st = NULL;
  169. static RRDDIM *rd_inuse = NULL;
  170. if(unlikely(!st)) {
  171. st = rrdset_create_localhost(
  172. "ipv6"
  173. , "sockstat6_raw_sockets"
  174. , NULL
  175. , "raw6"
  176. , NULL
  177. , "IPv6 RAW Sockets"
  178. , "sockets"
  179. , PLUGIN_PROC_NAME
  180. , PLUGIN_PROC_MODULE_NET_SOCKSTAT6_NAME
  181. , NETDATA_CHART_PRIO_IPV6_RAW
  182. , update_every
  183. , RRDSET_TYPE_LINE
  184. );
  185. rd_inuse = rrddim_add(st, "inuse", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
  186. }
  187. else rrdset_next(st);
  188. rrddim_set_by_pointer(st, rd_inuse, (collected_number)sockstat6_root.raw6_inuse);
  189. rrdset_done(st);
  190. }
  191. // ------------------------------------------------------------------------
  192. if(do_frag_sockets == CONFIG_BOOLEAN_YES || (do_frag_sockets == CONFIG_BOOLEAN_AUTO && sockstat6_root.frag6_inuse)) {
  193. do_frag_sockets = CONFIG_BOOLEAN_YES;
  194. static RRDSET *st = NULL;
  195. static RRDDIM *rd_inuse = NULL;
  196. if(unlikely(!st)) {
  197. st = rrdset_create_localhost(
  198. "ipv6"
  199. , "sockstat6_frag_sockets"
  200. , NULL
  201. , "fragments6"
  202. , NULL
  203. , "IPv6 FRAG Sockets"
  204. , "fragments"
  205. , PLUGIN_PROC_NAME
  206. , PLUGIN_PROC_MODULE_NET_SOCKSTAT6_NAME
  207. , NETDATA_CHART_PRIO_IPV6_FRAGMENTS
  208. , update_every
  209. , RRDSET_TYPE_LINE
  210. );
  211. rd_inuse = rrddim_add(st, "inuse", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
  212. }
  213. else rrdset_next(st);
  214. rrddim_set_by_pointer(st, rd_inuse, (collected_number)sockstat6_root.frag6_inuse);
  215. rrdset_done(st);
  216. }
  217. return 0;
  218. }