summaryrefslogtreecommitdiffstats
path: root/src/fluent-bit/plugins/in_proc/in_proc.c
blob: ff1df585ca8ad3f73a830dc4f5ffd307e087363a (plain)
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

/*  Fluent Bit
 *  ==========
 *  Copyright (C) 2015-2022 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_input_plugin.h>
#include <fluent-bit/flb_config.h>
#include <fluent-bit/flb_pack.h>
#include <msgpack.h>

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <signal.h>
#include <glob.h>
#include <libgen.h>
#include <unistd.h>
#include <stdlib.h>
#include <limits.h>
#include <dirent.h>

#include "in_proc.h"

struct flb_in_proc_mem_offset mem_linux[] = {
    {
        "Peak",
        "mem.VmPeak",
        offsetof(struct flb_in_proc_mem_linux, vmpeak)
    },
    {
        "Size",
        "mem.VmSize",
        offsetof(struct flb_in_proc_mem_linux, vmsize)
    },
    {
        "Lck",
        "mem.VmLck",
        offsetof(struct flb_in_proc_mem_linux, vmlck)
    },
    {
        "HWM",
        "mem.VmHWM",
        offsetof(struct flb_in_proc_mem_linux, vmhwm)
    },
    {
        "RSS",
        "mem.VmRSS",
        offsetof(struct flb_in_proc_mem_linux, vmrss)
    },
    {
        "Data",
        "mem.VmData",
        offsetof(struct flb_in_proc_mem_linux, vmdata)
    },
    {
        "Stk",
        "mem.VmStk",
        offsetof(struct flb_in_proc_mem_linux, vmstk)
    },
    {
        "Exe",
        "mem.VmExe",
        offsetof(struct flb_in_proc_mem_linux, vmexe)
    },
    {
        "Lib",
        "mem.VmLib",
        offsetof(struct flb_in_proc_mem_linux, vmlib)
    },
    {
        "PTE",
        "mem.VmPTE",
        offsetof(struct flb_in_proc_mem_linux, vmpte)
    },
    {
        "Swap",
        "mem.VmSwap",
        offsetof(struct flb_in_proc_mem_linux, vmswap)
    },
    {NULL, NULL, 0}/* end of array */
};



static pid_t get_pid_from_procname_linux(struct flb_in_proc_config *ctx,
                                         const char* proc)
{
    pid_t ret = -1;
    glob_t glb;
    int i;
    int fd = -1;
    long ret_scan = -1;
    int ret_glb = -1;
    ssize_t count;

    char  cmdname[FLB_CMD_LEN];
    char* bname = NULL;

    ret_glb = glob("/proc/*/cmdline", 0 ,NULL, &glb);
    if (ret_glb != 0) {
        switch(ret_glb){
        case GLOB_NOSPACE:
            flb_plg_warn(ctx->ins, "glob: no space");
            break;
        case GLOB_NOMATCH:
            flb_plg_warn(ctx->ins, "glob: no match");
            break;
        case GLOB_ABORTED:
            flb_plg_warn(ctx->ins, "glob: aborted");
            break;
        default:
            flb_plg_warn(ctx->ins, "glob: other error");
        }
        return ret;
    }

    for (i = 0; i < glb.gl_pathc; i++) {
        fd = open(glb.gl_pathv[i], O_RDONLY);
        if (fd < 0) {
            continue;
        }
        count = read(fd, &cmdname, FLB_CMD_LEN);
        if (count <= 0){
            close(fd);
            continue;
        }
        cmdname[FLB_CMD_LEN-1] = '\0';
        bname = basename(cmdname);

        if (strncmp(proc, bname, FLB_CMD_LEN) == 0) {
            sscanf(glb.gl_pathv[i],"/proc/%ld/cmdline",&ret_scan);
            ret = (pid_t)ret_scan;
            close(fd);
            break;
        }
        close(fd);
    }
    globfree(&glb);
    return ret;
}

static int configure(struct flb_in_proc_config *ctx,
                     struct flb_input_instance *in)
{
    int ret;

