Skip to content

Commit 2766d32

Browse files
authored
in_tail: make inotify file watch configurable at runtime (#3283)
Add a "inotify_watcher" option to the tail input to allow disabling inotify at runtime in favor of stat. Signed-off-by: Zack Wine <[email protected]>
1 parent 0b43260 commit 2766d32

File tree

9 files changed

+157
-29
lines changed

9 files changed

+157
-29
lines changed

plugins/in_tail/CMakeLists.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,15 @@ set(src
33
tail_dockermode.c
44
tail_scan.c
55
tail_config.c
6-
tail_fs.c
6+
tail_fs_stat.c
77
tail.c)
88

9+
if(FLB_HAVE_INOTIFY)
10+
set(src
11+
${src}
12+
tail_fs_inotify.c)
13+
endif()
14+
915
if(FLB_SQLDB)
1016
set(src
1117
${src}

plugins/in_tail/tail.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,13 @@ static struct flb_config_map config_map[] = {
573573
0, FLB_TRUE, offsetof(struct flb_tail_config, exit_on_eof),
574574
"exit Fluent Bit when reaching EOF on a monitored file."
575575
},
576+
#ifdef FLB_HAVE_INOTIFY
577+
{
578+
FLB_CONFIG_MAP_BOOL, "inotify_watcher", "true",
579+
0, FLB_TRUE, offsetof(struct flb_tail_config, inotify_watcher),
580+
"set to false to use file stat watcher instead of inotify."
581+
},
582+
#endif
576583
#ifdef FLB_HAVE_REGEX
577584
{
578585
FLB_CONFIG_MAP_STR, "parser", NULL,

plugins/in_tail/tail_config.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ struct flb_tail_config {
7979
flb_sds_t key; /* key for unstructured record */
8080
int skip_long_lines; /* skip long lines */
8181
int exit_on_eof; /* exit fluent-bit on EOF, test */
82+
#ifdef FLB_HAVE_INOTIFY
83+
int inotify_watcher; /* enable/disable inotify monitor */
84+
#endif
8285
flb_sds_t offset_key; /* key name of file offset */
8386

8487
/* Database */

plugins/in_tail/tail_file.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -751,7 +751,7 @@ int flb_tail_file_append(char *path, struct stat *st, int mode,
751751
mk_list_add(&file->_head, &ctx->files_event);
752752

753753
/* Register this file into the fs_event monitoring */
754-
ret = flb_tail_fs_add(file);
754+
ret = flb_tail_fs_add(ctx, file);
755755
if (ret == -1) {
756756
flb_plg_error(ctx->ins, "could not register file into fs_events");
757757
goto error;
@@ -817,7 +817,7 @@ void flb_tail_file_remove(struct flb_tail_file *file)
817817
flb_sds_destroy(file->dmode_buf);
818818
flb_sds_destroy(file->dmode_lastline);
819819
mk_list_del(&file->_head);
820-
flb_tail_fs_remove(file);
820+
flb_tail_fs_remove(ctx, file);
821821
/* avoid deleting file with -1 fd */
822822
if (file->fd != -1) {
823823
close(file->fd);
@@ -1133,7 +1133,7 @@ int flb_tail_file_to_event(struct flb_tail_file *file)
11331133
}
11341134

11351135
/* Notify the fs-event handler that we will start monitoring this 'file' */
1136-
ret = flb_tail_fs_add(file);
1136+
ret = flb_tail_fs_add(ctx, file);
11371137
if (ret == -1) {
11381138
return -1;
11391139
}

plugins/in_tail/tail_fs.h

Lines changed: 66 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,71 @@
2727
#include "tail_config.h"
2828
#include "tail_file_internal.h"
2929

30-
int flb_tail_fs_init(struct flb_input_instance *in,
31-
struct flb_tail_config *ctx, struct flb_config *config);
32-
int flb_tail_fs_add(struct flb_tail_file *file);
33-
int flb_tail_fs_remove(struct flb_tail_file *file);
34-
int flb_tail_fs_exit(struct flb_tail_config *ctx);
35-
void flb_tail_fs_pause(struct flb_tail_config *ctx);
36-
void flb_tail_fs_resume(struct flb_tail_config *ctx);
30+
#include "tail_fs_stat.h"
31+
#ifdef FLB_HAVE_INOTIFY
32+
#include "tail_fs_inotify.h"
33+
#endif
34+
35+
static inline int flb_tail_fs_init(struct flb_input_instance *in,
36+
struct flb_tail_config *ctx, struct flb_config *config)
37+
{
38+
#ifdef FLB_HAVE_INOTIFY
39+
if (ctx->inotify_watcher) {
40+
return flb_tail_fs_inotify_init(in, ctx, config);
41+
}
42+
#endif
43+
return flb_tail_fs_stat_init(in, ctx, config);
44+
}
45+
46+
static inline void flb_tail_fs_pause(struct flb_tail_config *ctx)
47+
{
48+
#ifdef FLB_HAVE_INOTIFY
49+
if (ctx->inotify_watcher) {
50+
return flb_tail_fs_inotify_pause(ctx);
51+
}
52+
#endif
53+
return flb_tail_fs_stat_pause(ctx);
54+
}
55+
56+
static inline void flb_tail_fs_resume(struct flb_tail_config *ctx)
57+
{
58+
#ifdef FLB_HAVE_INOTIFY
59+
if (ctx->inotify_watcher) {
60+
return flb_tail_fs_inotify_resume(ctx);
61+
}
62+
#endif
63+
return flb_tail_fs_stat_resume(ctx);
64+
}
65+
66+
static inline int flb_tail_fs_add(struct flb_tail_config *ctx, struct flb_tail_file *file)
67+
{
68+
#ifdef FLB_HAVE_INOTIFY
69+
if (ctx->inotify_watcher) {
70+
return flb_tail_fs_inotify_add(file);
71+
}
72+
#endif
73+
return flb_tail_fs_stat_add(file);
74+
}
75+
76+
static inline int flb_tail_fs_remove(struct flb_tail_config *ctx, struct flb_tail_file *file)
77+
{
78+
#ifdef FLB_HAVE_INOTIFY
79+
if (ctx->inotify_watcher) {
80+
return flb_tail_fs_inotify_remove(file);
81+
}
82+
#endif
83+
return flb_tail_fs_stat_remove(file);
84+
}
85+
86+
static inline int flb_tail_fs_exit(struct flb_tail_config *ctx)
87+
{
88+
#ifdef FLB_HAVE_INOTIFY
89+
if (ctx->inotify_watcher) {
90+
return flb_tail_fs_inotify_exit(ctx);
91+
}
92+
#endif
93+
return flb_tail_fs_stat_exit(ctx);
94+
}
95+
3796

3897
#endif

plugins/in_tail/tail_fs_inotify.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ static int tail_fs_event(struct flb_input_instance *ins,
203203

204204
/* A rotated file must be re-registered */
205205
flb_tail_file_rotated(file);
206-
flb_tail_fs_remove(file);
206+
flb_tail_fs_remove(ctx, file);
207207
flb_tail_fs_add_rotated(file);
208208
}
209209

@@ -291,12 +291,14 @@ static int tail_fs_event(struct flb_input_instance *ins,
291291
}
292292

293293
/* File System events based on Inotify(2). Linux >= 2.6.32 is suggested */
294-
int flb_tail_fs_init(struct flb_input_instance *in,
294+
int flb_tail_fs_inotify_init(struct flb_input_instance *in,
295295
struct flb_tail_config *ctx, struct flb_config *config)
296296
{
297297
int fd;
298298
int ret;
299299

300+
flb_plg_debug(ctx->ins, "flb_tail_fs_inotify_init() initializing inotify tail input");
301+
300302
/* Create inotify instance */
301303
fd = inotify_init1(IN_NONBLOCK | IN_CLOEXEC);
302304
if (fd == -1) {
@@ -318,17 +320,17 @@ int flb_tail_fs_init(struct flb_input_instance *in,
318320
return 0;
319321
}
320322

321-
void flb_tail_fs_pause(struct flb_tail_config *ctx)
323+
void flb_tail_fs_inotify_pause(struct flb_tail_config *ctx)
322324
{
323325
flb_input_collector_pause(ctx->coll_fd_fs1, ctx->ins);
324326
}
325327

326-
void flb_tail_fs_resume(struct flb_tail_config *ctx)
328+
void flb_tail_fs_inotify_resume(struct flb_tail_config *ctx)
327329
{
328330
flb_input_collector_resume(ctx->coll_fd_fs1, ctx->ins);
329331
}
330332

331-
int flb_tail_fs_add(struct flb_tail_file *file)
333+
int flb_tail_fs_inotify_add(struct flb_tail_file *file)
332334
{
333335
int ret;
334336
struct flb_tail_config *ctx = file->config;
@@ -343,7 +345,7 @@ int flb_tail_fs_add(struct flb_tail_file *file)
343345
return 0;
344346
}
345347

346-
int flb_tail_fs_remove(struct flb_tail_file *file)
348+
int flb_tail_fs_inotify_remove(struct flb_tail_file *file)
347349
{
348350
struct flb_tail_config *ctx = file->config;
349351

@@ -359,7 +361,7 @@ int flb_tail_fs_remove(struct flb_tail_file *file)
359361
return 0;
360362
}
361363

362-
int flb_tail_fs_exit(struct flb_tail_config *ctx)
364+
int flb_tail_fs_inotify_exit(struct flb_tail_config *ctx)
363365
{
364366
(void) ctx;
365367
return 0;

plugins/in_tail/tail_fs_inotify.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2+
3+
/* Fluent Bit
4+
* ==========
5+
* Copyright (C) 2019-2021 The Fluent Bit Authors
6+
* Copyright (C) 2015-2018 Treasure Data Inc.
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License");
9+
* you may not use this file except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
*/
20+
21+
#ifndef FLB_TAIL_FS_INOTIFY_H
22+
#define FLB_TAIL_FS_INOTIFY_H
23+
24+
#include <fluent-bit/flb_info.h>
25+
#include <fluent-bit/flb_input.h>
26+
27+
#include "tail_config.h"
28+
#include "tail_file_internal.h"
29+
30+
int flb_tail_fs_inotify_init(struct flb_input_instance *in,
31+
struct flb_tail_config *ctx, struct flb_config *config);
32+
int flb_tail_fs_inotify_add(struct flb_tail_file *file);
33+
int flb_tail_fs_inotify_remove(struct flb_tail_file *file);
34+
int flb_tail_fs_inotify_exit(struct flb_tail_config *ctx);
35+
void flb_tail_fs_inotify_pause(struct flb_tail_config *ctx);
36+
void flb_tail_fs_inotify_resume(struct flb_tail_config *ctx);
37+
38+
#endif

plugins/in_tail/tail_fs_stat.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -178,11 +178,13 @@ static int tail_fs_check(struct flb_input_instance *ins,
178178
}
179179

180180
/* File System events based on stat(2) */
181-
int flb_tail_fs_init(struct flb_input_instance *in,
182-
struct flb_tail_config *ctx, struct flb_config *config)
181+
int flb_tail_fs_stat_init(struct flb_input_instance *in,
182+
struct flb_tail_config *ctx, struct flb_config *config)
183183
{
184184
int ret;
185185

186+
flb_plg_debug(ctx->ins, "flb_tail_fs_stat_init() initializing stat tail input");
187+
186188
/* Set a manual timer to collect events every 0.250 seconds */
187189
ret = flb_input_set_collector_time(in, tail_fs_event,
188190
0, 250000000, config);
@@ -202,19 +204,19 @@ int flb_tail_fs_init(struct flb_input_instance *in,
202204
return 0;
203205
}
204206

205-
void flb_tail_fs_pause(struct flb_tail_config *ctx)
207+
void flb_tail_fs_stat_pause(struct flb_tail_config *ctx)
206208
{
207209
flb_input_collector_pause(ctx->coll_fd_fs1, ctx->ins);
208210
flb_input_collector_pause(ctx->coll_fd_fs2, ctx->ins);
209211
}
210212

211-
void flb_tail_fs_resume(struct flb_tail_config *ctx)
213+
void flb_tail_fs_stat_resume(struct flb_tail_config *ctx)
212214
{
213215
flb_input_collector_resume(ctx->coll_fd_fs1, ctx->ins);
214216
flb_input_collector_resume(ctx->coll_fd_fs2, ctx->ins);
215217
}
216218

217-
int flb_tail_fs_add(struct flb_tail_file *file)
219+
int flb_tail_fs_stat_add(struct flb_tail_file *file)
218220
{
219221
int ret;
220222
struct fs_stat *fst;
@@ -237,15 +239,15 @@ int flb_tail_fs_add(struct flb_tail_file *file)
237239
return 0;
238240
}
239241

240-
int flb_tail_fs_remove(struct flb_tail_file *file)
242+
int flb_tail_fs_stat_remove(struct flb_tail_file *file)
241243
{
242244
if (file->tail_mode == FLB_TAIL_EVENT) {
243245
flb_free(file->fs_backend);
244246
}
245247
return 0;
246248
}
247249

248-
int flb_tail_fs_exit(struct flb_tail_config *ctx)
250+
int flb_tail_fs_stat_exit(struct flb_tail_config *ctx)
249251
{
250252
(void) ctx;
251253
return 0;

plugins/in_tail/tail_fs.c renamed to plugins/in_tail/tail_fs_stat.h

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,21 @@
1818
* limitations under the License.
1919
*/
2020

21+
#ifndef FLB_TAIL_FS_STAT_H
22+
#define FLB_TAIL_FS_STAT_H
23+
2124
#include <fluent-bit/flb_info.h>
25+
#include <fluent-bit/flb_input.h>
26+
27+
#include "tail_config.h"
28+
#include "tail_file_internal.h"
29+
30+
int flb_tail_fs_stat_init(struct flb_input_instance *in,
31+
struct flb_tail_config *ctx, struct flb_config *config);
32+
int flb_tail_fs_stat_add(struct flb_tail_file *file);
33+
int flb_tail_fs_stat_remove(struct flb_tail_file *file);
34+
int flb_tail_fs_stat_exit(struct flb_tail_config *ctx);
35+
void flb_tail_fs_stat_pause(struct flb_tail_config *ctx);
36+
void flb_tail_fs_stat_resume(struct flb_tail_config *ctx);
2237

23-
#ifdef FLB_HAVE_INOTIFY
24-
#include "tail_fs_inotify.c"
25-
#else
26-
#include "tail_fs_stat.c"
2738
#endif

0 commit comments

Comments
 (0)