forked from fluent/fluent-bit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
in_dummy.c
190 lines (159 loc) · 5.25 KB
/
in_dummy.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
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* Fluent Bit
* ==========
* Copyright (C) 2019-2020 The Fluent Bit Authors
* Copyright (C) 2015-2018 Treasure Data Inc.
*
* 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 <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <msgpack.h>
#include <fluent-bit/flb_input.h>
#include <fluent-bit/flb_input_plugin.h>
#include <fluent-bit/flb_config.h>
#include <fluent-bit/flb_error.h>
#include <fluent-bit/flb_time.h>
#include <fluent-bit/flb_pack.h>
#include "in_dummy.h"
/* cb_collect callback */
static int in_dummy_collect(struct flb_input_instance *ins,
struct flb_config *config, void *in_context)
{
size_t off = 0;
size_t start = 0;
struct flb_dummy *ctx = in_context;
char *pack = ctx->ref_msgpack;
int pack_size = ctx->ref_msgpack_size;
msgpack_unpacked result;
msgpack_packer mp_pck;
msgpack_sbuffer mp_sbuf;
msgpack_unpacked_init(&result);
/* Initialize local msgpack buffer */
msgpack_sbuffer_init(&mp_sbuf);
msgpack_packer_init(&mp_pck, &mp_sbuf, msgpack_sbuffer_write);
while (msgpack_unpack_next(&result, pack, pack_size, &off) == MSGPACK_UNPACK_SUCCESS) {
if (result.data.type == MSGPACK_OBJECT_MAP) {
/* { map => val, map => val, map => val } */
msgpack_pack_array(&mp_pck, 2);
flb_pack_time_now(&mp_pck);
msgpack_pack_str_body(&mp_pck, pack + start, off - start);
}
start = off;
}
msgpack_unpacked_destroy(&result);
flb_input_chunk_append_raw(ins, NULL, 0, mp_sbuf.data, mp_sbuf.size);
msgpack_sbuffer_destroy(&mp_sbuf);
return 0;
}
static int config_destroy(struct flb_dummy *ctx)
{
flb_free(ctx->dummy_message);
flb_free(ctx->ref_msgpack);
flb_free(ctx);
return 0;
}
/* Set plugin configuration */
static int configure(struct flb_dummy *ctx,
struct flb_input_instance *in,
struct timespec *tm)
{
const char *str = NULL;
int root_type;
int ret = -1;
long val = 0;
ctx->ref_msgpack = NULL;
/* samples */
str = flb_input_get_property("dummy", in);
if (str != NULL) {
ctx->dummy_message = flb_strdup(str);
}
else {
ctx->dummy_message = flb_strdup(DEFAULT_DUMMY_MESSAGE);
}
ctx->dummy_message_len = strlen(ctx->dummy_message);
/* interval settings */
tm->tv_sec = 1;
tm->tv_nsec = 0;
str = flb_input_get_property("rate", in);
if (str != NULL && (val = atoi(str)) > 1) {
tm->tv_sec = 0;
tm->tv_nsec = 1000000000 / val;
}
ret = flb_pack_json(ctx->dummy_message,
ctx->dummy_message_len,
&ctx->ref_msgpack, &ctx->ref_msgpack_size, &root_type);
if (ret != 0) {
flb_plg_warn(ctx->ins, "data is incomplete. Use default string.");
flb_free(ctx->dummy_message);
ctx->dummy_message = flb_strdup(DEFAULT_DUMMY_MESSAGE);
ctx->dummy_message_len = strlen(ctx->dummy_message);
ret = flb_pack_json(ctx->dummy_message,
ctx->dummy_message_len,
&ctx->ref_msgpack, &ctx->ref_msgpack_size,
&root_type);
if (ret != 0) {
flb_plg_error(ctx->ins, "unexpected error");
return -1;
}
}
return 0;
}
/* Initialize plugin */
static int in_dummy_init(struct flb_input_instance *in,
struct flb_config *config, void *data)
{
int ret = -1;
struct flb_dummy *ctx = NULL;
struct timespec tm;
/* Allocate space for the configuration */
ctx = flb_malloc(sizeof(struct flb_dummy));
if (ctx == NULL) {
return -1;
}
ctx->ins = in;
/* Initialize head config */
ret = configure(ctx, in, &tm);
if (ret < 0) {
config_destroy(ctx);
return -1;
}
flb_input_set_context(in, ctx);
ret = flb_input_set_collector_time(in,
in_dummy_collect,
tm.tv_sec,
tm.tv_nsec, config);
if (ret < 0) {
flb_plg_error(ctx->ins, "could not set collector for dummy input plugin");
config_destroy(ctx);
return -1;
}
return 0;
}
static int in_dummy_exit(void *data, struct flb_config *config)
{
(void) *config;
struct flb_dummy *ctx = data;
config_destroy(ctx);
return 0;
}
struct flb_input_plugin in_dummy_plugin = {
.name = "dummy",
.description = "Generate dummy data",
.cb_init = in_dummy_init,
.cb_pre_run = NULL,
.cb_collect = in_dummy_collect,
.cb_flush_buf = NULL,
.cb_exit = in_dummy_exit
};