-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
ne_processes_linux.c
425 lines (356 loc) · 12.4 KB
/
ne_processes_linux.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* Fluent Bit
* ==========
* Copyright (C) 2023 The Fluent Bit Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <fluent-bit/flb_info.h>
#include <fluent-bit/flb_sds.h>
#include <fluent-bit/flb_input_plugin.h>
#include "ne.h"
#include "ne_utils.h"
#include <unistd.h>
static int processes_configure(struct flb_ne *ctx)
{
struct cmt_gauge *g;
/* node_processes_threads_max */
g = cmt_gauge_create(ctx->cmt, "node", "processes", "threads",
"Allocated threads in the system",
0, NULL);
if (!g) {
return -1;
}
ctx->processes_thread_alloc = g;
/* node_processes_threads_max */
g = cmt_gauge_create(ctx->cmt, "node", "processes", "max_threads",
"Limit of threads in the system",
0, NULL);
if (!g) {
return -1;
}
ctx->processes_threads_limit = g;
/* node_processes_threads_state */
g = cmt_gauge_create(ctx->cmt, "node", "processes", "threads_state",
"The number of processes in each thread state",
1, (char *[]) {"thread_state"});
if (!g) {
return -1;
}
ctx->processes_threads_state = g;
/* node_processes_state */
g = cmt_gauge_create(ctx->cmt, "node", "processes", "state",
"The number of processes in each state",
1, (char *[]) {"state"});
if (!g) {
return -1;
}
ctx->processes_procs_state = g;
/* node_processes_pids */
g = cmt_gauge_create(ctx->cmt, "node", "processes", "pids",
"The number of PIDs in the system",
0, NULL);
if (!g) {
return -1;
}
ctx->processes_pid_used = g;
/* node_processes_max_processeses */
g = cmt_gauge_create(ctx->cmt, "node", "processes", "max_processeses",
"Limit of PID in the system",
0, NULL);
if (!g) {
return -1;
}
ctx->processes_pid_max = g;
return 0;
}
struct proc_state {
int64_t running;
int64_t interruptible_sleeping;
int64_t uninterruptible_sleeping;
int64_t zombie;
int64_t stopped;
int64_t idle;
};
static int update_processes_proc_state(struct flb_ne *ctx, struct proc_state *state, char* state_str)
{
if (strcmp(state_str, "R") == 0) {
state->running++;
}
else if (strcmp(state_str, "S") == 0) {
state->interruptible_sleeping++;
}
else if (strcmp(state_str, "D") == 0) {
state->uninterruptible_sleeping++;
}
else if (strcmp(state_str, "Z") == 0) {
state->zombie++;
}
else if (strcmp(state_str, "T") == 0) {
state->stopped++;
}
else if (strcmp(state_str, "I") == 0) {
state->idle++;
}
return 0;
}
static int check_path_for_proc(struct flb_ne *ctx, const char *prefix, const char *path)
{
int len;
flb_sds_t p;
/* Compose the proc path */
p = flb_sds_create(prefix);
if (!p) {
return -1;
}
if (path) {
flb_sds_cat_safe(&p, "/", 1);
len = strlen(path);
flb_sds_cat_safe(&p, path, len);
}
if (access(p, F_OK) == -1 &&
(errno == ENOENT || errno == ESRCH)) {
flb_plg_debug(ctx->ins, "error reading stat for path %s. errno = %d", p, errno);
flb_sds_destroy(p);
return -1;
}
flb_sds_destroy(p);
return 0;
}
static int processes_thread_update(struct flb_ne *ctx, flb_sds_t pid_str, flb_sds_t pstate_str,
struct proc_state *tstate)
{
int ret;
flb_sds_t tmp;
flb_sds_t tid_str;
flb_sds_t state_str;
const char *pattern = "/[0-9]*";
struct mk_list *head;
struct mk_list *ehead;
struct mk_list thread_list;
struct mk_list stat_list;
struct mk_list split_list;
struct flb_slist_entry *thread;
struct flb_slist_entry *entry;
char thread_procfs[PATH_MAX];
snprintf(thread_procfs, sizeof(thread_procfs) - 1, "%s/%s/task", ctx->path_procfs, pid_str);
/* scan thread entries */
ret = ne_utils_path_scan(ctx, thread_procfs, pattern, NE_SCAN_DIR, &thread_list);
if (ret != 0) {
return -1;
}
if (mk_list_size(&thread_list) == 0) {
return 0;
}
/* thread entries */
mk_list_foreach(head, &thread_list) {
thread = mk_list_entry(head, struct flb_slist_entry, _head);
tid_str = thread->str + strlen(thread_procfs) + 1;
/* When pid and tid are equal, the state of the thread should be the same
* for pid's. */
if (strcmp(tid_str, pid_str) == 0) {
update_processes_proc_state(ctx, tstate, pstate_str);
continue;
}
if (check_path_for_proc(ctx, thread->str, "stat") != 0) {
continue;
}
mk_list_init(&stat_list);
ret = ne_utils_file_read_lines(thread->str, "/stat", &stat_list);
if (ret == -1) {
continue;
}
mk_list_foreach(ehead, &stat_list) {
entry = mk_list_entry(ehead, struct flb_slist_entry, _head);
/* split with the close parenthesis.
* The entry of processes stat will start after that. */
tmp = strstr(entry->str, ")");
if (tmp == NULL) {
continue;
}
mk_list_init(&split_list);
ret = flb_slist_split_string(&split_list, tmp+2, ' ', -1);
if (ret == -1) {
continue;
}
/* Thread State */
entry = flb_slist_entry_get(&split_list, 0);
state_str = entry->str;
update_processes_proc_state(ctx, tstate, state_str);
flb_slist_destroy(&split_list);
}
flb_slist_destroy(&stat_list);
}
flb_slist_destroy(&thread_list);
return 0;
}
static int processes_update(struct flb_ne *ctx)
{
int ret;
flb_sds_t tmp;
flb_sds_t pid_str;
flb_sds_t state_str;
flb_sds_t thread_str;
struct mk_list *head;
struct mk_list *ehead;
struct mk_list procfs_list;
struct mk_list stat_list;
struct mk_list split_list;
struct flb_slist_entry *process;
struct flb_slist_entry *entry;
uint64_t val;
uint64_t ts;
const char *pattern = "/[0-9]*";
int64_t pids = 0;
int64_t threads = 0;
struct proc_state pstate = {
.running = 0,
.interruptible_sleeping = 0,
.uninterruptible_sleeping = 0,
.zombie = 0,
.stopped = 0,
.idle = 0
};
struct proc_state tstate = {
.running = 0,
.interruptible_sleeping = 0,
.uninterruptible_sleeping = 0,
.zombie = 0,
.stopped = 0,
.idle = 0
};
mk_list_init(&procfs_list);
ts = cfl_time_now();
ret = ne_utils_file_read_uint64(ctx->path_procfs, "/sys", "kernel", "threads-max", &val);
if (ret == -1) {
return -1;
}
/* node_processes_threads_max */
if (ret == 0) {
cmt_gauge_set(ctx->processes_threads_limit, ts,
(double)val, 0, NULL);
}
ret = ne_utils_file_read_uint64(ctx->path_procfs, "/sys", "kernel", "pid_max", &val);
if (ret == -1) {
return -1;
}
/* node_processes_max_processes */
if (ret == 0) {
cmt_gauge_set(ctx->processes_pid_max, ts,
(double)val, 0, NULL);
}
/* scan pid entries */
ret = ne_utils_path_scan(ctx, ctx->path_procfs, pattern, NE_SCAN_DIR, &procfs_list);
if (ret != 0) {
return -1;
}
if (mk_list_size(&procfs_list) == 0) {
return 0;
}
/* PID entries */
mk_list_foreach(head, &procfs_list) {
process = mk_list_entry(head, struct flb_slist_entry, _head);
pid_str = process->str + strlen(ctx->path_procfs) + 1;
if (check_path_for_proc(ctx, process->str, "stat") != 0) {
continue;
}
mk_list_init(&stat_list);
ret = ne_utils_file_read_lines(process->str, "/stat", &stat_list);
if (ret == -1) {
continue;
}
mk_list_foreach(ehead, &stat_list) {
entry = mk_list_entry(ehead, struct flb_slist_entry, _head);
/* split with the close parenthesis.
* The entry of processes stat will start after that. */
tmp = strstr(entry->str, ")");
if (tmp == NULL) {
continue;
}
mk_list_init(&split_list);
ret = flb_slist_split_string(&split_list, tmp+2, ' ', -1);
if (ret == -1) {
continue;
}
/* State */
entry = flb_slist_entry_get(&split_list, 0);
state_str = entry->str;
update_processes_proc_state(ctx, &pstate, state_str);
/* Threads */
entry = flb_slist_entry_get(&split_list, 17);
thread_str = entry->str;
/* Collect the number of threads */
if (ne_utils_str_to_uint64(thread_str, &val) != -1) {
threads += val;
}
/* Collect the states of threads */
ret = processes_thread_update(ctx, pid_str, state_str, &tstate);
if (ret != 0) {
flb_slist_destroy(&split_list);
continue;
}
flb_slist_destroy(&split_list);
}
flb_slist_destroy(&stat_list);
pids++;
}
/* node_processes_state
* Note: we don't use hash table for it. Because we need to update
* every state of the processes due to architecture reasons of cmetrics.
*/
cmt_gauge_set(ctx->processes_procs_state, ts, pstate.running, 1, (char *[]){ "R" });
cmt_gauge_set(ctx->processes_procs_state, ts, pstate.interruptible_sleeping, 1, (char *[]){ "S" });
cmt_gauge_set(ctx->processes_procs_state, ts, pstate.uninterruptible_sleeping, 1, (char *[]){ "D" });
cmt_gauge_set(ctx->processes_procs_state, ts, pstate.zombie, 1, (char *[]){ "Z" });
cmt_gauge_set(ctx->processes_procs_state, ts, pstate.stopped, 1, (char *[]){ "T" });
cmt_gauge_set(ctx->processes_procs_state, ts, pstate.idle, 1, (char *[]){ "I" });
/* node_processes_threads_state
* Note: we don't use hash table for it. Because we need to update
* every state of the processes due to architecture reasons of cmetrics.
*/
cmt_gauge_set(ctx->processes_threads_state, ts, tstate.running, 1, (char *[]){ "R" });
cmt_gauge_set(ctx->processes_threads_state, ts, tstate.interruptible_sleeping, 1, (char *[]){ "S" });
cmt_gauge_set(ctx->processes_threads_state, ts, tstate.uninterruptible_sleeping, 1, (char *[]){ "D" });
cmt_gauge_set(ctx->processes_threads_state, ts, tstate.zombie, 1, (char *[]){ "Z" });
cmt_gauge_set(ctx->processes_threads_state, ts, tstate.stopped, 1, (char *[]){ "T" });
cmt_gauge_set(ctx->processes_threads_state, ts, tstate.idle, 1, (char *[]){ "I" });
/* node_processes_threads */
cmt_gauge_set(ctx->processes_thread_alloc, ts,
(double)threads, 0, NULL);
/* node_processes_pids */
cmt_gauge_set(ctx->processes_pid_used, ts,
(double)pids, 0, NULL);
flb_slist_destroy(&procfs_list);
return 0;
}
static int ne_processes_init(struct flb_ne *ctx)
{
processes_configure(ctx);
return 0;
}
static int ne_processes_update(struct flb_input_instance *ins, struct flb_config *config, void *in_context)
{
struct flb_ne *ctx = (struct flb_ne *)in_context;
processes_update(ctx);
return 0;
}
static int ne_processes_exit(struct flb_ne *ctx)
{
return 0;
}
struct flb_ne_collector processes_collector = {
.name = "processes",
.cb_init = ne_processes_init,
.cb_update = ne_processes_update,
.cb_exit = ne_processes_exit
};