    /* Load the config map */
    ret = flb_input_config_map_set(in, (void *)ctx);
    if (ret == -1) {
        flb_plg_error(in, "unable to load configuration");
        return -1;
    }
    
    if (ctx->interval_sec <= 0 && ctx->interval_nsec <= 0) {
        /* Illegal settings. Override them. */
        ctx->interval_sec = atoi(DEFAULT_INTERVAL_SEC);
        ctx->interval_nsec = atoi(DEFAULT_INTERVAL_NSEC);
    }

    if (ctx->proc_name != NULL && strcmp(ctx->proc_name, "") != 0) {
        ctx->len_proc_name = strlen(ctx->proc_name);
    }

    return 0;
}

static int get_pid_status(pid_t pid)
{
    int ret =  kill(pid, 0);
    return ((ret != ESRCH)  && (ret != EPERM) && (ret != ESRCH));
}

static int generate_record_linux(struct flb_input_instance *i_ins,
                                 struct flb_config *config, void *in_context,
                                 struct flb_in_proc_mem_linux *mem_stat,
                                 uint64_t fds)
{
    int i;
    int ret;
    struct flb_in_proc_config *ctx = in_context;

    if (ctx->alive == FLB_TRUE && ctx->alert == FLB_TRUE) {
        return 0;
    }

    ret = flb_log_event_encoder_begin_record(ctx->log_encoder);

    if (ret == FLB_EVENT_ENCODER_SUCCESS) {
        ret = flb_log_event_encoder_set_current_timestamp(ctx->log_encoder);
    }

    if (ret == FLB_EVENT_ENCODER_SUCCESS) {
        ret = flb_log_event_encoder_append_body_values(
                ctx->log_encoder,
                FLB_LOG_EVENT_CSTRING_VALUE("alive"),
                FLB_LOG_EVENT_BOOLEAN_VALUE(ctx->alive),
                /* proc name */
                FLB_LOG_EVENT_CSTRING_VALUE("proc_name"),
                FLB_LOG_EVENT_CSTRING_VALUE(ctx->proc_name),
                /* pid */
                FLB_LOG_EVENT_CSTRING_VALUE("pid"),
                FLB_LOG_EVENT_INT64_VALUE(ctx->pid));
    }

    /* memory */
    if (ctx->mem == FLB_TRUE) {
        char *str = NULL;
        uint64_t *val = NULL;
        for (i = 0;
             mem_linux[i].key != NULL &&
             ret == FLB_EVENT_ENCODER_SUCCESS;
             i++) {
            str = mem_linux[i].msgpack_key;
            val = (uint64_t*)((char*)mem_stat + mem_linux[i].offset);

            ret = flb_log_event_encoder_append_body_values(
                    ctx->log_encoder,
                    FLB_LOG_EVENT_CSTRING_VALUE(str),
                    FLB_LOG_EVENT_UINT64_VALUE(*val));
        }
    }

    /* file descriptor */
    if (ctx->fds) {
        if (ret == FLB_EVENT_ENCODER_SUCCESS) {
            ret = flb_log_event_encoder_append_body_values(
                    ctx->log_encoder,
                    FLB_LOG_EVENT_CSTRING_VALUE("fd"),
                    FLB_LOG_EVENT_UINT64_VALUE(fds));
        }
    }

    if (ret == FLB_EVENT_ENCODER_SUCCESS) {
        ret = flb_log_event_encoder_commit_record(ctx->log_encoder);
    }

    if (ret == FLB_EVENT_ENCODER_SUCCESS) {
        flb_input_log_append(i_ins, NULL, 0,
                             ctx->log_encoder->output_buffer,
                             ctx->log_encoder->output_length);
        ret = 0;
    }
    else {
        flb_plg_error(i_ins, "log event encoding error : %d", ret);

        ret = -1;
    }

    flb_log_event_encoder_reset(ctx->log_encoder);

    return ret;
}

