@cryptotaxi247 / netdata-1 / commits / 75acdd85d

local_listeners: add cmd args for reading specific files (#16273)

Ilya Mashchenko committed Oct 27, 2023 at 15:55 UTC 75acdd85d0b6a82713cd45b9566f06a3baf7ac47
1 file changed +44 -10
collectors/plugins.d/local_listeners.c
+44 -10
@@ -338,25 +338,59 @@ bool read_proc_net_x(const char *filename, PROC_NET_PROTOCOLS protocol) {
338 }
339
340 // ----------------------------------------------------------------------------
341 -
342 -int main(int argc __maybe_unused, char **argv __maybe_unused) {
341 +typedef struct {
342 + bool read_tcp;
343 + bool read_tcp6;
344 + bool read_udp;
345 + bool read_udp6;
346 +} CommandLineArguments;
347 +
348 +int main(int argc, char **argv) {
349 char path[FILENAME_MAX + 1];
350 hashTable_key_inode_port_value = createHashTable();
351
352 netdata_configured_host_prefix = getenv("NETDATA_HOST_PREFIX");
353 if(!netdata_configured_host_prefix) netdata_configured_host_prefix = "";
354
349 - snprintfz(path, FILENAME_MAX, "%s/proc/net/tcp", netdata_configured_host_prefix);
350 - read_proc_net_x(path, PROC_NET_PROTOCOL_TCP);
355 + CommandLineArguments args = {.read_tcp = false, .read_tcp6 = false, .read_udp = false, .read_udp6 = false};
356 +
357 + for (int i = 1; i < argc; i++) {
358 + if (strcmp("tcp", argv[i]) == 0) {
359 + args.read_tcp = true;
360 + continue;
361 + } else if (strcmp("tcp6", argv[i]) == 0) {
362 + args.read_tcp6 = true;
363 + continue;
364 + } else if (strcmp("udp", argv[i]) == 0) {
365 + args.read_udp = true;
366 + continue;
367 + } else if (strcmp("udp6", argv[i]) == 0) {
368 + args.read_udp6 = true;
369 + continue;
370 + }
371 + }
372 +
373 + bool read_all_files = (!args.read_tcp && !args.read_tcp6 && !args.read_udp && !args.read_udp6);
374
352 - snprintfz(path, FILENAME_MAX, "%s/proc/net/udp", netdata_configured_host_prefix);
353 - read_proc_net_x(path, PROC_NET_PROTOCOL_UDP);
375 + if (read_all_files || args.read_tcp) {
376 + snprintfz(path, FILENAME_MAX, "%s/proc/net/tcp", netdata_configured_host_prefix);
377 + read_proc_net_x(path, PROC_NET_PROTOCOL_TCP);
378 + }
379
355 - snprintfz(path, FILENAME_MAX, "%s/proc/net/tcp6", netdata_configured_host_prefix);
356 - read_proc_net_x(path, PROC_NET_PROTOCOL_TCP6);
380 + if (read_all_files || args.read_udp) {
381 + snprintfz(path, FILENAME_MAX, "%s/proc/net/udp", netdata_configured_host_prefix);
382 + read_proc_net_x(path, PROC_NET_PROTOCOL_UDP);
383 + }
384
358 - snprintfz(path, FILENAME_MAX, "%s/proc/net/udp6", netdata_configured_host_prefix);
359 - read_proc_net_x(path, PROC_NET_PROTOCOL_UDP6);
385 + if (read_all_files || args.read_tcp6) {
386 + snprintfz(path, FILENAME_MAX, "%s/proc/net/tcp6", netdata_configured_host_prefix);
387 + read_proc_net_x(path, PROC_NET_PROTOCOL_TCP6);
388 + }
389 +
390 + if (read_all_files || args.read_udp6) {
391 + snprintfz(path, FILENAME_MAX, "%s/proc/net/udp6", netdata_configured_host_prefix);
392 + read_proc_net_x(path, PROC_NET_PROTOCOL_UDP6);
393 + }
394
395 snprintfz(path, FILENAME_MAX, "%s/proc", netdata_configured_host_prefix);
396 find_all_sockets_in_proc(path);