flb_plugin.c 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. /** @file flb_plugin.c
  3. * @brief This file includes all functions that act as an API to
  4. * the Fluent Bit library.
  5. */
  6. #include "flb_plugin.h"
  7. #include <lz4.h>
  8. #include "helper.h"
  9. #include "defaults.h"
  10. #include "circular_buffer.h"
  11. #include "daemon/common.h"
  12. #include "libnetdata/libnetdata.h"
  13. #include "../fluent-bit/lib/msgpack-c/include/msgpack/unpack.h"
  14. #include "../fluent-bit/lib/msgpack-c/include/msgpack/object.h"
  15. #include "../fluent-bit/lib/monkey/include/monkey/mk_core/mk_list.h"
  16. #include <dlfcn.h>
  17. #ifdef HAVE_SYSTEMD
  18. #include <systemd/sd-journal.h>
  19. #define SD_JOURNAL_SEND_DEFAULT_FIELDS \
  20. "%s_LOG_SOURCE=%s" , sd_journal_field_prefix, log_src_t_str[p_file_info->log_source], \
  21. "%s_LOG_TYPE=%s" , sd_journal_field_prefix, log_src_type_t_str[p_file_info->log_type]
  22. #endif
  23. #define LOG_REC_KEY "msg" /**< key to represent log message field in most log sources **/
  24. #define LOG_REC_KEY_SYSTEMD "MESSAGE" /**< key to represent log message field in systemd log source **/
  25. #define SYSLOG_TIMESTAMP_SIZE 16
  26. #define UNKNOWN "unknown"
  27. /* Including "../fluent-bit/include/fluent-bit/flb_macros.h" causes issues
  28. * with CI, as it requires mk_core/mk_core_info.h which is generated only
  29. * after Fluent Bit has been built. We can instead just redefined a couple
  30. * of macros here: */
  31. #define FLB_FALSE 0
  32. #define FLB_TRUE !FLB_FALSE
  33. /* For similar reasons, (re)define the following macros from "flb_lib.h": */
  34. /* Lib engine status */
  35. #define FLB_LIB_ERROR -1
  36. #define FLB_LIB_NONE 0
  37. #define FLB_LIB_OK 1
  38. #define FLB_LIB_NO_CONFIG_MAP 2
  39. /* Following structs are the same as defined in fluent-bit/flb_lib.h and
  40. * fluent-bit/flb_time.h, but need to be redefined due to use of dlsym(). */
  41. struct flb_time {
  42. struct timespec tm;
  43. };
  44. /* Library mode context data */
  45. struct flb_lib_ctx {
  46. int status;
  47. struct mk_event_loop *event_loop;
  48. struct mk_event *event_channel;
  49. struct flb_config *config;
  50. };
  51. struct flb_parser_types {
  52. char *key;
  53. int key_len;
  54. int type;
  55. };
  56. struct flb_parser {
  57. /* configuration */
  58. int type; /* parser type */
  59. char *name; /* format name */
  60. char *p_regex; /* pattern for main regular expression */
  61. int skip_empty; /* skip empty regex matches */
  62. char *time_fmt; /* time format */
  63. char *time_fmt_full; /* original given time format */
  64. char *time_key; /* field name that contains the time */
  65. int time_offset; /* fixed UTC offset */
  66. int time_keep; /* keep time field */
  67. int time_strict; /* parse time field strictly */
  68. int logfmt_no_bare_keys; /* in logfmt parsers, require all keys to have values */
  69. char *time_frac_secs; /* time format have fractional seconds ? */
  70. struct flb_parser_types *types; /* type casting */
  71. int types_len;
  72. /* Field decoders */
  73. struct mk_list *decoders;
  74. /* internal */
  75. int time_with_year; /* do time_fmt consider a year (%Y) ? */
  76. char *time_fmt_year;
  77. int time_with_tz; /* do time_fmt consider a timezone ? */
  78. struct flb_regex *regex;
  79. struct mk_list _head;
  80. };
  81. struct flb_lib_out_cb {
  82. int (*cb) (void *record, size_t size, void *data);
  83. void *data;
  84. };
  85. typedef struct flb_lib_ctx flb_ctx_t;
  86. static flb_ctx_t *(*flb_create)(void);
  87. static int (*flb_service_set)(flb_ctx_t *ctx, ...);
  88. static int (*flb_start)(flb_ctx_t *ctx);
  89. static int (*flb_stop)(flb_ctx_t *ctx);
  90. static void (*flb_destroy)(flb_ctx_t *ctx);
  91. static int (*flb_time_pop_from_msgpack)(struct flb_time *time, msgpack_unpacked *upk, msgpack_object **map);
  92. static int (*flb_lib_free)(void *data);
  93. static struct flb_parser *(*flb_parser_create)( const char *name, const char *format, const char *p_regex, int skip_empty,
  94. const char *time_fmt, const char *time_key, const char *time_offset,
  95. int time_keep, int time_strict, int logfmt_no_bare_keys,
  96. struct flb_parser_types *types, int types_len,struct mk_list *decoders,
  97. struct flb_config *config);
  98. static int (*flb_input)(flb_ctx_t *ctx, const char *input, void *data);
  99. static int (*flb_input_set)(flb_ctx_t *ctx, int ffd, ...);
  100. // static int (*flb_filter)(flb_ctx_t *ctx, const char *filter, void *data);
  101. // static int (*flb_filter_set)(flb_ctx_t *ctx, int ffd, ...);
  102. static int (*flb_output)(flb_ctx_t *ctx, const char *output, struct flb_lib_out_cb *cb);
  103. static int (*flb_output_set)(flb_ctx_t *ctx, int ffd, ...);
  104. static msgpack_unpack_return (*dl_msgpack_unpack_next)(msgpack_unpacked* result, const char* data, size_t len, size_t* off);
  105. static void (*dl_msgpack_zone_free)(msgpack_zone* zone);
  106. static int (*dl_msgpack_object_print_buffer)(char *buffer, size_t buffer_size, msgpack_object o);
  107. static flb_ctx_t *ctx = NULL;
  108. static void *flb_lib_handle = NULL;
  109. static struct flb_lib_out_cb *fwd_input_out_cb = NULL;
  110. static const char *sd_journal_field_prefix = SD_JOURNAL_FIELD_PREFIX;
  111. extern netdata_mutex_t stdout_mut;
  112. int flb_init(flb_srvc_config_t flb_srvc_config,
  113. const char *const stock_config_dir,
  114. const char *const new_sd_journal_field_prefix){
  115. int rc = 0;
  116. char *dl_error;
  117. char *flb_lib_path = strdupz_path_subpath(stock_config_dir, "/../libfluent-bit.so");
  118. if (unlikely(NULL == (flb_lib_handle = dlopen(flb_lib_path, RTLD_LAZY)))){
  119. if (NULL != (dl_error = dlerror()))
  120. collector_error("dlopen() libfluent-bit.so error: %s", dl_error);
  121. rc = -1;
  122. goto do_return;
  123. }
  124. dlerror(); /* Clear any existing error */
  125. /* Load Fluent-Bit functions from the shared library */
  126. #define load_function(FUNC_NAME){ \
  127. *(void **) (&FUNC_NAME) = dlsym(flb_lib_handle, LOGS_MANAG_STR(FUNC_NAME)); \
  128. if ((dl_error = dlerror()) != NULL) { \
  129. collector_error("dlerror loading %s: %s", LOGS_MANAG_STR(FUNC_NAME), dl_error); \
  130. rc = -1; \
  131. goto do_return; \
  132. } \
  133. }
  134. load_function(flb_create);
  135. load_function(flb_service_set);
  136. load_function(flb_start);
  137. load_function(flb_stop);
  138. load_function(flb_destroy);
  139. load_function(flb_time_pop_from_msgpack);
  140. load_function(flb_lib_free);
  141. load_function(flb_parser_create);
  142. load_function(flb_input);
  143. load_function(flb_input_set);
  144. // load_function(flb_filter);
  145. // load_function(flb_filter_set);
  146. load_function(flb_output);
  147. load_function(flb_output_set);
  148. *(void **) (&dl_msgpack_unpack_next) = dlsym(flb_lib_handle, "msgpack_unpack_next");
  149. if ((dl_error = dlerror()) != NULL) {
  150. collector_error("dlerror loading msgpack_unpack_next: %s", dl_error);
  151. rc = -1;
  152. goto do_return;
  153. }
  154. *(void **) (&dl_msgpack_zone_free) = dlsym(flb_lib_handle, "msgpack_zone_free");
  155. if ((dl_error = dlerror()) != NULL) {
  156. collector_error("dlerror loading msgpack_zone_free: %s", dl_error);
  157. rc = -1;
  158. goto do_return;
  159. }
  160. *(void **) (&dl_msgpack_object_print_buffer) = dlsym(flb_lib_handle, "msgpack_object_print_buffer");
  161. if ((dl_error = dlerror()) != NULL) {
  162. collector_error("dlerror loading msgpack_object_print_buffer: %s", dl_error);
  163. rc = -1;
  164. goto do_return;
  165. }
  166. ctx = flb_create();
  167. if (unlikely(!ctx)){
  168. rc = -1;
  169. goto do_return;
  170. }
  171. /* Global service settings */
  172. if(unlikely(flb_service_set(ctx,
  173. "Flush" , flb_srvc_config.flush,
  174. "HTTP_Listen" , flb_srvc_config.http_listen,
  175. "HTTP_Port" , flb_srvc_config.http_port,
  176. "HTTP_Server" , flb_srvc_config.http_server,
  177. "Log_File" , flb_srvc_config.log_path,
  178. "Log_Level" , flb_srvc_config.log_level,
  179. "Coro_stack_size" , flb_srvc_config.coro_stack_size,
  180. NULL) != 0 )){
  181. rc = -1;
  182. goto do_return;
  183. }
  184. if(new_sd_journal_field_prefix && *new_sd_journal_field_prefix)
  185. sd_journal_field_prefix = new_sd_journal_field_prefix;
  186. do_return:
  187. freez(flb_lib_path);
  188. if(unlikely(rc && flb_lib_handle))
  189. dlclose(flb_lib_handle);
  190. return rc;
  191. }
  192. int flb_run(void){
  193. if (likely(flb_start(ctx)) == 0) return 0;
  194. else return -1;
  195. }
  196. void flb_terminate(void){
  197. if(ctx){
  198. flb_stop(ctx);
  199. flb_destroy(ctx);
  200. ctx = NULL;
  201. }
  202. if(flb_lib_handle)
  203. dlclose(flb_lib_handle);
  204. }
  205. static void flb_complete_buff_item(struct File_info *p_file_info){
  206. Circ_buff_t *buff = p_file_info->circ_buff;
  207. m_assert(buff->in->timestamp, "buff->in->timestamp cannot be 0");
  208. m_assert(buff->in->data, "buff->in->text cannot be NULL");
  209. m_assert(*buff->in->data, "*buff->in->text cannot be 0");
  210. m_assert(buff->in->text_size, "buff->in->text_size cannot be 0");
  211. /* Replace last '\n' with '\0' to null-terminate text */
  212. buff->in->data[buff->in->text_size - 1] = '\0';
  213. /* Store status (timestamp and text_size must have already been
  214. * stored during flb_collect_logs_cb() ). */
  215. buff->in->status = CIRC_BUFF_ITEM_STATUS_UNPROCESSED;
  216. /* Load max size of compressed buffer, as calculated previously */
  217. size_t text_compressed_buff_max_size = buff->in->text_compressed_size;
  218. /* Do compression.
  219. * TODO: Validate compression option? */
  220. buff->in->text_compressed = buff->in->data + buff->in->text_size;
  221. buff->in->text_compressed_size = LZ4_compress_fast( buff->in->data,
  222. buff->in->text_compressed,
  223. buff->in->text_size,
  224. text_compressed_buff_max_size,
  225. p_file_info->compression_accel);
  226. m_assert(buff->in->text_compressed_size != 0, "Text_compressed_size should be != 0");
  227. p_file_info->parser_metrics->last_update = buff->in->timestamp / MSEC_PER_SEC;
  228. p_file_info->parser_metrics->num_lines += buff->in->num_lines;
  229. /* Perform custom log chart parsing */
  230. for(int i = 0; p_file_info->parser_cus_config[i]; i++){
  231. p_file_info->parser_metrics->parser_cus[i]->count +=
  232. search_keyword( buff->in->data, buff->in->text_size, NULL, NULL,
  233. NULL, &p_file_info->parser_cus_config[i]->regex, 0);
  234. }
  235. /* Update charts */
  236. netdata_mutex_lock(&stdout_mut);
  237. p_file_info->chart_meta->update(p_file_info);
  238. fflush(stdout);
  239. netdata_mutex_unlock(&stdout_mut);
  240. circ_buff_insert(buff);
  241. uv_timer_again(&p_file_info->flb_tmp_buff_cpy_timer);
  242. }
  243. void flb_complete_item_timer_timeout_cb(uv_timer_t *handle) {
  244. struct File_info *p_file_info = handle->data;
  245. Circ_buff_t *buff = p_file_info->circ_buff;
  246. uv_mutex_lock(&p_file_info->flb_tmp_buff_mut);
  247. if(!buff->in->data || !*buff->in->data || !buff->in->text_size){
  248. p_file_info->parser_metrics->last_update = now_realtime_sec();
  249. netdata_mutex_lock(&stdout_mut);
  250. p_file_info->chart_meta->update(p_file_info);
  251. fflush(stdout);
  252. netdata_mutex_unlock(&stdout_mut);
  253. uv_mutex_unlock(&p_file_info->flb_tmp_buff_mut);
  254. return;
  255. }
  256. flb_complete_buff_item(p_file_info);
  257. uv_mutex_unlock(&p_file_info->flb_tmp_buff_mut);
  258. }
  259. static int flb_collect_logs_cb(void *record, size_t size, void *data){
  260. /* "data" is NULL for Forward-type sources and non-NULL for local sources */
  261. struct File_info *p_file_info = (struct File_info *) data;
  262. Circ_buff_t *buff = NULL;
  263. msgpack_unpacked result;
  264. size_t off = 0;
  265. struct flb_time tmp_time;
  266. msgpack_object *x;
  267. char timestamp_str[TIMESTAMP_MS_STR_SIZE] = "";
  268. msec_t timestamp = 0;
  269. struct resizable_key_val_arr {
  270. char **key;
  271. char **val;
  272. size_t *key_size;
  273. size_t *val_size;
  274. int size, max_size;
  275. };
  276. /* FLB_WEB_LOG case */
  277. Log_line_parsed_t line_parsed = (Log_line_parsed_t) {0};
  278. /* FLB_WEB_LOG case end */
  279. /* FLB_KMSG case */
  280. static int skip_kmsg_log_buffering = 1;
  281. int kmsg_sever = -1; // -1 equals invalid
  282. /* FLB_KMSG case end */
  283. /* FLB_SYSTEMD or FLB_SYSLOG case */
  284. char syslog_prival[4] = "";
  285. size_t syslog_prival_size = 0;
  286. char syslog_severity[2] = "";
  287. char syslog_facility[3] = "";
  288. char *syslog_timestamp = NULL;
  289. size_t syslog_timestamp_size = 0;
  290. char *hostname = NULL;
  291. size_t hostname_size = 0;
  292. char *syslog_identifier = NULL;
  293. size_t syslog_identifier_size = 0;
  294. char *pid = NULL;
  295. size_t pid_size = 0;
  296. char *message = NULL;
  297. size_t message_size = 0;
  298. /* FLB_SYSTEMD or FLB_SYSLOG case end */
  299. /* FLB_DOCKER_EV case */
  300. long docker_ev_time = 0;
  301. long docker_ev_timeNano = 0;
  302. char *docker_ev_type = NULL;
  303. size_t docker_ev_type_size = 0;
  304. char *docker_ev_action = NULL;
  305. size_t docker_ev_action_size = 0;
  306. char *docker_ev_id = NULL;
  307. size_t docker_ev_id_size = 0;
  308. static struct resizable_key_val_arr docker_ev_attr = {0};
  309. docker_ev_attr.size = 0;
  310. /* FLB_DOCKER_EV case end */
  311. /* FLB_MQTT case */
  312. char *mqtt_topic = NULL;
  313. size_t mqtt_topic_size = 0;
  314. static char *mqtt_message = NULL;
  315. static size_t mqtt_message_size_max = 0;
  316. /* FLB_MQTT case end */
  317. size_t new_tmp_text_size = 0;
  318. msgpack_unpacked_init(&result);
  319. int iter = 0;
  320. while (dl_msgpack_unpack_next(&result, record, size, &off) == MSGPACK_UNPACK_SUCCESS) {
  321. iter++;
  322. m_assert(iter == 1, "We do not expect more than one loop iteration here");
  323. flb_time_pop_from_msgpack(&tmp_time, &result, &x);
  324. if(likely(x->type == MSGPACK_OBJECT_MAP && x->via.map.size != 0)){
  325. msgpack_object_kv* p = x->via.map.ptr;
  326. msgpack_object_kv* pend = x->via.map.ptr + x->via.map.size;
  327. /* ================================================================
  328. * If p_file_info == NULL, it means it is a "Forward" source, so
  329. * we need to search for the associated p_file_info. This code can
  330. * be optimized further.
  331. * ============================================================== */
  332. if(p_file_info == NULL){
  333. do{
  334. if(!strncmp(p->key.via.str.ptr, "stream guid", (size_t) p->key.via.str.size)){
  335. char *stream_guid = (char *) p->val.via.str.ptr;
  336. size_t stream_guid_size = p->val.via.str.size;
  337. debug_log( "stream guid:%.*s", (int) stream_guid_size, stream_guid);
  338. for (int i = 0; i < p_file_infos_arr->count; i++) {
  339. if(!strncmp(p_file_infos_arr->data[i]->stream_guid, stream_guid, stream_guid_size)){
  340. p_file_info = p_file_infos_arr->data[i];
  341. // debug_log( "p_file_info match found: %s type[%s]",
  342. // p_file_info->stream_guid,
  343. // log_src_type_t_str[p_file_info->log_type]);
  344. break;
  345. }
  346. }
  347. }
  348. ++p;
  349. // continue;
  350. } while(p < pend);
  351. }
  352. if(unlikely(p_file_info == NULL))
  353. goto skip_collect_and_drop_logs;
  354. uv_mutex_lock(&p_file_info->flb_tmp_buff_mut);
  355. buff = p_file_info->circ_buff;
  356. p = x->via.map.ptr;
  357. pend = x->via.map.ptr + x->via.map.size;
  358. do{
  359. switch(p_file_info->log_type){
  360. case FLB_TAIL:
  361. case FLB_WEB_LOG:
  362. case FLB_SERIAL:
  363. {
  364. if( !strncmp(p->key.via.str.ptr, LOG_REC_KEY, (size_t) p->key.via.str.size) ||
  365. /* The following line is in case we collect systemd logs
  366. * (tagged as "MESSAGE") or docker_events (tagged as
  367. * "message") via a "Forward" source to an FLB_TAIL parent. */
  368. !strncasecmp(p->key.via.str.ptr, LOG_REC_KEY_SYSTEMD, (size_t) p->key.via.str.size)){
  369. message = (char *) p->val.via.str.ptr;
  370. message_size = p->val.via.str.size;
  371. if(p_file_info->log_type == FLB_WEB_LOG){
  372. parse_web_log_line( (Web_log_parser_config_t *) p_file_info->parser_config->gen_config,
  373. message, message_size, &line_parsed);
  374. if(likely(p_file_info->use_log_timestamp)){
  375. timestamp = line_parsed.timestamp * MSEC_PER_SEC; // convert to msec from sec
  376. { /* ------------------ FIXME ------------------------
  377. * Temporary kludge so that metrics don't break when
  378. * a new record has timestamp before the current one.
  379. */
  380. static msec_t previous_timestamp = 0;
  381. if((((long long) timestamp - (long long) previous_timestamp) < 0))
  382. timestamp = previous_timestamp;
  383. previous_timestamp = timestamp;
  384. }
  385. }
  386. }
  387. new_tmp_text_size = message_size + 1; // +1 for '\n'
  388. m_assert(message_size, "message_size is 0");
  389. m_assert(message, "message is NULL");
  390. }
  391. break;
  392. }
  393. case FLB_KMSG:
  394. {
  395. if(unlikely(skip_kmsg_log_buffering)){
  396. static time_t netdata_start_time = 0;
  397. if (!netdata_start_time) netdata_start_time = now_boottime_sec();
  398. if(now_boottime_sec() - netdata_start_time < KERNEL_LOGS_COLLECT_INIT_WAIT)
  399. goto skip_collect_and_drop_logs;
  400. else skip_kmsg_log_buffering = 0;
  401. }
  402. /* NOTE/WARNING:
  403. * kmsg timestamps are tricky. The timestamp will be
  404. * *wrong** if the system has gone into hibernation since
  405. * last boot and "p_file_info->use_log_timestamp" is set.
  406. * Even if "p_file_info->use_log_timestamp" is NOT set, we
  407. * need to use now_realtime_msec() as Fluent Bit timestamp
  408. * will also be wrong. */
  409. if( !strncmp(p->key.via.str.ptr, "sec", (size_t) p->key.via.str.size)){
  410. if(p_file_info->use_log_timestamp){
  411. timestamp += (now_realtime_sec() - now_boottime_sec() + p->val.via.i64) * MSEC_PER_SEC;
  412. }
  413. else if(!timestamp)
  414. timestamp = now_realtime_msec();
  415. }
  416. else if(!strncmp(p->key.via.str.ptr, "usec", (size_t) p->key.via.str.size) &&
  417. p_file_info->use_log_timestamp){
  418. timestamp += p->val.via.i64 / USEC_PER_MS;
  419. }
  420. else if(!strncmp(p->key.via.str.ptr, LOG_REC_KEY, (size_t) p->key.via.str.size)){
  421. message = (char *) p->val.via.str.ptr;
  422. message_size = p->val.via.str.size;
  423. m_assert(message, "message is NULL");
  424. m_assert(message_size, "message_size is 0");
  425. new_tmp_text_size += message_size + 1; // +1 for '\n'
  426. }
  427. else if(!strncmp(p->key.via.str.ptr, "priority", (size_t) p->key.via.str.size)){
  428. kmsg_sever = (int) p->val.via.u64;
  429. }
  430. break;
  431. }
  432. case FLB_SYSTEMD:
  433. case FLB_SYSLOG:
  434. {
  435. if( p_file_info->use_log_timestamp && !strncmp( p->key.via.str.ptr,
  436. "SOURCE_REALTIME_TIMESTAMP",
  437. (size_t) p->key.via.str.size)){
  438. m_assert(p->val.via.str.size - 3 == TIMESTAMP_MS_STR_SIZE - 1,
  439. "p->val.via.str.size - 3 != TIMESTAMP_MS_STR_SIZE");
  440. strncpyz(timestamp_str, p->val.via.str.ptr, (size_t) p->val.via.str.size);
  441. char *endptr = NULL;
  442. timestamp = str2ll(timestamp_str, &endptr);
  443. timestamp = *endptr ? 0 : timestamp / USEC_PER_MS;
  444. }
  445. else if(!strncmp(p->key.via.str.ptr, "PRIVAL", (size_t) p->key.via.str.size)){
  446. m_assert(p->val.via.str.size <= 3, "p->val.via.str.size > 3");
  447. strncpyz(syslog_prival, p->val.via.str.ptr, (size_t) p->val.via.str.size);
  448. syslog_prival_size = (size_t) p->val.via.str.size;
  449. m_assert(syslog_prival, "syslog_prival is NULL");
  450. }
  451. else if(!strncmp(p->key.via.str.ptr, "PRIORITY", (size_t) p->key.via.str.size)){
  452. m_assert(p->val.via.str.size <= 1, "p->val.via.str.size > 1");
  453. strncpyz(syslog_severity, p->val.via.str.ptr, (size_t) p->val.via.str.size);
  454. m_assert(syslog_severity, "syslog_severity is NULL");
  455. }
  456. else if(!strncmp(p->key.via.str.ptr, "SYSLOG_FACILITY", (size_t) p->key.via.str.size)){
  457. m_assert(p->val.via.str.size <= 2, "p->val.via.str.size > 2");
  458. strncpyz(syslog_facility, p->val.via.str.ptr, (size_t) p->val.via.str.size);
  459. m_assert(syslog_facility, "syslog_facility is NULL");
  460. }
  461. else if(!strncmp(p->key.via.str.ptr, "SYSLOG_TIMESTAMP", (size_t) p->key.via.str.size)){
  462. syslog_timestamp = (char *) p->val.via.str.ptr;
  463. syslog_timestamp_size = p->val.via.str.size;
  464. m_assert(syslog_timestamp, "syslog_timestamp is NULL");
  465. m_assert(syslog_timestamp_size, "syslog_timestamp_size is 0");
  466. new_tmp_text_size += syslog_timestamp_size;
  467. }
  468. else if(!strncmp(p->key.via.str.ptr, "HOSTNAME", (size_t) p->key.via.str.size)){
  469. hostname = (char *) p->val.via.str.ptr;
  470. hostname_size = p->val.via.str.size;
  471. m_assert(hostname, "hostname is NULL");
  472. m_assert(hostname_size, "hostname_size is 0");
  473. new_tmp_text_size += hostname_size + 1; // +1 for ' ' char
  474. }
  475. else if(!strncmp(p->key.via.str.ptr, "SYSLOG_IDENTIFIER", (size_t) p->key.via.str.size)){
  476. syslog_identifier = (char *) p->val.via.str.ptr;
  477. syslog_identifier_size = p->val.via.str.size;
  478. new_tmp_text_size += syslog_identifier_size;
  479. }
  480. else if(!strncmp(p->key.via.str.ptr, "PID", (size_t) p->key.via.str.size)){
  481. pid = (char *) p->val.via.str.ptr;
  482. pid_size = p->val.via.str.size;
  483. new_tmp_text_size += pid_size;
  484. }
  485. else if(!strncmp(p->key.via.str.ptr, LOG_REC_KEY_SYSTEMD, (size_t) p->key.via.str.size)){
  486. message = (char *) p->val.via.str.ptr;
  487. message_size = p->val.via.str.size;
  488. m_assert(message, "message is NULL");
  489. m_assert(message_size, "message_size is 0");
  490. new_tmp_text_size += message_size;
  491. }
  492. break;
  493. }
  494. case FLB_DOCKER_EV:
  495. {
  496. if(!strncmp(p->key.via.str.ptr, "time", (size_t) p->key.via.str.size)){
  497. docker_ev_time = p->val.via.i64;
  498. m_assert(docker_ev_time, "docker_ev_time is 0");
  499. }
  500. else if(!strncmp(p->key.via.str.ptr, "timeNano", (size_t) p->key.via.str.size)){
  501. docker_ev_timeNano = p->val.via.i64;
  502. m_assert(docker_ev_timeNano, "docker_ev_timeNano is 0");
  503. if(likely(p_file_info->use_log_timestamp))
  504. timestamp = docker_ev_timeNano / NSEC_PER_MSEC;
  505. }
  506. else if(!strncmp(p->key.via.str.ptr, "Type", (size_t) p->key.via.str.size)){
  507. docker_ev_type = (char *) p->val.via.str.ptr;
  508. docker_ev_type_size = p->val.via.str.size;
  509. m_assert(docker_ev_type, "docker_ev_type is NULL");
  510. m_assert(docker_ev_type_size, "docker_ev_type_size is 0");
  511. // debug_log("docker_ev_type: %.*s", docker_ev_type_size, docker_ev_type);
  512. }
  513. else if(!strncmp(p->key.via.str.ptr, "Action", (size_t) p->key.via.str.size)){
  514. docker_ev_action = (char *) p->val.via.str.ptr;
  515. docker_ev_action_size = p->val.via.str.size;
  516. m_assert(docker_ev_action, "docker_ev_action is NULL");
  517. m_assert(docker_ev_action_size, "docker_ev_action_size is 0");
  518. // debug_log("docker_ev_action: %.*s", docker_ev_action_size, docker_ev_action);
  519. }
  520. else if(!strncmp(p->key.via.str.ptr, "id", (size_t) p->key.via.str.size)){
  521. docker_ev_id = (char *) p->val.via.str.ptr;
  522. docker_ev_id_size = p->val.via.str.size;
  523. m_assert(docker_ev_id, "docker_ev_id is NULL");
  524. m_assert(docker_ev_id_size, "docker_ev_id_size is 0");
  525. // debug_log("docker_ev_id: %.*s", docker_ev_id_size, docker_ev_id);
  526. }
  527. else if(!strncmp(p->key.via.str.ptr, "Actor", (size_t) p->key.via.str.size)){
  528. // debug_log( "msg key:[%.*s]val:[%.*s]", (int) p->key.via.str.size,
  529. // p->key.via.str.ptr,
  530. // (int) p->val.via.str.size,
  531. // p->val.via.str.ptr);
  532. if(likely(p->val.type == MSGPACK_OBJECT_MAP && p->val.via.map.size != 0)){
  533. msgpack_object_kv* ac = p->val.via.map.ptr;
  534. msgpack_object_kv* const ac_pend= p->val.via.map.ptr + p->val.via.map.size;
  535. do{
  536. if(!strncmp(ac->key.via.str.ptr, "ID", (size_t) ac->key.via.str.size)){
  537. docker_ev_id = (char *) ac->val.via.str.ptr;
  538. docker_ev_id_size = ac->val.via.str.size;
  539. m_assert(docker_ev_id, "docker_ev_id is NULL");
  540. m_assert(docker_ev_id_size, "docker_ev_id_size is 0");
  541. // debug_log("docker_ev_id: %.*s", docker_ev_id_size, docker_ev_id);
  542. }
  543. else if(!strncmp(ac->key.via.str.ptr, "Attributes", (size_t) ac->key.via.str.size)){
  544. if(likely(ac->val.type == MSGPACK_OBJECT_MAP && ac->val.via.map.size != 0)){
  545. msgpack_object_kv* att = ac->val.via.map.ptr;
  546. msgpack_object_kv* const att_pend = ac->val.via.map.ptr + ac->val.via.map.size;
  547. do{
  548. if(unlikely(++docker_ev_attr.size > docker_ev_attr.max_size)){
  549. docker_ev_attr.max_size = docker_ev_attr.size;
  550. docker_ev_attr.key = reallocz(docker_ev_attr.key,
  551. docker_ev_attr.max_size * sizeof(char *));
  552. docker_ev_attr.val = reallocz(docker_ev_attr.val,
  553. docker_ev_attr.max_size * sizeof(char *));
  554. docker_ev_attr.key_size = reallocz(docker_ev_attr.key_size,
  555. docker_ev_attr.max_size * sizeof(size_t));
  556. docker_ev_attr.val_size = reallocz(docker_ev_attr.val_size,
  557. docker_ev_attr.max_size * sizeof(size_t));
  558. }
  559. docker_ev_attr.key[docker_ev_attr.size - 1] = (char *) att->key.via.str.ptr;
  560. docker_ev_attr.val[docker_ev_attr.size - 1] = (char *) att->val.via.str.ptr;
  561. docker_ev_attr.key_size[docker_ev_attr.size - 1] = (size_t) att->key.via.str.size;
  562. docker_ev_attr.val_size[docker_ev_attr.size - 1] = (size_t) att->val.via.str.size;
  563. att++;
  564. continue;
  565. } while(att < att_pend);
  566. }
  567. }
  568. ac++;
  569. continue;
  570. } while(ac < ac_pend);
  571. }
  572. }
  573. break;
  574. }
  575. case FLB_MQTT:
  576. {
  577. if(!strncmp(p->key.via.str.ptr, "topic", (size_t) p->key.via.str.size)){
  578. mqtt_topic = (char *) p->val.via.str.ptr;
  579. mqtt_topic_size = (size_t) p->val.via.str.size;
  580. while(0 == (message_size = dl_msgpack_object_print_buffer(mqtt_message, mqtt_message_size_max, *x)))
  581. mqtt_message = reallocz(mqtt_message, (mqtt_message_size_max += 10));
  582. new_tmp_text_size = message_size + 1; // +1 for '\n'
  583. m_assert(message_size, "message_size is 0");
  584. m_assert(mqtt_message, "mqtt_message is NULL");
  585. break; // watch out, MQTT requires a 'break' here, as we parse the entire 'x' msgpack_object
  586. }
  587. else m_assert(0, "missing mqtt topic");
  588. break;
  589. }
  590. default:
  591. break;
  592. }
  593. } while(++p < pend);
  594. }
  595. }
  596. /* If no log timestamp was found, use Fluent Bit collection timestamp. */
  597. if(timestamp == 0)
  598. timestamp = (msec_t) tmp_time.tm.tv_sec * MSEC_PER_SEC + (msec_t) tmp_time.tm.tv_nsec / (NSEC_PER_MSEC);
  599. m_assert(TEST_MS_TIMESTAMP_VALID(timestamp), "timestamp is invalid");
  600. /* If input buffer timestamp is not set, now is the time to set it,
  601. * else just be done with the previous buffer */
  602. if(unlikely(buff->in->timestamp == 0)) buff->in->timestamp = timestamp / 1000 * 1000; // rounding down
  603. else if((timestamp - buff->in->timestamp) >= MSEC_PER_SEC) {
  604. flb_complete_buff_item(p_file_info);
  605. buff->in->timestamp = timestamp / 1000 * 1000; // rounding down
  606. }
  607. m_assert(TEST_MS_TIMESTAMP_VALID(buff->in->timestamp), "buff->in->timestamp is invalid");
  608. new_tmp_text_size += buff->in->text_size;
  609. /* ========================================================================
  610. * Step 2: Extract metrics and reconstruct log record
  611. * ====================================================================== */
  612. /* Parse number of log lines - common for all log source types */
  613. buff->in->num_lines++;
  614. /* FLB_TAIL, FLB_WEB_LOG and FLB_SERIAL case */
  615. if( p_file_info->log_type == FLB_TAIL ||
  616. p_file_info->log_type == FLB_WEB_LOG ||
  617. p_file_info->log_type == FLB_SERIAL){
  618. if(p_file_info->log_type == FLB_WEB_LOG)
  619. extract_web_log_metrics(p_file_info->parser_config, &line_parsed,
  620. p_file_info->parser_metrics->web_log);
  621. // TODO: Fix: Metrics will still be collected if circ_buff_prepare_write() returns 0.
  622. if(unlikely(!circ_buff_prepare_write(buff, new_tmp_text_size)))
  623. goto skip_collect_and_drop_logs;
  624. size_t tmp_item_off = buff->in->text_size;
  625. memcpy_iscntrl_fix(&buff->in->data[tmp_item_off], message, message_size);
  626. tmp_item_off += message_size;
  627. buff->in->data[tmp_item_off++] = '\n';
  628. m_assert(tmp_item_off == new_tmp_text_size, "tmp_item_off should be == new_tmp_text_size");
  629. buff->in->text_size = new_tmp_text_size;
  630. #ifdef HAVE_SYSTEMD
  631. if(p_file_info->do_sd_journal_send){
  632. if(p_file_info->log_type == FLB_WEB_LOG){
  633. sd_journal_send(
  634. SD_JOURNAL_SEND_DEFAULT_FIELDS,
  635. *line_parsed.vhost ? "%sWEB_LOG_VHOST=%s" : "_%s=%s", sd_journal_field_prefix, line_parsed.vhost,
  636. line_parsed.port ? "%sWEB_LOG_PORT=%d" : "_%s=%d", sd_journal_field_prefix, line_parsed.port,
  637. *line_parsed.req_scheme ? "%sWEB_LOG_REQ_SCHEME=%s" : "_%s=%s", sd_journal_field_prefix, line_parsed.req_scheme,
  638. *line_parsed.req_client ? "%sWEB_LOG_REQ_CLIENT=%s" : "_%s=%s", sd_journal_field_prefix, line_parsed.req_client,
  639. "%sWEB_LOG_REQ_METHOD=%s" , sd_journal_field_prefix, line_parsed.req_method,
  640. *line_parsed.req_URL ? "%sWEB_LOG_REQ_URL=%s" : "_%s=%s", sd_journal_field_prefix, line_parsed.req_URL,
  641. *line_parsed.req_proto ? "%sWEB_LOG_REQ_PROTO=%s" : "_%s=%s", sd_journal_field_prefix, line_parsed.req_proto,
  642. line_parsed.req_size ? "%sWEB_LOG_REQ_SIZE=%d" : "_%s=%d", sd_journal_field_prefix, line_parsed.req_size,
  643. line_parsed.req_proc_time ? "%sWEB_LOG_REC_PROC_TIME=%d" : "_%s=%d", sd_journal_field_prefix, line_parsed.req_proc_time,
  644. line_parsed.resp_code ? "%sWEB_LOG_RESP_CODE=%d" : "_%s=%d", sd_journal_field_prefix ,line_parsed.resp_code,
  645. line_parsed.ups_resp_time ? "%sWEB_LOG_UPS_RESP_TIME=%d" : "_%s=%d", sd_journal_field_prefix ,line_parsed.ups_resp_time,
  646. *line_parsed.ssl_proto ? "%sWEB_LOG_SSL_PROTO=%s" : "_%s=%s", sd_journal_field_prefix ,line_parsed.ssl_proto,
  647. *line_parsed.ssl_cipher ? "%sWEB_LOB_SSL_CIPHER=%s" : "_%s=%s", sd_journal_field_prefix ,line_parsed.ssl_cipher,
  648. LOG_REC_KEY_SYSTEMD "=%.*s", (int) message_size, message,
  649. NULL
  650. );
  651. }
  652. else if(p_file_info->log_type == FLB_SERIAL){
  653. Flb_serial_config_t *serial_config = (Flb_serial_config_t *) p_file_info->flb_config;
  654. sd_journal_send(
  655. SD_JOURNAL_SEND_DEFAULT_FIELDS,
  656. serial_config->bitrate && *serial_config->bitrate ?
  657. "%sSERIAL_BITRATE=%s" : "_%s=%s", sd_journal_field_prefix, serial_config->bitrate,
  658. LOG_REC_KEY_SYSTEMD "=%.*s", (int) message_size, message,
  659. NULL
  660. );
  661. }
  662. else{
  663. sd_journal_send(
  664. SD_JOURNAL_SEND_DEFAULT_FIELDS,
  665. LOG_REC_KEY_SYSTEMD "=%.*s", (int) message_size, message,
  666. NULL
  667. );
  668. }
  669. }
  670. #endif
  671. } /* FLB_TAIL, FLB_WEB_LOG and FLB_SERIAL case end */
  672. /* FLB_KMSG case */
  673. else if(p_file_info->log_type == FLB_KMSG){
  674. char *c;
  675. // see https://www.kernel.org/doc/Documentation/ABI/testing/dev-kmsg
  676. if((c = memchr(message, '\n', message_size))){
  677. const char subsys_str[] = "SUBSYSTEM=",
  678. device_str[] = "DEVICE=";
  679. const size_t subsys_str_len = sizeof(subsys_str) - 1,
  680. device_str_len = sizeof(device_str) - 1;
  681. size_t bytes_remain = message_size - (c - message);
  682. /* Extract machine-readable info for charts, such as subsystem and device. */
  683. while(bytes_remain){
  684. size_t sz = 0;
  685. while(--bytes_remain && c[++sz] != '\n');
  686. if(bytes_remain) --sz;
  687. *(c++) = '\\';
  688. *(c++) = 'n';
  689. sz--;
  690. DICTIONARY *dict = NULL;
  691. char *str = NULL;
  692. size_t str_len = 0;
  693. if(!strncmp(c, subsys_str, subsys_str_len)){
  694. dict = p_file_info->parser_metrics->kernel->subsystem;
  695. str = &c[subsys_str_len];
  696. str_len = (sz - subsys_str_len);
  697. }
  698. else if (!strncmp(c, device_str, device_str_len)){
  699. dict = p_file_info->parser_metrics->kernel->device;
  700. str = &c[device_str_len];
  701. str_len = (sz - device_str_len);
  702. }
  703. if(likely(str)){
  704. char *const key = mallocz(str_len + 1);
  705. memcpy(key, str, str_len);
  706. key[str_len] = '\0';
  707. metrics_dict_item_t item = {.dim_initialized = false, .num_new = 1};
  708. dictionary_set_advanced(dict, key, str_len + 1, &item, sizeof(item), NULL);
  709. }
  710. c = &c[sz];
  711. }
  712. }
  713. if(likely(kmsg_sever >= 0))
  714. p_file_info->parser_metrics->kernel->sever[kmsg_sever]++;
  715. // TODO: Fix: Metrics will still be collected if circ_buff_prepare_write() returns 0.
  716. if(unlikely(!circ_buff_prepare_write(buff, new_tmp_text_size)))
  717. goto skip_collect_and_drop_logs;
  718. size_t tmp_item_off = buff->in->text_size;
  719. memcpy_iscntrl_fix(&buff->in->data[tmp_item_off], message, message_size);
  720. tmp_item_off += message_size;
  721. buff->in->data[tmp_item_off++] = '\n';
  722. m_assert(tmp_item_off == new_tmp_text_size, "tmp_item_off should be == new_tmp_text_size");
  723. buff->in->text_size = new_tmp_text_size;
  724. } /* FLB_KMSG case end */
  725. /* FLB_SYSTEMD or FLB_SYSLOG case */
  726. else if(p_file_info->log_type == FLB_SYSTEMD ||
  727. p_file_info->log_type == FLB_SYSLOG){
  728. int syslog_prival_d = SYSLOG_PRIOR_ARR_SIZE - 1; // Initialise to 'unknown'
  729. int syslog_severity_d = SYSLOG_SEVER_ARR_SIZE - 1; // Initialise to 'unknown'
  730. int syslog_facility_d = SYSLOG_FACIL_ARR_SIZE - 1; // Initialise to 'unknown'
  731. /* FLB_SYSTEMD case has syslog_severity and syslog_facility values that
  732. * are used to calculate syslog_prival from. FLB_SYSLOG is the opposite
  733. * case, as it has a syslog_prival value that is used to calculate
  734. * syslog_severity and syslog_facility from. */
  735. if(p_file_info->log_type == FLB_SYSTEMD){
  736. /* Parse syslog_severity char* field into int and extract metrics.
  737. * syslog_severity_s will consist of 1 char (plus '\0'),
  738. * see https://datatracker.ietf.org/doc/html/rfc5424#section-6.2.1 */
  739. if(likely(syslog_severity[0])){
  740. if(likely(str2int(&syslog_severity_d, syslog_severity, 10) == STR2XX_SUCCESS)){
  741. p_file_info->parser_metrics->systemd->sever[syslog_severity_d]++;
  742. } // else parsing errors ++ ??
  743. } else p_file_info->parser_metrics->systemd->sever[SYSLOG_SEVER_ARR_SIZE - 1]++; // 'unknown'
  744. /* Parse syslog_facility char* field into int and extract metrics.
  745. * syslog_facility_s will consist of up to 2 chars (plus '\0'),
  746. * see https://datatracker.ietf.org/doc/html/rfc5424#section-6.2.1 */
  747. if(likely(syslog_facility[0])){
  748. if(likely(str2int(&syslog_facility_d, syslog_facility, 10) == STR2XX_SUCCESS)){
  749. p_file_info->parser_metrics->systemd->facil[syslog_facility_d]++;
  750. } // else parsing errors ++ ??
  751. } else p_file_info->parser_metrics->systemd->facil[SYSLOG_FACIL_ARR_SIZE - 1]++; // 'unknown'
  752. if(likely(syslog_severity[0] && syslog_facility[0])){
  753. /* Definition of syslog priority value == facility * 8 + severity */
  754. syslog_prival_d = syslog_facility_d * 8 + syslog_severity_d;
  755. syslog_prival_size = snprintfz(syslog_prival, 4, "%d", syslog_prival_d);
  756. m_assert(syslog_prival_size < 4 && syslog_prival_size > 0, "error with snprintf()");
  757. new_tmp_text_size += syslog_prival_size + 2; // +2 for '<' and '>'
  758. p_file_info->parser_metrics->systemd->prior[syslog_prival_d]++;
  759. } else {
  760. new_tmp_text_size += 3; // +3 for "<->" string
  761. p_file_info->parser_metrics->systemd->prior[SYSLOG_PRIOR_ARR_SIZE - 1]++; // 'unknown'
  762. }
  763. } else if(p_file_info->log_type == FLB_SYSLOG){
  764. if(likely(syslog_prival[0])){
  765. if(likely(str2int(&syslog_prival_d, syslog_prival, 10) == STR2XX_SUCCESS)){
  766. syslog_severity_d = syslog_prival_d % 8;
  767. syslog_facility_d = syslog_prival_d / 8;
  768. p_file_info->parser_metrics->systemd->prior[syslog_prival_d]++;
  769. p_file_info->parser_metrics->systemd->sever[syslog_severity_d]++;
  770. p_file_info->parser_metrics->systemd->facil[syslog_facility_d]++;
  771. new_tmp_text_size += syslog_prival_size + 2; // +2 for '<' and '>'
  772. } // else parsing errors ++ ??
  773. } else {
  774. new_tmp_text_size += 3; // +3 for "<->" string
  775. p_file_info->parser_metrics->systemd->prior[SYSLOG_PRIOR_ARR_SIZE - 1]++; // 'unknown'
  776. p_file_info->parser_metrics->systemd->sever[SYSLOG_SEVER_ARR_SIZE - 1]++; // 'unknown'
  777. p_file_info->parser_metrics->systemd->facil[SYSLOG_FACIL_ARR_SIZE - 1]++; // 'unknown'
  778. }
  779. } else m_assert(0, "shoudn't get here");
  780. char syslog_time_from_flb_time[25]; // 25 just to be on the safe side, but 16 + 1 chars bytes needed only.
  781. if(unlikely(!syslog_timestamp)){
  782. const time_t ts = tmp_time.tm.tv_sec;
  783. struct tm *const tm = localtime(&ts);
  784. strftime(syslog_time_from_flb_time, sizeof(syslog_time_from_flb_time), "%b %d %H:%M:%S ", tm);
  785. new_tmp_text_size += SYSLOG_TIMESTAMP_SIZE;
  786. }
  787. if(unlikely(!syslog_identifier)) new_tmp_text_size += sizeof(UNKNOWN) - 1;
  788. if(unlikely(!pid)) new_tmp_text_size += sizeof(UNKNOWN) - 1;
  789. new_tmp_text_size += 5; // +5 for '[', ']', ':' and ' ' characters around and after pid and '\n' at the end
  790. /* Metrics extracted, now prepare circular buffer for write */
  791. // TODO: Fix: Metrics will still be collected if circ_buff_prepare_write() returns 0.
  792. if(unlikely(!circ_buff_prepare_write(buff, new_tmp_text_size)))
  793. goto skip_collect_and_drop_logs;
  794. size_t tmp_item_off = buff->in->text_size;
  795. buff->in->data[tmp_item_off++] = '<';
  796. if(likely(syslog_prival[0])){
  797. memcpy(&buff->in->data[tmp_item_off], syslog_prival, syslog_prival_size);
  798. m_assert(syslog_prival_size, "syslog_prival_size cannot be 0");
  799. tmp_item_off += syslog_prival_size;
  800. } else buff->in->data[tmp_item_off++] = '-';
  801. buff->in->data[tmp_item_off++] = '>';
  802. if(likely(syslog_timestamp)){
  803. memcpy(&buff->in->data[tmp_item_off], syslog_timestamp, syslog_timestamp_size);
  804. // FLB_SYSLOG doesn't add space, but FLB_SYSTEMD does:
  805. // if(buff->in->data[tmp_item_off] != ' ') buff->in->data[tmp_item_off++] = ' ';
  806. tmp_item_off += syslog_timestamp_size;
  807. } else {
  808. memcpy(&buff->in->data[tmp_item_off], syslog_time_from_flb_time, SYSLOG_TIMESTAMP_SIZE);
  809. tmp_item_off += SYSLOG_TIMESTAMP_SIZE;
  810. }
  811. if(likely(hostname)){
  812. memcpy(&buff->in->data[tmp_item_off], hostname, hostname_size);
  813. tmp_item_off += hostname_size;
  814. buff->in->data[tmp_item_off++] = ' ';
  815. }
  816. if(likely(syslog_identifier)){
  817. memcpy(&buff->in->data[tmp_item_off], syslog_identifier, syslog_identifier_size);
  818. tmp_item_off += syslog_identifier_size;
  819. } else {
  820. memcpy(&buff->in->data[tmp_item_off], UNKNOWN, sizeof(UNKNOWN) - 1);
  821. tmp_item_off += sizeof(UNKNOWN) - 1;
  822. }
  823. buff->in->data[tmp_item_off++] = '[';
  824. if(likely(pid)){
  825. memcpy(&buff->in->data[tmp_item_off], pid, pid_size);
  826. tmp_item_off += pid_size;
  827. } else {
  828. memcpy(&buff->in->data[tmp_item_off], UNKNOWN, sizeof(UNKNOWN) - 1);
  829. tmp_item_off += sizeof(UNKNOWN) - 1;
  830. }
  831. buff->in->data[tmp_item_off++] = ']';
  832. buff->in->data[tmp_item_off++] = ':';
  833. buff->in->data[tmp_item_off++] = ' ';
  834. if(likely(message)){
  835. memcpy_iscntrl_fix(&buff->in->data[tmp_item_off], message, message_size);
  836. tmp_item_off += message_size;
  837. }
  838. buff->in->data[tmp_item_off++] = '\n';
  839. m_assert(tmp_item_off == new_tmp_text_size, "tmp_item_off should be == new_tmp_text_size");
  840. buff->in->text_size = new_tmp_text_size;
  841. } /* FLB_SYSTEMD or FLB_SYSLOG case end */
  842. /* FLB_DOCKER_EV case */
  843. else if(p_file_info->log_type == FLB_DOCKER_EV){
  844. const size_t docker_ev_datetime_size = sizeof "2022-08-26T15:33:20.802840200+0000" /* example datetime */;
  845. char docker_ev_datetime[docker_ev_datetime_size];
  846. docker_ev_datetime[0] = 0;
  847. if(likely(docker_ev_time && docker_ev_timeNano)){
  848. struct timespec ts;
  849. ts.tv_sec = docker_ev_time;
  850. if(unlikely(0 == strftime( docker_ev_datetime, docker_ev_datetime_size,
  851. "%Y-%m-%dT%H:%M:%S.000000000%z", localtime(&ts.tv_sec)))) { /* TODO: do what if error? */};
  852. const size_t docker_ev_timeNano_s_size = sizeof "802840200";
  853. char docker_ev_timeNano_s[docker_ev_timeNano_s_size];
  854. snprintfz( docker_ev_timeNano_s, docker_ev_timeNano_s_size, "%0*ld",
  855. (int) docker_ev_timeNano_s_size, docker_ev_timeNano % 1000000000);
  856. memcpy(&docker_ev_datetime[20], &docker_ev_timeNano_s, docker_ev_timeNano_s_size - 1);
  857. new_tmp_text_size += docker_ev_datetime_size; // -1 for null terminator, +1 for ' ' character
  858. }
  859. if(likely(docker_ev_type && docker_ev_action)){
  860. int ev_off = -1;
  861. while(++ev_off < NUM_OF_DOCKER_EV_TYPES){
  862. if(!strncmp(docker_ev_type, docker_ev_type_string[ev_off], docker_ev_type_size)){
  863. p_file_info->parser_metrics->docker_ev->ev_type[ev_off]++;
  864. int act_off = -1;
  865. while(docker_ev_action_string[ev_off][++act_off] != NULL){
  866. if(!strncmp(docker_ev_action, docker_ev_action_string[ev_off][act_off], docker_ev_action_size)){
  867. p_file_info->parser_metrics->docker_ev->ev_action[ev_off][act_off]++;
  868. break;
  869. }
  870. }
  871. if(unlikely(docker_ev_action_string[ev_off][act_off] == NULL))
  872. p_file_info->parser_metrics->docker_ev->ev_action[NUM_OF_DOCKER_EV_TYPES - 1][0]++; // 'unknown'
  873. break;
  874. }
  875. }
  876. if(unlikely(ev_off >= NUM_OF_DOCKER_EV_TYPES - 1)){
  877. p_file_info->parser_metrics->docker_ev->ev_type[ev_off]++; // 'unknown'
  878. p_file_info->parser_metrics->docker_ev->ev_action[NUM_OF_DOCKER_EV_TYPES - 1][0]++; // 'unknown'
  879. }
  880. new_tmp_text_size += docker_ev_type_size + docker_ev_action_size + 2; // +2 for ' ' chars
  881. }
  882. if(likely(docker_ev_id)){
  883. // debug_log("docker_ev_id: %.*s", (int) docker_ev_id_size, docker_ev_id);
  884. new_tmp_text_size += docker_ev_id_size + 1; // +1 for ' ' char
  885. }
  886. if(likely(docker_ev_attr.size)){
  887. for(int i = 0; i < docker_ev_attr.size; i++){
  888. new_tmp_text_size += docker_ev_attr.key_size[i] +
  889. docker_ev_attr.val_size[i] + 3; // +3 for '=' ',' ' ' characters
  890. }
  891. /* new_tmp_text_size = -2 + 2;
  892. * -2 due to missing ',' ' ' from last attribute and +2 for the two
  893. * '(' and ')' characters, so no need to add or subtract */
  894. }
  895. new_tmp_text_size += 1; // +1 for '\n' character at the end
  896. /* Metrics extracted, now prepare circular buffer for write */
  897. // TODO: Fix: Metrics will still be collected if circ_buff_prepare_write() returns 0.
  898. if(unlikely(!circ_buff_prepare_write(buff, new_tmp_text_size)))
  899. goto skip_collect_and_drop_logs;
  900. size_t tmp_item_off = buff->in->text_size;
  901. message_size = new_tmp_text_size - 1 - tmp_item_off;
  902. if(likely(*docker_ev_datetime)){
  903. memcpy(&buff->in->data[tmp_item_off], docker_ev_datetime, docker_ev_datetime_size - 1);
  904. tmp_item_off += docker_ev_datetime_size - 1; // -1 due to null terminator
  905. buff->in->data[tmp_item_off++] = ' ';
  906. }
  907. if(likely(docker_ev_type)){
  908. memcpy(&buff->in->data[tmp_item_off], docker_ev_type, docker_ev_type_size);
  909. tmp_item_off += docker_ev_type_size;
  910. buff->in->data[tmp_item_off++] = ' ';
  911. }
  912. if(likely(docker_ev_action)){
  913. memcpy(&buff->in->data[tmp_item_off], docker_ev_action, docker_ev_action_size);
  914. tmp_item_off += docker_ev_action_size;
  915. buff->in->data[tmp_item_off++] = ' ';
  916. }
  917. if(likely(docker_ev_id)){
  918. memcpy(&buff->in->data[tmp_item_off], docker_ev_id, docker_ev_id_size);
  919. tmp_item_off += docker_ev_id_size;
  920. buff->in->data[tmp_item_off++] = ' ';
  921. }
  922. if(likely(docker_ev_attr.size)){
  923. buff->in->data[tmp_item_off++] = '(';
  924. for(int i = 0; i < docker_ev_attr.size; i++){
  925. memcpy(&buff->in->data[tmp_item_off], docker_ev_attr.key[i], docker_ev_attr.key_size[i]);
  926. tmp_item_off += docker_ev_attr.key_size[i];
  927. buff->in->data[tmp_item_off++] = '=';
  928. memcpy(&buff->in->data[tmp_item_off], docker_ev_attr.val[i], docker_ev_attr.val_size[i]);
  929. tmp_item_off += docker_ev_attr.val_size[i];
  930. buff->in->data[tmp_item_off++] = ',';
  931. buff->in->data[tmp_item_off++] = ' ';
  932. }
  933. tmp_item_off -= 2; // overwrite last ',' and ' ' characters with a ')' character
  934. buff->in->data[tmp_item_off++] = ')';
  935. }
  936. buff->in->data[tmp_item_off++] = '\n';
  937. m_assert(tmp_item_off == new_tmp_text_size, "tmp_item_off should be == new_tmp_text_size");
  938. buff->in->text_size = new_tmp_text_size;
  939. #ifdef HAVE_SYSTEMD
  940. if(p_file_info->do_sd_journal_send){
  941. sd_journal_send(
  942. SD_JOURNAL_SEND_DEFAULT_FIELDS,
  943. "%sDOCKER_EVENTS_TYPE=%.*s", sd_journal_field_prefix, (int) docker_ev_type_size, docker_ev_type,
  944. "%sDOCKER_EVENTS_ACTION=%.*s", sd_journal_field_prefix, (int) docker_ev_action_size, docker_ev_action,
  945. "%sDOCKER_EVENTS_ID=%.*s", sd_journal_field_prefix, (int) docker_ev_id_size, docker_ev_id,
  946. LOG_REC_KEY_SYSTEMD "=%.*s", (int) message_size, &buff->in->data[tmp_item_off - 1 - message_size],
  947. NULL
  948. );
  949. }
  950. #endif
  951. } /* FLB_DOCKER_EV case end */
  952. /* FLB_MQTT case */
  953. else if(p_file_info->log_type == FLB_MQTT){
  954. if(likely(mqtt_topic)){
  955. char *const key = mallocz(mqtt_topic_size + 1);
  956. memcpy(key, mqtt_topic, mqtt_topic_size);
  957. key[mqtt_topic_size] = '\0';
  958. metrics_dict_item_t item = {.dim_initialized = false, .num_new = 1};
  959. dictionary_set_advanced(p_file_info->parser_metrics->mqtt->topic, key, mqtt_topic_size + 1, &item, sizeof(item), NULL);
  960. // TODO: Fix: Metrics will still be collected if circ_buff_prepare_write() returns 0.
  961. if(unlikely(!circ_buff_prepare_write(buff, new_tmp_text_size)))
  962. goto skip_collect_and_drop_logs;
  963. size_t tmp_item_off = buff->in->text_size;
  964. memcpy(&buff->in->data[tmp_item_off], mqtt_message, message_size);
  965. tmp_item_off += message_size;
  966. buff->in->data[tmp_item_off++] = '\n';
  967. m_assert(tmp_item_off == new_tmp_text_size, "tmp_item_off should be == new_tmp_text_size");
  968. buff->in->text_size = new_tmp_text_size;
  969. #ifdef HAVE_SYSTEMD
  970. if(p_file_info->do_sd_journal_send){
  971. sd_journal_send(
  972. SD_JOURNAL_SEND_DEFAULT_FIELDS,
  973. "%sMQTT_TOPIC=%s", key,
  974. LOG_REC_KEY_SYSTEMD "=%.*s", (int) message_size, mqtt_message,
  975. NULL
  976. );
  977. }
  978. #endif
  979. }
  980. else m_assert(0, "missing mqtt topic");
  981. }
  982. skip_collect_and_drop_logs:
  983. /* Following code is equivalent to msgpack_unpacked_destroy(&result) due
  984. * to that function call being unavailable when using dl_open() */
  985. if(result.zone != NULL) {
  986. dl_msgpack_zone_free(result.zone);
  987. result.zone = NULL;
  988. memset(&result.data, 0, sizeof(msgpack_object));
  989. }
  990. if(p_file_info)
  991. uv_mutex_unlock(&p_file_info->flb_tmp_buff_mut);
  992. flb_lib_free(record);
  993. return 0;
  994. }
  995. /**
  996. * @brief Add a Fluent-Bit input that outputs to the "lib" Fluent-Bit plugin.
  997. * @param[in] p_file_info Pointer to the log source struct where the input will
  998. * be registered to.
  999. * @return 0 on success, a negative number for any errors (see enum).
  1000. */
  1001. int flb_add_input(struct File_info *const p_file_info){
  1002. enum return_values {
  1003. SUCCESS = 0,
  1004. INVALID_LOG_TYPE = -1,
  1005. CONFIG_READ_ERROR = -2,
  1006. FLB_PARSER_CREATE_ERROR = -3,
  1007. FLB_INPUT_ERROR = -4,
  1008. FLB_INPUT_SET_ERROR = -5,
  1009. FLB_OUTPUT_ERROR = -6,
  1010. FLB_OUTPUT_SET_ERROR = -7,
  1011. DEFAULT_ERROR = -8
  1012. };
  1013. const int tag_max_size = 5;
  1014. static unsigned tag = 0; // incremental tag id to link flb inputs to outputs
  1015. char tag_s[tag_max_size];
  1016. snprintfz(tag_s, tag_max_size, "%u", tag++);
  1017. switch(p_file_info->log_type){
  1018. case FLB_TAIL:
  1019. case FLB_WEB_LOG: {
  1020. char update_every_str[10];
  1021. snprintfz(update_every_str, 10, "%d", p_file_info->update_every);
  1022. debug_log("Setting up %s tail for %s (basename:%s)",
  1023. p_file_info->log_type == FLB_TAIL ? "FLB_TAIL" : "FLB_WEB_LOG",
  1024. p_file_info->filename, p_file_info->file_basename);
  1025. Flb_tail_config_t *tail_config = (Flb_tail_config_t *) p_file_info->flb_config;
  1026. if(unlikely(!tail_config)) return CONFIG_READ_ERROR;
  1027. /* Set up input from log source */
  1028. p_file_info->flb_input = flb_input(ctx, "tail", NULL);
  1029. if(p_file_info->flb_input < 0 ) return FLB_INPUT_ERROR;
  1030. if(flb_input_set(ctx, p_file_info->flb_input,
  1031. "Tag", tag_s,
  1032. "Path", p_file_info->filename,
  1033. "Key", LOG_REC_KEY,
  1034. "Refresh_Interval", update_every_str,
  1035. "Skip_Long_Lines", "On",
  1036. "Skip_Empty_Lines", "On",
  1037. #if defined(FLB_HAVE_INOTIFY)
  1038. "Inotify_Watcher", tail_config->use_inotify ? "true" : "false",
  1039. #endif
  1040. NULL) != 0) return FLB_INPUT_SET_ERROR;
  1041. break;
  1042. }
  1043. case FLB_KMSG: {
  1044. debug_log( "Setting up FLB_KMSG collector");
  1045. Flb_kmsg_config_t *kmsg_config = (Flb_kmsg_config_t *) p_file_info->flb_config;
  1046. if(unlikely(!kmsg_config ||
  1047. !kmsg_config->prio_level ||
  1048. !*kmsg_config->prio_level)) return CONFIG_READ_ERROR;
  1049. /* Set up kmsg input */
  1050. p_file_info->flb_input = flb_input(ctx, "kmsg", NULL);
  1051. if(p_file_info->flb_input < 0 ) return FLB_INPUT_ERROR;
  1052. if(flb_input_set(ctx, p_file_info->flb_input,
  1053. "Tag", tag_s,
  1054. "Prio_Level", kmsg_config->prio_level,
  1055. NULL) != 0) return FLB_INPUT_SET_ERROR;
  1056. break;
  1057. }
  1058. case FLB_SYSTEMD: {
  1059. debug_log( "Setting up FLB_SYSTEMD collector");
  1060. /* Set up systemd input */
  1061. p_file_info->flb_input = flb_input(ctx, "systemd", NULL);
  1062. if(p_file_info->flb_input < 0 ) return FLB_INPUT_ERROR;
  1063. if(!strcmp(p_file_info->filename, SYSTEMD_DEFAULT_PATH)){
  1064. if(flb_input_set(ctx, p_file_info->flb_input,
  1065. "Tag", tag_s,
  1066. "Read_From_Tail", "On",
  1067. "Strip_Underscores", "On",
  1068. NULL) != 0) return FLB_INPUT_SET_ERROR;
  1069. } else {
  1070. if(flb_input_set(ctx, p_file_info->flb_input,
  1071. "Tag", tag_s,
  1072. "Read_From_Tail", "On",
  1073. "Strip_Underscores", "On",
  1074. "Path", p_file_info->filename,
  1075. NULL) != 0) return FLB_INPUT_SET_ERROR;
  1076. }
  1077. break;
  1078. }
  1079. case FLB_DOCKER_EV: {
  1080. debug_log( "Setting up FLB_DOCKER_EV collector");
  1081. /* Set up Docker Events parser */
  1082. if(flb_parser_create( "docker_events_parser", /* parser name */
  1083. "json", /* backend type */
  1084. NULL, /* regex */
  1085. FLB_TRUE, /* skip_empty */
  1086. NULL, /* time format */
  1087. NULL, /* time key */
  1088. NULL, /* time offset */
  1089. FLB_TRUE, /* time keep */
  1090. FLB_FALSE, /* time strict */
  1091. FLB_FALSE, /* no bare keys */
  1092. NULL, /* parser types */
  1093. 0, /* types len */
  1094. NULL, /* decoders */
  1095. ctx->config) == NULL) return FLB_PARSER_CREATE_ERROR;
  1096. /* Set up Docker Events input */
  1097. p_file_info->flb_input = flb_input(ctx, "docker_events", NULL);
  1098. if(p_file_info->flb_input < 0 ) return FLB_INPUT_ERROR;
  1099. if(flb_input_set(ctx, p_file_info->flb_input,
  1100. "Tag", tag_s,
  1101. "Parser", "docker_events_parser",
  1102. "Unix_Path", p_file_info->filename,
  1103. NULL) != 0) return FLB_INPUT_SET_ERROR;
  1104. break;
  1105. }
  1106. case FLB_SYSLOG: {
  1107. debug_log( "Setting up FLB_SYSLOG collector");
  1108. /* Set up syslog parser */
  1109. const char syslog_parser_prfx[] = "syslog_parser_";
  1110. size_t parser_name_size = sizeof(syslog_parser_prfx) + tag_max_size - 1;
  1111. char parser_name[parser_name_size];
  1112. snprintfz(parser_name, parser_name_size, "%s%u", syslog_parser_prfx, tag);
  1113. Syslog_parser_config_t *syslog_config = (Syslog_parser_config_t *) p_file_info->parser_config->gen_config;
  1114. if(unlikely(!syslog_config ||
  1115. !syslog_config->socket_config ||
  1116. !syslog_config->socket_config->mode ||
  1117. !p_file_info->filename)) return CONFIG_READ_ERROR;
  1118. if(flb_parser_create( parser_name, /* parser name */
  1119. "regex", /* backend type */
  1120. syslog_config->log_format, /* regex */
  1121. FLB_TRUE, /* skip_empty */
  1122. NULL, /* time format */
  1123. NULL, /* time key */
  1124. NULL, /* time offset */
  1125. FLB_TRUE, /* time keep */
  1126. FLB_TRUE, /* time strict */
  1127. FLB_FALSE, /* no bare keys */
  1128. NULL, /* parser types */
  1129. 0, /* types len */
  1130. NULL, /* decoders */
  1131. ctx->config) == NULL) return FLB_PARSER_CREATE_ERROR;
  1132. /* Set up syslog input */
  1133. p_file_info->flb_input = flb_input(ctx, "syslog", NULL);
  1134. if(p_file_info->flb_input < 0 ) return FLB_INPUT_ERROR;
  1135. if( !strcmp(syslog_config->socket_config->mode, "unix_udp") ||
  1136. !strcmp(syslog_config->socket_config->mode, "unix_tcp")){
  1137. m_assert(syslog_config->socket_config->unix_perm, "unix_perm is not set");
  1138. if(flb_input_set(ctx, p_file_info->flb_input,
  1139. "Tag", tag_s,
  1140. "Path", p_file_info->filename,
  1141. "Parser", parser_name,
  1142. "Mode", syslog_config->socket_config->mode,
  1143. "Unix_Perm", syslog_config->socket_config->unix_perm,
  1144. NULL) != 0) return FLB_INPUT_SET_ERROR;
  1145. } else if( !strcmp(syslog_config->socket_config->mode, "udp") ||
  1146. !strcmp(syslog_config->socket_config->mode, "tcp")){
  1147. m_assert(syslog_config->socket_config->listen, "listen is not set");
  1148. m_assert(syslog_config->socket_config->port, "port is not set");
  1149. if(flb_input_set(ctx, p_file_info->flb_input,
  1150. "Tag", tag_s,
  1151. "Parser", parser_name,
  1152. "Mode", syslog_config->socket_config->mode,
  1153. "Listen", syslog_config->socket_config->listen,
  1154. "Port", syslog_config->socket_config->port,
  1155. NULL) != 0) return FLB_INPUT_SET_ERROR;
  1156. } else return FLB_INPUT_SET_ERROR; // should never reach this line
  1157. break;
  1158. }
  1159. case FLB_SERIAL: {
  1160. debug_log( "Setting up FLB_SERIAL collector");
  1161. Flb_serial_config_t *serial_config = (Flb_serial_config_t *) p_file_info->flb_config;
  1162. if(unlikely(!serial_config ||
  1163. !serial_config->bitrate ||
  1164. !*serial_config->bitrate ||
  1165. !serial_config->min_bytes ||
  1166. !*serial_config->min_bytes ||
  1167. !p_file_info->filename)) return CONFIG_READ_ERROR;
  1168. /* Set up serial input */
  1169. p_file_info->flb_input = flb_input(ctx, "serial", NULL);
  1170. if(p_file_info->flb_input < 0 ) return FLB_INPUT_ERROR;
  1171. if(flb_input_set(ctx, p_file_info->flb_input,
  1172. "Tag", tag_s,
  1173. "File", p_file_info->filename,
  1174. "Bitrate", serial_config->bitrate,
  1175. "Min_Bytes", serial_config->min_bytes,
  1176. "Separator", serial_config->separator,
  1177. "Format", serial_config->format,
  1178. NULL) != 0) return FLB_INPUT_SET_ERROR;
  1179. break;
  1180. }
  1181. case FLB_MQTT: {
  1182. debug_log( "Setting up FLB_MQTT collector");
  1183. Flb_socket_config_t *socket_config = (Flb_socket_config_t *) p_file_info->flb_config;
  1184. if(unlikely(!socket_config || !socket_config->listen || !*socket_config->listen ||
  1185. !socket_config->port || !*socket_config->port)) return CONFIG_READ_ERROR;
  1186. /* Set up MQTT input */
  1187. p_file_info->flb_input = flb_input(ctx, "mqtt", NULL);
  1188. if(p_file_info->flb_input < 0 ) return FLB_INPUT_ERROR;
  1189. if(flb_input_set(ctx, p_file_info->flb_input,
  1190. "Tag", tag_s,
  1191. "Listen", socket_config->listen,
  1192. "Port", socket_config->port,
  1193. NULL) != 0) return FLB_INPUT_SET_ERROR;
  1194. break;
  1195. }
  1196. default: {
  1197. m_assert(0, "default: case in flb_add_input() error");
  1198. return DEFAULT_ERROR; // Shouldn't reach here
  1199. }
  1200. }
  1201. /* Set up user-configured outputs */
  1202. for(Flb_output_config_t *output = p_file_info->flb_outputs; output; output = output->next){
  1203. debug_log( "setting up user output [%s]", output->plugin);
  1204. int out = flb_output(ctx, output->plugin, NULL);
  1205. if(out < 0) return FLB_OUTPUT_ERROR;
  1206. if(flb_output_set(ctx, out,
  1207. "Match", tag_s,
  1208. NULL) != 0) return FLB_OUTPUT_SET_ERROR;
  1209. for(struct flb_output_config_param *param = output->param; param; param = param->next){
  1210. debug_log( "setting up param [%s][%s] of output [%s]", param->key, param->val, output->plugin);
  1211. if(flb_output_set(ctx, out,
  1212. param->key, param->val,
  1213. NULL) != 0) return FLB_OUTPUT_SET_ERROR;
  1214. }
  1215. }
  1216. /* Set up "lib" output */
  1217. struct flb_lib_out_cb *callback = mallocz(sizeof(struct flb_lib_out_cb));
  1218. callback->cb = flb_collect_logs_cb;
  1219. callback->data = p_file_info;
  1220. if(((p_file_info->flb_lib_output = flb_output(ctx, "lib", callback)) < 0) ||
  1221. (flb_output_set(ctx, p_file_info->flb_lib_output, "Match", tag_s, NULL) != 0)){
  1222. freez(callback);
  1223. return FLB_OUTPUT_ERROR;
  1224. }
  1225. return SUCCESS;
  1226. }
  1227. /**
  1228. * @brief Add a Fluent-Bit Forward input.
  1229. * @details This creates a unix or network socket to accept logs using
  1230. * Fluent Bit's Forward protocol. For more information see:
  1231. * https://docs.fluentbit.io/manual/pipeline/inputs/forward
  1232. * @param[in] forward_in_config Configuration of the Forward input socket.
  1233. * @return 0 on success, -1 on error.
  1234. */
  1235. int flb_add_fwd_input(Flb_socket_config_t *forward_in_config){
  1236. if(forward_in_config == NULL){
  1237. debug_log( "forward: forward_in_config is NULL");
  1238. collector_info("forward_in_config is NULL");
  1239. return 0;
  1240. }
  1241. do{
  1242. debug_log( "forward: Setting up flb_add_fwd_input()");
  1243. int input, output;
  1244. if((input = flb_input(ctx, "forward", NULL)) < 0) break;
  1245. if( forward_in_config->unix_path && *forward_in_config->unix_path &&
  1246. forward_in_config->unix_perm && *forward_in_config->unix_perm){
  1247. if(flb_input_set(ctx, input,
  1248. "Tag_Prefix", "fwd",
  1249. "Unix_Path", forward_in_config->unix_path,
  1250. "Unix_Perm", forward_in_config->unix_perm,
  1251. NULL) != 0) break;
  1252. } else if( forward_in_config->listen && *forward_in_config->listen &&
  1253. forward_in_config->port && *forward_in_config->port){
  1254. if(flb_input_set(ctx, input,
  1255. "Tag_Prefix", "fwd",
  1256. "Listen", forward_in_config->listen,
  1257. "Port", forward_in_config->port,
  1258. NULL) != 0) break;
  1259. } else break; // should never reach this line
  1260. fwd_input_out_cb = mallocz(sizeof(struct flb_lib_out_cb));
  1261. /* Set up output */
  1262. fwd_input_out_cb->cb = flb_collect_logs_cb;
  1263. fwd_input_out_cb->data = NULL;
  1264. if((output = flb_output(ctx, "lib", fwd_input_out_cb)) < 0) break;
  1265. if(flb_output_set(ctx, output,
  1266. "Match", "fwd*",
  1267. NULL) != 0) break;
  1268. debug_log( "forward: Set up flb_add_fwd_input() with success");
  1269. return 0;
  1270. } while(0);
  1271. /* Error */
  1272. if(fwd_input_out_cb) freez(fwd_input_out_cb);
  1273. return -1;
  1274. }
  1275. void flb_free_fwd_input_out_cb(void){
  1276. freez(fwd_input_out_cb);
  1277. }