0
0
Fork 0
mirror of https://github.com/netdata/netdata.git synced 2025-04-07 23:05:41 +00:00
netdata_netdata/src/claim/cloud-conf.c
Costa Tsaousis a399128dbf
config parsers ()
* added parser for durations

* preliminary work for timeframes

* Update CMakeLists.txt

* updated parsing and generation for durations

* renames

* report parser errors; added compatibility to existing config_parse_duration()

* duration parsing is used on most netdata.conf and stream.conf entries

* more uses of duration parsing; simplification of stream.conf

* code cleanup

* more duration changes

* added html playground

* improved js code

* duration parsing applied to dbengine retention

* fixed doc

* simplified logic; added size parser

* added parsing for sizes

* renames and documentation updates

* hide appconfig internals from the rest of netdata

* fix crash on cleanup of streaming receivers

* fix buffer overflow in gorilla compression

* config return values are const

* ksm set to auto

* support reformatting migrated values

* removed obsolete metrics correlations settings

* split appconfig to multiple files

* durations documentation

* sizes documentation

* added backward compatibility in retention configuration

* provide description on migrations and reformattings

* config options are now a double linked list

* config sections are now a double linked list; config uses spinlocks; code cleanup and renames

* added data type to all config options

* update data types

* split appconfig api to multiple files

* code cleanup and renames

* removed size units above PiB

* Revert "fix buffer overflow in gorilla compression"

This reverts commit 3d5c48e84b.

* appconfig internal api changes
2024-09-04 14:42:01 +03:00

117 lines
4.8 KiB
C

// SPDX-License-Identifier: GPL-3.0-or-later
#include "claim.h"
struct config cloud_config = APPCONFIG_INITIALIZER;
const char *cloud_config_url_get(void) {
return appconfig_get(&cloud_config, CONFIG_SECTION_GLOBAL, "url", DEFAULT_CLOUD_BASE_URL);
}
void cloud_config_url_set(const char *url) {
if(!url || *url) return;
const char *existing = cloud_config_url_get();
if(strcmp(existing, url) != 0)
appconfig_set(&cloud_config, CONFIG_SECTION_GLOBAL, "url", url);
}
const char *cloud_config_proxy_get(void) {
// load cloud.conf or internal default
const char *proxy = appconfig_get(&cloud_config, CONFIG_SECTION_GLOBAL, "proxy", "env");
// backwards compatibility, from when proxy was in netdata.conf
// netdata.conf has bigger priority
if (config_exists(CONFIG_SECTION_CLOUD, "proxy")) {
// get it from netdata.conf
proxy = config_get(CONFIG_SECTION_CLOUD, "proxy", proxy);
// update cloud.conf
proxy = appconfig_set(&cloud_config, CONFIG_SECTION_GLOBAL, "proxy", proxy);
}
else {
// set in netdata.conf the proxy of cloud.conf
config_set(CONFIG_SECTION_CLOUD, "proxy", proxy);
}
return proxy;
}
bool cloud_config_insecure_get(void) {
// load it from cloud.conf or use internal default
return appconfig_get_boolean(&cloud_config, CONFIG_SECTION_GLOBAL, "insecure", CONFIG_BOOLEAN_NO);
}
static void cloud_conf_load_defaults(void) {
appconfig_get(&cloud_config, CONFIG_SECTION_GLOBAL, "url", DEFAULT_CLOUD_BASE_URL);
appconfig_get(&cloud_config, CONFIG_SECTION_GLOBAL, "proxy", "env");
appconfig_get(&cloud_config, CONFIG_SECTION_GLOBAL, "token", "");
appconfig_get(&cloud_config, CONFIG_SECTION_GLOBAL, "rooms", "");
appconfig_get_boolean(&cloud_config, CONFIG_SECTION_GLOBAL, "insecure", CONFIG_BOOLEAN_NO);
appconfig_get(&cloud_config, CONFIG_SECTION_GLOBAL, "machine_guid", "");
appconfig_get(&cloud_config, CONFIG_SECTION_GLOBAL, "claimed_id", "");
appconfig_get(&cloud_config, CONFIG_SECTION_GLOBAL, "hostname", "");
}
void cloud_conf_load(int silent) {
errno_clear();
char *filename = filename_from_path_entry_strdupz(netdata_configured_cloud_dir, "cloud.conf");
int ret = appconfig_load(&cloud_config, filename, 1, NULL);
if(!ret && !silent)
nd_log(NDLS_DAEMON, NDLP_ERR,
"CLAIM: cannot load cloud config '%s'. Running with internal defaults.", filename);
freez(filename);
appconfig_move(&cloud_config,
CONFIG_SECTION_GLOBAL, "cloud base url",
CONFIG_SECTION_GLOBAL, "url");
cloud_conf_load_defaults();
}
void cloud_conf_init_after_registry(void) {
const char *machine_guid = appconfig_get(&cloud_config, CONFIG_SECTION_GLOBAL, "machine_guid", "");
const char *hostname = appconfig_get(&cloud_config, CONFIG_SECTION_GLOBAL, "hostname", "");
// for machine guid and hostname we have to use appconfig_set() for that they will be saved uncommented
if(!machine_guid || !*machine_guid)
appconfig_set(&cloud_config, CONFIG_SECTION_GLOBAL, "machine_guid", registry_get_this_machine_guid());
if(!hostname || !*hostname)
appconfig_set(&cloud_config, CONFIG_SECTION_GLOBAL, "hostname", registry_get_this_machine_hostname());
}
bool cloud_conf_save(void) {
char filename[FILENAME_MAX + 1];
CLEAN_BUFFER *wb = buffer_create(0, NULL);
appconfig_generate(&cloud_config, wb, false, false);
snprintfz(filename, sizeof(filename), "%s/cloud.conf", netdata_configured_cloud_dir);
FILE *fp = fopen(filename, "w");
if(!fp) {
nd_log(NDLS_DAEMON, NDLP_ERR, "Cannot open file '%s' for writing.", filename);
return false;
}
fprintf(fp, "%s", buffer_tostring(wb));
fclose(fp);
return true;
}
bool cloud_conf_regenerate(const char *claimed_id_str, const char *machine_guid, const char *hostname, const char *token, const char *rooms, const char *url, const char *proxy, int insecure) {
// for backwards compatibility (older agents), save the claimed_id to its file
claimed_id_save_to_file(claimed_id_str);
appconfig_set(&cloud_config, CONFIG_SECTION_GLOBAL, "url", url);
appconfig_set(&cloud_config, CONFIG_SECTION_GLOBAL, "proxy", proxy ? proxy : "");
appconfig_set(&cloud_config, CONFIG_SECTION_GLOBAL, "token", token ? token : "");
appconfig_set(&cloud_config, CONFIG_SECTION_GLOBAL, "rooms", rooms ? rooms : "");
appconfig_set_boolean(&cloud_config, CONFIG_SECTION_GLOBAL, "insecure", insecure);
appconfig_set(&cloud_config, CONFIG_SECTION_GLOBAL, "machine_guid", machine_guid ? machine_guid : "");
appconfig_set(&cloud_config, CONFIG_SECTION_GLOBAL, "claimed_id", claimed_id_str ? claimed_id_str : "");
appconfig_set(&cloud_config, CONFIG_SECTION_GLOBAL, "hostname", hostname ? hostname : "");
return cloud_conf_save();
}