Skip to content

Commit 118d7c0

Browse files
committed
Port interface plugin to Windows.
1 parent e31b08d commit 118d7c0

File tree

4 files changed

+77
-4
lines changed

4 files changed

+77
-4
lines changed

Makefile.am

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,6 +1015,7 @@ interface_la_SOURCES = src/interface.c
10151015
interface_la_CFLAGS = $(AM_CFLAGS)
10161016
interface_la_LDFLAGS = $(PLUGIN_LDFLAGS)
10171017
interface_la_LIBADD = libignorelist.la
1018+
interface_la_DEPENDENCIES = libignorelist.la
10181019
if BUILD_WITH_LIBSTATGRAB
10191020
interface_la_CFLAGS += $(BUILD_WITH_LIBSTATGRAB_CFLAGS)
10201021
interface_la_LIBADD += $(BUILD_WITH_LIBSTATGRAB_LDFLAGS)
@@ -1029,6 +1030,12 @@ endif # !BUILD_WITH_LIBSTATGRAB
10291030
if BUILD_WITH_PERFSTAT
10301031
interface_la_LIBADD += -lperfstat
10311032
endif
1033+
if BUILD_WIN32
1034+
interface_la_SOURCES += src/utils_wmi.c src/utils_wmi.h
1035+
interface_la_LDFLAGS += -L.
1036+
interface_la_LIBADD += -loleaut32 -lole32 -luuid libcollectd.la
1037+
interface_la_DEPENDENCIES += libuuid.dll
1038+
endif
10321039
endif # BUILD_PLUGIN_INTERFACE
10331040

10341041
if BUILD_PLUGIN_IPC

build.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ build_windows ()
153153
--enable-logfile \
154154
--enable-disk \
155155
--enable-eventlog \
156+
--enable-interface \
156157
--enable-match_regex \
157158
--enable-network \
158159
--enable-target_replace \

configure.ac

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6249,6 +6249,7 @@ fi
62496249
if test "x$ac_system" = "xWindows"; then
62506250
plugin_disk="yes"
62516251
plugin_eventlog="yes"
6252+
plugin_interface="yes"
62526253
plugin_wmi="yes"
62536254
fi
62546255

src/interface.c

Lines changed: 68 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,13 @@ static int nif;
7373
static int pnif;
7474
#endif /* HAVE_PERFSTAT */
7575

76+
#if KERNEL_WIN32
77+
#include "utils_wmi.h"
78+
static wmi_connection_t *wmi;
79+
#endif
80+
7681
#if !HAVE_GETIFADDRS && !KERNEL_LINUX && !HAVE_LIBKSTAT && \
77-
!HAVE_LIBSTATGRAB && !HAVE_PERFSTAT
82+
!HAVE_LIBSTATGRAB && !HAVE_PERFSTAT && !KERNEL_WIN32
7883
#error "No applicable input method."
7984
#endif
8085

@@ -151,7 +156,18 @@ static int interface_init(void) {
151156

152157
return 0;
153158
} /* int interface_init */
154-
#endif /* HAVE_LIBKSTAT */
159+
/* #endif HAVE_LIBKSTAT */
160+
#elif KERNEL_WIN32
161+
static int interface_init(void) {
162+
wmi = wmi_connect();
163+
return 0;
164+
}
165+
166+
static int interface_shutdown(void) {
167+
wmi_release(wmi);
168+
return 0;
169+
} /* int interface_shutdown */
170+
#endif /* KERNEL_WIN32 */
155171

156172
static void if_submit(const char *dev, const char *type, derive_t rx,
157173
derive_t tx) {
@@ -378,16 +394,64 @@ static int interface_read(void) {
378394
if_submit(ifstat[i].name, "if_errors", ifstat[i].ierrors,
379395
ifstat[i].oerrors);
380396
}
381-
#endif /* HAVE_PERFSTAT */
397+
/* #endif HAVE_PERFSTAT */
398+
#elif KERNEL_WIN32
399+
const char *statement = "select * from Win32_PerfRawData_Tcpip_NetworkInterface";
400+
wmi_result_list_t *results;
401+
results = wmi_query(wmi, statement);
402+
403+
if (results->count == 0) {
404+
WARNING("interface plugin: no results for query %s.", statement);
405+
wmi_result_list_release(results);
406+
return 0;
407+
}
408+
409+
wmi_result_t *result;
410+
while ((result = wmi_get_next_result(results))) {
411+
VARIANT rx_value_v;
412+
VARIANT tx_value_v;
413+
VARIANT plugin_instance_v;
414+
char *plugin_instance_s = NULL;
415+
416+
if (wmi_result_get_value(result, "BytesReceivedPersec", &rx_value_v) != 0) {
417+
VariantClear(&rx_value_v);
418+
ERROR("interface plugin: failed to read field 'BytesReceivedPersec'");
419+
continue;
420+
}
421+
if (wmi_result_get_value(result, "BytesSentPersec", &tx_value_v) != 0) {
422+
VariantClear(&rx_value_v);
423+
VariantClear(&tx_value_v);
424+
ERROR("interface plugin: failed to read field 'BytesSentPersec'");
425+
continue;
426+
}
427+
if (wmi_result_get_value(result, "Name", &plugin_instance_v) != 0) {
428+
ERROR("interface plugin: failed to read field 'Name'");
429+
}
430+
plugin_instance_s = variant_get_string(&plugin_instance_v);
431+
if (plugin_instance_s == NULL) {
432+
ERROR("interface plugin: failed to convert plugin_instance to string");
433+
}
434+
if_submit(plugin_instance_s, "if_octets", variant_get_int64(&rx_value_v), variant_get_int64(&tx_value_v));
435+
free(plugin_instance_s);
436+
VariantClear(&rx_value_v);
437+
VariantClear(&tx_value_v);
438+
VariantClear(&plugin_instance_v);
439+
wmi_result_release(result);
440+
}
441+
wmi_result_list_release(results);
442+
#endif /* KERNEL_WIN32 */
382443

383444
return 0;
384445
} /* int interface_read */
385446

386447
void module_register(void) {
387448
plugin_register_config("interface", interface_config, config_keys,
388449
config_keys_num);
389-
#if HAVE_LIBKSTAT
450+
#if HAVE_LIBKSTAT || KERNEL_WIN32
390451
plugin_register_init("interface", interface_init);
452+
#endif
453+
#if KERNEL_WIN32
454+
plugin_register_shutdown("interface", interface_shutdown);
391455
#endif
392456
plugin_register_read("interface", interface_read);
393457
} /* void module_register */

0 commit comments

Comments
 (0)