summaryrefslogtreecommitdiffstats
path: root/fluent-bit/plugins/out_splunk/splunk_conf.c
blob: cc911cbeb2c054c7ed383e6bdce16b34e6a66f36 (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
/* -*- 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_output_plugin.h>
#include <fluent-bit/flb_utils.h>

#include "splunk.h"
#include "splunk_conf.h"

static int event_fields_create(struct flb_splunk *ctx)
{
    int i = 0;
    struct mk_list *head;
    struct flb_slist_entry *kname;
    struct flb_slist_entry *pattern;
    struct flb_config_map_val *mv;
    struct flb_splunk_field *f;

    if (!ctx->event_fields) {
        return 0;
    }

    flb_config_map_foreach(head, mv, ctx->event_fields) {
        kname = mk_list_entry_first(mv->val.list, struct flb_slist_entry, _head);
        pattern = mk_list_entry_last(mv->val.list, struct flb_slist_entry, _head);

        f = flb_malloc(sizeof(struct flb_splunk_field));
        if (!f) {
            flb_errno();
            return -1;
        }

        f->key_name = flb_sds_create(kname->str);
        if (!f->key_name) {
            flb_free(f);
            return -1;
        }

        f->ra = flb_ra_create(pattern->str, FLB_TRUE);
        if (!f->ra) {
            flb_plg_error(ctx->ins,
                          "could not process event_field number #%i with "
                          "pattern '%s'",
                          i, pattern->str);
            flb_sds_destroy(f->key_name);
            flb_free(f);
            return -1;
        }

        mk_list_add(&f->_head, &ctx->fields);
    }

    return 0;
}

static void event_fields_destroy(struct flb_splunk *ctx)
{
    struct mk_list *tmp;
    struct mk_list *head;
    struct flb_splunk_field *f;

    mk_list_foreach_safe(head, tmp, &ctx->fields) {
        f = mk_list_entry(head, struct flb_splunk_field, _head);
        flb_sds_destroy(f->key_name);
        flb_ra_destroy(f->ra);
        mk_list_del(&f->_head);
        flb_free(f);
    }
}

struct flb_splunk *flb_splunk_conf_create(struct flb_output_instance *ins,
                                          struct flb_config *config)
{
    int ret;
    int io_flags = 0;
    size_t size;
    flb_sds_t t;
    const char *tmp;
    struct flb_upstream *upstream;
    struct flb_splunk *ctx;

    ctx = flb_calloc(1, sizeof(struct flb_splunk));
    if (!ctx) {
        flb_errno();
        return NULL;
    }
    ctx->ins = ins;
    mk_list_init(&ctx->fields);

    ret = flb_output_config_map_set(ins, (void *) ctx);
    if (ret == -1) {
        flb_free(ctx);
        return NULL;
    }

    /* Set default network configuration */
    flb_output_net_default(FLB_SPLUNK_DEFAULT_HOST, FLB_SPLUNK_DEFAULT_PORT, ins);

    /* use TLS ? */
    if (ins->use_tls == FLB_TRUE) {
        io_flags = FLB_IO_TLS;
    }
    else {
        io_flags = FLB_IO_TCP;
    }

    if (ins->host.ipv6 == FLB_TRUE) {
        io_flags |= FLB_IO_IPV6;
    }

    /* Prepare an upstream handler */
    upstream = flb_upstream_create(config,
                                   ins->host.name,
                                   ins->host.port,
                                   io_flags,
                                   ins->tls);
    if (!upstream) {
        flb_plg_error(ctx->ins, "cannot create Upstream context");
        flb_splunk_conf_destroy(ctx);
        return NULL;
    }

    /* Set manual Index and Type */
    ctx->u = upstream;

    tmp = flb_output_get_property("http_buffer_size", ins);
    if (!tmp) {
        ctx->buffer_size = 0;
    }
    else {
        size = flb_utils_size_to_bytes(tmp);
        if (size == -1) {
            flb_plg_error(ctx->ins, "invalid 'buffer_size' value");
            flb_splunk_conf_destroy(ctx);
            return NULL;
        }
        if (size < 4 *1024) {
            size = 4 * 1024;
        }
        ctx->buffer_size = size;
    }

    /* Compress (gzip) */
    tmp = flb_output_get_property("compress", ins);
    ctx->compress_gzip = FLB_FALSE;
    if (tmp) {
        if (strcasecmp(tmp, "gzip") == 0) {
            ctx->compress_gzip = FLB_TRUE;
        }
    }

    /* Event key */
    if (ctx->event_key) {
        if (ctx->event_key[0] != '$') {
            flb_plg_error(ctx->ins,
                          "invalid event_key pattern, it must start with '$'");
            flb_splunk_conf_destroy(ctx);
            return NULL;
        }
        ctx->ra_event_key = flb_ra_create(ctx->event_key, FLB_TRUE);
        if (!ctx->ra_event_key) {
            flb_plg_error(ctx->ins,
                          "cannot create record accessor for event_key pattern: '%s'",
                          ctx->event_key);
            flb_splunk_conf_destroy(ctx);
            return NULL;
        }
    }

    /* Event host */
    if (ctx->event_host) {
        ctx->ra_event_host = flb_ra_create(ctx->event_host, FLB_TRUE);
        if (!ctx->ra_event_host) {
            flb_plg_error(ctx->ins,
                          "cannot create record accessor for event_key pattern: '%s'",
                          ctx->event_host);
            flb_splunk_conf_destroy(ctx);
            return NULL;
        }
    }

    /* Event source */
    if (ctx->event_source) {
        ctx->ra_event_source = flb_ra_create(ctx->event_source, FLB_TRUE);
        if (!ctx->ra_event_source) {
            flb_plg_error(ctx->ins,
                          "cannot create record accessor for event_source pattern: '%s'",
                          ctx->event_host);
            flb_splunk_conf_destroy(ctx);
            return NULL;
        }
    }

    /* Event source (key lookup) */
    if (ctx->event_sourcetype_key) {
        ctx->ra_event_sourcetype_key = flb_ra_create(ctx->event_sourcetype_key, FLB_TRUE);
        if (!ctx->ra_event_sourcetype_key) {
            flb_plg_error(ctx->ins,
                          "cannot create record accessor for "
                          "event_sourcetype_key pattern: '%s'",
                          ctx->event_host);
            flb_splunk_conf_destroy(ctx);
            return NULL;
        }
    }

    /* Event index (key lookup) */
    if (ctx->event_index_key) {
        ctx->ra_event_index_key = flb_ra_create(ctx->event_index_key, FLB_TRUE);
        if (!ctx->ra_event_index_key) {
            flb_plg_error(ctx->ins,
                          "cannot create record accessor for "
                          "event_index_key pattern: '%s'",
                          ctx->event_host);
            flb_splunk_conf_destroy(ctx);
            return NULL;
        }
    }

    /* Event fields */
    ret = event_fields_create(ctx);
    if (ret == -1) {
        flb_splunk_conf_destroy(ctx);
        return NULL;
    }

    /* No http_user is set, fallback to splunk_token, if splunk_token is unset, fail. */
    if (!ctx->http_user) {
        /* Splunk Auth Token */
        tmp = flb_output_get_property("splunk_token", ins);
        if(!tmp) {
            flb_plg_error(ctx->ins, "either splunk_token or http_user should be set");
            flb_splunk_conf_destroy(ctx);
            return NULL;
        }
        ctx->auth_header = flb_sds_create("Splunk ");
        t = flb_sds_cat(ctx->auth_header, tmp, strlen(tmp));
        if (t) {
            ctx->auth_header = t;
        }
        else {
            flb_plg_error(ctx->ins, "error on token generation");
            flb_splunk_conf_destroy(ctx);
            return NULL;
        }
    }

    /* channel */
    if (ctx->channel != NULL) {
        ctx->channel_len = flb_sds_len(ctx->channel);
    }

    /* Set instance flags into upstream */
    flb_output_upstream_set(ctx->u, ins);

    return ctx;
}

int flb_splunk_conf_destroy(struct flb_splunk *ctx)
{
    if (!ctx) {
        return -1;
    }

    if (ctx->auth_header) {
        flb_sds_destroy(ctx->auth_header);
    }
    if (ctx->u) {
        flb_upstream_destroy(ctx->u);
    }

    if (ctx->ra_event_key) {
        flb_ra_destroy(ctx->ra_event_key);
    }

    if (ctx->ra_event_host) {
        flb_ra_destroy(ctx->ra_event_host);
    }

    if (ctx->ra_event_source) {
        flb_ra_destroy(ctx->ra_event_source);
    }

    if (ctx->ra_event_sourcetype_key) {
        flb_ra_destroy(ctx->ra_event_sourcetype_key);
    }

    if (ctx->ra_event_index_key) {
        flb_ra_destroy(ctx->ra_event_index_key);
    }

    event_fields_destroy(ctx);

    flb_free(ctx);

    return 0;
}