Adjust hardware collection (windows.plugin) (#21433)
thiagoftsm committed
Dec 11, 2025 at 12:28 UTC
9b023c394771ec2ed6eeebca04528ebe0d2ee8f0
4 files changed
+66
-25
src/collectors/windows.plugin/GetHardwareInfo.c
+7
-7
@@ -236,7 +236,7 @@ static void netdata_detect_cpu()
236
temperature_fcnt = netdata_amd_cpu_temp;
237
}
238
239
-static int initialize(int update_every)
239
+static int initialize()
240
{
241
netdata_detect_cpu();
242
if (!temperature_fcnt) {
@@ -254,7 +254,7 @@ static int initialize(int update_every)
254
ncpus = os_get_system_cpus();
255
cpus = callocz(ncpus, sizeof(struct cpu_data));
256
257
- hardware_info_thread = nd_thread_create("hi_threads", NETDATA_THREAD_OPTION_DEFAULT, get_hardware_info_thread, &update_every);
257
+ hardware_info_thread = nd_thread_create("hi_threads", NETDATA_THREAD_OPTION_DEFAULT, get_hardware_info_thread, NULL);
258
259
return 0;
260
}
@@ -270,7 +270,7 @@ static RRDSET *netdata_publish_cpu_chart(int update_every)
270
"temperature",
271
"cpu.temperature",
272
"Core temperature",
273
- "Celcius",
273
+ "Celsius",
274
PLUGIN_WINDOWS_NAME,
275
"GetHardwareInfo",
276
NETDATA_CHART_PRIO_CPU_TEMPERATURE,
@@ -284,11 +284,11 @@ static RRDSET *netdata_publish_cpu_chart(int update_every)
284
static void netdata_loop_cpu_chart(int update_every)
285
{
286
RRDSET *chart = netdata_publish_cpu_chart(update_every);
287
- for (size_t i = 0; i < ncpus; i++) {
287
+ for (int i = 0; i < (int)ncpus; i++) {
288
struct cpu_data *lcpu = &cpus[i];
289
if (!lcpu->rd_cpu_temp) {
290
char id[RRD_ID_LENGTH_MAX + 1];
291
- snprintfz(id, RRD_ID_LENGTH_MAX, "cpu%lu.temp", i);
291
+ snprintfz(id, RRD_ID_LENGTH_MAX, "cpu%d.temp", i);
292
lcpu->rd_cpu_temp = rrddim_add(chart, id, NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
293
}
294
rrddim_set_by_pointer(chart, lcpu->rd_cpu_temp, lcpu->cpu_temp);
@@ -301,7 +301,7 @@ int do_GetHardwareInfo(int update_every, usec_t dt __maybe_unused)
301
static bool initialized = false;
302
if (unlikely(!initialized)) {
303
initialized = true;
304
- if (initialize(update_every)) {
304
+ if (initialize()) {
305
return -1;
306
}
307
}
@@ -314,7 +314,7 @@ int do_GetHardwareInfo(int update_every, usec_t dt __maybe_unused)
314
void do_GetHardwareInfo_cleanup()
315
{
316
if (nd_thread_join(hardware_info_thread))
317
- nd_log_daemon(NDLP_ERR, "Failed to join mssql queries thread");
317
+ nd_log_daemon(NDLP_ERR, "Failed to join Get Hardware Info thread");
318
319
netdata_stop_driver();
320
}
src/collectors/windows.plugin/GetPowerSupply.c
+12
-6
@@ -40,7 +40,7 @@ static inline void netdata_update_power_supply_values(
40
if (bs.Capacity != BATTERY_UNKNOWN_CAPACITY) {
41
NETDATA_DOUBLE num = bs.Capacity;
42
NETDATA_DOUBLE den = bi->FullChargedCapacity;
43
- num /= den;
43
+ num = (den) ? num/ den : 0;
44
45
power_supply_root->capacity->value = (unsigned long long)(num * 100.0);
46
}
@@ -84,7 +84,7 @@ int do_GetPowerSupply(int update_every, usec_t dt __maybe_unused)
84
SP_DEVICE_INTERFACE_DATA did = {0};
85
did.cbSize = sizeof(did);
86
87
- for (LONG i = 0; i < 32 && SetupDiEnumDeviceInterfaces(hdev, 0, &GUID_DEVCLASS_BATTERY, 0, &did); i++) {
87
+ for (LONG i = 0; i < 32 && SetupDiEnumDeviceInterfaces(hdev, 0, &GUID_DEVCLASS_BATTERY, i, &did); i++) {
88
DWORD cbRequired = 0;
89
PSP_DEVICE_INTERFACE_DETAIL_DATA pdidd = NULL;
90
HANDLE hBattery = NULL;
@@ -124,8 +124,8 @@ int do_GetPowerSupply(int update_every, usec_t dt __maybe_unused)
124
&bqi.BatteryTag,
125
sizeof(bqi.BatteryTag),
126
&dwOut,
127
- NULL) &&
128
- bqi.BatteryTag)
127
+ NULL) ||
128
+ !bqi.BatteryTag)
129
goto endPowerSupply;
130
131
BATTERY_INFORMATION bi = {0};
@@ -141,8 +141,14 @@ int do_GetPowerSupply(int update_every, usec_t dt __maybe_unused)
141
char name[RRD_ID_LENGTH_MAX + 1];
142
snprintfz(name, sizeof(name), "BAT%d", i + 1);
143
144
- power_supply_root->name = name;
145
- power_supply_root->capacity->filename = power_supply_root->name;
144
+ if (likely(power_supply_root->name))
145
+ freez(power_supply_root->name);
146
+ if (likely(power_supply_root->capacity->filename))
147
+ freez(power_supply_root->capacity->filename);
148
+
149
+ power_supply_root->name = power_supply_root->capacity->filename = NULL;
150
+ power_supply_root->name = strdupz(name);
151
+ power_supply_root->capacity->filename = strdupz(power_supply_root->name);
152
153
netdata_update_power_supply_values(hBattery, &voltage, &bi, &bqi);
154
src/collectors/windows.plugin/GetSensors.c
+28
-8
@@ -346,23 +346,37 @@ struct sensor_data {
346
DICTIONARY *sensors;
347
348
// Microsoft appends additional data
349
-#define ADDTIONAL_UUID_STR_LEN (UUID_STR_LEN + 8)
349
+#define ADDTIONAL_UUID_STR_LEN (UUID_STR_LEN + 17)
350
351
-static void netdata_clsid_to_char(char *output, const GUID *pguid)
351
+static bool netdata_clsid_to_char(char *output, size_t output_len, const GUID *pguid)
352
{
353
+ if (unlikely(!output))
354
+ return false;
355
+
356
LPWSTR wguid = NULL;
354
- if (SUCCEEDED(StringFromCLSID(pguid, &wguid)) && wguid) {
355
- size_t len = wcslen(wguid);
356
- wcstombs(output, wguid, len);
357
- CoTaskMemFree(wguid);
357
+ HRESULT hr = StringFromCLSID(pguid, &wguid);
358
+ output[0] = '\0';
359
+ if (FAILED(hr) || unlikely(!wguid))
360
+ return false;
361
+
362
+ size_t converted = wcstombs(output, wguid, output_len - 1);
363
+ CoTaskMemFree(wguid);
364
+
365
+ if (unlikely(converted == (size_t)-1)) {
366
+ output[0] = '\0';
367
+ return false;
368
}
369
+
370
+ output[converted] = '\0';
371
+
372
+ return true;
373
}
374
375
static inline char *netdata_convert_guid_to_string(HRESULT hr, GUID *value)
376
{
377
if (SUCCEEDED(hr)) {
378
char cguid[ADDTIONAL_UUID_STR_LEN];
365
- netdata_clsid_to_char(cguid, value);
379
+ netdata_clsid_to_char(cguid, ADDTIONAL_UUID_STR_LEN, value);
380
return strdupz(cguid);
381
}
382
return NULL;
@@ -540,6 +554,7 @@ static void netdata_get_sensors()
554
ULONG count = 0;
555
hr = pSensorCollection->lpVtbl->GetCount(pSensorCollection, &count);
556
if (FAILED(hr)) {
557
+ pSensorCollection->lpVtbl->Release(pSensorCollection);
558
return;
559
}
560
@@ -555,9 +570,14 @@ static void netdata_get_sensors()
570
GUID id = {0};
571
hr = pSensor->lpVtbl->GetID(pSensor, &id);
572
if (FAILED(hr)) {
573
+ pSensor->lpVtbl->Release(pSensor);
574
+ continue;
575
+ }
576
+
577
+ if (!netdata_clsid_to_char(thread_values, sizeof(thread_values), &id)) {
578
+ pSensor->lpVtbl->Release(pSensor);
579
continue;
580
}
560
- netdata_clsid_to_char(thread_values, &id);
581
582
struct sensor_data *sd = dictionary_set(sensors, thread_values, NULL, sizeof(*sd));
583
src/collectors/windows.plugin/GetServicesStatus.c
+19
-4
@@ -41,13 +41,13 @@ static void initialize(void)
41
42
static BOOL fill_dictionary_with_content()
43
{
44
- static PVOID buffer = NULL;
44
+ PVOID buffer = NULL;
45
static DWORD bytes_needed = 0;
46
47
LPENUM_SERVICE_STATUS_PROCESS service, services;
48
DWORD total_services = 0;
49
SC_HANDLE ndSCMH = OpenSCManager(NULL, NULL, SC_MANAGER_ENUMERATE_SERVICE | SC_MANAGER_CONNECT);
50
- if (!ndSCMH) {
50
+ if (unlikely(!ndSCMH)) {
51
return FALSE;
52
}
53
@@ -64,11 +64,24 @@ static BOOL fill_dictionary_with_content()
64
NULL,
65
NULL);
66
67
- if (GetLastError() == ERROR_MORE_DATA) {
68
- if (!buffer)
67
+ DWORD test = GetLastError();
68
+ if (test == ERROR_MORE_DATA) {
69
+ if (unlikely(!buffer))
70
buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, bytes_needed);
71
else
72
buffer = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, buffer, bytes_needed);
73
+ } else {
74
+ switch (test) {
75
+ case ERROR_ACCESS_DENIED:
76
+ case ERROR_INVALID_PARAMETER:
77
+ case ERROR_INVALID_HANDLE:
78
+ case ERROR_INVALID_LEVEL:
79
+ case ERROR_SHUTDOWN_IN_PROGRESS:
80
+ ret = FALSE;
81
+ goto endServiceCollection;
82
+ default:
83
+ ret = TRUE;
84
+ }
85
}
86
87
if (!buffer) {
@@ -112,6 +125,8 @@ static BOOL fill_dictionary_with_content()
125
ret = TRUE;
126
127
endServiceCollection:
128
+ if (buffer)
129
+ HeapFree(GetProcessHeap(), 0, buffer);
130
131
CloseServiceHandle(ndSCMH);
132
return ret;