Skip to content

Commit a36f95e

Browse files
cosmo0920edsiper
authored andcommitted
in_windows_exporter_metrics: Handle we.service.include and we.service.exclude options to construct where clause
Signed-off-by: Hiroshi Hatake <[email protected]>
1 parent 4d4a30d commit a36f95e

File tree

4 files changed

+326
-1
lines changed

4 files changed

+326
-1
lines changed

plugins/in_windows_exporter_metrics/we.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -916,6 +916,16 @@ static struct flb_config_map config_map[] = {
916916
0, FLB_TRUE, offsetof(struct flb_we, raw_where_clause),
917917
"Specify the where clause for retrieving service metrics."
918918
},
919+
{
920+
FLB_CONFIG_MAP_STR, "we.service.include", NULL,
921+
0, FLB_TRUE, offsetof(struct flb_we, raw_service_include),
922+
"Specify the key value condition pairs for includeing condition to construct where clause of service metrics."
923+
},
924+
{
925+
FLB_CONFIG_MAP_STR, "we.service.exclude", NULL,
926+
0, FLB_TRUE, offsetof(struct flb_we, raw_service_exclude),
927+
"Specify the key value condition pairs for excludeing condition to construct where clause of service metrics."
928+
},
919929
/* EOF */
920930
{0}
921931
};

