summaryrefslogtreecommitdiffstats
path: root/src/fluent-bit/plugins/in_docker_events/docker_events.c
blob: 7534eb1d6a01022a5b4021883415aa5d05c8bcf7 (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
/* -*- 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_network.h>
#include <fluent-bit/flb_pack.h>
#include <sys/socket.h>
#include <sys/un.h>

#include "docker_events.h"
#include "docker_events_config.h"


/**
 * Creates the connection to docker's unix socket and sends the
 * HTTP GET /events
 *
 * @param ctx  Pointer to flb_in_de_config
 *
 * @return int 0 on success, -1 on failure
 */
static int de_unix_create(struct flb_in_de_config *ctx)
{
    ssize_t bytes;
    unsigned long len;
    size_t address_length;
    struct sockaddr_un address;
    char request[512];

    ctx->fd = flb_net_socket_create(AF_UNIX, FLB_FALSE);
    if (ctx->fd == -1) {
        return -1;
    }

    /* Prepare the unix socket path */
    len = strlen(ctx->unix_path);
    address.sun_family = AF_UNIX;
    sprintf(address.sun_path, "%s", ctx->unix_path);
    address_length = sizeof(address.sun_family) + len + 1;
    if (connect(ctx->fd, (struct sockaddr *)&address, address_length) == -1) {
        flb_errno();
        close(ctx->fd);
        return -1;
    }

    strcpy(request, "GET /events HTTP/1.0\r\n\r\n");
    flb_plg_trace(ctx->ins, "writing to socket %s", request);
    write(ctx->fd, request, strlen(request));

    /* Read the initial http response */
    bytes = read(ctx->fd, ctx->buf, ctx->buf_size - 1);
    if (bytes == -1) {
        flb_errno();
    }
    flb_plg_debug(ctx->ins, "read %zu bytes from socket", bytes);

    return 0;
}

static int in_de_collect(struct flb_input_instance *ins,
                         struct flb_config *config, void *in_context);

static int reconnect_docker_sock(struct flb_input_instance *ins,
                                 struct flb_config *config,
                                 struct flb_in_de_config *ctx)
{
    int ret;

    /* remove old socket collector */
    if (ctx->coll_id >= 0) {
        ret = flb_input_collector_delete(ctx->coll_id, ins);
        if (ret < 0) {
            flb_plg_error(ctx->ins, "failed to pause event");
            return -1;
        }
        ctx->coll_id = -1;
    }
    if (ctx->fd > 0) {
        flb_plg_debug(ctx->ins, "close socket fd=%d", ctx->fd);
        close(ctx->fd);
        ctx->fd = -1;
    }

    /* create socket again */
    if (de_unix_create(ctx) < 0) {
        flb_plg_error(ctx->ins, "failed to re-initialize socket");
        if (ctx->fd > 0) {
            flb_plg_debug(ctx->ins, "close socket fd=%d", ctx->fd);
            close(ctx->fd);
            ctx->fd = -1;
        }
        return -1;
    }
    /* set event */
    ctx->coll_id = flb_input_set_collector_event(ins,
                                                 in_de_collect,
                                                 ctx->fd, config);
    if (ctx->coll_id < 0) {
        flb_plg_error(ctx->ins,
                      "could not set collector for IN_DOCKER_EVENTS plugin");
        close(ctx->fd);
        ctx->fd = -1;
        return -1;
    }
    ret = flb_input_collector_start(ctx->coll_id, ins);
    if (ret < 0) {
        flb_plg_error(ctx->ins,
                      "could not start collector for IN_DOCKER_EVENTS plugin");
        flb_input_collector_delete(ctx->coll_id, ins);
        close(ctx->fd);
        ctx->coll_id = -1;
        ctx->fd = -1;
        return -1;
    }

    flb_plg_info(ctx->ins, "Reconnect successful");
    return 0;
}

static int cb_reconnect(struct flb_input_instance *ins,
                       struct flb_config *config,
                       void *in_context)
{
    struct flb_in_de_config *ctx = in_context;
    int ret;

    flb_plg_info(ctx->ins, "Retry(%d/%d)",
                 ctx->current_retries, ctx->reconnect_retry_limits);
    ret = reconnect_docker_sock(ins, config, ctx);
    if (ret < 0) {
        /* Failed to reconnect */
        ctx->current_retries++;
        if (ctx->current_retries > ctx->reconnect_retry_limits) {
            /* give up */
            flb_plg_error(ctx->ins, "Failed to retry. Giving up...");
            goto cb_reconnect_end;
        }
        flb_plg_info(ctx->ins, "Failed. Waiting for next retry..");
        return 0;
    }

 cb_reconnect_end:
    if(flb_input_collector_delete(ctx->retry_coll_id, ins) < 0) {
        flb_plg_error(ctx->ins, "failed to delete timer event");
    }
    ctx->current_retries = 0;
    ctx->retry_coll_id = -1;
    return ret;
}

static int create_reconnect_event(struct flb_input_instance *ins,
                                  struct flb_config *config,
                                  struct flb_in_de_config *ctx)
{
    int ret;

    if (ctx->retry_coll_id >= 0) {
        flb_plg_debug(ctx->ins, "already retring ?");
        return 0;
    }

    /* try before creating event to stop incoming event */
    ret = reconnect_docker_sock(ins, config, ctx);
    if (ret == 0) {        
        return 0;
    }

    ctx->current_retries = 1;
    ctx->retry_coll_id = flb_input_set_collector_time(ins,
                                                      cb_reconnect,
                                                      ctx->reconnect_retry_interval,
                                                      0,
                                                      config);
    if (ctx->retry_coll_id < 0) {
        flb_plg_error(ctx->ins, "failed to create timer event");
        return -1;
    }
    ret = flb_input_collector_start(ctx->retry_coll_id, ins);
    if (ret < 0) {
        flb_plg_error(ctx->ins, "failed to start timer event");
        flb_input_collector_delete(ctx->retry_coll_id, ins);
        ctx->retry_coll_id = -1;
        return -1;
    }
    flb_plg_info(ctx->ins, "create reconnect event. interval=%d second",
                 ctx->reconnect_retry_interval);

    return 0;
}

static int is_recoverable_error(int error)
{
    /* ENOTTY: 
          It reports on Docker in Docker mode.
          https://github.com/fluent/fluent-bit/issues/3439#issuecomment-831424674
     */
    if (error == ENOTTY || error == EBADF) {
        return FLB_TRUE;
    }
    return FLB_FALSE;
}


/**
 * Callback function to process events recieved on the unix
 * socket.
 *
 * @param ins           Pointer to flb_input_instance
 * @param config        Pointer to flb_config
 * @param in_context    void Pointer used to cast to
 *                      flb_in_de_config
 *
 * @return int Always returns success
 */
static int in_de_collect(struct flb_input_instance *ins,
                         struct flb_config *config, void *in_context)
{
    int ret = 0;
    int error;
    size_t str_len = 0;
    struct flb_in_de_config *ctx = in_context;

    /* variables for parser */
    int parser_ret = -1;
    void *out_buf = NULL;
    size_t out_size = 0;
    struct flb_time out_time;

    ret = read(ctx->fd, ctx->buf, ctx->buf_size - 1);

    if (ret > 0) {
        str_len = ret;
        ctx->buf[str_len] = '\0';

        ret = flb_log_event_encoder_begin_record(&ctx->log_encoder);

        if (!ctx->parser) {
            /* Initialize local msgpack buffer */
            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(ctx->key),
                        FLB_LOG_EVENT_STRING_VALUE(ctx->buf, str_len));
            }

            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(ins, NULL, 0,
                                     ctx->log_encoder.output_buffer,
                                     ctx->log_encoder.output_length);

            }
            else {
                flb_plg_error(ctx->ins, "Error encoding record : %d", ret);
            }
        }
        else {
            flb_time_get(&out_time);

            parser_ret = flb_parser_do(ctx->parser, ctx->buf, str_len - 1,
                                       &out_buf, &out_size, &out_time);
            if (parser_ret >= 0) {
                if (flb_time_to_nanosec(&out_time) == 0L) {
                    flb_time_get(&out_time);
                }

                if (ret == FLB_EVENT_ENCODER_SUCCESS) {
                    ret = flb_log_event_encoder_set_timestamp(
                            &ctx->log_encoder,
                            &out_time);
                }

                if (ret == FLB_EVENT_ENCODER_SUCCESS) {
                    ret = flb_log_event_encoder_set_body_from_raw_msgpack(
                            &ctx->log_encoder,
                            out_buf,
                            out_size);
                }

                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(ins, NULL, 0,
                                         ctx->log_encoder.output_buffer,
                                         ctx->log_encoder.output_length);

                }
                else {
                    flb_plg_error(ctx->ins, "Error encoding record : %d", ret);
                }


                flb_free(out_buf);
            }
            else {
                flb_plg_trace(ctx->ins, "tried to parse: %s", ctx->buf);
                flb_plg_trace(ctx->ins, "buf_size %zu", ctx->buf_size);
                flb_plg_error(ctx->ins, "parser returned an error: %d",
                              parser_ret);
            }
        }

        flb_log_event_encoder_reset(&ctx->log_encoder);
    }
    else if (ret == 0) {
        /* EOF */

        /* docker service may be restarted */
        flb_plg_info(ctx->ins, "EOF detected. Re-initialize");
        if (ctx->reconnect_retry_limits > 0) {
            ret = create_reconnect_event(ins, config, ctx);
            if (ret < 0) {
                return ret;
            }
        }
    }
    else {
        error = errno;
        flb_plg_error(ctx->ins, "read returned error: %d, %s", error,
                      strerror(error));
        if (is_recoverable_error(error)) {
            if (ctx->reconnect_retry_limits > 0) {
                ret = create_reconnect_event(ins, config, ctx);
                if (ret < 0) {
                    return ret;
                }
            }
        }
    }

    return 0;
}

