@cryptotaxi247 / netdata-1 / commits / ed4a91fb6

delay collecting double linked network interfaces (#16701)

Ilya Mashchenko committed Dec 30, 2023 at 00:03 UTC ed4a91fb605a5526daf244a3c48b5fbb2b6aaf2f
1 file changed +27 -2
collectors/proc.plugin/proc_net_dev.c
+27 -2
@@ -11,6 +11,8 @@
11
12 #define READ_RETRY_PERIOD 60 // seconds
13
14 +time_t double_linked_device_collect_delay_secs = 120;
15 +
16 void cgroup_netdev_reset_all(void);
17 void cgroup_netdev_release(const DICTIONARY_ITEM *link);
18 const void *cgroup_netdev_dup(const DICTIONARY_ITEM *link);
@@ -99,6 +101,8 @@ static struct netdev {
101
102 bool function_ready;
103
104 + bool double_linked; // iflink != ifindex
105 +
106 time_t discover_time;
107
108 int carrier_file_exists;
@@ -988,6 +992,7 @@ static struct netdev *get_netdev(const char *name) {
992 d->len = strlen(d->name);
993 d->chart_labels = rrdlabels_create();
994 d->function_ready = false;
995 + d->double_linked = false;
996
997 d->chart_type_net_bytes = strdupz("net");
998 d->chart_type_net_compressed = strdupz("net_compressed");
@@ -1047,7 +1052,21 @@ static struct netdev *get_netdev(const char *name) {
1052 return d;
1053 }
1054
1050 -#define NETDEV_VIRTUAL_COLLECT_DELAY 15 // 1 full run of the cgroups discovery thread (10 secs by default)
1055 +static bool is_iface_double_linked(struct netdev *d) {
1056 + char filename[FILENAME_MAX + 1];
1057 + unsigned long long iflink = 0;
1058 + unsigned long long ifindex = 0;
1059 +
1060 + snprintfz(filename, FILENAME_MAX, "%s/sys/class/net/%s/iflink", netdata_configured_host_prefix, d->name);
1061 + if (read_single_number_file(filename, &iflink))
1062 + return false;
1063 +
1064 + snprintfz(filename, FILENAME_MAX, "%s/sys/class/net/%s/ifindex", netdata_configured_host_prefix, d->name);
1065 + if (read_single_number_file(filename, &ifindex))
1066 + return false;
1067 +
1068 + return iflink != ifindex;
1069 +}
1070
1071 int do_proc_net_dev(int update_every, usec_t dt) {
1072 (void)dt;
@@ -1197,6 +1216,8 @@ int do_proc_net_dev(int update_every, usec_t dt) {
1216 if(d->enabled == CONFIG_BOOLEAN_NO)
1217 continue;
1218
1219 + d->double_linked = is_iface_double_linked(d);
1220 +
1221 d->do_bandwidth = do_bandwidth;
1222 d->do_packets = do_packets;
1223 d->do_errors = do_errors;
@@ -1243,7 +1264,7 @@ int do_proc_net_dev(int update_every, usec_t dt) {
1264 // This is necessary to prevent the creation of charts for virtual interfaces that will later be
1265 // recreated as container interfaces (create container) or
1266 // rediscovered and recreated only to be deleted almost immediately (stop/remove container)
1246 - if (d->virtual && (now - d->discover_time < NETDEV_VIRTUAL_COLLECT_DELAY)) {
1267 + if (d->double_linked && d->virtual && (now - d->discover_time < double_linked_device_collect_delay_secs)) {
1268 continue;
1269 }
1270
@@ -1928,6 +1949,10 @@ void *netdev_main(void *ptr)
1949 worker_register("NETDEV");
1950 worker_register_job_name(0, "netdev");
1951
1952 + if (getenv("KUBERNETES_SERVICE_HOST") != NULL && getenv("KUBERNETES_SERVICE_PORT") != NULL) {
1953 + double_linked_device_collect_delay_secs = 300;
1954 + }
1955 +
1956 netdata_thread_cleanup_push(netdev_main_cleanup, ptr) {
1957 rrd_collector_started();
1958 rrd_function_add(localhost, NULL, "network-interfaces", 10, RRDFUNCTIONS_PRIORITY_DEFAULT, RRDFUNCTIONS_NETDEV_HELP,