static void update_alive(struct flb_in_proc_config *ctx)
{
    if (ctx->pid >= 0 && get_pid_status(ctx->pid)) {
        ctx->alive = FLB_TRUE;
    }
    else {
        ctx->alive = FLB_FALSE;
    }
}

static void mem_linux_clear(struct flb_in_proc_mem_linux *mem_stat)
{
    int i;
    uint64_t *temp = NULL;
    for (i=0;mem_linux[i].key != NULL;i++) {
        temp   = (uint64_t*)((char*)mem_stat + mem_linux[i].offset);
        *temp  = 0;
    }
}

static int update_mem_linux(struct flb_in_proc_config *ctx,
                            struct flb_in_proc_mem_linux *mem_stat)
{
    int ret  = -1;
    int i;
    char path[PATH_MAX] = {0};
    char str_name[32] = {0};
    char *line = NULL;
    char *fmt = NULL;
    char *buf = NULL;
    ssize_t count;
    size_t len = 256;
    uint64_t mem_size;
    uint64_t *temp = NULL;
    FILE *fp  = NULL;

    snprintf(path, sizeof(path), "/proc/%d/status",ctx->pid);
    fp = fopen(path, "r");

    if (fp == NULL) {
        flb_plg_error(ctx->ins, "open error: %s", path);
        mem_linux_clear(mem_stat);
        return -1;
    }

    line = (char*)flb_malloc(len);
    while(1) {
        count = getline(&line, &len, fp);
        if (count < 0) {
            break;
        }

        /* VmPeak:	   14860 kB */
        fmt = "Vm%s"; /* e.g. "Peak:" */
        memset(str_name, '\0', sizeof(str_name));
        ret = sscanf(line, fmt, str_name);
        if (ret < 1) {
            continue;
        }
        /* replace : -> NULL char*/
        if ((buf = strchr(str_name, ':')) != NULL) {
            *buf = '\0';
        }

        /* calcurate size */
        mem_size = 0;
        for (i=0;line[i] != '\0';i++) {
            if (line[i] >= 0x30 && line[i] <= 0x39 /* is number*/) {
                mem_size *= 10;
                mem_size += line[i] - 0x30;
            }
        }

        for (i=0;mem_linux[i].key != NULL;i++) {
            if (!strcmp(str_name, mem_linux[i].key)) {
                temp   = (uint64_t*)((char*)mem_stat + mem_linux[i].offset);
                *temp  = mem_size * 1000; /* kB size */
                break;
            }
        }
    }
    flb_free(line);
    fclose(fp);
    return ret;
}

static int update_fds_linux(struct flb_in_proc_config *ctx,
                            uint64_t *fds)
{
    DIR *dirp = NULL;
    struct dirent *entry = NULL;
    char path[PATH_MAX] = {0};

    *fds = 0;

    snprintf(path, sizeof(path), "/proc/%d/fd", ctx->pid);
    dirp = opendir(path);
    if (dirp == NULL) {
        perror("opendir");
        flb_plg_error(ctx->ins, "opendir error %s", path);
        return -1;
    }

    entry = readdir(dirp);
    while (entry != NULL) {
        *fds += 1;/* should we check entry->d_name ? */
        entry = readdir(dirp);
    }
    *fds -= 2; /* '.' and '..' */
    closedir(dirp);

    return 0;
}

static int in_proc_collect_linux(struct flb_input_instance *i_ins,
                           struct flb_config *config, void *in_context)
{
    uint64_t fds = 0;
    struct flb_in_proc_config *ctx = in_context;
    struct flb_in_proc_mem_linux mem;

    if (ctx->proc_name != NULL){
        ctx->pid = get_pid_from_procname_linux(ctx, ctx->proc_name);
        update_alive(ctx);

        if (ctx->mem == FLB_TRUE && ctx->alive == FLB_TRUE) {
            mem_linux_clear(&mem);
            update_mem_linux(ctx, &mem);
        }
        if (ctx->fds == FLB_TRUE && ctx->alive == FLB_TRUE) {
            update_fds_linux(ctx, &fds);
        }
        generate_record_linux(i_ins, config, in_context, &mem, fds);
    }

