Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions plugins/in_windows_exporter_metrics/we.h
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ struct flb_we {
/* WMI locator and service contexts */
IWbemLocator *locator;
IWbemServices *service;
IWbemContext *wmi_context;

float windows_version;

Expand Down
2 changes: 1 addition & 1 deletion plugins/in_windows_exporter_metrics/we_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ wchar_t* we_convert_str(char *str)
return NULL;
}

buf = flb_calloc(1, sizeof(PWSTR) * size);
buf = flb_calloc(1, sizeof(wchar_t) * size);
if (buf == NULL) {
flb_errno();
return NULL;
Expand Down
80 changes: 75 additions & 5 deletions plugins/in_windows_exporter_metrics/we_wmi.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ static int wmi_coinitialize(struct flb_we *ctx, char* wmi_namespace)
IWbemServices *service = 0;
HRESULT hr;
wchar_t *wnamespace;
BSTR bstrNamespace;
SYSTEM_INFO sysInfo;
VARIANT vArch;
VARIANT vReq;

flb_plg_debug(ctx->ins, "initializing WMI instance....");

Expand Down Expand Up @@ -69,27 +73,62 @@ static int wmi_coinitialize(struct flb_we *ctx, char* wmi_namespace)
}
ctx->locator = locator;

hr = CoCreateInstance(&CLSID_WbemContext, 0, CLSCTX_INPROC_SERVER, &IID_IWbemContext, (LPVOID *) &ctx->wmi_context);
if (SUCCEEDED(hr) && ctx->wmi_context != NULL) {
GetNativeSystemInfo(&sysInfo);

VariantInit(&vArch);
vArch.vt = VT_I4;

if (sysInfo.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64 ||
sysInfo.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_ARM64) {
vArch.lVal = 64;
}
else {
vArch.lVal = 32;
}

ctx->wmi_context->lpVtbl->SetValue(ctx->wmi_context, L"__ProviderArchitecture", 0, &vArch);
VariantClear(&vArch);

VariantInit(&vReq);
vReq.vt = VT_BOOL;
vReq.boolVal = VARIANT_TRUE;
ctx->wmi_context->lpVtbl->SetValue(ctx->wmi_context, L"__RequiredArchitecture", 0, &vReq);
Comment thread
cosmo0920 marked this conversation as resolved.
VariantClear(&vReq);
}

if (wmi_namespace == NULL) {
wnamespace = we_convert_str("ROOT\\CIMV2");
}
else {
wnamespace = we_convert_str(wmi_namespace);
}

bstrNamespace = SysAllocString(wnamespace);

/* Connect WMI server */
hr = locator->lpVtbl->ConnectServer(locator,
wnamespace,
bstrNamespace,
NULL,
NULL,
0,
0,
0,
NULL,
ctx->wmi_context,
&service);

SysFreeString(bstrNamespace);
flb_free(wnamespace);

if (FAILED(hr)) {
flb_plg_error(ctx->ins, "Could not connect. Error code = %x", hr);
locator->lpVtbl->Release(locator);
ctx->locator = NULL;
if (ctx->wmi_context != NULL) {
ctx->wmi_context->lpVtbl->Release(ctx->wmi_context);
ctx->wmi_context = NULL;
}
CoUninitialize();
return hr;
}
Expand All @@ -109,6 +148,12 @@ static int wmi_coinitialize(struct flb_we *ctx, char* wmi_namespace)
flb_plg_error(ctx->ins, "Could not set proxy blanket. Error code = %x", hr);
service->lpVtbl->Release(service);
locator->lpVtbl->Release(locator);
ctx->service = NULL;
ctx->locator = NULL;
if (ctx->wmi_context != NULL) {
ctx->wmi_context->lpVtbl->Release(ctx->wmi_context);
ctx->wmi_context = NULL;
}
CoUninitialize();
return hr;
}
Expand Down Expand Up @@ -299,6 +344,8 @@ static inline int wmi_execute_query(struct flb_we *ctx, struct wmi_query_spec *s
char *query = NULL;
IEnumWbemClassObject* enumerator = NULL;
size_t size;
BSTR bstrLanguage;
BSTR bstrQuery;

size = 14 + strlen(spec->wmi_counter);
if (spec->where_clause != NULL) {
Expand All @@ -319,14 +366,20 @@ static inline int wmi_execute_query(struct flb_we *ctx, struct wmi_query_spec *s
wquery = we_convert_str(query);
flb_free(query);

bstrLanguage = SysAllocString(L"WQL");
bstrQuery = SysAllocString(wquery);

hr = ctx->service->lpVtbl->ExecQuery(
ctx->service,
L"WQL",
wquery,
bstrLanguage,
bstrQuery,
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
NULL,
ctx->wmi_context,
&enumerator);

SysFreeString(bstrLanguage);
SysFreeString(bstrQuery);

flb_free(wquery);

if (FAILED(hr)) {
Expand Down Expand Up @@ -363,6 +416,12 @@ static int wmi_exec_query_fixed_val(struct flb_we *ctx, struct wmi_query_spec *s
&class_obj, &ret);

if(0 == ret) {
if (FAILED(hr)) {
flb_plg_error(ctx->ins, "WMI Next() failed for fixed val. Error code = %x", hr);
}
else {
flb_plg_debug(ctx->ins, "WMI Next() returned 0 items for %s", spec->wmi_counter);
}
break;
}

Expand Down Expand Up @@ -397,6 +456,12 @@ static int wmi_exec_query(struct flb_we *ctx, struct wmi_query_spec *spec)
&class_obj, &ret);

if(0 == ret) {
if (FAILED(hr)) {
flb_plg_error(ctx->ins, "WMI Next() failed for query. Error code = %x", hr);
}
else {
flb_plg_debug(ctx->ins, "WMI Next() returned 0 items for %s", spec->wmi_counter);
}
break;
}

Expand Down Expand Up @@ -425,6 +490,10 @@ static int wmi_cleanup(struct flb_we *ctx)
ctx->locator->lpVtbl->Release(ctx->locator);
ctx->locator = NULL;
}
if (ctx->wmi_context != NULL) {
ctx->wmi_context->lpVtbl->Release(ctx->wmi_context);
ctx->wmi_context = NULL;
}
CoUninitialize();

return 0;
Expand Down Expand Up @@ -476,6 +545,7 @@ int we_wmi_init(struct flb_we *ctx)
{
ctx->locator = NULL;
ctx->service = NULL;
ctx->wmi_context = NULL;

return 0;
}
Expand Down
Loading