| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | #include "plugin_proc.h" |
| 4 | |
| 5 | #define PLUGIN_PROC_MODULE_SYNPROXY_NAME "/proc/net/stat/synproxy" |
| 6 | |
| 7 | #define RRD_TYPE_NET_STAT_NETFILTER "netfilter" |
| 8 | #define RRD_TYPE_NET_STAT_SYNPROXY "synproxy" |
| 9 | |
| 10 | int do_proc_net_stat_synproxy(int update_every, usec_t dt) { |
| 11 | (void)dt; |
| 12 | |
| 13 | static int do_cookies = -1, do_syns = -1, do_reopened = -1; |
| 14 | static procfile *ff = NULL; |
| 15 | |
| 16 | if(unlikely(do_cookies == -1)) { |
| 17 | do_cookies = inicfg_get_boolean_ondemand(&netdata_config, "plugin:proc:/proc/net/stat/synproxy", "SYNPROXY cookies", CONFIG_BOOLEAN_AUTO); |
| 18 | do_syns = inicfg_get_boolean_ondemand(&netdata_config, "plugin:proc:/proc/net/stat/synproxy", "SYNPROXY SYN received", CONFIG_BOOLEAN_AUTO); |
| 19 | do_reopened = inicfg_get_boolean_ondemand(&netdata_config, "plugin:proc:/proc/net/stat/synproxy", "SYNPROXY connections reopened", CONFIG_BOOLEAN_AUTO); |
| 20 | } |
| 21 | |
| 22 | if(unlikely(!ff)) { |
| 23 | char filename[FILENAME_MAX + 1]; |
| 24 | snprintfz(filename, FILENAME_MAX, "%s%s", netdata_configured_host_prefix, "/proc/net/stat/synproxy"); |
| 25 | ff = procfile_open(inicfg_get(&netdata_config, "plugin:proc:/proc/net/stat/synproxy", "filename to monitor", filename), " \t,:|", PROCFILE_FLAG_DEFAULT); |
| 26 | if(unlikely(!ff)) |
| 27 | return 1; |
| 28 | } |
| 29 | |
| 30 | ff = procfile_readall(ff); |
| 31 | if(unlikely(!ff)) |
| 32 | return 0; // we return 0, so that we will retry to open it next time |
| 33 | |
| 34 | // make sure we have 3 lines |
| 35 | size_t lines = procfile_lines(ff), l; |
| 36 | if(unlikely(lines < 2)) { |
| 37 | collector_error("/proc/net/stat/synproxy has %zu lines, expected no less than 2. Disabling it.", lines); |
| 38 | return 1; |
| 39 | } |
| 40 | |
| 41 | unsigned long long syn_received = 0, cookie_invalid = 0, cookie_valid = 0, cookie_retrans = 0, conn_reopened = 0; |
| 42 | |
| 43 | // synproxy gives its values per CPU |
| 44 | for(l = 1; l < lines ;l++) { |
| 45 | size_t words = procfile_linewords(ff, l); |
| 46 | if(unlikely(words < 6)) |
| 47 | continue; |
| 48 | |
| 49 | syn_received += strtoull(procfile_lineword(ff, l, 1), NULL, 16); |
| 50 | cookie_invalid += strtoull(procfile_lineword(ff, l, 2), NULL, 16); |
| 51 | cookie_valid += strtoull(procfile_lineword(ff, l, 3), NULL, 16); |
| 52 | cookie_retrans += strtoull(procfile_lineword(ff, l, 4), NULL, 16); |
| 53 | conn_reopened += strtoull(procfile_lineword(ff, l, 5), NULL, 16); |
| 54 | } |
| 55 | |
| 56 | // -------------------------------------------------------------------- |
| 57 | |
| 58 | if (do_syns == CONFIG_BOOLEAN_YES || do_syns == CONFIG_BOOLEAN_AUTO) { |
| 59 | do_syns = CONFIG_BOOLEAN_YES; |
| 60 | |
| 61 | static RRDSET *st = NULL; |
| 62 | if(unlikely(!st)) { |
| 63 | st = rrdset_create_localhost( |
| 64 | RRD_TYPE_NET_STAT_NETFILTER |
| 65 | , RRD_TYPE_NET_STAT_SYNPROXY "_syn_received" |
| 66 | , NULL |
| 67 | , RRD_TYPE_NET_STAT_SYNPROXY |
| 68 | , NULL |
| 69 | , "SYNPROXY SYN Packets received" |
| 70 | , "packets/s" |
| 71 | , PLUGIN_PROC_NAME |
| 72 | , PLUGIN_PROC_MODULE_SYNPROXY_NAME |
| 73 | , NETDATA_CHART_PRIO_SYNPROXY_SYN_RECEIVED |
| 74 | , update_every |
| 75 | , RRDSET_TYPE_LINE |
| 76 | ); |
| 77 | |
| 78 | rrddim_add(st, "received", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL); |
| 79 | } |
| 80 | |
| 81 | rrddim_set(st, "received", syn_received); |
| 82 | rrdset_done(st); |
| 83 | } |
| 84 | |
| 85 | // -------------------------------------------------------------------- |
| 86 | |
| 87 | if (do_reopened == CONFIG_BOOLEAN_YES || do_reopened == CONFIG_BOOLEAN_AUTO) { |
| 88 | do_reopened = CONFIG_BOOLEAN_YES; |
| 89 | |
| 90 | static RRDSET *st = NULL; |
| 91 | if(unlikely(!st)) { |
| 92 | st = rrdset_create_localhost( |
| 93 | RRD_TYPE_NET_STAT_NETFILTER |
| 94 | , RRD_TYPE_NET_STAT_SYNPROXY "_conn_reopened" |
| 95 | , NULL |
| 96 | , RRD_TYPE_NET_STAT_SYNPROXY |
| 97 | , NULL |
| 98 | , "SYNPROXY Connections Reopened" |
| 99 | , "connections/s" |
| 100 | , PLUGIN_PROC_NAME |
| 101 | , PLUGIN_PROC_MODULE_SYNPROXY_NAME |
| 102 | , NETDATA_CHART_PRIO_SYNPROXY_CONN_OPEN |
| 103 | , update_every |
| 104 | , RRDSET_TYPE_LINE |
| 105 | ); |
| 106 | |
| 107 | rrddim_add(st, "reopened", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL); |
| 108 | } |
| 109 | |
| 110 | rrddim_set(st, "reopened", conn_reopened); |
| 111 | rrdset_done(st); |
| 112 | } |
| 113 | |
| 114 | // -------------------------------------------------------------------- |
| 115 | |
| 116 | if (do_cookies == CONFIG_BOOLEAN_YES || do_cookies == CONFIG_BOOLEAN_AUTO) { |
| 117 | do_cookies = CONFIG_BOOLEAN_YES; |
| 118 | |
| 119 | static RRDSET *st = NULL; |
| 120 | if(unlikely(!st)) { |
| 121 | st = rrdset_create_localhost( |
| 122 | RRD_TYPE_NET_STAT_NETFILTER |
| 123 | , RRD_TYPE_NET_STAT_SYNPROXY "_cookies" |
| 124 | , NULL |
| 125 | , RRD_TYPE_NET_STAT_SYNPROXY |
| 126 | , NULL |
| 127 | , "SYNPROXY TCP Cookies" |
| 128 | , "cookies/s" |
| 129 | , PLUGIN_PROC_NAME |
| 130 | , PLUGIN_PROC_MODULE_SYNPROXY_NAME |
| 131 | , NETDATA_CHART_PRIO_SYNPROXY_COOKIES |
| 132 | , update_every |
| 133 | , RRDSET_TYPE_LINE |
| 134 | ); |
| 135 | |
| 136 | rrddim_add(st, "valid", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL); |
| 137 | rrddim_add(st, "invalid", NULL, -1, 1, RRD_ALGORITHM_INCREMENTAL); |
| 138 | rrddim_add(st, "retransmits", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL); |
| 139 | } |
| 140 | |
| 141 | rrddim_set(st, "valid", cookie_valid); |
| 142 | rrddim_set(st, "invalid", cookie_invalid); |
| 143 | rrddim_set(st, "retransmits", cookie_retrans); |
| 144 | rrdset_done(st); |
| 145 | } |
| 146 | |
| 147 | return 0; |
| 148 | } |