plugin_nfacct.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "plugin_nfacct.h"
  3. #if defined(INTERNAL_PLUGIN_NFACCT)
  4. #define PLUGIN_NFACCT_NAME "nfacct.plugin"
  5. #ifdef HAVE_LIBMNL
  6. #include <libmnl/libmnl.h>
  7. static inline size_t mnl_buffer_size() {
  8. long s = MNL_SOCKET_BUFFER_SIZE;
  9. if(s <= 0) return 8192;
  10. return (size_t)s;
  11. }
  12. // ----------------------------------------------------------------------------
  13. // DO_NFSTAT - collect netfilter connection tracker statistics via netlink
  14. // example: https://github.com/formorer/pkg-conntrack-tools/blob/master/src/conntrack.c
  15. #ifdef HAVE_LINUX_NETFILTER_NFNETLINK_CONNTRACK_H
  16. #define DO_NFSTAT 1
  17. #define RRD_TYPE_NET_STAT_NETFILTER "netfilter"
  18. #define RRD_TYPE_NET_STAT_CONNTRACK "netlink"
  19. #include <linux/netfilter/nfnetlink_conntrack.h>
  20. static struct {
  21. int update_every;
  22. char *buf;
  23. size_t buf_size;
  24. struct mnl_socket *mnl;
  25. struct nlmsghdr *nlh;
  26. struct nfgenmsg *nfh;
  27. unsigned int seq;
  28. uint32_t portid;
  29. struct nlattr *tb[CTA_STATS_MAX+1];
  30. const char *attr2name[CTA_STATS_MAX+1];
  31. kernel_uint_t metrics[CTA_STATS_MAX+1];
  32. struct nlattr *tb_exp[CTA_STATS_EXP_MAX+1];
  33. const char *attr2name_exp[CTA_STATS_EXP_MAX+1];
  34. kernel_uint_t metrics_exp[CTA_STATS_EXP_MAX+1];
  35. } nfstat_root = {
  36. .update_every = 1,
  37. .buf = NULL,
  38. .buf_size = 0,
  39. .mnl = NULL,
  40. .nlh = NULL,
  41. .nfh = NULL,
  42. .seq = 0,
  43. .portid = 0,
  44. .tb = {},
  45. .attr2name = {
  46. [CTA_STATS_SEARCHED] = "searched",
  47. [CTA_STATS_FOUND] = "found",
  48. [CTA_STATS_NEW] = "new",
  49. [CTA_STATS_INVALID] = "invalid",
  50. [CTA_STATS_IGNORE] = "ignore",
  51. [CTA_STATS_DELETE] = "delete",
  52. [CTA_STATS_DELETE_LIST] = "delete_list",
  53. [CTA_STATS_INSERT] = "insert",
  54. [CTA_STATS_INSERT_FAILED] = "insert_failed",
  55. [CTA_STATS_DROP] = "drop",
  56. [CTA_STATS_EARLY_DROP] = "early_drop",
  57. [CTA_STATS_ERROR] = "icmp_error",
  58. [CTA_STATS_SEARCH_RESTART] = "search_restart",
  59. },
  60. .metrics = {},
  61. .tb_exp = {},
  62. .attr2name_exp = {
  63. [CTA_STATS_EXP_NEW] = "new",
  64. [CTA_STATS_EXP_CREATE] = "created",
  65. [CTA_STATS_EXP_DELETE] = "deleted",
  66. },
  67. .metrics_exp = {}
  68. };
  69. static int nfstat_init(int update_every) {
  70. nfstat_root.update_every = update_every;
  71. nfstat_root.buf_size = mnl_buffer_size();
  72. nfstat_root.buf = mallocz(nfstat_root.buf_size);
  73. nfstat_root.mnl = mnl_socket_open(NETLINK_NETFILTER);
  74. if(!nfstat_root.mnl) {
  75. error("NFSTAT: mnl_socket_open() failed");
  76. return 1;
  77. }
  78. nfstat_root.seq = (unsigned int)now_realtime_sec() - 1;
  79. if(mnl_socket_bind(nfstat_root.mnl, 0, MNL_SOCKET_AUTOPID) < 0) {
  80. error("NFSTAT: mnl_socket_bind() failed");
  81. return 1;
  82. }
  83. nfstat_root.portid = mnl_socket_get_portid(nfstat_root.mnl);
  84. return 0;
  85. }
  86. static void nfstat_cleanup() {
  87. if(nfstat_root.mnl) {
  88. mnl_socket_close(nfstat_root.mnl);
  89. nfstat_root.mnl = NULL;
  90. }
  91. freez(nfstat_root.buf);
  92. nfstat_root.buf = NULL;
  93. nfstat_root.buf_size = 0;
  94. }
  95. static struct nlmsghdr * nfct_mnl_nlmsghdr_put(char *buf, uint16_t subsys, uint16_t type, uint8_t family, uint32_t seq) {
  96. struct nlmsghdr *nlh;
  97. struct nfgenmsg *nfh;
  98. nlh = mnl_nlmsg_put_header(buf);
  99. nlh->nlmsg_type = (subsys << 8) | type;
  100. nlh->nlmsg_flags = NLM_F_REQUEST|NLM_F_DUMP;
  101. nlh->nlmsg_seq = seq;
  102. nfh = mnl_nlmsg_put_extra_header(nlh, sizeof(struct nfgenmsg));
  103. nfh->nfgen_family = family;
  104. nfh->version = NFNETLINK_V0;
  105. nfh->res_id = 0;
  106. return nlh;
  107. }
  108. static int nfct_stats_attr_cb(const struct nlattr *attr, void *data) {
  109. const struct nlattr **tb = data;
  110. int type = mnl_attr_get_type(attr);
  111. if (mnl_attr_type_valid(attr, CTA_STATS_MAX) < 0)
  112. return MNL_CB_OK;
  113. if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0) {
  114. error("NFSTAT: mnl_attr_validate() failed");
  115. return MNL_CB_ERROR;
  116. }
  117. tb[type] = attr;
  118. return MNL_CB_OK;
  119. }
  120. static int nfstat_callback(const struct nlmsghdr *nlh, void *data) {
  121. (void)data;
  122. struct nfgenmsg *nfg = mnl_nlmsg_get_payload(nlh);
  123. mnl_attr_parse(nlh, sizeof(*nfg), nfct_stats_attr_cb, nfstat_root.tb);
  124. // printf("cpu=%-4u\t", ntohs(nfg->res_id));
  125. int i;
  126. // add the metrics of this CPU into the metrics
  127. for (i = 0; i < CTA_STATS_MAX+1; i++) {
  128. if (nfstat_root.tb[i]) {
  129. // printf("%s=%u ", nfstat_root.attr2name[i], ntohl(mnl_attr_get_u32(nfstat_root.tb[i])));
  130. nfstat_root.metrics[i] += ntohl(mnl_attr_get_u32(nfstat_root.tb[i]));
  131. }
  132. }
  133. // printf("\n");
  134. return MNL_CB_OK;
  135. }
  136. static int nfstat_collect_conntrack() {
  137. // zero all metrics - we will sum the metrics of all CPUs later
  138. int i;
  139. for (i = 0; i < CTA_STATS_MAX+1; i++)
  140. nfstat_root.metrics[i] = 0;
  141. // prepare the request
  142. nfstat_root.nlh = nfct_mnl_nlmsghdr_put(nfstat_root.buf, NFNL_SUBSYS_CTNETLINK, IPCTNL_MSG_CT_GET_STATS_CPU, AF_UNSPEC, nfstat_root.seq);
  143. // send the request
  144. if(mnl_socket_sendto(nfstat_root.mnl, nfstat_root.nlh, nfstat_root.nlh->nlmsg_len) < 0) {
  145. error("NFSTAT: mnl_socket_sendto() failed");
  146. return 1;
  147. }
  148. // get the reply
  149. ssize_t ret;
  150. while ((ret = mnl_socket_recvfrom(nfstat_root.mnl, nfstat_root.buf, nfstat_root.buf_size)) > 0) {
  151. if(mnl_cb_run(
  152. nfstat_root.buf
  153. , (size_t)ret
  154. , nfstat_root.nlh->nlmsg_seq
  155. , nfstat_root.portid
  156. , nfstat_callback
  157. , NULL
  158. ) <= MNL_CB_STOP)
  159. break;
  160. }
  161. // verify we run without issues
  162. if (ret == -1) {
  163. error("NFSTAT: error communicating with kernel. This plugin can only work when netdata runs as root.");
  164. return 1;
  165. }
  166. return 0;
  167. }
  168. static int nfexp_stats_attr_cb(const struct nlattr *attr, void *data)
  169. {
  170. const struct nlattr **tb = data;
  171. int type = mnl_attr_get_type(attr);
  172. if (mnl_attr_type_valid(attr, CTA_STATS_EXP_MAX) < 0)
  173. return MNL_CB_OK;
  174. if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0) {
  175. error("NFSTAT EXP: mnl_attr_validate() failed");
  176. return MNL_CB_ERROR;
  177. }
  178. tb[type] = attr;
  179. return MNL_CB_OK;
  180. }
  181. static int nfstat_callback_exp(const struct nlmsghdr *nlh, void *data) {
  182. (void)data;
  183. struct nfgenmsg *nfg = mnl_nlmsg_get_payload(nlh);
  184. mnl_attr_parse(nlh, sizeof(*nfg), nfexp_stats_attr_cb, nfstat_root.tb_exp);
  185. int i;
  186. for (i = 0; i < CTA_STATS_EXP_MAX+1; i++) {
  187. if (nfstat_root.tb_exp[i]) {
  188. nfstat_root.metrics_exp[i] += ntohl(mnl_attr_get_u32(nfstat_root.tb_exp[i]));
  189. }
  190. }
  191. return MNL_CB_OK;
  192. }
  193. static int nfstat_collect_conntrack_expectations() {
  194. // zero all metrics - we will sum the metrics of all CPUs later
  195. int i;
  196. for (i = 0; i < CTA_STATS_EXP_MAX+1; i++)
  197. nfstat_root.metrics_exp[i] = 0;
  198. // prepare the request
  199. nfstat_root.nlh = nfct_mnl_nlmsghdr_put(nfstat_root.buf, NFNL_SUBSYS_CTNETLINK_EXP, IPCTNL_MSG_EXP_GET_STATS_CPU, AF_UNSPEC, nfstat_root.seq);
  200. // send the request
  201. if(mnl_socket_sendto(nfstat_root.mnl, nfstat_root.nlh, nfstat_root.nlh->nlmsg_len) < 0) {
  202. error("NFSTAT: mnl_socket_sendto() failed");
  203. return 1;
  204. }
  205. // get the reply
  206. ssize_t ret;
  207. while ((ret = mnl_socket_recvfrom(nfstat_root.mnl, nfstat_root.buf, nfstat_root.buf_size)) > 0) {
  208. if(mnl_cb_run(
  209. nfstat_root.buf
  210. , (size_t)ret
  211. , nfstat_root.nlh->nlmsg_seq
  212. , nfstat_root.portid
  213. , nfstat_callback_exp
  214. , NULL
  215. ) <= MNL_CB_STOP)
  216. break;
  217. }
  218. // verify we run without issues
  219. if (ret == -1) {
  220. error("NFSTAT: error communicating with kernel. This plugin can only work when netdata runs as root.");
  221. return 1;
  222. }
  223. return 0;
  224. }
  225. static int nfstat_collect() {
  226. nfstat_root.seq++;
  227. if(nfstat_collect_conntrack())
  228. return 1;
  229. if(nfstat_collect_conntrack_expectations())
  230. return 1;
  231. return 0;
  232. }
  233. static void nfstat_send_metrics() {
  234. {
  235. static RRDSET *st_new = NULL;
  236. static RRDDIM *rd_new = NULL, *rd_ignore = NULL, *rd_invalid = NULL;
  237. if(!st_new) {
  238. st_new = rrdset_create_localhost(
  239. RRD_TYPE_NET_STAT_NETFILTER
  240. , RRD_TYPE_NET_STAT_CONNTRACK "_new"
  241. , NULL
  242. , RRD_TYPE_NET_STAT_CONNTRACK
  243. , NULL
  244. , "Connection Tracker New Connections"
  245. , "connections/s"
  246. , PLUGIN_NFACCT_NAME
  247. , NULL
  248. , NETDATA_CHART_PRIO_NETFILTER_NEW
  249. , nfstat_root.update_every
  250. , RRDSET_TYPE_LINE
  251. );
  252. rd_new = rrddim_add(st_new, nfstat_root.attr2name[CTA_STATS_NEW], NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
  253. rd_ignore = rrddim_add(st_new, nfstat_root.attr2name[CTA_STATS_IGNORE], NULL, -1, 1, RRD_ALGORITHM_INCREMENTAL);
  254. rd_invalid = rrddim_add(st_new, nfstat_root.attr2name[CTA_STATS_INVALID], NULL, -1, 1, RRD_ALGORITHM_INCREMENTAL);
  255. }
  256. else
  257. rrdset_next(st_new);
  258. rrddim_set_by_pointer(st_new, rd_new, (collected_number) nfstat_root.metrics[CTA_STATS_NEW]);
  259. rrddim_set_by_pointer(st_new, rd_ignore, (collected_number) nfstat_root.metrics[CTA_STATS_IGNORE]);
  260. rrddim_set_by_pointer(st_new, rd_invalid, (collected_number) nfstat_root.metrics[CTA_STATS_INVALID]);
  261. rrdset_done(st_new);
  262. }
  263. // ----------------------------------------------------------------
  264. {
  265. static RRDSET *st_changes = NULL;
  266. static RRDDIM *rd_inserted = NULL, *rd_deleted = NULL, *rd_delete_list = NULL;
  267. if(!st_changes) {
  268. st_changes = rrdset_create_localhost(
  269. RRD_TYPE_NET_STAT_NETFILTER
  270. , RRD_TYPE_NET_STAT_CONNTRACK "_changes"
  271. , NULL
  272. , RRD_TYPE_NET_STAT_CONNTRACK
  273. , NULL
  274. , "Connection Tracker Changes"
  275. , "changes/s"
  276. , PLUGIN_NFACCT_NAME
  277. , NULL
  278. , NETDATA_CHART_PRIO_NETFILTER_CHANGES
  279. , nfstat_root.update_every
  280. , RRDSET_TYPE_LINE
  281. );
  282. rrdset_flag_set(st_changes, RRDSET_FLAG_DETAIL);
  283. rd_inserted = rrddim_add(st_changes, nfstat_root.attr2name[CTA_STATS_INSERT], NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
  284. rd_deleted = rrddim_add(st_changes, nfstat_root.attr2name[CTA_STATS_DELETE], NULL, -1, 1, RRD_ALGORITHM_INCREMENTAL);
  285. rd_delete_list = rrddim_add(st_changes, nfstat_root.attr2name[CTA_STATS_DELETE_LIST], NULL, -1, 1, RRD_ALGORITHM_INCREMENTAL);
  286. }
  287. else
  288. rrdset_next(st_changes);
  289. rrddim_set_by_pointer(st_changes, rd_inserted, (collected_number) nfstat_root.metrics[CTA_STATS_INSERT]);
  290. rrddim_set_by_pointer(st_changes, rd_deleted, (collected_number) nfstat_root.metrics[CTA_STATS_DELETE]);
  291. rrddim_set_by_pointer(st_changes, rd_delete_list, (collected_number) nfstat_root.metrics[CTA_STATS_DELETE_LIST]);
  292. rrdset_done(st_changes);
  293. }
  294. // ----------------------------------------------------------------
  295. {
  296. static RRDSET *st_search = NULL;
  297. static RRDDIM *rd_searched = NULL, *rd_restarted = NULL, *rd_found = NULL;
  298. if(!st_search) {
  299. st_search = rrdset_create_localhost(
  300. RRD_TYPE_NET_STAT_NETFILTER
  301. , RRD_TYPE_NET_STAT_CONNTRACK "_search"
  302. , NULL
  303. , RRD_TYPE_NET_STAT_CONNTRACK
  304. , NULL
  305. , "Connection Tracker Searches"
  306. , "searches/s"
  307. , PLUGIN_NFACCT_NAME
  308. , NULL
  309. , NETDATA_CHART_PRIO_NETFILTER_SEARCH
  310. , nfstat_root.update_every
  311. , RRDSET_TYPE_LINE
  312. );
  313. rrdset_flag_set(st_search, RRDSET_FLAG_DETAIL);
  314. rd_searched = rrddim_add(st_search, nfstat_root.attr2name[CTA_STATS_SEARCHED], NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
  315. rd_restarted = rrddim_add(st_search, nfstat_root.attr2name[CTA_STATS_SEARCH_RESTART], NULL, -1, 1, RRD_ALGORITHM_INCREMENTAL);
  316. rd_found = rrddim_add(st_search, nfstat_root.attr2name[CTA_STATS_FOUND], NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
  317. }
  318. else
  319. rrdset_next(st_search);
  320. rrddim_set_by_pointer(st_search, rd_searched, (collected_number) nfstat_root.metrics[CTA_STATS_SEARCHED]);
  321. rrddim_set_by_pointer(st_search, rd_restarted, (collected_number) nfstat_root.metrics[CTA_STATS_SEARCH_RESTART]);
  322. rrddim_set_by_pointer(st_search, rd_found, (collected_number) nfstat_root.metrics[CTA_STATS_FOUND]);
  323. rrdset_done(st_search);
  324. }
  325. // ----------------------------------------------------------------
  326. {
  327. static RRDSET *st_errors = NULL;
  328. static RRDDIM *rd_error = NULL, *rd_insert_failed = NULL, *rd_drop = NULL, *rd_early_drop = NULL;
  329. if(!st_errors) {
  330. st_errors = rrdset_create_localhost(
  331. RRD_TYPE_NET_STAT_NETFILTER
  332. , RRD_TYPE_NET_STAT_CONNTRACK "_errors"
  333. , NULL
  334. , RRD_TYPE_NET_STAT_CONNTRACK
  335. , NULL
  336. , "Connection Tracker Errors"
  337. , "events/s"
  338. , PLUGIN_NFACCT_NAME
  339. , NULL
  340. , NETDATA_CHART_PRIO_NETFILTER_ERRORS
  341. , nfstat_root.update_every
  342. , RRDSET_TYPE_LINE
  343. );
  344. rrdset_flag_set(st_errors, RRDSET_FLAG_DETAIL);
  345. rd_error = rrddim_add(st_errors, nfstat_root.attr2name[CTA_STATS_ERROR], NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
  346. rd_insert_failed = rrddim_add(st_errors, nfstat_root.attr2name[CTA_STATS_INSERT_FAILED], NULL, -1, 1, RRD_ALGORITHM_INCREMENTAL);
  347. rd_drop = rrddim_add(st_errors, nfstat_root.attr2name[CTA_STATS_DROP], NULL, -1, 1, RRD_ALGORITHM_INCREMENTAL);
  348. rd_early_drop = rrddim_add(st_errors, nfstat_root.attr2name[CTA_STATS_EARLY_DROP], NULL, -1, 1, RRD_ALGORITHM_INCREMENTAL);
  349. }
  350. else
  351. rrdset_next(st_errors);
  352. rrddim_set_by_pointer(st_errors, rd_error, (collected_number) nfstat_root.metrics[CTA_STATS_ERROR]);
  353. rrddim_set_by_pointer(st_errors, rd_insert_failed, (collected_number) nfstat_root.metrics[CTA_STATS_INSERT_FAILED]);
  354. rrddim_set_by_pointer(st_errors, rd_drop, (collected_number) nfstat_root.metrics[CTA_STATS_DROP]);
  355. rrddim_set_by_pointer(st_errors, rd_early_drop, (collected_number) nfstat_root.metrics[CTA_STATS_EARLY_DROP]);
  356. rrdset_done(st_errors);
  357. }
  358. // ----------------------------------------------------------------
  359. {
  360. static RRDSET *st_expect = NULL;
  361. static RRDDIM *rd_new = NULL, *rd_created = NULL, *rd_deleted = NULL;
  362. if(!st_expect) {
  363. st_expect = rrdset_create_localhost(
  364. RRD_TYPE_NET_STAT_NETFILTER
  365. , RRD_TYPE_NET_STAT_CONNTRACK "_expect"
  366. , NULL
  367. , RRD_TYPE_NET_STAT_CONNTRACK
  368. , NULL
  369. , "Connection Tracker Expectations"
  370. , "expectations/s"
  371. , PLUGIN_NFACCT_NAME
  372. , NULL
  373. , NETDATA_CHART_PRIO_NETFILTER_EXPECT
  374. , nfstat_root.update_every
  375. , RRDSET_TYPE_LINE
  376. );
  377. rrdset_flag_set(st_expect, RRDSET_FLAG_DETAIL);
  378. rd_created = rrddim_add(st_expect, nfstat_root.attr2name_exp[CTA_STATS_EXP_CREATE], NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
  379. rd_deleted = rrddim_add(st_expect, nfstat_root.attr2name_exp[CTA_STATS_EXP_DELETE], NULL, -1, 1, RRD_ALGORITHM_INCREMENTAL);
  380. rd_new = rrddim_add(st_expect, nfstat_root.attr2name_exp[CTA_STATS_EXP_NEW], NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
  381. }
  382. else
  383. rrdset_next(st_expect);
  384. rrddim_set_by_pointer(st_expect, rd_created, (collected_number) nfstat_root.metrics_exp[CTA_STATS_EXP_CREATE]);
  385. rrddim_set_by_pointer(st_expect, rd_deleted, (collected_number) nfstat_root.metrics_exp[CTA_STATS_EXP_DELETE]);
  386. rrddim_set_by_pointer(st_expect, rd_new, (collected_number) nfstat_root.metrics_exp[CTA_STATS_EXP_NEW]);
  387. rrdset_done(st_expect);
  388. }
  389. }
  390. #endif // HAVE_LINUX_NETFILTER_NFNETLINK_CONNTRACK_H
  391. // ----------------------------------------------------------------------------
  392. // DO_NFACCT - collect netfilter accounting statistics via netlink
  393. #ifdef HAVE_LIBNETFILTER_ACCT
  394. #define DO_NFACCT 1
  395. #include <libnetfilter_acct/libnetfilter_acct.h>
  396. struct nfacct_data {
  397. char *name;
  398. uint32_t hash;
  399. uint64_t pkts;
  400. uint64_t bytes;
  401. RRDDIM *rd_bytes;
  402. RRDDIM *rd_packets;
  403. int updated;
  404. struct nfacct_data *next;
  405. };
  406. static struct {
  407. int update_every;
  408. char *buf;
  409. size_t buf_size;
  410. struct mnl_socket *mnl;
  411. struct nlmsghdr *nlh;
  412. unsigned int seq;
  413. uint32_t portid;
  414. struct nfacct *nfacct_buffer;
  415. struct nfacct_data *nfacct_metrics;
  416. } nfacct_root = {
  417. .update_every = 1,
  418. .buf = NULL,
  419. .buf_size = 0,
  420. .mnl = NULL,
  421. .nlh = NULL,
  422. .seq = 0,
  423. .portid = 0,
  424. .nfacct_buffer = NULL,
  425. .nfacct_metrics = NULL
  426. };
  427. static inline struct nfacct_data *nfacct_data_get(const char *name, uint32_t hash) {
  428. struct nfacct_data *d = NULL, *last = NULL;
  429. for(d = nfacct_root.nfacct_metrics; d ; last = d, d = d->next) {
  430. if(unlikely(d->hash == hash && !strcmp(d->name, name)))
  431. return d;
  432. }
  433. d = callocz(1, sizeof(struct nfacct_data));
  434. d->name = strdupz(name);
  435. d->hash = hash;
  436. if(!last) {
  437. d->next = nfacct_root.nfacct_metrics;
  438. nfacct_root.nfacct_metrics = d;
  439. }
  440. else {
  441. d->next = last->next;
  442. last->next = d;
  443. }
  444. return d;
  445. }
  446. static int nfacct_init(int update_every) {
  447. nfacct_root.update_every = update_every;
  448. nfacct_root.buf_size = mnl_buffer_size();
  449. nfacct_root.buf = mallocz(nfacct_root.buf_size);
  450. nfacct_root.nfacct_buffer = nfacct_alloc();
  451. if(!nfacct_root.nfacct_buffer) {
  452. error("nfacct.plugin: nfacct_alloc() failed.");
  453. return 0;
  454. }
  455. nfacct_root.seq = (unsigned int)now_realtime_sec() - 1;
  456. nfacct_root.mnl = mnl_socket_open(NETLINK_NETFILTER);
  457. if(!nfacct_root.mnl) {
  458. error("nfacct.plugin: mnl_socket_open() failed");
  459. return 1;
  460. }
  461. if(mnl_socket_bind(nfacct_root.mnl, 0, MNL_SOCKET_AUTOPID) < 0) {
  462. error("nfacct.plugin: mnl_socket_bind() failed");
  463. return 1;
  464. }
  465. nfacct_root.portid = mnl_socket_get_portid(nfacct_root.mnl);
  466. return 0;
  467. }
  468. static void nfacct_cleanup() {
  469. if(nfacct_root.mnl) {
  470. mnl_socket_close(nfacct_root.mnl);
  471. nfacct_root.mnl = NULL;
  472. }
  473. if(nfacct_root.nfacct_buffer) {
  474. nfacct_free(nfacct_root.nfacct_buffer);
  475. nfacct_root.nfacct_buffer = NULL;
  476. }
  477. freez(nfacct_root.buf);
  478. nfacct_root.buf = NULL;
  479. nfacct_root.buf_size = 0;
  480. // TODO: cleanup the metrics linked list
  481. }
  482. static int nfacct_callback(const struct nlmsghdr *nlh, void *data) {
  483. (void)data;
  484. if(nfacct_nlmsg_parse_payload(nlh, nfacct_root.nfacct_buffer) < 0) {
  485. error("NFACCT: nfacct_nlmsg_parse_payload() failed.");
  486. return MNL_CB_OK;
  487. }
  488. const char *name = nfacct_attr_get_str(nfacct_root.nfacct_buffer, NFACCT_ATTR_NAME);
  489. uint32_t hash = simple_hash(name);
  490. struct nfacct_data *d = nfacct_data_get(name, hash);
  491. d->pkts = nfacct_attr_get_u64(nfacct_root.nfacct_buffer, NFACCT_ATTR_PKTS);
  492. d->bytes = nfacct_attr_get_u64(nfacct_root.nfacct_buffer, NFACCT_ATTR_BYTES);
  493. d->updated = 1;
  494. return MNL_CB_OK;
  495. }
  496. static int nfacct_collect() {
  497. // mark all old metrics as not-updated
  498. struct nfacct_data *d;
  499. for(d = nfacct_root.nfacct_metrics; d ; d = d->next)
  500. d->updated = 0;
  501. // prepare the request
  502. nfacct_root.seq++;
  503. nfacct_root.nlh = nfacct_nlmsg_build_hdr(nfacct_root.buf, NFNL_MSG_ACCT_GET, NLM_F_DUMP, (uint32_t)nfacct_root.seq);
  504. if(!nfacct_root.nlh) {
  505. error("NFACCT: nfacct_nlmsg_build_hdr() failed");
  506. return 1;
  507. }
  508. // send the request
  509. if(mnl_socket_sendto(nfacct_root.mnl, nfacct_root.nlh, nfacct_root.nlh->nlmsg_len) < 0) {
  510. error("NFACCT: mnl_socket_sendto() failed");
  511. return 1;
  512. }
  513. // get the reply
  514. ssize_t ret;
  515. while((ret = mnl_socket_recvfrom(nfacct_root.mnl, nfacct_root.buf, nfacct_root.buf_size)) > 0) {
  516. if(mnl_cb_run(
  517. nfacct_root.buf
  518. , (size_t)ret
  519. , nfacct_root.seq
  520. , nfacct_root.portid
  521. , nfacct_callback
  522. , NULL
  523. ) <= 0)
  524. break;
  525. }
  526. // verify we run without issues
  527. if (ret == -1) {
  528. error("NFACCT: error communicating with kernel. This plugin can only work when netdata runs as root.");
  529. return 1;
  530. }
  531. return 0;
  532. }
  533. static void nfacct_send_metrics() {
  534. static RRDSET *st_bytes = NULL, *st_packets = NULL;
  535. if(!nfacct_root.nfacct_metrics) return;
  536. struct nfacct_data *d;
  537. if(!st_packets) {
  538. st_packets = rrdset_create_localhost(
  539. "netfilter"
  540. , "nfacct_packets"
  541. , NULL
  542. , "nfacct"
  543. , NULL
  544. , "Netfilter Accounting Packets"
  545. , "packets/s"
  546. , PLUGIN_NFACCT_NAME
  547. , NULL
  548. , NETDATA_CHART_PRIO_NETFILTER_PACKETS
  549. , nfacct_root.update_every
  550. , RRDSET_TYPE_STACKED
  551. );
  552. }
  553. else rrdset_next(st_packets);
  554. for(d = nfacct_root.nfacct_metrics; d ; d = d->next) {
  555. if(likely(d->updated)) {
  556. if(unlikely(!d->rd_packets))
  557. d->rd_packets = rrddim_add(
  558. st_packets
  559. , d->name
  560. , NULL
  561. , 1
  562. , nfacct_root.update_every
  563. , RRD_ALGORITHM_INCREMENTAL
  564. );
  565. rrddim_set_by_pointer(
  566. st_packets
  567. , d->rd_packets
  568. , (collected_number)d->pkts
  569. );
  570. }
  571. }
  572. rrdset_done(st_packets);
  573. // ----------------------------------------------------------------
  574. st_bytes = rrdset_find_bytype_localhost("netfilter", "nfacct_bytes");
  575. if(!st_bytes) {
  576. st_bytes = rrdset_create_localhost(
  577. "netfilter"
  578. , "nfacct_bytes"
  579. , NULL
  580. , "nfacct"
  581. , NULL
  582. , "Netfilter Accounting Bandwidth"
  583. , "kilobytes/s"
  584. , PLUGIN_NFACCT_NAME
  585. , NULL
  586. , NETDATA_CHART_PRIO_NETFILTER_BYTES
  587. , nfacct_root.update_every
  588. , RRDSET_TYPE_STACKED
  589. );
  590. }
  591. else rrdset_next(st_bytes);
  592. for(d = nfacct_root.nfacct_metrics; d ; d = d->next) {
  593. if(likely(d->updated)) {
  594. if(unlikely(!d->rd_bytes))
  595. d->rd_bytes = rrddim_add(
  596. st_bytes
  597. , d->name
  598. , NULL
  599. , 1
  600. , 1000 * nfacct_root.update_every
  601. , RRD_ALGORITHM_INCREMENTAL
  602. );
  603. rrddim_set_by_pointer(
  604. st_bytes
  605. , d->rd_bytes
  606. , (collected_number)d->bytes
  607. );
  608. }
  609. }
  610. rrdset_done(st_bytes);
  611. }
  612. #endif // HAVE_LIBNETFILTER_ACCT
  613. #endif // HAVE_LIBMNL
  614. // ----------------------------------------------------------------------------
  615. static void nfacct_main_cleanup(void *ptr) {
  616. struct netdata_static_thread *static_thread = (struct netdata_static_thread *)ptr;
  617. static_thread->enabled = NETDATA_MAIN_THREAD_EXITING;
  618. info("cleaning up...");
  619. #ifdef DO_NFACCT
  620. nfacct_cleanup();
  621. #endif
  622. #ifdef DO_NFSTAT
  623. nfstat_cleanup();
  624. #endif
  625. static_thread->enabled = NETDATA_MAIN_THREAD_EXITED;
  626. }
  627. void *nfacct_main(void *ptr) {
  628. netdata_thread_cleanup_push(nfacct_main_cleanup, ptr);
  629. int update_every = (int)config_get_number("plugin:netfilter", "update every", localhost->rrd_update_every);
  630. if(update_every < localhost->rrd_update_every)
  631. update_every = localhost->rrd_update_every;
  632. #ifdef DO_NFACCT
  633. int nfacct = !nfacct_init(update_every);
  634. #endif
  635. #ifdef DO_NFSTAT
  636. int nfstat = !nfstat_init(update_every);
  637. #endif
  638. // ------------------------------------------------------------------------
  639. usec_t step = update_every * USEC_PER_SEC;
  640. heartbeat_t hb;
  641. heartbeat_init(&hb);
  642. for(;;) {
  643. heartbeat_next(&hb, step);
  644. if(unlikely(netdata_exit)) break;
  645. #ifdef DO_NFACCT
  646. if(likely(nfacct)) {
  647. nfacct = !nfacct_collect();
  648. if(likely(nfacct))
  649. nfacct_send_metrics();
  650. }
  651. #endif
  652. #ifdef DO_NFSTAT
  653. if(likely(nfstat)) {
  654. nfstat = !nfstat_collect();
  655. if(likely(nfstat))
  656. nfstat_send_metrics();
  657. }
  658. #endif
  659. }
  660. netdata_thread_cleanup_pop(1);
  661. return NULL;
  662. }
  663. #endif // INTERNAL_PLUGIN_NFACCT