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
|
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* Fluent Bit
* ==========
* Copyright (C) 2015-2023 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_config.h>
#include <fluent-bit/flb_random.h>
#include "in_elasticsearch.h"
#include "in_elasticsearch_config.h"
#include "in_elasticsearch_bulk_conn.h"
/*
* For a server event, the collection event means a new client have arrived, we
* accept the connection and create a new TCP instance which will wait for
* JSON map messages.
*/
static int in_elasticsearch_bulk_collect(struct flb_input_instance *ins,
struct flb_config *config, void *in_context)
{
struct flb_connection *connection;
struct in_elasticsearch_bulk_conn *conn;
struct flb_in_elasticsearch *ctx;
ctx = in_context;
connection = flb_downstream_conn_get(ctx->downstream);
if (connection == NULL) {
flb_plg_error(ctx->ins, "could not accept new connection");
return -1;
}
flb_plg_trace(ctx->ins, "new TCP connection arrived FD=%i",
connection->fd);
conn = in_elasticsearch_bulk_conn_add(connection, ctx);
if (conn == NULL) {
flb_downstream_conn_release(connection);
return -1;
}
return 0;
}
static void bytes_to_groupname(unsigned char *data, char *buf, size_t len) {
int index;
char charset[] = "0123456789"
"abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
while (len-- > 0) {
index = (int) data[len];
index = index % (sizeof(charset) - 1);
buf[len] = charset[index];
}
}
static void bytes_to_nodename(unsigned char *data, char *buf, size_t len) {
int index;
char charset[] = "0123456789"
"abcdefghijklmnopqrstuvwxyz";
while (len-- > 0) {
index = (int) data[len];
index = index % (sizeof(charset) - 1);
buf[len] = charset[index];
}
}
static int in_elasticsearch_bulk_init(struct flb_input_instance *ins,
struct flb_config *config, void *data)
{
unsigned short int port;
int ret;
struct flb_in_elasticsearch *ctx;
unsigned char rand[16];
(void) data;
/* Create context and basic conf */
ctx = in_elasticsearch_config_create(ins);
if (!ctx) {
return -1;
}
ctx->collector_id = -1;
/* Populate context with config map defaults and incoming properties */
ret = flb_input_config_map_set(ins, (void *) ctx);
if (ret == -1) {
flb_plg_error(ctx->ins, "configuration error");
in_elasticsearch_config_destroy(ctx);
return -1;
}
/* Set the context */
flb_input_set_context(ins, ctx);
port = (unsigned short int) strtoul(ctx->tcp_port, NULL, 10);
if (flb_random_bytes(rand, 16)) {
flb_plg_error(ctx->ins, "cannot generate cluster name");
in_elasticsearch_config_destroy(ctx);
return -1;
}
bytes_to_groupname(rand, ctx->cluster_name, 16);
if (flb_random_bytes(rand, 12)) {
flb_plg_error(ctx->ins, "cannot generate node name");
in_elasticsearch_config_destroy(ctx);
return -1;
}
bytes_to_nodename(rand, ctx->node_name, 12);
ctx->downstream = flb_downstream_create(FLB_TRANSPORT_TCP,
ins->flags,
ctx->listen,
port,
ins->tls,
config,
&ins->net_setup);
if (ctx->downstream == NULL) {
flb_plg_error(ctx->ins,
"could not initialize downstream on %s:%s. Aborting",
ctx->listen, ctx->tcp_port);
in_elasticsearch_config_destroy(ctx);
return -1;
}
flb_input_downstream_set(ctx->downstream, ctx->ins);
/* Collect upon data available on the standard input */
ret = flb_input_set_collector_socket(ins,
in_elasticsearch_bulk_collect,
ctx->downstream->server_fd,
config);
if (ret == -1) {
flb_plg_error(ctx->ins, "Could not set collector for IN_ELASTICSEARCH input plugin");
in_elasticsearch_config_destroy(ctx);
return -1;
}
ctx->collector_id = ret;
return 0;
}
static int in_elasticsearch_bulk_exit(void *data, struct flb_config *config)
{
struct flb_in_elasticsearch *ctx;
(void) config;
ctx = data;
if (ctx != NULL) {
in_elasticsearch_config_destroy(ctx);
}
return 0;
}
/* Configuration properties map */
static struct flb_config_map config_map[] = {
{
FLB_CONFIG_MAP_SIZE, "buffer_max_size", HTTP_BUFFER_MAX_SIZE,
0, FLB_TRUE, offsetof(struct flb_in_elasticsearch, buffer_max_size),
"Set the maximum size of buffer"
},
{
FLB_CONFIG_MAP_SIZE, "buffer_chunk_size", HTTP_BUFFER_CHUNK_SIZE,
0, FLB_TRUE, offsetof(struct flb_in_elasticsearch, buffer_chunk_size),
"Set the buffer chunk size"
},
{
FLB_CONFIG_MAP_STR, "tag_key", NULL,
0, FLB_TRUE, offsetof(struct flb_in_elasticsearch, tag_key),
"Specify a key name for extracting as a tag"
},
{
FLB_CONFIG_MAP_STR, "meta_key", "@meta",
0, FLB_TRUE, offsetof(struct flb_in_elasticsearch, meta_key),
"Specify a key name for meta information"
},
{
FLB_CONFIG_MAP_STR, "hostname", "localhost",
0, FLB_TRUE, offsetof(struct flb_in_elasticsearch, hostname),
"Specify hostname or FQDN. This parameter is effective for sniffering node information."
},
{
FLB_CONFIG_MAP_STR, "version", "8.0.0",
0, FLB_TRUE, offsetof(struct flb_in_elasticsearch, es_version),
"Specify returning Elasticsearch server version."
},
/* EOF */
{0}
};
/* Plugin reference */
struct flb_input_plugin in_elasticsearch_plugin = {
.name = "elasticsearch",
.description = "HTTP Endpoints for Elasticsearch (Bulk API)",
.cb_init = in_elasticsearch_bulk_init,
.cb_pre_run = NULL,
.cb_collect = in_elasticsearch_bulk_collect,
.cb_flush_buf = NULL,
.cb_pause = NULL,
.cb_resume = NULL,
.cb_exit = in_elasticsearch_bulk_exit,
.config_map = config_map,
.flags = FLB_INPUT_NET_SERVER | FLB_IO_OPT_TLS
};
|