master
c 305 lines 10.6 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 #include "plugin_proc.h"
4
5 static struct proc_module {
6 const char *name;
7 const char *dim;
8
9 int enabled;
10
11 int (*func)(int update_every, usec_t dt);
12 void (*cleanup)(void); // Cleanup function pointer
13
14 RRDDIM *rd;
15
16 } proc_modules[] = {
17
18 // system metrics
19 {.name = "/proc/stat", .dim = "stat", .func = do_proc_stat, .cleanup = proc_stat_plugin_cleanup},
20 {.name = "/proc/uptime", .dim = "uptime", .func = do_proc_uptime},
21 {.name = "/proc/loadavg", .dim = "loadavg", .func = do_proc_loadavg, .cleanup = proc_loadavg_plugin_cleanup},
22 {.name = "/proc/sys/fs/file-nr", .dim = "file-nr", .func = do_proc_sys_fs_file_nr},
23 {.name = "/proc/sys/kernel/random/entropy_avail", .dim = "entropy", .func = do_proc_sys_kernel_random_entropy_avail},
24
25 {.name = "/run/reboot_required", .dim = "reboot-required", .func = do_run_reboot_required},
26
27 // pressure metrics
28 {.name = "/proc/pressure", .dim = "pressure", .func = do_proc_pressure},
29
30 // CPU metrics
31 {.name = "/proc/interrupts", .dim = "interrupts", .func = do_proc_interrupts},
32 {.name = "/proc/softirqs", .dim = "softirqs", .func = do_proc_softirqs},
33
34 // memory metrics
35 {.name = "/proc/vmstat", .dim = "vmstat", .func = do_proc_vmstat},
36 {.name = "/proc/meminfo", .dim = "meminfo", .func = do_proc_meminfo},
37 {.name = "/sys/kernel/mm/ksm", .dim = "ksm", .func = do_sys_kernel_mm_ksm},
38 {.name = "/sys/block/zram", .dim = "zram", .func = do_sys_block_zram},
39 {.name = "/sys/devices/system/edac/mc", .dim = "edac", .func = do_proc_sys_devices_system_edac_mc},
40 {.name = "/sys/devices/pci/aer", .dim = "pci_aer", .func = do_proc_sys_devices_pci_aer, .cleanup = pci_aer_plugin_cleanup},
41 {.name = "/sys/devices/system/node", .dim = "numa", .func = do_proc_sys_devices_system_node},
42 {.name = "/proc/pagetypeinfo", .dim = "pagetypeinfo", .func = do_proc_pagetypeinfo},
43
44 // network metrics
45 {.name = "/proc/net/wireless", .dim = "netwireless", .func = do_proc_net_wireless},
46 {.name = "/proc/net/sockstat", .dim = "sockstat", .func = do_proc_net_sockstat, .cleanup = proc_net_sockstat_plugin_cleanup},
47 {.name = "/proc/net/sockstat6", .dim = "sockstat6", .func = do_proc_net_sockstat6},
48 {.name = "/proc/net/netstat", .dim = "netstat", .func = do_proc_net_netstat, .cleanup = proc_net_netstat_cleanup},
49 {.name = "/proc/net/sctp/snmp", .dim = "sctp", .func = do_proc_net_sctp_snmp},
50 {.name = "/proc/net/softnet_stat", .dim = "softnet", .func = do_proc_net_softnet_stat},
51 {.name = "/proc/net/ip_vs/stats", .dim = "ipvs", .func = do_proc_net_ip_vs_stats},
52 {.name = "/sys/class/infiniband", .dim = "infiniband", .func = do_sys_class_infiniband, .cleanup = sys_class_infiniband_plugin_cleanup},
53
54 // firewall metrics
55 {.name = "/proc/net/stat/conntrack", .dim = "conntrack", .func = do_proc_net_stat_conntrack, .cleanup = proc_net_stat_conntrack_cleanup},
56 {.name = "/proc/net/stat/synproxy", .dim = "synproxy", .func = do_proc_net_stat_synproxy},
57
58 // disk metrics
59 {.name = "/proc/diskstats", .dim = "diskstats", .func = do_proc_diskstats},
60 {.name = "/proc/mdstat", .dim = "mdstat", .func = do_proc_mdstat},
61
62 // NFS metrics
63 {.name = "/proc/net/rpc/nfsd", .dim = "nfsd", .func = do_proc_net_rpc_nfsd},
64 {.name = "/proc/net/rpc/nfs", .dim = "nfs", .func = do_proc_net_rpc_nfs},
65
66 // ZFS metrics
67 {.name = "/proc/spl/kstat/zfs/arcstats", .dim = "zfs_arcstats", .func = do_proc_spl_kstat_zfs_arcstats},
68
69 // BTRFS metrics
70 {.name = "/sys/fs/btrfs", .dim = "btrfs", .func = do_sys_fs_btrfs},
71
72 // IPC metrics
73 {.name = "ipc", .dim = "ipc", .func = do_proc_ipc, .cleanup = proc_ipc_cleanup},
74
75 // linux power supply metrics
76 {.name = "/sys/class/power_supply", .dim = "power_supply", .func = do_sys_class_power_supply},
77
78 // GPU metrics
79 {.name = "/sys/class/drm", .dim = "drm", .func = do_sys_class_drm},
80
81 // the terminator of this array
82 {.name = NULL, .dim = NULL, .func = NULL}
83 };
84
85 #if WORKER_UTILIZATION_MAX_JOB_TYPES < 36
86 #error WORKER_UTILIZATION_MAX_JOB_TYPES has to be at least 36
87 #endif
88
89 static ND_THREAD *netdev_thread = NULL;
90
91 static void proc_main_cleanup(void *pptr)
92 {
93 struct netdata_static_thread *static_thread = CLEANUP_FUNCTION_GET_PTR(pptr);
94 if(!static_thread) return;
95
96 static_thread->enabled = NETDATA_MAIN_THREAD_EXITING;
97
98 // Run all module cleanup functions
99 int i;
100 for(i = 0; proc_modules[i].name; i++) {
101 if(proc_modules[i].cleanup)
102 proc_modules[i].cleanup();
103 }
104
105 nd_thread_join(netdev_thread);
106 worker_unregister();
107
108 static_thread->enabled = NETDATA_MAIN_THREAD_EXITED;
109 }
110
111 bool inside_lxc_container = false;
112 bool is_mem_swap_enabled = false;
113 bool is_mem_zswap_enabled = false;
114 bool is_mem_ksm_enabled = false;
115
116 bool is_lxcfs_proc_mounted(void) {
117 char filename[FILENAME_MAX + 1];
118 snprintfz(filename, FILENAME_MAX, "/proc/self/mounts");
119
120 procfile *ff = procfile_open(filename, " \t", PROCFILE_FLAG_DEFAULT);
121 if (unlikely(!ff))
122 return false;
123
124 ff = procfile_readall(ff);
125 if (unlikely(!ff))
126 return false;
127
128 bool found = false;
129 unsigned long lines = procfile_lines(ff);
130
131 for (unsigned long l = 0; l < lines; l++) {
132 size_t words = procfile_linewords(ff, l);
133 if (words < 2)
134 continue;
135
136 if (!strcmp(procfile_lineword(ff, l, 0), "lxcfs") &&
137 !strncmp(procfile_lineword(ff, l, 1), "/proc", 5)) {
138 found = true;
139 break;
140 }
141 }
142
143 procfile_close(ff);
144 return found;
145 }
146
147 static bool is_ksm_enabled() {
148 unsigned long long ksm_run = 0;
149
150 char filename[FILENAME_MAX + 1];
151 snprintfz(filename, FILENAME_MAX, "%s/sys/kernel/mm/ksm/run", netdata_configured_host_prefix);
152
153 return !read_single_number_file(filename, &ksm_run) && ksm_run == 1;
154 }
155
156 static bool is_zswap_enabled() {
157 char filename[FILENAME_MAX + 1];
158 snprintfz(filename, FILENAME_MAX, "/sys/module/zswap/parameters/enabled"); // host prefix is not needed here
159 char state[1 + 1]; // Y or N
160
161 int ret = read_txt_file(filename, state, sizeof(state));
162
163 return !ret && !strcmp(state, "Y");
164 }
165
166 static bool is_swap_enabled() {
167 char filename[FILENAME_MAX + 1];
168 snprintfz(filename, FILENAME_MAX, "%s/proc/meminfo", netdata_configured_host_prefix);
169
170 procfile *ff = procfile_open(filename, " \t:", PROCFILE_FLAG_DEFAULT);
171 if (!ff) {
172 return false;
173 }
174
175 ff = procfile_readall(ff);
176 if (!ff) {
177 procfile_close(ff);
178 return false;
179 }
180
181 unsigned long long swap_total = 0;
182
183 size_t lines = procfile_lines(ff), l;
184
185 for (l = 0; l < lines; l++) {
186 size_t words = procfile_linewords(ff, l);
187 if (words < 2)
188 continue;
189
190 const char *key = procfile_lineword(ff, l, 0);
191 if (strcmp(key, "SwapTotal") == 0) {
192 swap_total = str2ull(procfile_lineword(ff, l, 1), NULL);
193 break;
194 }
195 }
196
197 procfile_close(ff);
198
199 return swap_total > 0;
200 }
201
202 static bool log_proc_module(BUFFER *wb, void *data) {
203 struct proc_module *pm = data;
204 buffer_sprintf(wb, "proc.plugin[%s]", pm->name);
205 return true;
206 }
207
208 void proc_main(void *ptr)
209 {
210 CLEANUP_FUNCTION_REGISTER(proc_main_cleanup) cleanup_ptr = ptr;
211
212 worker_register("PROC");
213
214 rrd_collector_started();
215
216 if (inicfg_get_boolean(&netdata_config, "plugin:proc", "/proc/net/dev", CONFIG_BOOLEAN_YES)) {
217 netdata_log_debug(D_SYSTEM, "Starting thread %s.", THREAD_NETDEV_NAME);
218 netdev_thread = nd_thread_create(THREAD_NETDEV_NAME, NETDATA_THREAD_OPTION_DEFAULT, netdev_main, NULL);
219 }
220
221 inicfg_get_boolean(&netdata_config, "plugin:proc", "/proc/pagetypeinfo", CONFIG_BOOLEAN_NO);
222
223 // check the enabled status for each module
224 int i;
225 for(i = 0; proc_modules[i].name; i++) {
226 struct proc_module *pm = &proc_modules[i];
227
228 pm->enabled = inicfg_get_boolean(&netdata_config, "plugin:proc", pm->name, CONFIG_BOOLEAN_YES);
229 pm->rd = NULL;
230
231 worker_register_job_name(i, proc_modules[i].dim);
232 }
233
234 heartbeat_t hb;
235 heartbeat_init(&hb, localhost->rrd_update_every * USEC_PER_SEC);
236
237 inside_lxc_container = is_lxcfs_proc_mounted();
238 is_mem_swap_enabled = is_swap_enabled();
239 is_mem_zswap_enabled = is_zswap_enabled();
240 is_mem_ksm_enabled = is_ksm_enabled();
241
242 #define LGS_MODULE_ID 0
243
244 ND_LOG_STACK lgs[] = {
245 [LGS_MODULE_ID] = ND_LOG_FIELD_TXT(NDF_MODULE, "proc.plugin"),
246 ND_LOG_FIELD_END(),
247 };
248 ND_LOG_STACK_PUSH(lgs);
249
250 while(service_running(SERVICE_COLLECTORS)) {
251 worker_is_idle();
252 usec_t hb_dt = heartbeat_next(&hb);
253
254 if(unlikely(!service_running(SERVICE_COLLECTORS)))
255 break;
256
257 for(i = 0; proc_modules[i].name; i++) {
258 if(unlikely(!service_running(SERVICE_COLLECTORS)))
259 break;
260
261 struct proc_module *pm = &proc_modules[i];
262 if(unlikely(!pm->enabled))
263 continue;
264
265 worker_is_busy(i);
266 lgs[LGS_MODULE_ID] = ND_LOG_FIELD_CB(NDF_MODULE, log_proc_module, pm);
267 pm->enabled = !pm->func(localhost->rrd_update_every, hb_dt);
268 lgs[LGS_MODULE_ID] = ND_LOG_FIELD_TXT(NDF_MODULE, "proc.plugin");
269 }
270 }
271 }
272
273 int get_numa_node_count(void)
274 {
275 static int numa_node_count = -1;
276
277 if (numa_node_count != -1)
278 return numa_node_count;
279
280 numa_node_count = 0;
281
282 char name[FILENAME_MAX + 1];
283 snprintfz(name, FILENAME_MAX, "%s%s", netdata_configured_host_prefix, "/sys/devices/system/node");
284 const char *dirname = inicfg_get(&netdata_config, "plugin:proc:/sys/devices/system/node", "directory to monitor", name);
285
286 DIR *dir = opendir(dirname);
287 if (dir) {
288 struct dirent *de = NULL;
289 while ((de = readdir(dir))) {
290 if (de->d_type != DT_DIR)
291 continue;
292
293 if (strncmp(de->d_name, "node", 4) != 0)
294 continue;
295
296 if (!isdigit(de->d_name[4]))
297 continue;
298
299 numa_node_count++;
300 }
301 closedir(dir);
302 }
303
304 return numa_node_count;
305 }