/src/fluent-bit/plugins/in_elasticsearch/in_elasticsearch_config.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | |
3 | | /* Fluent Bit |
4 | | * ========== |
5 | | * Copyright (C) 2015-2024 The Fluent Bit Authors |
6 | | * |
7 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
8 | | * you may not use this file except in compliance with the License. |
9 | | * You may obtain a copy of the License at |
10 | | * |
11 | | * http://www.apache.org/licenses/LICENSE-2.0 |
12 | | * |
13 | | * Unless required by applicable law or agreed to in writing, software |
14 | | * distributed under the License is distributed on an "AS IS" BASIS, |
15 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16 | | * See the License for the specific language governing permissions and |
17 | | * limitations under the License. |
18 | | */ |
19 | | |
20 | | #include <fluent-bit/flb_input_plugin.h> |
21 | | |
22 | | #include "in_elasticsearch.h" |
23 | | #include "in_elasticsearch_config.h" |
24 | | #include "in_elasticsearch_bulk_conn.h" |
25 | | |
26 | | struct flb_in_elasticsearch *in_elasticsearch_config_create(struct flb_input_instance *ins) |
27 | 0 | { |
28 | 0 | int ret; |
29 | 0 | char port[8]; |
30 | 0 | struct flb_in_elasticsearch *ctx; |
31 | |
|
32 | 0 | ctx = flb_calloc(1, sizeof(struct flb_in_elasticsearch)); |
33 | 0 | if (!ctx) { |
34 | 0 | flb_errno(); |
35 | 0 | return NULL; |
36 | 0 | } |
37 | 0 | ctx->ins = ins; |
38 | 0 | mk_list_init(&ctx->connections); |
39 | | |
40 | | /* Load the config map */ |
41 | 0 | ret = flb_input_config_map_set(ins, (void *) ctx); |
42 | 0 | if (ret == -1) { |
43 | 0 | flb_free(ctx); |
44 | 0 | return NULL; |
45 | 0 | } |
46 | | |
47 | | /* Listen interface (if not set, defaults to 0.0.0.0:9200) */ |
48 | 0 | flb_input_net_default_listener("0.0.0.0", 9200, ins); |
49 | |
|
50 | 0 | ctx->listen = flb_sds_create(ins->host.listen); |
51 | 0 | snprintf(port, sizeof(port) - 1, "%d", ins->host.port); |
52 | 0 | ctx->tcp_port = flb_sds_create(port); |
53 | | |
54 | | /* HTTP Server specifics */ |
55 | 0 | ctx->server = flb_calloc(1, sizeof(struct mk_server)); |
56 | 0 | ctx->server->keep_alive = MK_TRUE; |
57 | | |
58 | | /* monkey detects server->workers == 0 as the server not being initialized at the |
59 | | * moment so we want to make sure that it stays that way! |
60 | | */ |
61 | |
|
62 | 0 | ctx->log_encoder = flb_log_event_encoder_create(FLB_LOG_EVENT_FORMAT_DEFAULT); |
63 | 0 | if (ctx->log_encoder == NULL) { |
64 | 0 | flb_plg_error(ctx->ins, "event encoder initialization error"); |
65 | 0 | in_elasticsearch_config_destroy(ctx); |
66 | |
|
67 | 0 | return NULL; |
68 | 0 | } |
69 | | |
70 | | |
71 | 0 | return ctx; |
72 | 0 | } |
73 | | |
74 | | int in_elasticsearch_config_destroy(struct flb_in_elasticsearch *ctx) |
75 | 0 | { |
76 | 0 | flb_log_event_encoder_destroy(ctx->log_encoder); |
77 | | |
78 | | /* release all connections */ |
79 | 0 | in_elasticsearch_bulk_conn_release_all(ctx); |
80 | | |
81 | |
|
82 | 0 | if (ctx->collector_id != -1) { |
83 | 0 | flb_input_collector_delete(ctx->collector_id, ctx->ins); |
84 | |
|
85 | 0 | ctx->collector_id = -1; |
86 | 0 | } |
87 | |
|
88 | 0 | if (ctx->downstream != NULL) { |
89 | 0 | flb_downstream_destroy(ctx->downstream); |
90 | 0 | } |
91 | |
|
92 | 0 | if (ctx->enable_http2) { |
93 | 0 | flb_http_server_destroy(&ctx->http_server); |
94 | 0 | } |
95 | |
|
96 | 0 | if (ctx->server) { |
97 | 0 | flb_free(ctx->server); |
98 | 0 | } |
99 | |
|
100 | 0 | flb_sds_destroy(ctx->listen); |
101 | 0 | flb_sds_destroy(ctx->tcp_port); |
102 | |
|
103 | 0 | flb_free(ctx); |
104 | |
|
105 | 0 | return 0; |
106 | 0 | } |