/src/fluent-bit/plugins/in_splunk/splunk.c
Line | Count | Source |
1 | | /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | |
3 | | /* Fluent Bit |
4 | | * ========== |
5 | | * Copyright (C) 2015-2026 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 | | |
21 | | #include <fluent-bit/flb_input_plugin.h> |
22 | | #include <fluent-bit/flb_network.h> |
23 | | #include <fluent-bit/flb_config.h> |
24 | | |
25 | | #include "splunk.h" |
26 | | #include "splunk_prot.h" |
27 | | #include "splunk_config.h" |
28 | | |
29 | | static int in_splunk_init(struct flb_input_instance *ins, |
30 | | struct flb_config *config, void *data) |
31 | 0 | { |
32 | 0 | int ret; |
33 | 0 | struct flb_splunk *ctx; |
34 | 0 | struct flb_http_server_options http_server_options; |
35 | |
|
36 | 0 | (void) config; |
37 | 0 | (void) data; |
38 | | |
39 | | /* Create context and basic conf */ |
40 | 0 | ctx = splunk_config_create(ins); |
41 | 0 | if (!ctx) { |
42 | 0 | return -1; |
43 | 0 | } |
44 | | |
45 | | /* Populate context with config map defaults and incoming properties */ |
46 | 0 | ret = flb_input_config_map_set(ins, (void *) ctx); |
47 | 0 | if (ret == -1) { |
48 | 0 | flb_plg_error(ctx->ins, "configuration error"); |
49 | 0 | splunk_config_destroy(ctx); |
50 | 0 | return -1; |
51 | 0 | } |
52 | | |
53 | | /* Set the context */ |
54 | 0 | flb_input_set_context(ins, ctx); |
55 | |
|
56 | 0 | ret = flb_input_http_server_options_init( |
57 | 0 | &http_server_options, |
58 | 0 | ins, |
59 | 0 | (FLB_HTTP_SERVER_FLAG_KEEPALIVE | FLB_HTTP_SERVER_FLAG_AUTO_INFLATE), |
60 | 0 | splunk_prot_handle_ng, |
61 | 0 | ctx); |
62 | 0 | if (ret == 0) { |
63 | 0 | if (http_server_options.workers > 1) { |
64 | 0 | ret = flb_input_ingress_enable(ins); |
65 | 0 | } |
66 | 0 | } |
67 | 0 | if (ret == 0) { |
68 | 0 | ret = flb_http_server_init_with_options(&ctx->http_server, |
69 | 0 | &http_server_options); |
70 | |
|
71 | 0 | if (ret == 0) { |
72 | 0 | ret = flb_http_server_start(&ctx->http_server); |
73 | 0 | } |
74 | |
|
75 | 0 | if (ret == 0 && ctx->http_server.downstream != NULL) { |
76 | 0 | ret = flb_input_downstream_set(ctx->http_server.downstream, ins); |
77 | 0 | } |
78 | 0 | } |
79 | 0 | if (ret != 0) { |
80 | 0 | flb_plg_error(ctx->ins, |
81 | 0 | "could not start http server on %s:%u. Aborting", |
82 | 0 | ins->host.listen, ins->host.port); |
83 | |
|
84 | 0 | splunk_config_destroy(ctx); |
85 | 0 | return -1; |
86 | 0 | } |
87 | | |
88 | 0 | flb_plg_info(ctx->ins, "listening on %s:%u with %i worker%s", |
89 | 0 | ins->host.listen, |
90 | 0 | ins->host.port, |
91 | 0 | ctx->http_server.workers, |
92 | 0 | ctx->http_server.workers == 1 ? "" : "s"); |
93 | 0 | return 0; |
94 | 0 | } |
95 | | |
96 | | static int in_splunk_exit(void *data, struct flb_config *config) |
97 | 0 | { |
98 | 0 | struct flb_splunk *ctx; |
99 | |
|
100 | 0 | (void) config; |
101 | |
|
102 | 0 | ctx = data; |
103 | |
|
104 | 0 | if (ctx != NULL) { |
105 | 0 | splunk_config_destroy(ctx); |
106 | 0 | } |
107 | |
|
108 | 0 | return 0; |
109 | 0 | } |
110 | | |
111 | | /* Configuration properties map */ |
112 | | static struct flb_config_map config_map[] = { |
113 | | { |
114 | | FLB_CONFIG_MAP_SLIST_1, "success_header", NULL, |
115 | | FLB_CONFIG_MAP_MULT, FLB_TRUE, offsetof(struct flb_splunk, success_headers), |
116 | | "Add an HTTP header key/value pair on success. Multiple headers can be set" |
117 | | }, |
118 | | |
119 | | { |
120 | | FLB_CONFIG_MAP_STR, "splunk_token", NULL, |
121 | | 0, FLB_FALSE, 0, |
122 | | "Set valid Splunk HEC tokens for the requests" |
123 | | }, |
124 | | |
125 | | { |
126 | | FLB_CONFIG_MAP_BOOL, "store_token_in_metadata", "true", |
127 | | 0, FLB_TRUE, offsetof(struct flb_splunk, store_token_in_metadata), |
128 | | "Store Splunk HEC tokens in metadata. If set as false, they will be stored into records." |
129 | | }, |
130 | | |
131 | | { |
132 | | FLB_CONFIG_MAP_STR, "splunk_token_key", "@splunk_token", |
133 | | 0, FLB_TRUE, offsetof(struct flb_splunk, store_token_key), |
134 | | "Set a record key for storing Splunk HEC token for the request" |
135 | | }, |
136 | | |
137 | | { |
138 | | FLB_CONFIG_MAP_STR, "tag_key", NULL, |
139 | | 0, FLB_TRUE, offsetof(struct flb_splunk, tag_key), |
140 | | "Set a record key to specify the tag of the record" |
141 | | }, |
142 | | { |
143 | | FLB_CONFIG_MAP_BOOL, "add_remote_addr", "false", |
144 | | 0, FLB_TRUE, offsetof(struct flb_splunk, add_remote_addr), |
145 | | "Inject a remote address using the X-Forwarded-For header or connection address" |
146 | | }, |
147 | | { |
148 | | FLB_CONFIG_MAP_STR, "remote_addr_key", "remote_addr", |
149 | | 0, FLB_TRUE, offsetof(struct flb_splunk, remote_addr_key), |
150 | | "Set a record key for storing the remote address" |
151 | | }, |
152 | | |
153 | | |
154 | | /* EOF */ |
155 | | {0} |
156 | | }; |
157 | | |
158 | | /* Plugin reference */ |
159 | | struct flb_input_plugin in_splunk_plugin = { |
160 | | .name = "splunk", |
161 | | .description = "Input plugin for Splunk HEC payloads", |
162 | | .cb_init = in_splunk_init, |
163 | | .cb_pre_run = NULL, |
164 | | .cb_collect = NULL, |
165 | | .cb_flush_buf = NULL, |
166 | | .cb_pause = NULL, |
167 | | .cb_resume = NULL, |
168 | | .cb_exit = in_splunk_exit, |
169 | | .config_map = config_map, |
170 | | .flags = FLB_INPUT_NET_SERVER | FLB_INPUT_HTTP_SERVER | FLB_IO_OPT_TLS |
171 | | }; |