plugin_proc.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "plugin_proc.h"
  3. static struct proc_module {
  4. const char *name;
  5. const char *dim;
  6. int enabled;
  7. int (*func)(int update_every, usec_t dt);
  8. usec_t duration;
  9. RRDDIM *rd;
  10. } proc_modules[] = {
  11. // system metrics
  12. { .name = "/proc/stat", .dim = "stat", .func = do_proc_stat },
  13. { .name = "/proc/uptime", .dim = "uptime", .func = do_proc_uptime },
  14. { .name = "/proc/loadavg", .dim = "loadavg", .func = do_proc_loadavg },
  15. { .name = "/proc/sys/kernel/random/entropy_avail", .dim = "entropy", .func = do_proc_sys_kernel_random_entropy_avail },
  16. // CPU metrics
  17. { .name = "/proc/interrupts", .dim = "interrupts", .func = do_proc_interrupts },
  18. { .name = "/proc/softirqs", .dim = "softirqs", .func = do_proc_softirqs },
  19. // memory metrics
  20. { .name = "/proc/vmstat", .dim = "vmstat", .func = do_proc_vmstat },
  21. { .name = "/proc/meminfo", .dim = "meminfo", .func = do_proc_meminfo },
  22. { .name = "/sys/kernel/mm/ksm", .dim = "ksm", .func = do_sys_kernel_mm_ksm },
  23. { .name = "/sys/devices/system/edac/mc", .dim = "ecc", .func = do_proc_sys_devices_system_edac_mc },
  24. { .name = "/sys/devices/system/node", .dim = "numa", .func = do_proc_sys_devices_system_node },
  25. // network metrics
  26. { .name = "/proc/net/dev", .dim = "netdev", .func = do_proc_net_dev },
  27. { .name = "/proc/net/sockstat", .dim = "sockstat", .func = do_proc_net_sockstat },
  28. { .name = "/proc/net/sockstat6", .dim = "sockstat6", .func = do_proc_net_sockstat6 },
  29. { .name = "/proc/net/netstat", .dim = "netstat", .func = do_proc_net_netstat }, // this has to be before /proc/net/snmp, because there is a shared metric
  30. { .name = "/proc/net/snmp", .dim = "snmp", .func = do_proc_net_snmp },
  31. { .name = "/proc/net/snmp6", .dim = "snmp6", .func = do_proc_net_snmp6 },
  32. { .name = "/proc/net/sctp/snmp", .dim = "sctp", .func = do_proc_net_sctp_snmp },
  33. { .name = "/proc/net/softnet_stat", .dim = "softnet", .func = do_proc_net_softnet_stat },
  34. { .name = "/proc/net/ip_vs/stats", .dim = "ipvs", .func = do_proc_net_ip_vs_stats },
  35. // firewall metrics
  36. { .name = "/proc/net/stat/conntrack", .dim = "conntrack", .func = do_proc_net_stat_conntrack },
  37. { .name = "/proc/net/stat/synproxy", .dim = "synproxy", .func = do_proc_net_stat_synproxy },
  38. // disk metrics
  39. { .name = "/proc/diskstats", .dim = "diskstats", .func = do_proc_diskstats },
  40. // NFS metrics
  41. { .name = "/proc/net/rpc/nfsd", .dim = "nfsd", .func = do_proc_net_rpc_nfsd },
  42. { .name = "/proc/net/rpc/nfs", .dim = "nfs", .func = do_proc_net_rpc_nfs },
  43. // ZFS metrics
  44. { .name = "/proc/spl/kstat/zfs/arcstats", .dim = "zfs_arcstats", .func = do_proc_spl_kstat_zfs_arcstats },
  45. // BTRFS metrics
  46. { .name = "/sys/fs/btrfs", .dim = "btrfs", .func = do_sys_fs_btrfs },
  47. // IPC metrics
  48. { .name = "ipc", .dim = "ipc", .func = do_ipc },
  49. // the terminator of this array
  50. { .name = NULL, .dim = NULL, .func = NULL }
  51. };
  52. static void proc_main_cleanup(void *ptr) {
  53. struct netdata_static_thread *static_thread = (struct netdata_static_thread *)ptr;
  54. static_thread->enabled = NETDATA_MAIN_THREAD_EXITING;
  55. info("cleaning up...");
  56. static_thread->enabled = NETDATA_MAIN_THREAD_EXITED;
  57. }
  58. void *proc_main(void *ptr) {
  59. netdata_thread_cleanup_push(proc_main_cleanup, ptr);
  60. int vdo_cpu_netdata = config_get_boolean("plugin:proc", "netdata server resources", 1);
  61. // check the enabled status for each module
  62. int i;
  63. for(i = 0 ; proc_modules[i].name ;i++) {
  64. struct proc_module *pm = &proc_modules[i];
  65. pm->enabled = config_get_boolean("plugin:proc", pm->name, 1);
  66. pm->duration = 0ULL;
  67. pm->rd = NULL;
  68. }
  69. usec_t step = localhost->rrd_update_every * USEC_PER_SEC;
  70. heartbeat_t hb;
  71. heartbeat_init(&hb);
  72. size_t iterations = 0;
  73. while(!netdata_exit) {
  74. iterations++;
  75. (void)iterations;
  76. usec_t hb_dt = heartbeat_next(&hb, step);
  77. usec_t duration = 0ULL;
  78. if(unlikely(netdata_exit)) break;
  79. // BEGIN -- the job to be done
  80. for(i = 0 ; proc_modules[i].name ;i++) {
  81. struct proc_module *pm = &proc_modules[i];
  82. if(unlikely(!pm->enabled)) continue;
  83. debug(D_PROCNETDEV_LOOP, "PROC calling %s.", pm->name);
  84. //#ifdef NETDATA_LOG_ALLOCATIONS
  85. // if(pm->func == do_proc_interrupts)
  86. // log_thread_memory_allocations = iterations;
  87. //#endif
  88. pm->enabled = !pm->func(localhost->rrd_update_every, hb_dt);
  89. pm->duration = heartbeat_monotonic_dt_to_now_usec(&hb) - duration;
  90. duration += pm->duration;
  91. //#ifdef NETDATA_LOG_ALLOCATIONS
  92. // if(pm->func == do_proc_interrupts)
  93. // log_thread_memory_allocations = 0;
  94. //#endif
  95. if(unlikely(netdata_exit)) break;
  96. }
  97. // END -- the job is done
  98. // --------------------------------------------------------------------
  99. if(vdo_cpu_netdata) {
  100. static RRDSET *st = NULL;
  101. if(unlikely(!st)) {
  102. st = rrdset_find_bytype_localhost("netdata", "plugin_proc_modules");
  103. if(!st) {
  104. st = rrdset_create_localhost(
  105. "netdata"
  106. , "plugin_proc_modules"
  107. , NULL
  108. , "proc"
  109. , NULL
  110. , "NetData Proc Plugin Modules Durations"
  111. , "milliseconds/run"
  112. , "netdata"
  113. , "stats"
  114. , 132001
  115. , localhost->rrd_update_every
  116. , RRDSET_TYPE_STACKED
  117. );
  118. for(i = 0 ; proc_modules[i].name ;i++) {
  119. struct proc_module *pm = &proc_modules[i];
  120. if(unlikely(!pm->enabled)) continue;
  121. pm->rd = rrddim_add(st, pm->dim, NULL, 1, 1000, RRD_ALGORITHM_ABSOLUTE);
  122. }
  123. }
  124. }
  125. else rrdset_next(st);
  126. for(i = 0 ; proc_modules[i].name ;i++) {
  127. struct proc_module *pm = &proc_modules[i];
  128. if(unlikely(!pm->enabled)) continue;
  129. rrddim_set_by_pointer(st, pm->rd, pm->duration);
  130. }
  131. rrdset_done(st);
  132. global_statistics_charts();
  133. registry_statistics();
  134. }
  135. }
  136. netdata_thread_cleanup_pop(1);
  137. return NULL;
  138. }
  139. int get_numa_node_count(void)
  140. {
  141. static int numa_node_count = -1;
  142. if (numa_node_count != -1)
  143. return numa_node_count;
  144. numa_node_count = 0;
  145. char name[FILENAME_MAX + 1];
  146. snprintfz(name, FILENAME_MAX, "%s%s", netdata_configured_host_prefix, "/sys/devices/system/node");
  147. char *dirname = config_get("plugin:proc:/sys/devices/system/node", "directory to monitor", name);
  148. DIR *dir = opendir(dirname);
  149. if(dir) {
  150. struct dirent *de = NULL;
  151. while((de = readdir(dir))) {
  152. if(de->d_type != DT_DIR)
  153. continue;
  154. if(strncmp(de->d_name, "node", 4) != 0)
  155. continue;
  156. if(!isdigit(de->d_name[4]))
  157. continue;
  158. numa_node_count++;
  159. }
  160. closedir(dir);
  161. }
  162. return numa_node_count;
  163. }