Skip to content

Commit c15919b

Browse files
committed
in_windows_exporter_metrics: Implement user-defined performance counters w/ PDH
Signed-off-by: Hiroshi Hatake <hiroshi@chronosphere.io>
1 parent f317143 commit c15919b

5 files changed

Lines changed: 639 additions & 0 deletions

File tree

plugins/in_windows_exporter_metrics/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ set(src
1010
we_util.c
1111
we_metric.c
1212
we_cache.c
13+
we_performancecounter.c
1314
we_perflib.c
1415
we_wmi_thermalzone.c
1516
we_wmi_cpu_info.c
@@ -26,6 +27,7 @@ set(libs
2627
wbemuuid
2728
netapi32
2829
iphlpapi
30+
pdh
2931
)
3032

3133
FLB_PLUGIN(in_windows_exporter_metrics "${src}" "${libs}")

plugins/in_windows_exporter_metrics/we.c

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "we_logical_disk.h"
3636
#include "we_cs.h"
3737
#include "we_cache.h"
38+
#include "we_performancecounter.h"
3839

3940
/* wmi collectors */
4041
#include "we_wmi_cpu_info.h"
@@ -197,6 +198,17 @@ static int we_timer_wmi_tcp_metrics_cb(struct flb_input_instance *ins,
197198
return 0;
198199
}
199200

201+
static int we_timer_performancecounter_metrics_cb(struct flb_input_instance *ins,
202+
struct flb_config *config,
203+
void *in_context)
204+
{
205+
struct flb_we *ctx = in_context;
206+
207+
we_performancecounter_update(ctx);
208+
209+
return 0;
210+
}
211+
200212
struct flb_we_callback {
201213
char *name;
202214
void (*func)(char *, void *, void *);
@@ -354,6 +366,13 @@ static void we_wmi_tcp_update_cb(char *name, void *p1, void *p2)
354366
we_wmi_tcp_update(ctx);
355367
}
356368

369+
static void we_performancecounter_update_cb(char *name, void *p1, void *p2)
370+
{
371+
struct flb_we *ctx = p1;
372+
373+
we_performancecounter_update(ctx);
374+
}
375+
357376
static int we_update_cb(struct flb_we *ctx, char *name)
358377
{
359378
int ret;
@@ -382,6 +401,7 @@ struct flb_we_callback ne_callbacks[] = {
382401
{ "paging_file", we_wmi_paging_file_update_cb },
383402
{ "process", we_wmi_process_update_cb },
384403
{ "tcp", we_wmi_tcp_update_cb },
404+
{ "performancecounter", we_performancecounter_update_cb },
385405
{ 0 }
386406
};
387407

@@ -421,6 +441,7 @@ static int in_we_init(struct flb_input_instance *in,
421441
ctx->coll_wmi_paging_file_fd = -1;
422442
ctx->coll_wmi_process_fd = -1;
423443
ctx->coll_wmi_tcp_fd = -1;
444+
ctx->coll_performancecounter_fd = -1;
424445

425446
ctx->callback = flb_callback_create(in->name);
426447
if (!ctx->callback) {
@@ -474,6 +495,7 @@ static int in_we_init(struct flb_input_instance *in,
474495
if (ctx->metrics) {
475496
mk_list_foreach(head, ctx->metrics) {
476497
entry = mk_list_entry(head, struct flb_slist_entry, _head);
498+
metric_idx = -1;
477499
ret = flb_callback_exists(ctx->callback, entry->str);
478500

479501
if (ret == FLB_FALSE) {
@@ -851,6 +873,32 @@ static int in_we_init(struct flb_input_instance *in,
851873
return -1;
852874
}
853875
}
876+
else if (strncmp(entry->str, "performancecounter", 18) == 0) {
877+
if (ctx->performancecounter_scrape_interval == 0) {
878+
flb_plg_debug(ctx->ins, "enabled metrics %s", entry->str);
879+
metric_idx = 15;
880+
}
881+
else {
882+
/* Create the performancecounter collector */
883+
ret = flb_input_set_collector_time(
884+
in,
885+
we_timer_performancecounter_metrics_cb,
886+
ctx->performancecounter_scrape_interval, 0,
887+
config);
888+
if (ret == -1) {
889+
flb_plg_error(ctx->ins,
890+
"could not set performancecounter collector "
891+
"for Windows Exporter Metrics plugin");
892+
return -1;
893+
}
894+
ctx->coll_performancecounter_fd = ret;
895+
}
896+
897+
ret = we_performancecounter_init(ctx);
898+
if (ret) {
899+
return -1;
900+
}
901+
}
854902
else {
855903
flb_plg_warn(ctx->ins, "Unknown metrics: %s", entry->str);
856904
metric_idx = -1;
@@ -939,6 +987,9 @@ static int in_we_exit(void *data, struct flb_config *config)
939987
else if (strncmp(entry->str, "tcp", 3) == 0) {
940988
we_wmi_tcp_exit(ctx);
941989
}
990+
else if (strncmp(entry->str, "performancecounter", 18) == 0) {
991+
we_performancecounter_exit(ctx);
992+
}
942993
else {
943994
flb_plg_warn(ctx->ins, "Unknown metrics: %s", entry->str);
944995
}
@@ -994,6 +1045,9 @@ static int in_we_exit(void *data, struct flb_config *config)
9941045
if (ctx->coll_wmi_tcp_fd != -1) {
9951046
we_wmi_tcp_exit(ctx);
9961047
}
1048+
if (ctx->coll_performancecounter_fd != -1) {
1049+
we_performancecounter_exit(ctx);
1050+
}
9971051

9981052
flb_we_config_destroy(ctx);
9991053

@@ -1052,6 +1106,9 @@ static void in_we_pause(void *data, struct flb_config *config)
10521106
if (ctx->coll_wmi_tcp_fd != -1) {
10531107
flb_input_collector_pause(ctx->coll_wmi_tcp_fd, ctx->ins);
10541108
}
1109+
if (ctx->coll_performancecounter_fd != -1) {
1110+
flb_input_collector_pause(ctx->coll_performancecounter_fd, ctx->ins);
1111+
}
10551112
}
10561113

10571114
static void in_we_resume(void *data, struct flb_config *config)
@@ -1109,6 +1166,9 @@ static void in_we_resume(void *data, struct flb_config *config)
11091166
if (ctx->coll_wmi_tcp_fd != -1) {
11101167
flb_input_collector_resume(ctx->coll_wmi_tcp_fd, ctx->ins);
11111168
}
1169+
if (ctx->coll_performancecounter_fd != -1) {
1170+
flb_input_collector_resume(ctx->coll_performancecounter_fd, ctx->ins);
1171+
}
11121172
}
11131173

11141174
/* Configuration properties map */
@@ -1209,6 +1269,12 @@ static struct flb_config_map config_map[] = {
12091269
"scrape interval to collect tcp metrics from the node."
12101270
},
12111271

1272+
{
1273+
FLB_CONFIG_MAP_TIME, "collector.performancecounter.scrape_interval", "0",
1274+
0, FLB_TRUE, offsetof(struct flb_we, performancecounter_scrape_interval),
1275+
"scrape interval to collect performancecounter metrics from the node."
1276+
},
1277+
12121278
{
12131279
FLB_CONFIG_MAP_CLIST, "metrics",
12141280
"cpu,cpu_info,os,net,logical_disk,cs,cache,thermalzone,logon,system,service,tcp",
@@ -1255,6 +1321,11 @@ static struct flb_config_map config_map[] = {
12551321
0, FLB_TRUE, offsetof(struct flb_we, raw_denying_process),
12561322
"Specify the regex for process metrics to prevent collection of/ignore."
12571323
},
1324+
{
1325+
FLB_CONFIG_MAP_STR, "PerformanceCounter", NULL,
1326+
FLB_CONFIG_MAP_MULT, FLB_TRUE, offsetof(struct flb_we, raw_performance_counters),
1327+
"Windows Performance Counter in name=counter_path form."
1328+
},
12581329
/* EOF */
12591330
{0}
12601331
};

plugins/in_windows_exporter_metrics/we.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131

3232
#include <windows.h>
3333
#include <wbemidl.h>
34+
#include <pdh.h>
35+
#include <pdhmsg.h>
3436

3537
#include "we_metric.h"
3638

@@ -248,6 +250,26 @@ struct we_wmi_tcp_counters {
248250
struct cmt_gauge *segments_sent_total;
249251
};
250252

253+
struct we_performancecounter_counter {
254+
char *name;
255+
char *path;
256+
wchar_t *path_w;
257+
char *instance;
258+
char *label_values[1];
259+
PDH_HCOUNTER handle;
260+
struct cmt_gauge *metric;
261+
int label_count;
262+
int valid;
263+
int seen_valid;
264+
struct mk_list _head;
265+
};
266+
267+
struct we_performancecounter_counters {
268+
PDH_HQUERY query;
269+
int operational;
270+
struct mk_list counters;
271+
};
272+
251273
struct we_os_counters {
252274
struct cmt_gauge *info;
253275
struct cmt_gauge *users;
@@ -286,6 +308,7 @@ struct flb_we {
286308
char *raw_service_exclude;
287309
char *raw_allowing_process;
288310
char *raw_denying_process;
311+
struct mk_list *raw_performance_counters;
289312
char *service_include_buffer;
290313
int service_include_buffer_size;
291314
char *service_exclude_buffer;
@@ -324,6 +347,7 @@ struct flb_we {
324347
int wmi_paging_file_scrape_interval;
325348
int wmi_process_scrape_interval;
326349
int wmi_tcp_scrape_interval;
350+
int performancecounter_scrape_interval;
327351

328352
int coll_cpu_fd; /* collector fd (cpu) */
329353
int coll_net_fd; /* collector fd (net) */
@@ -340,6 +364,7 @@ struct flb_we {
340364
int coll_wmi_paging_file_fd; /* collector fd (wmi_paging_file) */
341365
int coll_wmi_process_fd; /* collector fd (wmi_process) */
342366
int coll_wmi_tcp_fd; /* collector fd (wmi_tcp) */
367+
int coll_performancecounter_fd; /* collector fd (performancecounter) */
343368

344369
/*
345370
* Metrics Contexts
@@ -361,6 +386,7 @@ struct flb_we {
361386
struct we_wmi_paging_file_counters *wmi_paging_file;
362387
struct we_wmi_process_counters *wmi_process;
363388
struct we_wmi_tcp_counters *wmi_tcp;
389+
struct we_performancecounter_counters *performancecounter;
364390
};
365391

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

0 commit comments

Comments
 (0)