diff --git a/CMakeLists.txt b/CMakeLists.txt index 31ad050a1e9320..7d150c7e4cdd86 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1636,7 +1636,18 @@ if(LINUX) detect_systemd() endif() -add_library(libnetdata STATIC ${LIBNETDATA_FILES}) +add_custom_command( + COMMAND ${CMAKE_COMMAND} + -DSRC=${CMAKE_SOURCE_DIR}/system/netdata.conf + -DDEST=${CMAKE_BINARY_DIR}/netdata_conf_header.h + -DNAME=NETDATA_CONF_HEADER + -P ${CMAKE_SOURCE_DIR}/packaging/cmake/txt2header.cmake + OUTPUT netdata_conf_header.h + DEPENDS packaging/cmake/txt2header.cmake system/netdata.conf + COMMENT "Generating netdata_conf_header.h from system/netdata.conf" +) + +add_library(libnetdata STATIC netdata_conf_header.h ${LIBNETDATA_FILES}) target_include_directories(libnetdata BEFORE PUBLIC ${CONFIG_H_DIR} ${CMAKE_SOURCE_DIR}/src) diff --git a/packaging/cmake/txt2header.cmake b/packaging/cmake/txt2header.cmake new file mode 100644 index 00000000000000..78810980fe2942 --- /dev/null +++ b/packaging/cmake/txt2header.cmake @@ -0,0 +1,42 @@ +# SPDX-License-Identifier: GPL-3.0 +# +# CMake script to generate a C header file from a text file. +# +# The source (SRC), destination (DEST), and define name (NAME) must be +# specified using `-D` options. + +# Get CMake to shut up about empty list elements (we want the new behavior anyway). +cmake_policy(SET CMP0007 NEW) + +# Read the source file as a list of lines. +file(STRINGS ${SRC} lines) + +# Escape everything that needs to be escaped in C strings. +# The replacement arguments below use bracket arguments instead of +# quoted arguments, because some varieties of CMake seem to choke on \" +# in a single-line quoted argument. +list(TRANSFORM lines REPLACE "\\\\" [[\\\\]]) +list(TRANSFORM lines REPLACE "\\\"" [[\\"]]) +list(TRANSFORM lines REPLACE "'" [[\\']]) +list(TRANSFORM lines REPLACE "\\\n" [[\\n]]) +list(TRANSFORM lines REPLACE "\\\r" [[\\r]]) +list(TRANSFORM lines REPLACE "\\\t" [[\\t]]) + +# Prepare each line to be a string in a multi-line macro. +list(TRANSFORM lines PREPEND "\"") +list(TRANSFORM lines APPEND "\\n\" \\\n") + +# Add the required header lines +list(INSERT lines 0 + "// DO NOT EDIT DIRECTLY\n" + "// Generated at build time by CMake from ${SRC}\n" + "\n" + "#define ${NAME} \\\n" +) +list(APPEND lines "\n") + +# Join the lines into one string. +list(JOIN lines "" content) + +# Write the output file +file(WRITE ${DEST} "${content}") diff --git a/src/libnetdata/config/appconfig.c b/src/libnetdata/config/appconfig.c index 81946b594a276f..542f432a245b5d 100644 --- a/src/libnetdata/config/appconfig.c +++ b/src/libnetdata/config/appconfig.c @@ -1,6 +1,7 @@ // SPDX-License-Identifier: GPL-3.0-or-later #include "../libnetdata.h" +#include "netdata_conf_header.h" /* * @Input: @@ -817,19 +818,7 @@ void appconfig_generate(struct config *root, BUFFER *wb, int only_changed) } } - buffer_strcat(wb, - "# netdata configuration\n" - "#\n" - "# You can download the latest version of this file, using:\n" - "#\n" - "# wget -O /etc/netdata/netdata.conf http://localhost:19999/netdata.conf\n" - "# or\n" - "# curl -o /etc/netdata/netdata.conf http://localhost:19999/netdata.conf\n" - "#\n" - "# You can uncomment and change any of the options below.\n" - "# The value shown in the commented settings, is the default value.\n" - "#\n" - "\n# global netdata configuration\n"); + buffer_strcat(wb, NETDATA_CONF_HEADER); for(i = 0; i <= 17 ;i++) { appconfig_wrlock(root); diff --git a/system/netdata.conf b/system/netdata.conf index 6131ab56ae71fe..728119718cf690 100644 --- a/system/netdata.conf +++ b/system/netdata.conf @@ -1,12 +1,18 @@ # netdata configuration # -# You can get the latest version of this file, using: +# Documentation can be found at: # -# netdatacli dumpconfig > /etc/netdata/netdata.conf +# https://learn.netdata.cloud/docs/netdata-agent/configuration/daemon-configuration # -# You can also download it using: +# You can retrieve the current effective configuration of the Netdata agent +# while it is running using: # -# wget -O /etc/netdata/netdata.conf http://localhost:19999/netdata.conf +# netdatacli dumpconfig +# +# Alternatively, it can be downloaded from the `/netdata.conf` endpoint +# of the web API of the agent: +# +# wget -O - http://localhost:19999/netdata.conf # or -# curl -o /etc/netdata/netdata.conf http://localhost:19999/netdata.conf +# curl http://localhost:19999/netdata.conf #