    return 0;
}

static int in_proc_collect(struct flb_input_instance *i_ins,
                           struct flb_config *config, void *in_context)
{
    return in_proc_collect_linux(i_ins, config, in_context);
}

static int in_proc_init(struct flb_input_instance *in,
                          struct flb_config *config, void *data)
{
    int ret;
    struct flb_in_proc_config *ctx = NULL;
    (void) data;

    /* Allocate space for the configuration */
    ctx = flb_calloc(1, sizeof(struct flb_in_proc_config));
    if (!ctx) {
        flb_errno();
        return -1;
    }
    ctx->alert = FLB_FALSE;
    ctx->mem   = FLB_TRUE;
    ctx->fds   = FLB_TRUE;
    ctx->proc_name = NULL;
    ctx->pid = -1;
    ctx->ins = in;

    ctx->log_encoder = flb_log_event_encoder_create(FLB_LOG_EVENT_FORMAT_DEFAULT);

    if (ctx->log_encoder == NULL) {
        flb_plg_error(in, "event encoder initialization error");
        flb_free(ctx);

        return -1;
    }

    configure(ctx, in);

    if (ctx->proc_name == NULL) {
        flb_plg_error(ctx->ins, "'proc_name' is not set");
        flb_free(ctx);
        return -1;
    }

    /* Set the context */
    flb_input_set_context(in, ctx);

    /* Set our collector based on time */
    ret = flb_input_set_collector_time(in,
                                       in_proc_collect,
                                       ctx->interval_sec,
                                       ctx->interval_nsec,
                                       config);
    if (ret == -1) {
        flb_plg_error(ctx->ins, "could not set collector for Proc input plugin");
        flb_free(ctx);
        return -1;
    }

    return 0;
}

static int in_proc_exit(void *data, struct flb_config *config)
{
    (void) *config;
    struct flb_in_proc_config *ctx = data;

    if (!ctx) {
        return 0;
    }

    if (ctx->log_encoder != NULL) {
        flb_log_event_encoder_destroy(ctx->log_encoder);
    }

    /* Destroy context */
    flb_free(ctx);

    return 0;
}

static struct flb_config_map config_map[] = {
    {
      FLB_CONFIG_MAP_INT, "interval_sec", DEFAULT_INTERVAL_SEC,
      0, FLB_TRUE, offsetof(struct flb_in_proc_config, interval_sec),
      "Set the collector interval"
    },
    {
      FLB_CONFIG_MAP_INT, "interval_nsec", DEFAULT_INTERVAL_NSEC,
      0, FLB_TRUE, offsetof(struct flb_in_proc_config, interval_nsec),
      "Set the collector interval (nanoseconds)"
    },
    {
     FLB_CONFIG_MAP_BOOL, "alert", "false",
     0, FLB_TRUE, offsetof(struct flb_in_proc_config, alert),
     "Only generate alerts if process is down"
    },
    {
     FLB_CONFIG_MAP_BOOL, "mem", "true",
     0, FLB_TRUE, offsetof(struct flb_in_proc_config, mem),
     "Append memory usage to record"
    },
    {
     FLB_CONFIG_MAP_BOOL, "fd", "true",
     0, FLB_TRUE, offsetof(struct flb_in_proc_config, fds),
     "Append fd count to record"
    },
    {
     FLB_CONFIG_MAP_STR, "proc_name", NULL,
     0, FLB_TRUE, offsetof(struct flb_in_proc_config, proc_name),
     "Define process name to health check"
    },
    /* EOF */
    {0}
};

/* Plugin reference */
struct flb_input_plugin in_proc_plugin = {
    .name         = "proc",
    .description  = "Check Process health",
    .cb_init      = in_proc_init,
    .cb_pre_run   = NULL,
    .cb_collect   = in_proc_collect,
    .cb_flush_buf = NULL,
    .cb_exit      = in_proc_exit,
    .config_map   = config_map,
    .flags        = 0,
};