plugins/in_windows_exporter_metrics/we.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,12 @@ struct flb_we {
188188
char *raw_denying_disk;
189189
char *raw_allowing_nic;
190190
char *raw_where_clause;
191+
char *raw_service_include;
192+
char *raw_service_exclude;
193+
char *service_include_buffer;
194+
int service_include_buffer_size;
195+
char *service_exclude_buffer;
196+
int service_exclude_buffer_size;
191197

192198
struct flb_regex *allowing_disk_regex;
193199
struct flb_regex *denying_disk_regex;

plugins/in_windows_exporter_metrics/we_config.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@
1919
*/
2020

2121
#include <fluent-bit/flb_input_plugin.h>
22+
#include <fluent-bit/flb_pack.h>
2223
#include "we.h"
2324

2425
struct flb_we *flb_we_config_create(struct flb_input_instance *ins,
2526
struct flb_config *config)
2627
{
2728
int ret;
2829
struct flb_we *ctx;
30+
int root_type;
2931

3032
ctx = flb_calloc(1, sizeof(struct flb_we));
3133
if (!ctx) {
@@ -36,6 +38,10 @@ struct flb_we *flb_we_config_create(struct flb_input_instance *ins,
3638
ctx->allowing_disk_regex = NULL;
3739
ctx->denying_disk_regex = NULL;
3840
ctx->allowing_nic_regex = NULL;
41+
ctx->service_include_buffer = NULL;
42+
ctx->service_include_buffer_size = 0;
43+
ctx->service_exclude_buffer = NULL;
44+
ctx->service_exclude_buffer_size = 0;
3945

4046
/* Load the config map */
4147
ret = flb_input_config_map_set(ins, (void *) ctx);
@@ -57,6 +63,34 @@ struct flb_we *flb_we_config_create(struct flb_input_instance *ins,
5763
ctx->allowing_nic_regex = flb_regex_create(ctx->raw_allowing_nic);
5864
}
5965

66+
if (ctx->raw_service_include != NULL) {
67+
ret = flb_pack_json(ctx->raw_service_include,
68+
strlen(ctx->raw_service_include),
69+
&ctx->service_include_buffer,
70+
&ctx->service_include_buffer_size,
71+
&root_type,
72+
NULL);
73+
if (ret != 0) {
74+
flb_plg_warn(ctx->ins, "we.service.include is incomplete. Ignored.");
75+
ctx->service_include_buffer = NULL;
76+
ctx->service_include_buffer_size = 0;
77+
}
78+
}
79+
80+
if (ctx->raw_service_exclude != NULL) {
81+
ret = flb_pack_json(ctx->raw_service_exclude,
82+
strlen(ctx->raw_service_exclude),
83+
&ctx->service_exclude_buffer,
84+
&ctx->service_exclude_buffer_size,
85+
&root_type,
86+
NULL);
87+
if (ret != 0) {
88+
flb_plg_warn(ctx->ins, "we.service.exclude is incomplete. Ignored.");
89+
ctx->service_exclude_buffer = NULL;
90+
ctx->service_exclude_buffer_size = 0;
91+
}
92+
}
93+
6094
ctx->cmt = cmt_create();
6195
if (!ctx->cmt) {
6296
flb_plg_error(ins, "could not initialize CMetrics");
@@ -85,6 +119,14 @@ void flb_we_config_destroy(struct flb_we *ctx)
85119
flb_regex_destroy(ctx->allowing_nic_regex);
86120
}
87121

122+
if (ctx->service_include_buffer != NULL) {
123+
flb_free(ctx->service_include_buffer);
124+
}
125+
126+
if (ctx->service_exclude_buffer != NULL) {
127+
flb_free(ctx->service_exclude_buffer);
128+
}
129+
88130
if (ctx->cmt) {
89131
cmt_destroy(ctx->cmt);
90132
}

plugins/in_windows_exporter_metrics/we_wmi_service.c

Lines changed: 268 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,268 @@ static double nop_adjust(double value)
3434
return value;
3535
}
3636

37+
static int construct_include_clause(struct flb_we *ctx, flb_sds_t *clause)
38+
{
39+
int ret = -1;
40+
size_t off = 0;
41+
msgpack_unpacked result;
42+
msgpack_object key;
43+
msgpack_object val;
44+
msgpack_object map;
45+
int map_size;
46+
int i;
47+
int idx = 0;
48+
int use_like = FLB_FALSE;
49+
char *key_str = NULL;
50+
size_t key_str_size = 0;
51+
char *val_str = NULL;
52+
size_t val_str_size = 0;
53+
flb_sds_t val_buf;
54+
55+
msgpack_unpacked_init(&result);
56+
while (msgpack_unpack_next(&result,
57+
ctx->service_include_buffer,
58+
ctx->service_include_buffer_size,
59+
&off) == MSGPACK_UNPACK_SUCCESS) {
60+
if (result.data.type != MSGPACK_OBJECT_MAP) {
61+
flb_plg_error(ctx->ins, "Invalid include buffer");
62+
ret = -2;
63+
64+
goto cleanup;
65+
}
66+
67+
map = result.data;
68+
map_size = map.via.map.size;
69+
70+
for (i = 0; i < map_size; i++) {
71+
use_like = FLB_FALSE;
72+
if (idx == 0) {
73+
flb_sds_cat_safe(clause, "(", 1);
74+
}
75+
else {
76+
flb_sds_cat_safe(clause, " OR ", 4);
77+
}
78+
79+
key = map.via.map.ptr[i].key;
80+
val = map.via.map.ptr[i].val;
81+
if (key.type == MSGPACK_OBJECT_BIN) {
82+
key_str = (char *) key.via.bin.ptr;
83+
key_str_size = key.via.bin.size;
84+
}
85+
else if (key.type == MSGPACK_OBJECT_STR) {
86+
key_str = (char *) key.via.str.ptr;
87+
key_str_size = key.via.str.size;
88+
}
89+
if (val.type == MSGPACK_OBJECT_BIN) {
90+
val_str = (char *) val.via.bin.ptr;
91+
val_str_size = val.via.bin.size;
92+
val_buf = flb_sds_create_len(val_str, val_str_size);
93+
if (val_buf == NULL) {
94+
flb_plg_error(ctx->ins, "val_buf creation is failed");
95+
ret = -3;
96+
97+
goto cleanup;
98+
}
99+
}
100+
else if (val.type == MSGPACK_OBJECT_STR) {
101+
val_str = (char *) val.via.str.ptr;
102+
val_str_size = val.via.str.size;
103+
val_buf = flb_sds_create_len(val_str, val_str_size);
104+
if (val_buf == NULL) {
105+
flb_plg_error(ctx->ins, "val_buf creation is failed");
106+
ret = -3;
107+
108+
goto cleanup;
109+
}
110+
}
111+
112+
if (val_str != NULL && strstr(val_buf, "%") != NULL) {
113+
use_like = FLB_TRUE;
114+
flb_sds_destroy(val_buf);
115+
}
116+
flb_sds_cat_safe(clause, key_str, key_str_size);
117+
if (use_like == FLB_TRUE) {
118+
flb_sds_cat_safe(clause, " LIKE ", 6);
119+
}
120+
else {
121+
flb_sds_cat_safe(clause, "=", 1);
122+
}
123+
flb_sds_cat_safe(clause, "'", 1);
124+
flb_sds_cat_safe(clause, val_str, val_str_size);
125+
flb_sds_cat_safe(clause, "'", 1);
126+
idx++;
127+
}
128+
flb_sds_cat_safe(clause, ")", 1);
129+
}
130+
msgpack_unpacked_destroy(&result);
131+
132+
return 0;
133+
134+
cleanup:
135+
msgpack_unpacked_destroy(&result);
136+
137+
return ret;
138+
}
139+
140+
static int construct_exclude_clause(struct flb_we *ctx, flb_sds_t *clause)
141+
{
142+
int ret = -1;
143+
size_t off = 0;
144+
msgpack_unpacked result;
145+
msgpack_object key;
146+
msgpack_object val;
147+
msgpack_object map;
148+
int map_size;
149+
int i;
150+
int idx = 0;
151+
int use_like = FLB_FALSE;
152+
char *key_str = NULL;
153+
size_t key_str_size = 0;
154+
char *val_str = NULL;
155+
size_t val_str_size = 0;
156+
flb_sds_t val_buf;
157+
158+
msgpack_unpacked_init(&result);
159+
while (msgpack_unpack_next(&result,
160+
ctx->service_exclude_buffer,
161+
ctx->service_exclude_buffer_size,
162+
&off) == MSGPACK_UNPACK_SUCCESS) {
163+
if (result.data.type != MSGPACK_OBJECT_MAP) {
164+
flb_plg_error(ctx->ins, "Invalid exclude buffer");
165+
ret = -2;
166+
167+
goto cleanup;
168+
}
169+
170+
map = result.data;
171+
map_size = map.via.map.size;
172+
173+
for (i = 0; i < map_size; i++) {
174+
use_like = FLB_FALSE;
175+
if (idx == 0) {
176+
flb_sds_cat_safe(clause, "(", 1);
177+
}
178+
else {
179+
flb_sds_cat_safe(clause, " AND ", 5);
180+
}
181+
182+
key = map.via.map.ptr[i].key;
183+
val = map.via.map.ptr[i].val;
184+
if (key.type == MSGPACK_OBJECT_BIN) {
185+
key_str = (char *) key.via.bin.ptr;
186+
key_str_size = key.via.bin.size;
187+
}
188+
else if (key.type == MSGPACK_OBJECT_STR) {
189+
key_str = (char *) key.via.str.ptr;
190+
key_str_size = key.via.str.size;
191+
}
192+
if (val.type == MSGPACK_OBJECT_BIN) {
193+
val_str = (char *) val.via.bin.ptr;
194+
val_str_size = val.via.bin.size;
195+
val_buf = flb_sds_create_len(val_str, val_str_size);
196+
if (val_buf == NULL) {
197+
flb_plg_error(ctx->ins, "val_buf creation is failed");
198+
ret = -3;
199+
200+
goto cleanup;
201+
}
202+
}
203+
else if (val.type == MSGPACK_OBJECT_STR) {
204+
val_str = (char *) val.via.str.ptr;
205+
val_str_size = val.via.str.size;
206+
val_buf = flb_sds_create_len(val_str, val_str_size);
207+
if (val_buf == NULL) {
208+
flb_plg_error(ctx->ins, "val_buf creation is failed");
209+
ret = -3;
210+
211+
goto cleanup;
212+
}
213+
}
214+
215+
if (val_str != NULL && strstr(val_buf, "%") != NULL) {
216+
use_like = FLB_TRUE;
217+
flb_sds_destroy(val_buf);
218+
}
219+
if (use_like == FLB_TRUE) {
220+
flb_sds_cat_safe(clause, "NOT ", 4);
221+
}
222+
flb_sds_cat_safe(clause, key_str, key_str_size);
223+
if (use_like == FLB_TRUE) {
224+
flb_sds_cat_safe(clause, " LIKE ", 6);
225+
}
226+
else {
227+
flb_sds_cat_safe(clause, "!=", 2);
228+
}
229+
flb_sds_cat_safe(clause, "'", 1);
230+
flb_sds_cat_safe(clause, val_str, val_str_size);
231+
flb_sds_cat_safe(clause, "'", 1);
232+
idx++;
233+
}
234+
flb_sds_cat_safe(clause, ")", 1);
235+
}
236+
msgpack_unpacked_destroy(&result);
237+
238+
return 0;
239+
240+
cleanup:
241+
msgpack_unpacked_destroy(&result);
242+
243+
return ret;
244+
}
245+
246+
static int construct_where_clause(struct flb_we *ctx)
247+
{
248+
int ret;
249+
flb_sds_t clause;
250+
251+
clause = flb_sds_create_size(256);
252+
if (!clause) {
253+
return -1;
254+
}
255+
256+
if (ctx->service_include_buffer != NULL && ctx->service_include_buffer_size > 0) {
257+
ret = construct_include_clause(ctx, &clause);
258+
if (ret != 0) {
259+
goto cleanup;
260+
}
261+
}
262+
263+
if (ctx->service_exclude_buffer != NULL && ctx->service_exclude_buffer_size > 0) {
264+
if (flb_sds_len(clause) > 0) {
265+
flb_sds_cat_safe(&clause, " AND ", 5);
266+
}
267+
ret = construct_exclude_clause(ctx, &clause);
268+
if (ret != 0) {
269+
goto cleanup;
270+
}
271+
}
272+
273+
if (ctx->raw_where_clause != NULL){
274+
if (flb_sds_len(clause) > 0) {
275+
flb_sds_cat_safe(&clause, " AND (", 6);
276+
flb_sds_cat_safe(&clause, ctx->raw_where_clause, strlen(ctx->raw_where_clause));
277+
flb_sds_cat_safe(&clause, ")", 1);
278+
}
279+
else {
280+
flb_sds_cat_safe(&clause, ctx->raw_where_clause, strlen(ctx->raw_where_clause));
281+
}
282+
}
283+
284+
if (flb_sds_len(clause) > 0) {
285+
ctx->wmi_service->info->where_clause = clause;
286+
}
287+
288+
return 0;
289+
290+
cleanup:
291+
flb_sds_destroy(clause);
292+
293+
return ret;
294+
}
295+
37296
int we_wmi_service_init(struct flb_we *ctx)
38297
{
298+
int ret;
39299
struct cmt_gauge *g;
40300

41301
ctx->wmi_service = flb_calloc(1, sizeof(struct we_wmi_service_counters));
@@ -91,7 +351,11 @@ int we_wmi_service_init(struct flb_we *ctx)
91351
ctx->wmi_service->info->wmi_property = "";
92352
ctx->wmi_service->info->label_property_count = 0;
93353
ctx->wmi_service->info->label_property_keys = NULL;
94-
ctx->wmi_service->info->where_clause = ctx->raw_where_clause;
354+
ctx->wmi_service->info->where_clause = NULL;
355+
ret = construct_where_clause(ctx);
356+
if (ret != 0) {
357+
return ret;
358+
}
95359

96360
ctx->wmi_service->operational = FLB_TRUE;
97361

@@ -102,6 +366,9 @@ int we_wmi_service_exit(struct flb_we *ctx)
102366
{
103367
ctx->wmi_service->operational = FLB_FALSE;
104368

369+
if (ctx->wmi_service->info->where_clause != NULL) {
370+
flb_sds_destroy(ctx->wmi_service->info->where_clause);
371+
}
105372
flb_free(ctx->wmi_service->info);
106373
flb_free(ctx->wmi_service);
107374

0 commit comments

Comments
 (0)