@@ -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+
37296int 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