@cryptotaxi247 / netdata / commits / 03ccc8b0a

Add Network Labels to Windows. (#20938)

thiagoftsm committed Sep 9, 2025 at 11:22 UTC 03ccc8b0a5eadc874cd30fccc4f016ab2f69f984
4 files changed +128 -1
CMakeLists.txt
+10 -1
@@ -1145,6 +1145,15 @@ set(LIBNETDATA_FILES
1145 src/libnetdata/signals/signal-code.h
1146 )
1147
1148 +if(OS_WINDOWS)
1149 + set(LIBNETDATA_WIN_FILES
1150 + src/libnetdata/os/windows-api/windows_api.c
1151 + src/libnetdata/os/windows-api/windows_api.h
1152 + )
1153 +
1154 + list(APPEND LIBNETDATA_FILES ${LIBNETDATA_WIN_FILES})
1155 +endif()
1156 +
1157 list(APPEND LIBNETDATA_FILES ${INICFG_FILES})
1158 list(APPEND LIBNETDATA_FILES ${CONFIG_FILES})
1159
@@ -2216,7 +2225,7 @@ target_link_libraries(libnetdata PUBLIC
2225 "$<$<BOOL:${ENABLE_MIMALLOC}>:mimalloc-static>"
2226 "$<$<NOT:$<BOOL:${HAVE_BUILTIN_ATOMICS}>>:atomic>"
2227 "$<$<OR:$<BOOL:${OS_LINUX}>,$<BOOL:${OS_FREEBSD}>>:pthread;rt>"
2219 - "$<$<BOOL:${OS_WINDOWS}>:kernel32;advapi32;winmm;rpcrt4;wevtapi;ole32;oleaut32;wbemuuid;sensorsapi>"
2228 + "$<$<BOOL:${OS_WINDOWS}>:kernel32;advapi32;winmm;rpcrt4;wevtapi;ole32;oleaut32;wbemuuid;sensorsapi;iphlpapi>"
2229 "$<$<BOOL:${LINK_LIBM}>:m>"
2230 "${SYSTEMD_LDFLAGS}")
2231
src/daemon/win_system-info.c
+15
@@ -2,9 +2,23 @@
2
3 #include "win_system-info.h"
4 #include "database/rrdhost-system-info.h"
5 +#include "libnetdata/os/windows-api/windows_api.h"
6
7 #ifdef OS_WINDOWS
8
9 +static void netdata_windows_ip(struct rrdhost_system_info *systemInfo)
10 +{
11 + (void)rrdhost_system_info_set_by_name(systemInfo, "NETDATA_SYSTEM_DEFAULT_INTERFACE_DETECTION", "WINAPI");
12 +
13 + char *ptr = netdata_win_local_interface();
14 + if (ptr)
15 + (void)rrdhost_system_info_set_by_name(systemInfo, "NETDATA_SYSTEM_DEFAULT_INTERFACE_NAME", ptr);
16 +
17 + ptr = netdata_win_local_ip();
18 + if (ptr)
19 + (void)rrdhost_system_info_set_by_name(systemInfo, "NETDATA_SYSTEM_DEFAULT_INTERFACE_IP", ptr);
20 +}
21 +
22 // Hardware
23 static char *netdata_windows_arch(DWORD value)
24 {
@@ -386,5 +400,6 @@ void netdata_windows_get_system_info(struct rrdhost_system_info *systemInfo)
400 netdata_windows_get_mem(systemInfo);
401 netdata_windows_get_total_disk_size(systemInfo);
402 netdata_windows_install_type(systemInfo);
403 + netdata_windows_ip(systemInfo);
404 }
405 #endif
src/libnetdata/os/windows-api/windows_api.c new
+90
@@ -0,0 +1,90 @@
1 +// SPDX-License-Identifier: GPL-3.0-or-later
2 +
3 +#include "windows_api.h"
4 +
5 +#include <winsock2.h>
6 +#include <ws2tcpip.h>
7 +#include <iphlpapi.h>
8 +#include <stdbool.h>
9 +
10 +struct netdata_windows_ip_labels {
11 + char *local_iface;
12 + char *ipaddr;
13 + bool initialized;
14 +} default_ip = {
15 + .local_iface = NULL,
16 + .ipaddr = NULL,
17 + .initialized = false
18 +};
19 +
20 +int netdata_fill_default_ip()
21 +{
22 + if (default_ip.initialized)
23 + return 0;
24 +
25 + default_ip.initialized = true;
26 +
27 + MIB_IPFORWARDROW route;
28 + DWORD dest = 0;
29 + if (GetBestRoute(dest, 0, &route) != NO_ERROR) {
30 + return -1;
31 + }
32 +
33 + DWORD ifIndex = route.dwForwardIfIndex;
34 +
35 + ULONG bufLen = 15000;
36 + PIP_ADAPTER_ADDRESSES adapters = (PIP_ADAPTER_ADDRESSES)malloc(bufLen);
37 + if (!adapters) {
38 + return 1;
39 + }
40 +
41 + int ret = GetAdaptersAddresses(AF_INET, GAA_FLAG_INCLUDE_PREFIX, NULL, adapters, &bufLen);
42 + if (ret != NO_ERROR) {
43 + goto end_ip_detection;
44 + }
45 +
46 + PIP_ADAPTER_ADDRESSES aa = adapters;
47 + while (aa) {
48 + if (aa->IfIndex == ifIndex) {
49 + char iface[1024];
50 + size_t required_size = wcstombs(NULL , aa->FriendlyName, 0) + 1;
51 + wcstombs(iface, aa->FriendlyName, required_size);
52 + default_ip.local_iface = strdup(iface);
53 +
54 + PIP_ADAPTER_UNICAST_ADDRESS ua = aa->FirstUnicastAddress;
55 + while (ua) {
56 + if (ua->Address.lpSockaddr->sa_family == AF_INET) {
57 + char ipstr[INET_ADDRSTRLEN];
58 + struct sockaddr_in *sa_in = (struct sockaddr_in *)ua->Address.lpSockaddr;
59 + inet_ntop(AF_INET, &(sa_in->sin_addr), ipstr, sizeof(ipstr));
60 + default_ip.ipaddr = strdup(ipstr);
61 + goto end_ip_detection;
62 + }
63 + ua = ua->Next;
64 + }
65 + break;
66 + }
67 + aa = aa->Next;
68 + }
69 +
70 + ret = NO_ERROR;
71 +end_ip_detection:
72 + free(adapters);
73 + return ret;
74 +}
75 +
76 +char *netdata_win_local_interface()
77 +{
78 + if (!default_ip.initialized)
79 + netdata_fill_default_ip();
80 +
81 + return default_ip.local_iface;
82 +}
83 +
84 +char *netdata_win_local_ip()
85 +{
86 + if (!default_ip.initialized)
87 + netdata_fill_default_ip();
88 +
89 + return default_ip.ipaddr;
90 +}
src/libnetdata/os/windows-api/windows_api.h new
+13
@@ -0,0 +1,13 @@
1 +// SPDX-License-Identifier: GPL-3.0-or-later
2 +
3 +#ifndef NETDATA_WINDOWS_API_H
4 +#define NETDATA_WINDOWS_API_H
5 +
6 +#if defined(OS_WINDOWS)
7 +
8 +char *netdata_win_local_interface();
9 +char *netdata_win_local_ip();
10 +
11 +#endif
12 +
13 +#endif //NETDATA_WINDOWS_API_H