plugin_proc.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. { .name = "/proc/mdstat", .dim = "mdstat", .func = do_proc_mdstat },
  41. // NFS metrics
  42. { .name = "/proc/net/rpc/nfsd", .dim = "nfsd", .func = do_proc_net_rpc_nfsd },
  43. { .name = "/proc/net/rpc/nfs", .dim = "nfs", .func = do_proc_net_rpc_nfs },
  44. // ZFS metrics
  45. { .name = "/proc/spl/kstat/zfs/arcstats", .dim = "zfs_arcstats", .func = do_proc_spl_kstat_zfs_arcstats },
  46. // BTRFS metrics
  47. { .name = "/sys/fs/btrfs", .dim = "btrfs", .func = do_sys_fs_btrfs },
  48. // IPC metrics
  49. { .name = "ipc", .dim = "ipc", .func = do_ipc },
  50. // the terminator of this array
  51. { .name = NULL, .dim = NULL, .func = NULL }
  52. };
  53. static void proc_main_cleanup(void *ptr) {
  54. struct netdata_static_thread *static_thread = (struct netdata_static_thread *)ptr;
  55. static_thread->enabled = NETDATA_MAIN_THREAD_EXITING;
  56. info("cleaning up...");
  57. static_thread->enabled = NETDATA_MAIN_THREAD_EXITED;
  58. }
  59. void *proc_main(void *ptr) {
  60. netdata_thread_cleanup_push(proc_main_cleanup, ptr);
  61. int vdo_cpu_netdata = config_get_boolean("plugin:proc", "netdata server resources", 1);
  62. // check the enabled status for each module
  63. int i;
  64. for(i = 0 ; proc_modules[i].name ;i++) {
  65. struct proc_module *pm = &proc_modules[i];
  66. pm->enabled = config_get_boolean("plugin:proc", pm->name, 1);
  67. pm->duration = 0ULL;
  68. pm->rd = NULL;
  69. }
  70. usec_t step = localhost->rrd_update_every * USEC_PER_SEC;
  71. heartbeat_t hb;
  72. heartbeat_init(&hb);
  73. size_t iterations = 0;
  74. while(!netdata_exit) {
  75. iterations++;
  76. (void)iterations;
  77. usec_t hb_dt = heartbeat_next(&hb, step);
  78. usec_t duration = 0ULL;
  79. if(unlikely(netdata_exit)) break;
  80. // BEGIN -- the job to be done
  81. for(i = 0 ; proc_modules[i].name ;i++) {
  82. struct proc_module *pm = &proc_modules[i];
  83. if(unlikely(!pm->enabled)) continue;
  84. debug(D_PROCNETDEV_LOOP, "PROC calling %s.", pm->name);
  85. //#ifdef NETDATA_LOG_ALLOCATIONS
  86. // if(pm->func == do_proc_interrupts)
  87. // log_thread_memory_allocations = iterations;
  88. //#endif
  89. pm->enabled = !pm->func(localhost->rrd_update_every, hb_dt);
  90. pm->duration = heartbeat_monotonic_dt_to_now_usec(&hb) - duration;
  91. duration += pm->duration;
  92. //#ifdef NETDATA_LOG_ALLOCATIONS
  93. // if(pm->func == do_proc_interrupts)
  94. // log_thread_memory_allocations = 0;
  95. //#endif
  96. if(unlikely(netdata_exit)) break;
  97. }
  98. // END -- the job is done
  99. // --------------------------------------------------------------------
  100. if(vdo_cpu_netdata) {
  101. static RRDSET *st = NULL;
  102. if(unlikely(!st)) {
  103. st = rrdset_find_bytype_localhost("netdata", "plugin_proc_modules");
  104. if(!st) {
  105. st = rrdset_create_localhost(
  106. "netdata"
  107. , "plugin_proc_modules"
  108. , NULL
  109. , "proc"
  110. , NULL
  111. , "NetData Proc Plugin Modules Durations"
  112. , "milliseconds/run"
  113. , "netdata"
  114. , "stats"
  115. , 132001
  116. , localhost->rrd_update_every
  117. , RRDSET_TYPE_STACKED
  118. );
  119. for(i = 0 ; proc_modules[i].name ;i++) {
  120. struct proc_module *pm = &proc_modules[i];
  121. if(unlikely(!pm->enabled)) continue;
  122. pm->rd = rrddim_add(st, pm->dim, NULL, 1, 1000, RRD_ALGORITHM_ABSOLUTE);
  123. }
  124. }
  125. }
  126. else rrdset_next(st);
  127. for(i = 0 ; proc_modules[i].name ;i++) {
  128. struct proc_module *pm = &proc_modules[i];
  129. if(unlikely(!pm->enabled)) continue;
  130. rrddim_set_by_pointer(st, pm->rd, pm->duration);
  131. }
  132. rrdset_done(st);
  133. global_statistics_charts();
  134. registry_statistics();
  135. }
  136. }
  137. netdata_thread_cleanup_pop(1);
  138. return NULL;
  139. }
  140. int get_numa_node_count(void)
  141. {
  142. static int numa_node_count = -1;
  143. if (numa_node_count != -1)
  144. return numa_node_count;
  145. numa_node_count = 0;
  146. char name[FILENAME_MAX + 1];
  147. snprintfz(name, FILENAME_MAX, "%s%s", netdata_configured_host_prefix, "/sys/devices/system/node");
  148. char *dirname = config_get("plugin:proc:/sys/devices/system/node", "directory to monitor", name);
  149. DIR *dir = opendir(dirname);
  150. if(dir) {
  151. struct dirent *de = NULL;
  152. while((de = readdir(dir))) {
  153. if(de->d_type != DT_DIR)
  154. continue;
  155. if(strncmp(de->d_name, "node", 4) != 0)
  156. continue;
  157. if(!isdigit(de->d_name[4]))
  158. continue;
  159. numa_node_count++;
  160. }
  161. closedir(dir);
  162. }
  163. return numa_node_count;
  164. }