Skip to content

Commit 642d97f

Browse files
cosmo0920edsiper
authored andcommitted
in_windows_exporter_metrics: Implement Windows Service metrics
Signed-off-by: Hiroshi Hatake <[email protected]>
1 parent 7173f37 commit 642d97f

File tree

11 files changed

+388
-3
lines changed

11 files changed

+388
-3
lines changed

plugins/in_windows_exporter_metrics/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ set(src
1414
we_wmi_cpu_info.c
1515
we_wmi_logon.c
1616
we_wmi_system.c
17+
we_wmi_service.c
1718
)
1819

1920
set(libs

plugins/in_windows_exporter_metrics/we.c

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@
3535
#include "we_logical_disk.h"
3636
#include "we_cs.h"
3737

38+
/* wmi collectors */
39+
#include "we_wmi_cpu_info.h"
40+
#include "we_wmi_logon.h"
41+
#include "we_wmi_system.h"
42+
#include "we_wmi_thermalzone.h"
43+
#include "we_wmi_service.h"
44+
3845
static int we_timer_cpu_metrics_cb(struct flb_input_instance *ins,
3946
struct flb_config *config, void *in_context)
4047
{
@@ -125,6 +132,16 @@ static int we_timer_wmi_system_metrics_cb(struct flb_input_instance *ins,
125132
return 0;
126133
}
127134

135+
static int we_timer_wmi_service_metrics_cb(struct flb_input_instance *ins,
136+
struct flb_config *config, void *in_context)
137+
{
138+
struct flb_ne *ctx = in_context;
139+
140+
we_wmi_service_update(ctx);
141+
142+
return 0;
143+
}
144+
128145
struct flb_we_callback {
129146
char *name;
130147
void (*func)(char *, void *, void *);
@@ -240,6 +257,13 @@ static void we_wmi_system_update_cb(char *name, void *p1, void *p2)
240257
we_wmi_system_update(ctx);
241258
}
242259

260+
static void we_wmi_service_update_cb(char *name, void *p1, void *p2)
261+
{
262+
struct flb_we *ctx = p1;
263+
264+
we_wmi_service_update(ctx);
265+
}
266+
243267
static int we_update_cb(struct flb_we *ctx, char *name)
244268
{
245269
int ret;
@@ -262,6 +286,7 @@ struct flb_we_callback ne_callbacks[] = {
262286
{ "thermalzone", we_wmi_thermalzone_update_cb },
263287
{ "logon", we_wmi_logon_update_cb },
264288
{ "system", we_wmi_system_update_cb },
289+
{ "service", we_wmi_service_update_cb },
265290
{ 0 }
266291
};
267292

@@ -295,6 +320,7 @@ static int in_we_init(struct flb_input_instance *in,
295320
ctx->coll_wmi_cpu_info_fd = -1;
296321
ctx->coll_wmi_logon_fd = -1;
297322
ctx->coll_wmi_system_fd = -1;
323+
ctx->coll_wmi_service_fd = -1;
298324

299325
ctx->callback = flb_callback_create(in->name);
300326
if (!ctx->callback) {
@@ -574,6 +600,31 @@ static int in_we_init(struct flb_input_instance *in,
574600
return -1;
575601
}
576602
}
603+
else if (strncmp(entry->str, "service", 7) == 0) {
604+
if (ctx->wmi_service_scrape_interval == 0) {
605+
flb_plg_debug(ctx->ins, "enabled metrics %s", entry->str);
606+
metric_idx = 9;
607+
}
608+
else {
609+
/* Create the service collector */
610+
ret = flb_input_set_collector_time(in,
611+
we_timer_wmi_service_metrics_cb,
612+
ctx->wmi_service_scrape_interval, 0,
613+
config);
614+
if (ret == -1) {
615+
flb_plg_error(ctx->ins,
616+
"could not set service collector for Windows Exporter Metrics plugin");
617+
return -1;
618+
}
619+
ctx->coll_wmi_service_fd = ret;
620+
}
621+
622+
/* Initialize service metric collectors */
623+
ret = we_wmi_service_init(ctx);
624+
if (ret) {
625+
return -1;
626+
}
627+
}
577628
else {
578629
flb_plg_warn(ctx->ins, "Unknown metrics: %s", entry->str);
579630
metric_idx = -1;
@@ -644,6 +695,9 @@ static int in_we_exit(void *data, struct flb_config *config)
644695
else if (strncmp(entry->str, "system", 6) == 0) {
645696
we_wmi_system_exit(ctx);
646697
}
698+
else if (strncmp(entry->str, "service", 7) == 0) {
699+
we_wmi_service_exit(ctx);
700+
}
647701
else {
648702
flb_plg_warn(ctx->ins, "Unknown metrics: %s", entry->str);
649703
}
@@ -681,6 +735,9 @@ static int in_we_exit(void *data, struct flb_config *config)
681735
if (ctx->coll_wmi_system_fd != -1) {
682736
we_wmi_system_exit(ctx);
683737
}
738+
if (ctx->coll_wmi_service_fd != -1) {
739+
we_wmi_service_exit(ctx);
740+
}
684741

685742
flb_we_config_destroy(ctx);
686743

@@ -721,6 +778,9 @@ static void in_we_pause(void *data, struct flb_config *config)
721778
if (ctx->coll_wmi_system_fd != -1) {
722779
flb_input_collector_pause(ctx->coll_wmi_system_fd, ctx->ins);
723780
}
781+
if (ctx->coll_wmi_service_fd != -1) {
782+
flb_input_collector_pause(ctx->coll_wmi_service_fd, ctx->ins);
783+
}
724784
}
725785

726786
static void in_we_resume(void *data, struct flb_config *config)
@@ -757,6 +817,9 @@ static void in_we_resume(void *data, struct flb_config *config)
757817
if (ctx->coll_wmi_system_fd != -1) {
758818
flb_input_collector_resume(ctx->coll_wmi_system_fd, ctx->ins);
759819
}
820+
if (ctx->coll_wmi_service_fd != -1) {
821+
flb_input_collector_resume(ctx->coll_wmi_service_fd, ctx->ins);
822+
}
760823
}
761824

762825
/* Configuration properties map */
@@ -822,9 +885,14 @@ static struct flb_config_map config_map[] = {
822885
0, FLB_TRUE, offsetof(struct flb_we, wmi_system_scrape_interval),
823886
"scrape interval to collect system metrics from the node."
824887
},
888+
{
889+
FLB_CONFIG_MAP_TIME, "collector.service.scrape_interval", "0",
890+
0, FLB_TRUE, offsetof(struct flb_we, wmi_service_scrape_interval),
891+
"scrape interval to collect service metrics from the node."
892+
},
825893
{
826894
FLB_CONFIG_MAP_CLIST, "metrics",
827-
"cpu,cpu_info,os,net,logical_disk,cs,thermalzone,logon,system",
895+
"cpu,cpu_info,os,net,logical_disk,cs,thermalzone,logon,system,service",
828896
0, FLB_TRUE, offsetof(struct flb_we, metrics),
829897
"Comma separated list of keys to enable metrics."
830898
},
@@ -843,6 +911,11 @@ static struct flb_config_map config_map[] = {
843911
0, FLB_TRUE, offsetof(struct flb_we, raw_allowing_nic),
844912
"Specify to be scribable regex for net metrics by name of NIC."
845913
},
914+
{
915+
FLB_CONFIG_MAP_STR, "we.service.where", NULL,
916+
0, FLB_TRUE, offsetof(struct flb_we, raw_where_clause),
917+
"Specify the where clause for retrieving service metrics."
918+
},
846919
/* EOF */
847920
{0}
848921
};

plugins/in_windows_exporter_metrics/we.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,15 @@ struct we_wmi_system_counters {
145145
int operational;
146146
};
147147

148+
struct we_wmi_service_counters {
149+
struct wmi_query_spec *info;
150+
struct cmt_gauge *information;
151+
struct cmt_gauge *state;
152+
struct cmt_gauge *start_mode;
153+
struct cmt_gauge *status;
154+
int operational;
155+
};
156+
148157
struct we_os_counters {
149158
struct cmt_gauge *info;
150159
struct cmt_gauge *users;
@@ -178,6 +187,7 @@ struct flb_we {
178187
char *raw_allowing_disk;
179188
char *raw_denying_disk;
180189
char *raw_allowing_nic;
190+
char *raw_where_clause;
181191

182192
struct flb_regex *allowing_disk_regex;
183193
struct flb_regex *denying_disk_regex;
@@ -203,6 +213,7 @@ struct flb_we {
203213
int wmi_cpu_info_scrape_interval;
204214
int wmi_logon_scrape_interval;
205215
int wmi_system_scrape_interval;
216+
int wmi_service_scrape_interval;
206217

207218
int coll_cpu_fd; /* collector fd (cpu) */
208219
int coll_net_fd; /* collector fd (net) */
@@ -213,6 +224,7 @@ struct flb_we {
213224
int coll_wmi_cpu_info_fd; /* collector fd (wmi_cpu_info) */
214225
int coll_wmi_logon_fd; /* collector fd (wmi_logon) */
215226
int coll_wmi_system_fd; /* collector fd (wmi_system) */
227+
int coll_wmi_service_fd; /* collector fd (wmi_service) */
216228

217229
/*
218230
* Metrics Contexts
@@ -228,7 +240,7 @@ struct flb_we {
228240
struct we_wmi_cpu_info_counters *wmi_cpu_info;
229241
struct we_wmi_logon_counters *wmi_logon;
230242
struct we_wmi_system_counters *wmi_system;
231-
243+
struct we_wmi_service_counters *wmi_service;
232244
};
233245

234246
typedef int (*collector_cb)(struct flb_we *);

plugins/in_windows_exporter_metrics/we_wmi.c

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,28 @@ static double wmi_get_property_value(struct flb_we *ctx, char *raw_property_key,
231231
return val;
232232
}
233233

234+
static char *wmi_get_property_str_value(struct flb_we *ctx, char *raw_property_key,
235+
IWbemClassObject *class_obj)
236+
{
237+
VARIANT prop;
238+
char *strprop;
239+
char *str_val = NULL;
240+
HRESULT hr;
241+
wchar_t *wproperty;
242+
243+
244+
VariantInit(&prop);
245+
wproperty = we_convert_str(raw_property_key);
246+
hr = class_obj->lpVtbl->Get(class_obj, wproperty, 0, &prop, 0, 0);
247+
if (FAILED(hr)) {
248+
flb_plg_warn(ctx->ins, "Retrive prop failed. Error code = %x", hr);
249+
}
250+
str_val = convert_prop_to_str(&prop, FLB_TRUE);
251+
VariantClear(&prop);
252+
flb_free(wproperty);
253+
254+
return str_val;
255+
}
234256

235257
static inline int wmi_update_metrics(struct flb_we *ctx, struct wmi_query_spec *spec,
236258
double val, IWbemClassObject *class_obj, uint64_t timestamp)
@@ -279,12 +301,21 @@ static inline int wmi_execute_query(struct flb_we *ctx, struct wmi_query_spec *s
279301
size_t size;
280302

281303
size = 14 + strlen(spec->wmi_counter);
304+
if (spec->where_clause != NULL) {
305+
size += 7 + strlen(spec->where_clause);
306+
}
282307
query = flb_calloc(size, sizeof(char *));
283308
if (!query) {
284309
flb_errno();
285310
return -1;
286311
}
287-
snprintf(query, size, "SELECT * FROM %s", spec->wmi_counter);
312+
if (spec->where_clause != NULL) {
313+
snprintf(query, size, "SELECT * FROM %s WHERE %s", spec->wmi_counter, spec->where_clause);
314+
}
315+
else {
316+
snprintf(query, size, "SELECT * FROM %s", spec->wmi_counter);
317+
}
318+
flb_trace("[wmi] query = %s", query);
288319
wquery = we_convert_str(query);
289320
flb_free(query);
290321

@@ -519,6 +550,12 @@ double we_wmi_get_property_value(struct flb_we *ctx, char *raw_property_key, IWb
519550
return wmi_get_property_value(ctx, raw_property_key, class_obj);
520551
}
521552

553+
char *we_wmi_get_property_str_value(struct flb_we *ctx, char *raw_property_key,
554+
IWbemClassObject *class_obj)
555+
{
556+
return wmi_get_property_str_value(ctx, raw_property_key, class_obj);
557+
}
558+
522559
int we_wmi_update_counters(struct flb_we *ctx, struct wmi_query_spec *spec, uint64_t timestamp, double val, int metric_label_count, char **metric_label_set)
523560
{
524561
wmi_update_counters(spec, timestamp, val, metric_label_count, metric_label_set);

plugins/in_windows_exporter_metrics/we_wmi.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ struct wmi_query_spec {
3434
char *wmi_property;
3535
int label_property_count;
3636
char **label_property_keys;
37+
char *where_clause;
3738
};
3839

3940
int we_wmi_init(struct flb_we *ctx);
@@ -50,6 +51,8 @@ int we_wmi_coinitialize(struct flb_we *ctx);
5051
int we_wmi_execute_query(struct flb_we *ctx, struct wmi_query_spec *spec, IEnumWbemClassObject **out_enumerator);
5152
double we_wmi_get_value(struct flb_we *ctx, struct wmi_query_spec *spec, IWbemClassObject *class_obj);
5253
double we_wmi_get_property_value(struct flb_we *ctx, char *raw_property_key, IWbemClassObject *class_obj);
54+
char *we_wmi_get_property_str_value(struct flb_we *ctx, char *raw_property_key,
55+
IWbemClassObject *class_obj);
5356
int we_wmi_update_counters(struct flb_we *ctx, struct wmi_query_spec *spec,
5457
uint64_t timestamp, double val, int metric_label_count, char **metric_label_set);
5558

plugins/in_windows_exporter_metrics/we_wmi_cpu_info.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ int we_wmi_cpu_info_init(struct flb_we *ctx)
8484
ctx->wmi_cpu_info->info->label_property_keys[4] = "l2cachesize" ;
8585
ctx->wmi_cpu_info->info->label_property_keys[5] = "l3cachesize" ;
8686
ctx->wmi_cpu_info->info->label_property_keys[6] = "name" ;
87+
ctx->wmi_cpu_info->info->where_clause = NULL;
8788

8889
ctx->wmi_cpu_info->operational = FLB_TRUE;
8990

plugins/in_windows_exporter_metrics/we_wmi_logon.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ int we_wmi_logon_init(struct flb_we *ctx)
7070
ctx->wmi_logon->info->wmi_property = "LogonType";
7171
ctx->wmi_logon->info->label_property_count = 1;
7272
ctx->wmi_logon->info->label_property_keys[0] = "status" ;
73+
ctx->wmi_logon->info->where_clause = NULL;
7374

7475
ctx->wmi_logon->operational = FLB_TRUE;
7576

0 commit comments

Comments
 (0)