summaryrefslogtreecommitdiffstats
path: root/src/fluent-bit/plugins/in_collectd/in_collectd.c
blob: 06ef5ae8a530ff06a9163ae4d1d7247d472e3572 (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
/* -*- 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_compat.h>
#include <fluent-bit/flb_pack.h>
#include <fluent-bit/flb_utils.h>
#include <msgpack.h>

#include "in_collectd.h"
#include "netprot.h"
#include "typesdb.h"

/*
 * Max payload size. By default, Collectd sends up to 1452 bytes
 * per a UDP packet, but the limit can be increased up to 65535
 * bytes through a configuration parameter.
 *
 * See network_config_set_buffer_size() in collectd/src/network.c.
 */
#define BUFFER_SIZE 65535

#define DEFAULT_LISTEN "0.0.0.0"
#define DEFAULT_PORT 25826

/* This is where most Linux systems places a default TypesDB */
#define DEFAULT_TYPESDB "/usr/share/collectd/types.db"

static int in_collectd_callback(struct flb_input_instance *i_ins,
                                struct flb_config *config, void *in_context);

static int in_collectd_init(struct flb_input_instance *in,
                            struct flb_config *config, void *data)
{
    int ret;
    struct flb_in_collectd_config *ctx;
    struct mk_list *tdb;
    char *listen = DEFAULT_LISTEN;
    int port = DEFAULT_PORT;

    /* Initialize context */
    ctx = flb_calloc(1, sizeof(struct flb_in_collectd_config));
    if (!ctx) {
        flb_errno();
        return -1;
    }
    ctx->ins = in;

    ctx->bufsize = BUFFER_SIZE;
    ctx->buf = flb_malloc(ctx->bufsize);
    if (!ctx->buf) {
        flb_errno();
        flb_free(ctx);
        return -1;
    }
    
    /* Load the config map */
    ret = flb_input_config_map_set(in, (void *)ctx);
    if (ret == -1) {
        flb_plg_error(in, "unable to load configuration");
        flb_free(ctx);
        return -1;
    }

    /* Listening address */
    if (in->host.listen) {
        listen = in->host.listen;
    }

    if (strlen(listen) > sizeof(ctx->listen) - 1) {
        flb_plg_error(ctx->ins, "too long address '%s'", listen);
        flb_free(ctx);
        return -1;
    }
    strcpy(ctx->listen, listen);

    /* Listening port */
    if (in->host.port) {
        port = in->host.port;
    }
    snprintf(ctx->port, sizeof(ctx->port), "%hu", (unsigned short) port);

    flb_plg_debug(ctx->ins, "Loading TypesDB from %s", ctx->types_db);

    tdb = typesdb_load_all(ctx, ctx->types_db);
    if (!tdb) {
        flb_plg_error(ctx->ins, "failed to load '%s'", ctx->types_db);
        flb_free(ctx->buf);
        flb_free(ctx);
        return -1;
    }
    ctx->tdb = tdb;

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

    ctx->server_fd = flb_net_server_udp(ctx->port, ctx->listen);
    if (ctx->server_fd < 0) {
        flb_plg_error(ctx->ins, "failed to bind to %s:%s", ctx->listen,
                      ctx->port);
        typesdb_destroy(ctx->tdb);
        flb_free(ctx->buf);
        flb_free(ctx);
        return -1;
    }

    /* Set the collector */
    ret = flb_input_set_collector_socket(in,
                                         in_collectd_callback,
                                         ctx->server_fd,
                                         config);
    if (ret == -1) {
        flb_plg_error(ctx->ins, "failed set up a collector");
        flb_socket_close(ctx->server_fd);
        typesdb_destroy(ctx->tdb);
        flb_free(ctx->buf);
        flb_free(ctx);
        return -1;
    }
    ctx->coll_fd = ret;

    ret = flb_log_event_encoder_init(&ctx->log_encoder,
                                     FLB_LOG_EVENT_FORMAT_DEFAULT);

    if (ret != FLB_EVENT_ENCODER_SUCCESS) {
        flb_plg_error(ctx->ins, "error initializing event encoder : %d", ret);

        flb_socket_close(ctx->server_fd);
        typesdb_destroy(ctx->tdb);
        flb_free(ctx->buf);
        flb_free(ctx);

        return -1;
    }

    flb_plg_info(ctx->ins, "start listening to %s:%s",
                 ctx->listen, ctx->port);

    return 0;
}

static int in_collectd_callback(struct flb_input_instance *i_ins,
                                struct flb_config *config, void *in_context)
{
    int len;
    struct flb_in_collectd_config *ctx = in_context;

    len = recv(ctx->server_fd, ctx->buf, ctx->bufsize, 0);
    if (len < 0) {
        flb_errno();
        return -1;
    }
    if (len == 0) {
        return 0;
    }

    flb_log_event_encoder_reset(&ctx->log_encoder);

    if (netprot_to_msgpack(ctx->buf, len, ctx->tdb, &ctx->log_encoder)) {
        flb_plg_error(ctx->ins, "netprot_to_msgpack fails");

        return -1;
    }

    if (ctx->log_encoder.output_length > 0) {
        flb_input_log_append(i_ins, NULL, 0,
                             ctx->log_encoder.output_buffer,
                             ctx->log_encoder.output_length);
    }

    return 0;
}

static int in_collectd_exit(void *data, struct flb_config *config)
{
    struct flb_in_collectd_config *ctx = data;

    flb_log_event_encoder_destroy(&ctx->log_encoder);
    flb_socket_close(ctx->server_fd);
    flb_pipe_close(ctx->coll_fd);
    typesdb_destroy(ctx->tdb);
    flb_free(ctx->buf);
    flb_free(ctx);

    return 0;
}

static struct flb_config_map config_map[] = {
    {
     FLB_CONFIG_MAP_STR, "typesdb", DEFAULT_TYPESDB,
     0, FLB_TRUE, offsetof(struct flb_in_collectd_config, types_db),
     "Set the types database filename"
    },
    /* EOF */
    {0}
};

struct flb_input_plugin in_collectd_plugin = {
    .name         = "collectd",
    .description  = "collectd input plugin",
    .cb_init      = in_collectd_init,
    .cb_pre_run   = NULL,
    .cb_collect   = NULL,
    .cb_flush_buf = NULL,
    .cb_pause     = NULL,
    .cb_resume    = NULL,
    .config_map   = config_map,
    .flags        = FLB_INPUT_NET_SERVER,
    .cb_exit      = in_collectd_exit
};