/**
 * Callback function to initialize docker events plugin
 *
 * @param ins     Pointer to flb_input_instance
 * @param config  Pointer to flb_config
 * @param data    Unused
 *
 * @return int 0 on success, -1 on failure
 */
static int in_de_init(struct flb_input_instance *ins,
                      struct flb_config *config, void *data)
{
    struct flb_in_de_config *ctx = NULL;
    (void) data;

    /* Allocate space for the configuration */
    ctx = de_config_init(ins, config);
    if (!ctx) {
        return -1;
    }
    ctx->ins = ins;
    ctx->retry_coll_id = -1;
    ctx->current_retries = 0;

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

    if (de_unix_create(ctx) != 0) {
        flb_plg_error(ctx->ins, "could not listen on unix://%s",
                      ctx->unix_path);
        de_config_destroy(ctx);
        return -1;
    }

    ctx->coll_id = flb_input_set_collector_event(ins, in_de_collect,
                                                 ctx->fd, config);
    if(ctx->coll_id < 0){
        flb_plg_error(ctx->ins,
                      "could not set collector for IN_DOCKER_EVENTS plugin");
        de_config_destroy(ctx);
        return -1;
    }

    flb_plg_info(ctx->ins, "listening for events on %s", ctx->unix_path);
    return 0;
}

