Skip to content

Commit

Permalink
Run the agent as a Windows service. (netdata#17766)
Browse files Browse the repository at this point in the history
* Run the agent as a Windows service.

This commit contains the boilerplate code for running the agent as a
Windows service.

We start the agent's main as a separate thread, although this is not
strictly required based on my experiments. We need similar logic for
calling netdata's exit function when someone wants to stop the agent.

However, at this point we need to resolve the issue of gaps when
running the agent as a service. It seems that sleeping for one second
with `sleep(1)`, actually sleeps for 2 to 4 seconds on my setup.

Once we resolve this, the work that remains concerns packaging: ie.
installing the binaries at the proper places so that the relevant
DLLs are found.

To test this PR you need to:
  - Build the agent: ./packaging/utils/compile-on-windows.sh
  - Install the files: `ninja -C build/ install`
  - Copy the main binary: `cp ./build/netdata /usr/bin/`
  - (Only once) Create the netdata service: `sc.exe config Netdata binPath="C:\msys64\usr\bin\netdata"`
  - Start the service: `sc.exe start Netdata`

A couple notes:
  - The health and the spawn client have been disabled for the time
    being. They will be re-enabled once we finish the agent-as-service
    issue and the packaging.
  - Last time I checked, the agent crashes after a while when using
    dbengine. In order to have something that works correctly, you
    should specify memory-mode ram in your netdata.conf.

* Add windows version for sleep_usec_with_now

* Split install prefix from runtime prefix

These paths are always the same for non-Windows
systems. On Windows, RFS is the top-level
installation path.

With the current setup, Netdata will be installed
at C:\msys64\opt\netdata at packaging time. However,
the layout of the application means that when the
agent starts, it'll look as if everything was installed
at /.

* Do not use mold linker on Windows.

* Use modern UI for installer.

* Make the service delayed-auto

* Use mutexes instead of spinlocks.

* Update service handling logic.

* Add proper ifdefs for spinlock implementation.

* Initialize analytics spinlock

* Add a macro to build the agent as regular cli tool.

* Add makensis dependency

* Let installer know it's installing Netdata.

* Disable pluginsd on Windows

When pluginsd is enabled, the agent freezes approximately
20% of the time during startup.

* Add service description.

* Return pthread_join result

* Print tag when we fail to join a thread.

* Do not use mutexes instead of spinlocks.

* Assorted changes to service/main code.

* Rework service functions.

With the current implementation we are not getting any
MUTEX_LOCK errors and thread joining succeeds.

The only case where joining fails is the parallel initialization
of dbengine threads, which we can easily avoid by serializing
the initialization step.

* Rework main functions

This will allow someone to run the agent either as a service
or as a command-line tool.

* Change runtime prefix only when building for packaging.

* Install binaries and dlls.

* Make netdata claiming through UI work correctly.

* Fix netdata path
  • Loading branch information
vkalintiris authored Jul 2, 2024
1 parent a38e6ee commit e99da8b
Show file tree
Hide file tree
Showing 19 changed files with 615 additions and 86 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,6 @@ src/go/collectors/go.d.plugin/mocks/springboot2/.gradle/
src/go/collectors/go.d.plugin/mocks/tmp/*
!src/go/collectors/go.d.plugin/mocks/tmp/.gitkeep
src/go/collectors/go.d.plugin/vendor

# ignore nsis installer
packaging/utils/netdata-installer.exe
128 changes: 104 additions & 24 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ set(OS_MACOS False)
set(OS_WINDOWS False)
set(ALLOW_PLATFORM_SENSITIVE_OPTIONS True)

set(NETDATA_RUNTIME_PREFIX "${CMAKE_INSTALL_PREFIX}")
set(BINDIR usr/sbin)

if("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin")
set(OS_MACOS True)
find_library(IOKIT IOKit)
Expand All @@ -111,6 +114,16 @@ elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux")
endif()
elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "CYGWIN" OR "${CMAKE_SYSTEM_NAME}" STREQUAL "MSYS" OR "${CMAKE_SYSTEM_NAME}" STREQUAL "Windows")
set(OS_WINDOWS True)

if(NOT "${CMAKE_INSTALL_PREFIX}" STREQUAL "/opt/netdata")
message(FATAL_ERROR "CMAKE_INSTALL_PREFIX must be set to /opt/netdata, but it is set to ${CMAKE_INSTALL_PREFIX}")
endif()

if(BUILD_FOR_PACKAGING)
set(NETDATA_RUNTIME_PREFIX "/")
endif()

set(BINDIR usr/bin)
add_definitions(-D_GNU_SOURCE)

if($ENV{CLION_IDE})
Expand Down Expand Up @@ -1434,6 +1447,7 @@ elseif(OS_FREEBSD)
elseif(OS_WINDOWS)
list(APPEND NETDATA_FILES
src/daemon/static_threads_windows.c
src/daemon/winsvc.cc
${WINDOWS_PLUGIN_FILES}
${INTERNAL_COLLECTORS_FILES}
)
Expand Down Expand Up @@ -2279,7 +2293,7 @@ target_link_libraries(systemd-cat-native libnetdata)

install(TARGETS systemd-cat-native
COMPONENT netdata
DESTINATION usr/sbin)
DESTINATION "${BINDIR}")

#
# build log2journal
Expand Down Expand Up @@ -2314,7 +2328,7 @@ if(PCRE2_FOUND)

install(TARGETS log2journal
COMPONENT netdata
DESTINATION usr/sbin)
DESTINATION "${BINDIR}")

install(DIRECTORY src/collectors/log2journal/log2journal.d
COMPONENT netdata
Expand All @@ -2337,7 +2351,7 @@ target_link_libraries(netdatacli libnetdata)

install(TARGETS netdatacli
COMPONENT netdata
DESTINATION usr/sbin)
DESTINATION "${BINDIR}")

#
# Build go.d.plugin
Expand All @@ -2354,17 +2368,16 @@ endif()
#
# Generate config file
#

if(NOT CMAKE_INSTALL_PREFIX STREQUAL "")
string(REGEX REPLACE "/$" "" CMAKE_INSTALL_PREFIX "${CMAKE_INSTALL_PREFIX}")
if(NOT NETDATA_RUNTIME_PREFIX STREQUAL "")
string(REGEX REPLACE "/$" "" NETDATA_RUNTIME_PREFIX "${NETDATA_RUNTIME_PREFIX}")
endif()

set(CACHE_DIR "${CMAKE_INSTALL_PREFIX}/var/cache/netdata")
set(CONFIG_DIR "${CMAKE_INSTALL_PREFIX}/etc/netdata")
set(LIBCONFIG_DIR "${CMAKE_INSTALL_PREFIX}/usr/lib/netdata/conf.d")
set(LOG_DIR "${CMAKE_INSTALL_PREFIX}/var/log/netdata")
set(PLUGINS_DIR "${CMAKE_INSTALL_PREFIX}/usr/libexec/netdata/plugins.d")
set(VARLIB_DIR "${CMAKE_INSTALL_PREFIX}/var/lib/netdata")
set(CACHE_DIR "${NETDATA_RUNTIME_PREFIX}/var/cache/netdata")
set(CONFIG_DIR "${NETDATA_RUNTIME_PREFIX}/etc/netdata")
set(LIBCONFIG_DIR "${NETDATA_RUNTIME_PREFIX}/usr/lib/netdata/conf.d")
set(LOG_DIR "${NETDATA_RUNTIME_PREFIX}/var/log/netdata")
set(PLUGINS_DIR "${NETDATA_RUNTIME_PREFIX}/usr/libexec/netdata/plugins.d")
set(VARLIB_DIR "${NETDATA_RUNTIME_PREFIX}/var/lib/netdata")

# A non-default value is only used when building Debian packages (/var/lib/netdata/www)
if(NOT DEFINED WEB_DIR)
Expand All @@ -2373,7 +2386,7 @@ else()
string(REGEX REPLACE "^/" "" WEB_DIR "${WEB_DIR}")
endif()
set(WEB_DEST "${WEB_DIR}")
set(WEB_DIR "${CMAKE_INSTALL_PREFIX}/${WEB_DEST}")
set(WEB_DIR "${NETDATA_RUNTIME_PREFIX}/${WEB_DEST}")


set(CONFIGURE_COMMAND "dummy-configure-command")
Expand All @@ -2387,7 +2400,7 @@ configure_file(packaging/cmake/config.cmake.h.in config.h)
# install
#

install(TARGETS netdata COMPONENT netdata DESTINATION usr/sbin)
install(TARGETS netdata COMPONENT netdata DESTINATION "${BINDIR}")

install(DIRECTORY COMPONENT netdata DESTINATION var/cache/netdata)
install(DIRECTORY COMPONENT netdata DESTINATION var/log/netdata)
Expand All @@ -2406,15 +2419,15 @@ install(DIRECTORY COMPONENT netdata DESTINATION usr/lib/netdata/conf.d/schema.d)
install(DIRECTORY COMPONENT netdata DESTINATION usr/libexec/netdata/plugins.d)
install(DIRECTORY COMPONENT netdata DESTINATION ${WEB_DEST})

set(libsysdir_POST "${CMAKE_INSTALL_PREFIX}/usr/lib/netdata/system")
set(pkglibexecdir_POST "${CMAKE_INSTALL_PREFIX}/usr/libexec/netdata")
set(localstatedir_POST "${CMAKE_INSTALL_PREFIX}/var")
set(sbindir_POST "${CMAKE_INSTALL_PREFIX}/usr/sbin")
set(configdir_POST "${CMAKE_INSTALL_PREFIX}/etc/netdata")
set(libconfigdir_POST "${CMAKE_INSTALL_PREFIX}/usr/lib/netdata/conf.d")
set(cachedir_POST "${CMAKE_INSTALL_PREFIX}/var/cache/netdata")
set(registrydir_POST "${CMAKE_INSTALL_PREFIX}/var/lib/netdata/registry")
set(varlibdir_POST "${CMAKE_INSTALL_PREFIX}/var/lib/netdata")
set(libsysdir_POST "${NETDATA_RUNTIME_PREFIX}/usr/lib/netdata/system")
set(pkglibexecdir_POST "${NETDATA_RUNTIME_PREFIX}/usr/libexec/netdata")
set(localstatedir_POST "${NETDATA_RUNTIME_PREFIX}/var")
set(sbindir_POST "${NETDATA_RUNTIME_PREFIX}/${BINDIR}")
set(configdir_POST "${NETDATA_RUNTIME_PREFIX}/etc/netdata")
set(libconfigdir_POST "${NETDATA_RUNTIME_PREFIX}/usr/lib/netdata/conf.d")
set(cachedir_POST "${NETDATA_RUNTIME_PREFIX}/var/cache/netdata")
set(registrydir_POST "${NETDATA_RUNTIME_PREFIX}/var/lib/netdata/registry")
set(varlibdir_POST "${NETDATA_RUNTIME_PREFIX}/var/lib/netdata")
set(netdata_user_POST "${NETDATA_USER}")

# netdata-claim.sh
Expand All @@ -2434,7 +2447,7 @@ configure_file(src/claim/netdata-claim.sh.in src/claim/netdata-claim.sh @ONLY)
install(PROGRAMS
${CMAKE_BINARY_DIR}/src/claim/netdata-claim.sh
COMPONENT netdata
DESTINATION usr/sbin)
DESTINATION "${BINDIR}")

#
# We don't check ENABLE_PLUGIN_CGROUP_NETWORK because rpm builds assume
Expand Down Expand Up @@ -3026,4 +3039,71 @@ if(NOT OS_WINDOWS)
DESTINATION ${WEB_DEST}/v0)
endif()

if(OS_WINDOWS)
install(FILES /usr/bin/awk.exe
/usr/bin/bash.exe
/usr/bin/cat.exe
/usr/bin/chown.exe
/usr/bin/curl.exe
/usr/bin/env.exe
/usr/bin/grep.exe
/usr/bin/mkdir.exe
/usr/bin/openssl.exe
/usr/bin/rm.exe
/usr/bin/sed.exe
/usr/bin/sh.exe
/usr/bin/tail.exe
/usr/bin/tr.exe
/usr/bin/uuidgen.exe
/usr/bin/whoami.exe
DESTINATION "${BINDIR}")

install(FILES /usr/bin/msys-2.0.dll
/usr/bin/msys-asn1-8.dll
/usr/bin/msys-brotlicommon-1.dll
/usr/bin/msys-brotlidec-1.dll
/usr/bin/msys-brotlienc-1.dll
/usr/bin/msys-com_err-1.dll
/usr/bin/msys-crypt-2.dll
/usr/bin/msys-crypto-3.dll
/usr/bin/msys-curl-4.dll
/usr/bin/msys-gcc_s-seh-1.dll
/usr/bin/msys-gmp-10.dll
/usr/bin/msys-gssapi-3.dll
/usr/bin/msys-hcrypto-4.dll
/usr/bin/msys-heimbase-1.dll
/usr/bin/msys-heimntlm-0.dll
/usr/bin/msys-hx509-5.dll
/usr/bin/msys-iconv-2.dll
/usr/bin/msys-idn2-0.dll
/usr/bin/msys-intl-8.dll
/usr/bin/msys-krb5-26.dll
/usr/bin/msys-lz4-1.dll
/usr/bin/msys-mpfr-6.dll
/usr/bin/msys-ncursesw6.dll
/usr/bin/msys-nghttp2-14.dll
/usr/bin/msys-pcre-1.dll
/usr/bin/msys-protobuf-32.dll
/usr/bin/msys-psl-5.dll
/usr/bin/msys-readline8.dll
/usr/bin/msys-roken-18.dll
/usr/bin/msys-sqlite3-0.dll
/usr/bin/msys-ssh2-1.dll
/usr/bin/msys-ssl-3.dll
/usr/bin/msys-stdc++-6.dll
/usr/bin/msys-unistring-5.dll
/usr/bin/msys-uuid-1.dll
/usr/bin/msys-uv-1.dll
/usr/bin/msys-wind-0.dll
/usr/bin/msys-z.dll
/usr/bin/msys-zstd-1.dll
DESTINATION "${BINDIR}")

# Make bash & netdata happy
install(DIRECTORY DESTINATION tmp)

# Make curl work with ssl
install(DIRECTORY /usr/ssl DESTINATION usr)
endif()

include(Packaging)
7 changes: 5 additions & 2 deletions packaging/utils/compile-on-windows.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ install_dependencies() {
msys/pcre2-devel mingw64/mingw-w64-x86_64-pcre2 ucrt64/mingw-w64-ucrt-x86_64-pcre2 \
msys/brotli-devel mingw64/mingw-w64-x86_64-brotli ucrt64/mingw-w64-ucrt-x86_64-brotli \
msys/ccache ucrt64/mingw-w64-ucrt-x86_64-ccache mingw64/mingw-w64-x86_64-ccache \
mingw64/mingw-w64-x86_64-go ucrt64/mingw-w64-ucrt-x86_64-go
mingw64/mingw-w64-x86_64-go ucrt64/mingw-w64-ucrt-x86_64-go \
mingw64/mingw-w64-x86_64-nsis
}

if [ "${1}" = "install" ]
Expand Down Expand Up @@ -52,7 +53,9 @@ fi
-G Ninja \
-DCMAKE_INSTALL_PREFIX="/opt/netdata" \
-DCMAKE_BUILD_TYPE="${BUILD_TYPE}" \
-DCMAKE_C_FLAGS="-O0 -ggdb -Wall -Wextra -Wno-char-subscripts -Wa,-mbig-obj -pipe -DNETDATA_INTERNAL_CHECKS=1 -D_FILE_OFFSET_BITS=64 -D__USE_MINGW_ANSI_STDIO=1" \
-DCMAKE_C_FLAGS="-fstack-protector-all -O0 -ggdb -Wall -Wextra -Wno-char-subscripts -Wa,-mbig-obj -pipe -DNETDATA_INTERNAL_CHECKS=1 -D_FILE_OFFSET_BITS=64 -D__USE_MINGW_ANSI_STDIO=1" \
-DBUILD_FOR_PACKAGING=On \
-DUSE_MOLD=Off \
-DNETDATA_USER="${USER}" \
-DDEFAULT_FEATURE_STATE=Off \
-DENABLE_H2O=Off \
Expand Down
16 changes: 16 additions & 0 deletions packaging/utils/find-dll-deps.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env bash

if [ "$#" -lt 1 ]; then
echo "Usage: $0 <command1> <command2> ... <commandN>"
exit 1
fi

results=()

for arg in "$@"; do
while IFS= read -r line; do
results+=("$line")
done < <(ldd "$arg" | grep /usr/bin | awk '{ print $3 }')
done

printf "%s\n" "${results[@]}" | sort | uniq
64 changes: 44 additions & 20 deletions packaging/utils/installer.nsi
Original file line number Diff line number Diff line change
@@ -1,34 +1,58 @@
Outfile "netdata-installer.exe"
InstallDir "C:\netdata"
!include "MUI2.nsh"

Name "Netdata"
Outfile "netdata-installer.exe"
InstallDir "$PROGRAMFILES\netdata"
RequestExecutionLevel admin

Section
SetOutPath $INSTDIR
WriteUninstaller $INSTDIR\uninstaller.exe
SectionEnd
!define MUI_ABORTWARNING
!define MUI_UNABORTWARNING

Section "Install MSYS2 environment"
SetOutPath $TEMP
!insertmacro MUI_PAGE_WELCOME
!insertmacro MUI_PAGE_DIRECTORY
!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_PAGE_FINISH

SetCompress off
File "C:\msys64\msys2-installer.exe"
nsExec::ExecToLog 'cmd.exe /C "$TEMP\msys2-installer.exe" in --confirm-command --accept-messages --root $INSTDIR'
!insertmacro MUI_UNPAGE_CONFIRM
!insertmacro MUI_UNPAGE_INSTFILES
!insertmacro MUI_UNPAGE_FINISH

Delete "$TEMP\msys2-installer.exe"
SectionEnd

Section "Install MSYS2 packages"
ExecWait '"$INSTDIR\usr\bin\bash.exe" -lc "pacman -S --noconfirm msys/libuv msys/protobuf"'
SectionEnd
!insertmacro MUI_LANGUAGE "English"

Section "Install Netdata"
SetOutPath $INSTDIR\opt\netdata

SetOutPath $INSTDIR
SetCompress off

File /r "C:\msys64\opt\netdata\*.*"

ClearErrors
ExecWait '"$SYSDIR\sc.exe" create Netdata binPath= "$INSTDIR\usr\bin\netdata.exe" start= delayed-auto'
IfErrors 0 +2
DetailPrint "Warning: Failed to create Netdata service."

ClearErrors
ExecWait '"$SYSDIR\sc.exe" description Netdata "Real-time system monitoring service"'
IfErrors 0 +2
DetailPrint "Warning: Failed to add Netdata service description."

ClearErrors
ExecWait '"$SYSDIR\sc.exe" start Netdata'
IfErrors 0 +2
DetailPrint "Warning: Failed to start Netdata service."

WriteUninstaller "$INSTDIR\Uninstall.exe"
SectionEnd

Section "Uninstall"
nsExec::ExecToLog 'cmd.exe /C "$INSTDIR\uninstall.exe" pr --confirm-command'
ClearErrors
ExecWait '"$SYSDIR\sc.exe" stop Netdata'
IfErrors 0 +2
DetailPrint "Warning: Failed to stop Netdata service."

ClearErrors
ExecWait '"$SYSDIR\sc.exe" delete Netdata'
IfErrors 0 +2
DetailPrint "Warning: Failed to delete Netdata service."

RMDir /r "$INSTDIR"
SectionEnd
8 changes: 6 additions & 2 deletions src/daemon/analytics.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// SPDX-License-Identifier: GPL-3.0-or-later

#include "analytics.h"
#include "common.h"
#include "buildinfo.h"

Expand Down Expand Up @@ -470,8 +471,6 @@ void analytics_alarms(void)
*/
void analytics_misc(void)
{
spinlock_init(&analytics_data.spinlock);

#ifdef ENABLE_ACLK
analytics_set_data(&analytics_data.netdata_host_cloud_available, "true");
analytics_set_data_str(&analytics_data.netdata_host_aclk_implementation, "Next Generation");
Expand Down Expand Up @@ -1081,3 +1080,8 @@ void analytics_statistic_send(const analytics_statistic_t *statistic) {

freez(command_to_run);
}

void analytics_init(void)
{
spinlock_init(&analytics_data.spinlock);
}
1 change: 1 addition & 0 deletions src/daemon/analytics.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ void analytics_log_dashboard(void);
void analytics_gather_mutable_meta_data(void);
void analytics_report_oom_score(long long int score);
void get_system_timezone(void);
void analytics_init(void);

typedef struct {
const char *action;
Expand Down
Loading

0 comments on commit e99da8b

Please sign in to comment.