forked from fluent/fluent-bit
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathflb_input.c
1997 lines (1690 loc) · 57.9 KB
/
flb_input.c
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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* Fluent Bit
* ==========
* Copyright (C) 2015-2024 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 <stdlib.h>
#include <monkey/mk_core.h>
#include <fluent-bit/flb_info.h>
#include <fluent-bit/flb_mem.h>
#include <fluent-bit/flb_str.h>
#include <fluent-bit/flb_env.h>
#include <fluent-bit/flb_pipe.h>
#include <fluent-bit/flb_macros.h>
#include <fluent-bit/flb_input.h>
#include <fluent-bit/flb_input_thread.h>
#include <fluent-bit/flb_error.h>
#include <fluent-bit/flb_utils.h>
#include <fluent-bit/flb_plugin_proxy.h>
#include <fluent-bit/flb_engine.h>
#include <fluent-bit/flb_metrics.h>
#include <fluent-bit/flb_storage.h>
#include <fluent-bit/flb_downstream.h>
#include <fluent-bit/flb_plugin.h>
#include <fluent-bit/flb_kv.h>
#include <fluent-bit/flb_hash_table.h>
#include <fluent-bit/flb_scheduler.h>
#include <fluent-bit/flb_ring_buffer.h>
#include <fluent-bit/flb_processor.h>
/* input plugin macro helpers */
#include <fluent-bit/flb_input_plugin.h>
#ifdef FLB_HAVE_CHUNK_TRACE
#include <fluent-bit/flb_chunk_trace.h>
#endif /* FLB_HAVE_CHUNK_TRACE */
struct flb_libco_in_params libco_in_param;
pthread_key_t libco_in_param_key;
#define protcmp(a, b) strncasecmp(a, b, strlen(a))
/*
* Ring buffer size: we make space for 512 entries that each input instance can
* use to enqueue data. Note that this value is fixed and only affect input plugins
* which runs in threaded mode (separate thread)
*
* Ring buffer window: the current window size is set to 5% which means that the
* ring buffer will emit a flush request whenever there are 51 records or more
* awaiting to be consumed.
*/
#define FLB_INPUT_RING_BUFFER_SIZE (sizeof(void *) * 1024)
#define FLB_INPUT_RING_BUFFER_WINDOW (5)
static int check_protocol(const char *prot, const char *output)
{
int len;
len = strlen(prot);
if (len != strlen(output)) {
return 0;
}
if (protcmp(prot, output) != 0) {
return 0;
}
return 1;
}
static inline int instance_id(struct flb_input_plugin *p,
struct flb_config *config) \
{
int c = 0;
struct mk_list *head;
struct flb_input_instance *entry;
mk_list_foreach(head, &config->inputs) {
entry = mk_list_entry(head, struct flb_input_instance, _head);
if (entry->id == c) {
c++;
}
}
return c;
}
/* Generate a new collector ID for the instance in question */
static int collector_id(struct flb_input_instance *ins)
{
int id = 0;
struct flb_input_collector *collector;
if (mk_list_is_empty(&ins->collectors) == 0) {
return id;
}
collector = mk_list_entry_last(&ins->collectors,
struct flb_input_collector,
_head);
return (collector->id + 1);
}
void flb_input_net_default_listener(const char *listen, int port,
struct flb_input_instance *ins)
{
/* Set default network configuration */
if (!ins->host.listen) {
ins->host.listen = flb_sds_create(listen);
}
if (ins->host.port == 0) {
ins->host.port = port;
}
}
/* Check input plugin's log level.
* Not for core plugins but for Golang plugins.
* Golang plugins do not have thread-local flb_worker_ctx information. */
int flb_input_log_check(struct flb_input_instance *ins, int l)
{
if (ins->log_level < l) {
return FLB_FALSE;
}
return FLB_TRUE;
}
/* Create an input plugin instance */
struct flb_input_instance *flb_input_new(struct flb_config *config,
const char *input, void *data,
int public_only)
{
int id;
int ret;
int flags = 0;
struct mk_list *head;
struct flb_input_plugin *plugin;
struct flb_input_instance *instance = NULL;
/* use for locking the use of the chunk trace context. */
#ifdef FLB_HAVE_CHUNK_TRACE
pthread_mutexattr_t attr = {0};
pthread_mutexattr_init(&attr);
#endif
if (!input) {
return NULL;
}
mk_list_foreach(head, &config->in_plugins) {
plugin = mk_list_entry(head, struct flb_input_plugin, _head);
if (!check_protocol(plugin->name, input)) {
plugin = NULL;
continue;
}
/*
* Check if the plugin is private and validate the 'public_only'
* requirement.
*/
if (public_only == FLB_TRUE && plugin->flags & FLB_INPUT_PRIVATE) {
return NULL;
}
/* Create plugin instance */
instance = flb_calloc(1, sizeof(struct flb_input_instance));
if (!instance) {
flb_errno();
return NULL;
}
instance->config = config;
/* Get an ID */
id = instance_id(plugin, config);
/* Index for log Chunks (hash table) */
instance->ht_log_chunks = flb_hash_table_create(FLB_HASH_TABLE_EVICT_NONE,
512, 0);
if (!instance->ht_log_chunks) {
flb_free(instance);
return NULL;
}
/* Index for metric Chunks (hash table) */
instance->ht_metric_chunks = flb_hash_table_create(FLB_HASH_TABLE_EVICT_NONE,
512, 0);
if (!instance->ht_metric_chunks) {
flb_hash_table_destroy(instance->ht_log_chunks);
flb_free(instance);
return NULL;
}
/* Index for trace Chunks (hash table) */
instance->ht_trace_chunks = flb_hash_table_create(FLB_HASH_TABLE_EVICT_NONE,
512, 0);
if (!instance->ht_trace_chunks) {
flb_hash_table_destroy(instance->ht_log_chunks);
flb_hash_table_destroy(instance->ht_metric_chunks);
flb_free(instance);
return NULL;
}
/* format name (with instance id) */
snprintf(instance->name, sizeof(instance->name) - 1,
"%s.%i", plugin->name, id);
if (plugin->type == FLB_INPUT_PLUGIN_CORE) {
instance->context = NULL;
}
else {
struct flb_plugin_proxy_context *ctx;
ctx = flb_calloc(1, sizeof(struct flb_plugin_proxy_context));
if (!ctx) {
flb_errno();
flb_free(instance);
return NULL;
}
ctx->proxy = plugin->proxy;
instance->context = ctx;
}
/* initialize remaining vars */
instance->alias = NULL;
instance->id = id;
instance->flags = plugin->flags;
instance->p = plugin;
instance->tag = NULL;
instance->tag_len = 0;
instance->tag_default = FLB_FALSE;
instance->routable = FLB_TRUE;
instance->data = data;
instance->storage = NULL;
instance->storage_type = -1;
instance->log_level = -1;
instance->log_suppress_interval = -1;
instance->runs_in_coroutine = FLB_FALSE;
/* net */
instance->host.name = NULL;
instance->host.address = NULL;
instance->host.uri = NULL;
instance->host.listen = NULL;
instance->host.ipv6 = FLB_FALSE;
/* Initialize list heads */
mk_list_init(&instance->routes_direct);
mk_list_init(&instance->routes);
mk_list_init(&instance->tasks);
mk_list_init(&instance->chunks);
mk_list_init(&instance->collectors);
mk_list_init(&instance->input_coro_list);
mk_list_init(&instance->input_coro_list_destroy);
mk_list_init(&instance->downstreams);
mk_list_init(&instance->upstreams);
/* Initialize properties list */
flb_kv_init(&instance->properties);
flb_kv_init(&instance->net_properties);
/* Plugin use networking */
if (plugin->flags & (FLB_INPUT_NET | FLB_INPUT_NET_SERVER)) {
ret = flb_net_host_set(plugin->name, &instance->host, input);
if (ret != 0) {
flb_free(instance);
return NULL;
}
}
/* initialize lock for access to chunk trace context. */
#ifdef FLB_HAVE_CHUNK_TRACE
pthread_mutex_init(&instance->chunk_trace_lock, &attr);
#endif
/* Parent plugin flags */
flags = instance->flags;
if (flags & FLB_IO_TCP) {
instance->use_tls = FLB_FALSE;
}
else if (flags & FLB_IO_TLS) {
instance->use_tls = FLB_TRUE;
}
else if (flags & FLB_IO_OPT_TLS) {
/* TLS must be enabled manually in the config */
instance->use_tls = FLB_FALSE;
instance->flags |= FLB_IO_TLS;
}
#ifdef FLB_HAVE_TLS
instance->tls = NULL;
instance->tls_debug = -1;
instance->tls_verify = FLB_TRUE;
instance->tls_vhost = NULL;
instance->tls_ca_path = NULL;
instance->tls_ca_file = NULL;
instance->tls_crt_file = NULL;
instance->tls_key_file = NULL;
instance->tls_key_passwd = NULL;
#endif
/* Plugin requires a co-routine context ? */
if (plugin->flags & FLB_INPUT_CORO) {
instance->runs_in_coroutine = FLB_TRUE;
}
/* Plugin will run in a separate thread ? */
if (plugin->flags & FLB_INPUT_THREADED) {
instance->is_threaded = FLB_TRUE;
}
/* allocate a ring buffer */
instance->rb = flb_ring_buffer_create(FLB_INPUT_RING_BUFFER_SIZE);
if (!instance->rb) {
flb_error("instance %s could not initialize ring buffer",
flb_input_name(instance));
flb_free(instance);
return NULL;
}
instance->mem_buf_status = FLB_INPUT_RUNNING;
instance->mem_buf_limit = 0;
instance->mem_chunks_size = 0;
instance->storage_buf_status = FLB_INPUT_RUNNING;
mk_list_add(&instance->_head, &config->inputs);
/* processor instance */
instance->processor = flb_processor_create(config, instance->name, instance, FLB_PLUGIN_INPUT);
}
return instance;
}
static inline int prop_key_check(const char *key, const char *kv, int k_len)
{
int len;
len = strlen(key);
if (strncasecmp(key, kv, k_len) == 0 && len == k_len) {
return 0;
}
return -1;
}
struct flb_input_instance *flb_input_get_instance(struct flb_config *config,
int ins_id)
{
struct mk_list *head;
struct flb_input_instance *ins;
mk_list_foreach(head, &config->inputs) {
ins = mk_list_entry(head, struct flb_input_instance, _head);
if (ins->id == ins_id) {
break;
}
ins = NULL;
}
if (!ins) {
return NULL;
}
return ins;
}
static void flb_input_coro_destroy(struct flb_input_coro *input_coro)
{
flb_debug("[input coro] destroy coro_id=%i", input_coro->id);
mk_list_del(&input_coro->_head);
flb_coro_destroy(input_coro->coro);
flb_free(input_coro);
}
int flb_input_coro_finished(struct flb_config *config, int ins_id)
{
struct mk_list *tmp;
struct mk_list *head;
struct flb_input_instance *ins;
struct flb_input_coro *input_coro;
ins = flb_input_get_instance(config, ins_id);
if (!ins) {
return -1;
}
/* Look for input coroutines that needs to be destroyed */
mk_list_foreach_safe(head, tmp, &ins->input_coro_list_destroy) {
input_coro = mk_list_entry(head, struct flb_input_coro, _head);
flb_input_coro_destroy(input_coro);
}
return 0;
}
void flb_input_coro_prepare_destroy(struct flb_input_coro *input_coro)
{
struct flb_input_instance *ins = input_coro->ins;
/* move flb_input_coro from 'input_coro_list' to 'input_coro_list_destroy' */
mk_list_del(&input_coro->_head);
mk_list_add(&input_coro->_head, &ins->input_coro_list_destroy);
}
int flb_input_name_exists(const char *name, struct flb_config *config)
{
struct mk_list *head;
struct flb_input_instance *ins;
mk_list_foreach(head, &config->inputs) {
ins = mk_list_entry(head, struct flb_input_instance, _head);
if (strcmp(ins->name, name) == 0) {
return FLB_TRUE;
}
if (ins->alias) {
if (strcmp(ins->alias, name) == 0) {
return FLB_TRUE;
}
}
}
return FLB_FALSE;
}
struct mk_event_loop *flb_input_event_loop_get(struct flb_input_instance *ins)
{
struct flb_input_thread_instance *thi;
if (flb_input_is_threaded(ins)) {
thi = ins->thi;
return thi->evl;
}
return ins->config->evl;
}
/* Override a configuration property for the given input_instance plugin */
int flb_input_set_property(struct flb_input_instance *ins,
const char *k, const char *v)
{
int len;
int ret;
int enabled;
ssize_t limit;
flb_sds_t tmp = NULL;
struct flb_kv *kv;
len = strlen(k);
tmp = flb_env_var_translate(ins->config->env, v);
if (tmp) {
if (flb_sds_len(tmp) == 0) {
flb_sds_destroy(tmp);
tmp = NULL;
}
}
/* Check if the key is a known/shared property */
if (prop_key_check("tag", k, len) == 0 && tmp) {
flb_utils_set_plugin_string_property("tag", &ins->tag, tmp);
ins->tag_len = flb_sds_len(tmp);
ins->tag_default = FLB_FALSE;
}
else if (prop_key_check("log_level", k, len) == 0 && tmp) {
ret = flb_log_get_level_str(tmp);
flb_sds_destroy(tmp);
if (ret == -1) {
return -1;
}
ins->log_level = ret;
}
else if (prop_key_check("log_suppress_interval", k, len) == 0 && tmp) {
ret = flb_utils_time_to_seconds(tmp);
flb_sds_destroy(tmp);
if (ret == -1) {
return -1;
}
ins->log_suppress_interval = ret;
}
else if (prop_key_check("routable", k, len) == 0 && tmp) {
ins->routable = flb_utils_bool(tmp);
flb_sds_destroy(tmp);
}
else if (prop_key_check("alias", k, len) == 0 && tmp) {
flb_utils_set_plugin_string_property("alias", &ins->alias, tmp);
}
else if (prop_key_check("mem_buf_limit", k, len) == 0 && tmp) {
limit = flb_utils_size_to_bytes(tmp);
flb_sds_destroy(tmp);
if (limit == -1) {
return -1;
}
ins->mem_buf_limit = (size_t) limit;
}
else if (prop_key_check("listen", k, len) == 0) {
flb_utils_set_plugin_string_property("listen", &ins->host.listen, tmp);
}
else if (prop_key_check("host", k, len) == 0) {
flb_utils_set_plugin_string_property("host", &ins->host.name, tmp);
}
else if (prop_key_check("port", k, len) == 0) {
if (tmp) {
ins->host.port = atoi(tmp);
flb_sds_destroy(tmp);
}
}
else if (prop_key_check("ipv6", k, len) == 0 && tmp) {
ins->host.ipv6 = flb_utils_bool(tmp);
flb_sds_destroy(tmp);
}
else if (strncasecmp("net.", k, 4) == 0 && tmp) {
kv = flb_kv_item_create(&ins->net_properties, (char *) k, NULL);
if (!kv) {
if (tmp) {
flb_sds_destroy(tmp);
}
return -1;
}
kv->val = tmp;
}
#ifdef FLB_HAVE_TLS
else if (prop_key_check("tls", k, len) == 0 && tmp) {
ins->use_tls = flb_utils_bool(tmp);
if (ins->use_tls == FLB_TRUE && ((ins->flags & FLB_IO_TLS) == 0)) {
flb_error("[config] %s does not support TLS", ins->name);
flb_sds_destroy(tmp);
return -1;
}
flb_sds_destroy(tmp);
}
else if (prop_key_check("tls.verify", k, len) == 0 && tmp) {
ins->tls_verify = flb_utils_bool(tmp);
flb_sds_destroy(tmp);
}
else if (prop_key_check("tls.debug", k, len) == 0 && tmp) {
ins->tls_debug = atoi(tmp);
flb_sds_destroy(tmp);
}
else if (prop_key_check("tls.vhost", k, len) == 0) {
flb_utils_set_plugin_string_property("tls.vhost", &ins->tls_vhost, tmp);
}
else if (prop_key_check("tls.ca_path", k, len) == 0) {
flb_utils_set_plugin_string_property("tls.ca_path", &ins->tls_ca_path, tmp);
}
else if (prop_key_check("tls.ca_file", k, len) == 0) {
flb_utils_set_plugin_string_property("tls.ca_file", &ins->tls_ca_file, tmp);
}
else if (prop_key_check("tls.crt_file", k, len) == 0) {
flb_utils_set_plugin_string_property("tls.crt_file", &ins->tls_crt_file, tmp);
}
else if (prop_key_check("tls.key_file", k, len) == 0) {
flb_utils_set_plugin_string_property("tls.key_file", &ins->tls_key_file, tmp);
}
else if (prop_key_check("tls.key_passwd", k, len) == 0) {
flb_utils_set_plugin_string_property("tls.key_passwd", &ins->tls_key_passwd, tmp);
}
#endif
else if (prop_key_check("storage.type", k, len) == 0 && tmp) {
/* Set the storage type */
if (strcasecmp(tmp, "filesystem") == 0) {
ins->storage_type = FLB_STORAGE_FS;
}
else if (strcasecmp(tmp, "memory") == 0) {
ins->storage_type = FLB_STORAGE_MEM;
}
else if (strcasecmp(tmp, "memrb") == 0) {
ins->storage_type = FLB_STORAGE_MEMRB;
}
else {
flb_sds_destroy(tmp);
return -1;
}
flb_sds_destroy(tmp);
}
else if (prop_key_check("threaded", k, len) == 0 && tmp) {
enabled = flb_utils_bool(tmp);
flb_sds_destroy(tmp);
if (enabled == -1) {
return -1;
}
ins->is_threaded = enabled;
}
else if (prop_key_check("storage.pause_on_chunks_overlimit", k, len) == 0 && tmp) {
if (ins->storage_type == FLB_STORAGE_FS) {
ret = flb_utils_bool(tmp);
flb_sds_destroy(tmp);
if (ret == -1) {
return -1;
}
ins->storage_pause_on_chunks_overlimit = ret;
}
}
else {
/*
* Create the property, we don't pass the value since we will
* map it directly to avoid an extra memory allocation.
*/
kv = flb_kv_item_create(&ins->properties, (char *) k, NULL);
if (!kv) {
if (tmp) {
flb_sds_destroy(tmp);
}
return -1;
}
kv->val = tmp;
}
return 0;
}
const char *flb_input_get_property(const char *key,
struct flb_input_instance *ins)
{
return flb_config_prop_get(key, &ins->properties);
}
#ifdef FLB_HAVE_METRICS
void *flb_input_get_cmt_instance(struct flb_input_instance *ins)
{
return (void *)ins->cmt;
}
#endif
/* Return an instance name or alias */
const char *flb_input_name(struct flb_input_instance *ins)
{
if (ins->alias) {
return ins->alias;
}
return ins->name;
}
void flb_input_instance_destroy(struct flb_input_instance *ins)
{
struct mk_list *tmp;
struct mk_list *head;
struct flb_input_collector *collector;
if (ins->alias) {
flb_sds_destroy(ins->alias);
}
/* Remove URI context */
if (ins->host.uri) {
flb_uri_destroy(ins->host.uri);
}
if (ins->host.name) {
flb_sds_destroy(ins->host.name);
}
if (ins->host.address) {
flb_sds_destroy(ins->host.address);
}
if (ins->host.listen) {
flb_sds_destroy(ins->host.listen);
}
#ifdef FLB_HAVE_TLS
if (ins->use_tls) {
if (ins->tls != NULL) {
flb_tls_destroy(ins->tls);
}
}
if (ins->tls_config_map) {
flb_config_map_destroy(ins->tls_config_map);
}
#endif
if (ins->tls_vhost) {
flb_sds_destroy(ins->tls_vhost);
}
if (ins->tls_ca_path) {
flb_sds_destroy(ins->tls_ca_path);
}
if (ins->tls_ca_file) {
flb_sds_destroy(ins->tls_ca_file);
}
if (ins->tls_crt_file) {
flb_sds_destroy(ins->tls_crt_file);
}
if (ins->tls_key_file) {
flb_sds_destroy(ins->tls_key_file);
}
if (ins->tls_key_passwd) {
flb_sds_destroy(ins->tls_key_passwd);
}
/* release the tag if any */
flb_sds_destroy(ins->tag);
/* Let the engine remove any pending task */
flb_engine_destroy_tasks(&ins->tasks);
/* release properties */
flb_kv_release(&ins->properties);
flb_kv_release(&ins->net_properties);
#ifdef FLB_HAVE_CHUNK_TRACE
flb_chunk_trace_context_destroy(ins);
#endif /* FLB_HAVE_CHUNK_TRACE */
/* Remove metrics */
#ifdef FLB_HAVE_METRICS
if (ins->cmt) {
cmt_destroy(ins->cmt);
}
if (ins->metrics) {
flb_metrics_destroy(ins->metrics);
}
#endif
if (ins->storage) {
flb_storage_input_destroy(ins);
}
/* destroy config map */
if (ins->config_map) {
flb_config_map_destroy(ins->config_map);
}
if (ins->net_config_map) {
flb_config_map_destroy(ins->net_config_map);
}
/* hash table for chunks */
if (ins->ht_log_chunks) {
flb_hash_table_destroy(ins->ht_log_chunks);
}
if (ins->ht_metric_chunks) {
flb_hash_table_destroy(ins->ht_metric_chunks);
}
if (ins->ht_trace_chunks) {
flb_hash_table_destroy(ins->ht_trace_chunks);
}
if (ins->ch_events[0] > 0) {
mk_event_closesocket(ins->ch_events[0]);
}
if (ins->ch_events[1] > 0) {
mk_event_closesocket(ins->ch_events[1]);
}
/* Collectors */
mk_list_foreach_safe(head, tmp, &ins->collectors) {
collector = mk_list_entry(head, struct flb_input_collector, _head);
mk_list_del(&collector->_head);
flb_input_collector_destroy(collector);
}
/* delete storage context */
flb_storage_input_destroy(ins);
mk_list_del(&ins->_head);
/* ring buffer */
if (ins->rb) {
flb_input_chunk_ring_buffer_cleanup(ins);
flb_ring_buffer_destroy(ins->rb);
}
/* processor */
if (ins->processor) {
flb_processor_destroy(ins->processor);
}
flb_free(ins);
}
int flb_input_coro_id_get(struct flb_input_instance *ins)
{
int id;
int max = (2 << 13) - 1; /* max for 14 bits */
id = ins->input_coro_id;
ins->input_coro_id++;
/* reset once it reach the maximum allowed */
if (ins->input_coro_id > max) {
ins->input_coro_id = 0;
}
return id;
}
static int input_instance_channel_events_init(struct flb_input_instance *ins)
{
int ret;
struct mk_event_loop *evl;
evl = flb_input_event_loop_get(ins);
/* Input event channel: used for co-routines to report return status */
ret = mk_event_channel_create(evl,
&ins->ch_events[0],
&ins->ch_events[1],
ins);
if (ret != 0) {
flb_error("could not create events channels for '%s'",
flb_input_name(ins));
return -1;
}
flb_debug("[%s:%s] created event channels: read=%i write=%i",
ins->p->name, flb_input_name(ins),
ins->ch_events[0], ins->ch_events[1]);
/*
* Note: mk_event_channel_create() sets a type = MK_EVENT_NOTIFICATION by
* default, we need to overwrite this value so we can do a clean check
* into the Engine when the event is triggered.
*/
ins->event.type = FLB_ENGINE_EV_INPUT;
return 0;
}
int flb_input_net_property_check(struct flb_input_instance *ins,
struct flb_config *config)
{
int ret = 0;
/* Get Downstream net_setup configmap */
ins->net_config_map = flb_downstream_get_config_map(config);
if (!ins->net_config_map) {
flb_input_instance_destroy(ins);
return -1;
}
/*
* Validate 'net.*' properties: if the plugin use the Downstream interface,
* it might receive some networking settings.
*/
if (mk_list_size(&ins->net_properties) > 0) {
ret = flb_config_map_properties_check(ins->p->name,
&ins->net_properties,
ins->net_config_map);
if (ret == -1) {
if (config->program_name) {
flb_helper("try the command: %s -i %s -h\n",
config->program_name, ins->p->name);
}
return -1;
}
}
return 0;
}
int flb_input_plugin_property_check(struct flb_input_instance *ins,
struct flb_config *config)
{
int ret = 0;
struct mk_list *config_map;
struct flb_input_plugin *p = ins->p;
if (p->config_map) {
/*
* Create a dynamic version of the configmap that will be used by the specific
* instance in question.
*/
config_map = flb_config_map_create(config, p->config_map);
if (!config_map) {
flb_error("[input] error loading config map for '%s' plugin",
p->name);
flb_input_instance_destroy(ins);
return -1;
}
ins->config_map = config_map;
/* Validate incoming properties against config map */
ret = flb_config_map_properties_check(ins->p->name,
&ins->properties, ins->config_map);
if (ret == -1) {
if (config->program_name) {
flb_helper("try the command: %s -i %s -h\n",
config->program_name, ins->p->name);
}
return -1;
}
}
return 0;
}
int flb_input_instance_init(struct flb_input_instance *ins,
struct flb_config *config)
{
int ret;
struct flb_config *ctx = ins->config;
struct flb_input_plugin *p = ins->p;
int tls_session_mode;
if (ins->log_level == -1 && config->log != NULL) {
ins->log_level = config->log->level;
}
/* Skip pseudo input plugins */
if (!p) {
return 0;
}
#ifdef FLB_HAVE_METRICS
uint64_t ts;
char *name;
name = (char *) flb_input_name(ins);
ts = cfl_time_now();
/* CMetrics */
ins->cmt = cmt_create();
if (!ins->cmt) {
flb_error("[input] could not create cmetrics context: %s",
flb_input_name(ins));
return -1;
}
/*
* Register generic input plugin metrics
* -------------------------------------
*/
/* fluentbit_input_bytes_total */
ins->cmt_bytes = \
cmt_counter_create(ins->cmt,
"fluentbit", "input", "bytes_total",
"Number of input bytes.",
1, (char *[]) {"name"});
cmt_counter_set(ins->cmt_bytes, ts, 0, 1, (char *[]) {name});
/* fluentbit_input_records_total */
ins->cmt_records = \
cmt_counter_create(ins->cmt,
"fluentbit", "input", "records_total",
"Number of input records.",
1, (char *[]) {"name"});
cmt_counter_set(ins->cmt_records, ts, 0, 1, (char *[]) {name});
/* fluentbit_input_ingestion_paused */
ins->cmt_ingestion_paused = \
cmt_gauge_create(ins->cmt,
"fluentbit", "input",
"ingestion_paused",
"Is the input paused or not?",
1, (char *[]) {"name"});
cmt_gauge_set(ins->cmt_ingestion_paused, ts, 0, 1, (char *[]) {name});
/* Storage Metrics */
if (ctx->storage_metrics == FLB_TRUE) {
/* fluentbit_input_storage_overlimit */
ins->cmt_storage_overlimit = \
cmt_gauge_create(ins->cmt,
"fluentbit", "input",
"storage_overlimit",
"Is the input memory usage overlimit ?.",
1, (char *[]) {"name"});
cmt_gauge_set(ins->cmt_storage_overlimit, ts, 0, 1, (char *[]) {name});
/* fluentbit_input_storage_memory_bytes */
ins->cmt_storage_memory_bytes = \
cmt_gauge_create(ins->cmt,
"fluentbit", "input",
"storage_memory_bytes",
"Memory bytes used by the chunks.",
1, (char *[]) {"name"});
cmt_gauge_set(ins->cmt_storage_memory_bytes, ts, 0, 1, (char *[]) {name});