/**
 * Callback exit function to cleanup plugin
 *
 * @param data    Pointer cast to flb_in_de_config
 * @param config  Unused
 *
 * @return int    Always returns 0
 */
static int in_de_exit(void *data, struct flb_config *config)
{
    (void) config;
    struct flb_in_de_config *ctx = data;

    if (!ctx) {
        return 0;
    }

    de_config_destroy(ctx);

    return 0;
}

/* Configuration properties map */
static struct flb_config_map config_map[] = {
    {
     FLB_CONFIG_MAP_STR, "unix_path", DEFAULT_UNIX_SOCKET_PATH,
     0, FLB_TRUE, offsetof(struct flb_in_de_config, unix_path),
     "Define Docker unix socket path to read events"
    },
    {
     FLB_CONFIG_MAP_SIZE, "buffer_size", "8k",
     0, FLB_TRUE, offsetof(struct flb_in_de_config, buf_size),
     "Set buffer size to read events"
    },
    {
     FLB_CONFIG_MAP_STR, "parser", NULL,
      0, FLB_FALSE, 0,
     "Optional parser for records, if not set, records are packages under 'key'"
    },
    {
     FLB_CONFIG_MAP_STR, "key", DEFAULT_FIELD_NAME,
     0, FLB_TRUE, offsetof(struct flb_in_de_config, key),
     "Set the key name to store unparsed Docker events"
    },
    {
     FLB_CONFIG_MAP_INT, "reconnect.retry_limits", "5",
     0, FLB_TRUE, offsetof(struct flb_in_de_config, reconnect_retry_limits),
     "Maximum number to retry to connect docker socket"
    },
    {
     FLB_CONFIG_MAP_INT, "reconnect.retry_interval", "1",
     0, FLB_TRUE, offsetof(struct flb_in_de_config, reconnect_retry_interval),
     "Retry interval to connect docker socket"
    },
    /* EOF */
    {0}
};

/* Plugin reference */
struct flb_input_plugin in_docker_events_plugin = {
    .name         = "docker_events",
    .description  = "Docker events",
    .cb_init      = in_de_init,
    .cb_pre_run   = NULL,
    .cb_collect   = in_de_collect,
    .cb_flush_buf = NULL,
    .cb_exit      = in_de_exit,
    .config_map   = config_map,
    .flags        = FLB_INPUT_NET
};