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
|
// SPDX-License-Identifier: GPL-3.0-or-later
#include "parser.h"
#include "collectors/plugins.d/pluginsd_parser.h"
inline int find_first_keyword(const char *str, char *keyword, int max_size, int (*custom_isspace)(char))
{
const char *s = str, *keyword_start;
while (unlikely(custom_isspace(*s))) s++;
keyword_start = s;
while (likely(*s && !custom_isspace(*s)) && max_size > 1) {
*keyword++ = *s++;
max_size--;
}
*keyword = '\0';
return max_size == 0 ? 0 : (int) (s - keyword_start);
}
/*
* Initialize a parser
* user : as defined by the user, will be shared across calls
* input : main input stream (auto detect stream -- file, socket, pipe)
* buffer : This is the buffer to be used (if null a buffer of size will be allocated)
* size : buffer size either passed or will be allocated
* If the buffer is auto allocated, it will auto freed when the parser is destroyed
*
*
*/
PARSER *parser_init(RRDHOST *host, void *user, void *input, void *output, PARSER_INPUT_TYPE flags)
{
PARSER *parser;
parser = callocz(1, sizeof(*parser));
parser->plugins_action = callocz(1, sizeof(PLUGINSD_ACTION));
parser->user = user;
parser->input = input;
parser->output = output;
parser->flags = flags;
parser->host = host;
parser->worker_job_next_id = WORKER_PARSER_FIRST_JOB;
inflight_functions_init(parser);
#ifdef ENABLE_HTTPS
parser->bytesleft = 0;
parser->readfrom = NULL;
#endif
if (unlikely(!(flags & PARSER_NO_PARSE_INIT))) {
parser_add_keyword(parser, PLUGINSD_KEYWORD_FLUSH, pluginsd_flush);
parser_add_keyword(parser, PLUGINSD_KEYWORD_CHART, pluginsd_chart);
parser_add_keyword(parser, PLUGINSD_KEYWORD_DIMENSION, pluginsd_dimension);
parser_add_keyword(parser, PLUGINSD_KEYWORD_DISABLE, pluginsd_disable);
parser_add_keyword(parser, PLUGINSD_KEYWORD_VARIABLE, pluginsd_variable);
parser_add_keyword(parser, PLUGINSD_KEYWORD_LABEL, pluginsd_label);
parser_add_keyword(parser, PLUGINSD_KEYWORD_OVERWRITE, pluginsd_overwrite);
parser_add_keyword(parser, PLUGINSD_KEYWORD_END, pluginsd_end);
parser_add_keyword(parser, PLUGINSD_KEYWORD_CLABEL_COMMIT, pluginsd_clabel_commit);
parser_add_keyword(parser, PLUGINSD_KEYWORD_CLABEL, pluginsd_clabel);
parser_add_keyword(parser, PLUGINSD_KEYWORD_BEGIN, pluginsd_begin);
parser_add_keyword(parser, PLUGINSD_KEYWORD_SET, pluginsd_set);
parser_add_keyword(parser, PLUGINSD_KEYWORD_FUNCTION, pluginsd_function);
parser_add_keyword(parser, PLUGINSD_KEYWORD_FUNCTION_RESULT_BEGIN, pluginsd_function_result_begin);
//parser_add_keyword(parser, PLUGINSD_KEYWORD_GAPS_REQUEST, pluginsd_gaps_request);
}
if(unlikely(!(flags & PARSER_NO_ACTION_INIT))) {
parser->plugins_action->begin_action = &pluginsd_begin_action;
parser->plugins_action->flush_action = &pluginsd_flush_action;
parser->plugins_action->end_action = &pluginsd_end_action;
parser->plugins_action->disable_action = &pluginsd_disable_action;
parser->plugins_action->variable_action = &pluginsd_variable_action;
parser->plugins_action->dimension_action = &pluginsd_dimension_action;
parser->plugins_action->label_action = &pluginsd_label_action;
parser->plugins_action->overwrite_action = &pluginsd_overwrite_action;
parser->plugins_action->chart_action = &pluginsd_chart_action;
parser->plugins_action->set_action = &pluginsd_set_action;
}
return parser;
}
/*
* Push a new line into the parsing stream
*
* This line will be the next one to process ie the next fetch will get this one
*
*/
int parser_push(PARSER *parser, char *line)
{
PARSER_DATA *tmp_parser_data;
if (unlikely(!parser))
return 1;
if (unlikely(!line))
return 0;
tmp_parser_data = callocz(1, sizeof(*tmp_parser_data));
tmp_parser_data->line = strdupz(line);
tmp_parser_data->next = parser->data;
parser->data = tmp_parser_data;
return 0;
}
/*
* Add a keyword and the corresponding function that will be called
* Multiple functions may be added
* Input : keyword
* : callback function
* : flags
* Output: > 0 registered function number
* : 0 Error
*/
int parser_add_keyword(PARSER *parser, char *keyword, keyword_function func)
{
PARSER_KEYWORD *tmp_keyword;
if (strcmp(keyword, "_read") == 0) {
parser->read_function = (void *) func;
return 0;
}
if (strcmp(keyword, "_eof") == 0) {
parser->eof_function = (void *) func;
return 0;
}
if (strcmp(keyword, "_unknown") == 0) {
parser->unknown_function = (void *) func;
return 0;
}
uint32_t keyword_hash = simple_hash(keyword);
tmp_keyword = parser->keyword;
while (tmp_keyword) {
if (tmp_keyword->keyword_hash == keyword_hash && (!strcmp(tmp_keyword->keyword, keyword))) {
if (tmp_keyword->func_no == PARSER_MAX_CALLBACKS)
return 0;
tmp_keyword->func[tmp_keyword->func_no++] = (void *) func;
return tmp_keyword->func_no;
}
tmp_keyword = tmp_keyword->next;
}
tmp_keyword = callocz(1, sizeof(*tmp_keyword));
tmp_keyword->worker_job_id = parser->worker_job_next_id++;
tmp_keyword->keyword = strdupz(keyword);
tmp_keyword->keyword_hash = keyword_hash;
tmp_keyword->func[tmp_keyword->func_no++] = (void *) func;
worker_register_job_name(tmp_keyword->worker_job_id, tmp_keyword->keyword);
tmp_keyword->next = parser->keyword;
parser->keyword = tmp_keyword;
return tmp_keyword->func_no;
}
/*
* Cleanup a previously allocated parser
*/
void parser_destroy(PARSER *parser)
{
if (unlikely(!parser))
return;
dictionary_destroy(parser->inflight.functions);
PARSER_KEYWORD *tmp_keyword, *tmp_keyword_next;
PARSER_DATA *tmp_parser_data, *tmp_parser_data_next;
// Remove keywords
tmp_keyword = parser->keyword;
while (tmp_keyword) {
tmp_keyword_next = tmp_keyword->next;
freez(tmp_keyword->keyword);
freez(tmp_keyword);
tmp_keyword = tmp_keyword_next;
}
// Remove pushed data if any
tmp_parser_data = parser->data;
while (tmp_parser_data) {
tmp_parser_data_next = tmp_parser_data->next;
freez(tmp_parser_data->line);
freez(tmp_parser_data);
tmp_parser_data = tmp_parser_data_next;
}
freez(parser->plugins_action);
freez(parser);
}
/*
* Fetch the next line to process
*
*/
int parser_next(PARSER *parser)
{
char *tmp = NULL;
if (unlikely(!parser))
return 1;
parser->flags &= ~(PARSER_INPUT_PROCESSED);
PARSER_DATA *tmp_parser_data = parser->data;
if (unlikely(tmp_parser_data)) {
strncpyz(parser->buffer, tmp_parser_data->line, PLUGINSD_LINE_MAX);
parser->data = tmp_parser_data->next;
freez(tmp_parser_data->line);
freez(tmp_parser_data);
return 0;
}
if (unlikely(parser->read_function))
tmp = parser->read_function(parser->buffer, PLUGINSD_LINE_MAX, parser->input);
else
tmp = fgets(parser->buffer, PLUGINSD_LINE_MAX,
|