rrdhost.c 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #define NETDATA_RRD_INTERNALS
  3. #include "rrd.h"
  4. static void rrdhost_streaming_sender_structures_init(RRDHOST *host);
  5. bool dbengine_enabled = false; // will become true if and when dbengine is initialized
  6. size_t storage_tiers = 3;
  7. bool use_direct_io = true;
  8. size_t storage_tiers_grouping_iterations[RRD_STORAGE_TIERS] = { 1, 60, 60, 60, 60 };
  9. RRD_BACKFILL storage_tiers_backfill[RRD_STORAGE_TIERS] = { RRD_BACKFILL_NEW, RRD_BACKFILL_NEW, RRD_BACKFILL_NEW, RRD_BACKFILL_NEW, RRD_BACKFILL_NEW };
  10. #if RRD_STORAGE_TIERS != 5
  11. #error RRD_STORAGE_TIERS is not 5 - you need to update the grouping iterations per tier
  12. #endif
  13. size_t get_tier_grouping(size_t tier) {
  14. if(unlikely(tier >= storage_tiers)) tier = storage_tiers - 1;
  15. size_t grouping = 1;
  16. // first tier is always 1 iteration of whatever update every the chart has
  17. for(size_t i = 1; i <= tier ;i++)
  18. grouping *= storage_tiers_grouping_iterations[i];
  19. return grouping;
  20. }
  21. RRDHOST *localhost = NULL;
  22. netdata_rwlock_t rrd_rwlock = NETDATA_RWLOCK_INITIALIZER;
  23. time_t rrdset_free_obsolete_time_s = 3600;
  24. time_t rrdhost_free_orphan_time_s = 3600;
  25. bool is_storage_engine_shared(STORAGE_INSTANCE *engine __maybe_unused) {
  26. #ifdef ENABLE_DBENGINE
  27. if(!rrdeng_is_legacy(engine))
  28. return true;
  29. #endif
  30. return false;
  31. }
  32. // ----------------------------------------------------------------------------
  33. // RRDHOST indexes management
  34. DICTIONARY *rrdhost_root_index = NULL;
  35. static DICTIONARY *rrdhost_root_index_hostname = NULL;
  36. static inline void rrdhost_init() {
  37. if(unlikely(!rrdhost_root_index)) {
  38. rrdhost_root_index = dictionary_create_advanced(
  39. DICT_OPTION_NAME_LINK_DONT_CLONE | DICT_OPTION_VALUE_LINK_DONT_CLONE | DICT_OPTION_DONT_OVERWRITE_VALUE,
  40. &dictionary_stats_category_rrdhost, 0);
  41. }
  42. if(unlikely(!rrdhost_root_index_hostname)) {
  43. rrdhost_root_index_hostname = dictionary_create_advanced(
  44. DICT_OPTION_NAME_LINK_DONT_CLONE | DICT_OPTION_VALUE_LINK_DONT_CLONE | DICT_OPTION_DONT_OVERWRITE_VALUE,
  45. &dictionary_stats_category_rrdhost, 0);
  46. }
  47. }
  48. // ----------------------------------------------------------------------------
  49. // RRDHOST index by UUID
  50. inline size_t rrdhost_hosts_available(void) {
  51. return dictionary_entries(rrdhost_root_index);
  52. }
  53. inline RRDHOST *rrdhost_find_by_guid(const char *guid) {
  54. return dictionary_get(rrdhost_root_index, guid);
  55. }
  56. static inline RRDHOST *rrdhost_index_add_by_guid(RRDHOST *host) {
  57. RRDHOST *ret_machine_guid = dictionary_set(rrdhost_root_index, host->machine_guid, host, sizeof(RRDHOST));
  58. if(ret_machine_guid == host)
  59. rrdhost_option_set(host, RRDHOST_OPTION_INDEXED_MACHINE_GUID);
  60. else {
  61. rrdhost_option_clear(host, RRDHOST_OPTION_INDEXED_MACHINE_GUID);
  62. error("RRDHOST: %s() host with machine guid '%s' is already indexed", __FUNCTION__, host->machine_guid);
  63. }
  64. return host;
  65. }
  66. static void rrdhost_index_del_by_guid(RRDHOST *host) {
  67. if(rrdhost_option_check(host, RRDHOST_OPTION_INDEXED_MACHINE_GUID)) {
  68. if(!dictionary_del(rrdhost_root_index, host->machine_guid))
  69. error("RRDHOST: %s() failed to delete machine guid '%s' from index", __FUNCTION__, host->machine_guid);
  70. rrdhost_option_clear(host, RRDHOST_OPTION_INDEXED_MACHINE_GUID);
  71. }
  72. }
  73. // ----------------------------------------------------------------------------
  74. // RRDHOST index by hostname
  75. inline RRDHOST *rrdhost_find_by_hostname(const char *hostname) {
  76. if(unlikely(!strcmp(hostname, "localhost")))
  77. return localhost;
  78. return dictionary_get(rrdhost_root_index_hostname, hostname);
  79. }
  80. static inline void rrdhost_index_del_hostname(RRDHOST *host) {
  81. if(unlikely(!host->hostname)) return;
  82. if(rrdhost_option_check(host, RRDHOST_OPTION_INDEXED_HOSTNAME)) {
  83. if(!dictionary_del(rrdhost_root_index_hostname, rrdhost_hostname(host)))
  84. error("RRDHOST: %s() failed to delete hostname '%s' from index", __FUNCTION__, rrdhost_hostname(host));
  85. rrdhost_option_clear(host, RRDHOST_OPTION_INDEXED_HOSTNAME);
  86. }
  87. }
  88. static inline RRDHOST *rrdhost_index_add_hostname(RRDHOST *host) {
  89. if(!host->hostname) return host;
  90. RRDHOST *ret_hostname = dictionary_set(rrdhost_root_index_hostname, rrdhost_hostname(host), host, sizeof(RRDHOST));
  91. if(ret_hostname == host)
  92. rrdhost_option_set(host, RRDHOST_OPTION_INDEXED_HOSTNAME);
  93. else {
  94. //have the same hostname but it's not the same host
  95. //keep the new one only if the old one is orphan or archived
  96. if (rrdhost_flag_check(ret_hostname, RRDHOST_FLAG_ORPHAN) || rrdhost_flag_check(ret_hostname, RRDHOST_FLAG_ARCHIVED)) {
  97. rrdhost_index_del_hostname(ret_hostname);
  98. rrdhost_index_add_hostname(host);
  99. }
  100. }
  101. return host;
  102. }
  103. // ----------------------------------------------------------------------------
  104. // RRDHOST - internal helpers
  105. static inline void rrdhost_init_tags(RRDHOST *host, const char *tags) {
  106. if(host->tags && tags && !strcmp(rrdhost_tags(host), tags))
  107. return;
  108. STRING *old = host->tags;
  109. host->tags = string_strdupz((tags && *tags)?tags:NULL);
  110. string_freez(old);
  111. }
  112. static inline void rrdhost_init_hostname(RRDHOST *host, const char *hostname, bool add_to_index) {
  113. if(unlikely(hostname && !*hostname)) hostname = NULL;
  114. if(host->hostname && hostname && !strcmp(rrdhost_hostname(host), hostname))
  115. return;
  116. rrdhost_index_del_hostname(host);
  117. STRING *old = host->hostname;
  118. host->hostname = string_strdupz(hostname?hostname:"localhost");
  119. string_freez(old);
  120. if(add_to_index)
  121. rrdhost_index_add_hostname(host);
  122. }
  123. static inline void rrdhost_init_os(RRDHOST *host, const char *os) {
  124. if(host->os && os && !strcmp(rrdhost_os(host), os))
  125. return;
  126. STRING *old = host->os;
  127. host->os = string_strdupz(os?os:"unknown");
  128. string_freez(old);
  129. }
  130. static inline void rrdhost_init_timezone(RRDHOST *host, const char *timezone, const char *abbrev_timezone, int32_t utc_offset) {
  131. if (host->timezone && timezone && !strcmp(rrdhost_timezone(host), timezone) && host->abbrev_timezone && abbrev_timezone &&
  132. !strcmp(rrdhost_abbrev_timezone(host), abbrev_timezone) && host->utc_offset == utc_offset)
  133. return;
  134. STRING *old = host->timezone;
  135. host->timezone = string_strdupz((timezone && *timezone)?timezone:"unknown");
  136. string_freez(old);
  137. old = (void *)host->abbrev_timezone;
  138. host->abbrev_timezone = string_strdupz((abbrev_timezone && *abbrev_timezone) ? abbrev_timezone : "UTC");
  139. string_freez(old);
  140. host->utc_offset = utc_offset;
  141. }
  142. void set_host_properties(RRDHOST *host, int update_every, RRD_MEMORY_MODE memory_mode,
  143. const char *registry_hostname, const char *os, const char *tags,
  144. const char *tzone, const char *abbrev_tzone, int32_t utc_offset, const char *program_name,
  145. const char *program_version)
  146. {
  147. host->rrd_update_every = update_every;
  148. host->rrd_memory_mode = memory_mode;
  149. rrdhost_init_os(host, os);
  150. rrdhost_init_timezone(host, tzone, abbrev_tzone, utc_offset);
  151. rrdhost_init_tags(host, tags);
  152. host->program_name = string_strdupz((program_name && *program_name) ? program_name : "unknown");
  153. host->program_version = string_strdupz((program_version && *program_version) ? program_version : "unknown");
  154. host->registry_hostname = string_strdupz((registry_hostname && *registry_hostname) ? registry_hostname : rrdhost_hostname(host));
  155. }
  156. // ----------------------------------------------------------------------------
  157. // RRDHOST - add a host
  158. static void rrdhost_initialize_rrdpush_sender(RRDHOST *host,
  159. unsigned int rrdpush_enabled,
  160. char *rrdpush_destination,
  161. char *rrdpush_api_key,
  162. char *rrdpush_send_charts_matching
  163. ) {
  164. if(rrdhost_flag_check(host, RRDHOST_FLAG_RRDPUSH_SENDER_INITIALIZED)) return;
  165. if(rrdpush_enabled && rrdpush_destination && *rrdpush_destination && rrdpush_api_key && *rrdpush_api_key) {
  166. rrdhost_flag_set(host, RRDHOST_FLAG_RRDPUSH_SENDER_INITIALIZED);
  167. rrdhost_streaming_sender_structures_init(host);
  168. #ifdef ENABLE_HTTPS
  169. host->sender->ssl.conn = NULL;
  170. host->sender->ssl.flags = NETDATA_SSL_START;
  171. #endif
  172. host->rrdpush_send_destination = strdupz(rrdpush_destination);
  173. rrdpush_destinations_init(host);
  174. host->rrdpush_send_api_key = strdupz(rrdpush_api_key);
  175. host->rrdpush_send_charts_matching = simple_pattern_create(rrdpush_send_charts_matching, NULL,
  176. SIMPLE_PATTERN_EXACT, true);
  177. rrdhost_option_set(host, RRDHOST_OPTION_SENDER_ENABLED);
  178. }
  179. else
  180. rrdhost_option_clear(host, RRDHOST_OPTION_SENDER_ENABLED);
  181. }
  182. static RRDHOST *rrdhost_create(
  183. const char *hostname,
  184. const char *registry_hostname,
  185. const char *guid,
  186. const char *os,
  187. const char *timezone,
  188. const char *abbrev_timezone,
  189. int32_t utc_offset,
  190. const char *tags,
  191. const char *program_name,
  192. const char *program_version,
  193. int update_every,
  194. long entries,
  195. RRD_MEMORY_MODE memory_mode,
  196. unsigned int health_enabled,
  197. unsigned int rrdpush_enabled,
  198. char *rrdpush_destination,
  199. char *rrdpush_api_key,
  200. char *rrdpush_send_charts_matching,
  201. bool rrdpush_enable_replication,
  202. time_t rrdpush_seconds_to_replicate,
  203. time_t rrdpush_replication_step,
  204. struct rrdhost_system_info *system_info,
  205. int is_localhost,
  206. bool archived
  207. ) {
  208. debug(D_RRDHOST, "Host '%s': adding with guid '%s'", hostname, guid);
  209. if(memory_mode == RRD_MEMORY_MODE_DBENGINE && !dbengine_enabled) {
  210. error("memory mode 'dbengine' is not enabled, but host '%s' is configured for it. Falling back to 'alloc'", hostname);
  211. memory_mode = RRD_MEMORY_MODE_ALLOC;
  212. }
  213. #ifdef ENABLE_DBENGINE
  214. int is_legacy = (memory_mode == RRD_MEMORY_MODE_DBENGINE) && is_legacy_child(guid);
  215. #else
  216. int is_legacy = 1;
  217. #endif
  218. int is_in_multihost = (memory_mode == RRD_MEMORY_MODE_DBENGINE && !is_legacy);
  219. RRDHOST *host = callocz(1, sizeof(RRDHOST));
  220. __atomic_add_fetch(&netdata_buffers_statistics.rrdhost_allocations_size, sizeof(RRDHOST), __ATOMIC_RELAXED);
  221. strncpyz(host->machine_guid, guid, GUID_LEN + 1);
  222. set_host_properties(host, (update_every > 0)?update_every:1, memory_mode, registry_hostname, os,
  223. tags, timezone, abbrev_timezone, utc_offset, program_name, program_version);
  224. rrdhost_init_hostname(host, hostname, false);
  225. host->rrd_history_entries = align_entries_to_pagesize(memory_mode, entries);
  226. host->health.health_enabled = ((memory_mode == RRD_MEMORY_MODE_NONE)) ? 0 : health_enabled;
  227. if (likely(!archived)) {
  228. rrdfunctions_init(host);
  229. host->rrdlabels = rrdlabels_create();
  230. rrdhost_initialize_rrdpush_sender(
  231. host, rrdpush_enabled, rrdpush_destination, rrdpush_api_key, rrdpush_send_charts_matching);
  232. }
  233. if(rrdpush_enable_replication)
  234. rrdhost_option_set(host, RRDHOST_OPTION_REPLICATION);
  235. else
  236. rrdhost_option_clear(host, RRDHOST_OPTION_REPLICATION);
  237. host->rrdpush_seconds_to_replicate = rrdpush_seconds_to_replicate;
  238. host->rrdpush_replication_step = rrdpush_replication_step;
  239. switch(memory_mode) {
  240. default:
  241. case RRD_MEMORY_MODE_ALLOC:
  242. case RRD_MEMORY_MODE_MAP:
  243. case RRD_MEMORY_MODE_SAVE:
  244. case RRD_MEMORY_MODE_RAM:
  245. if(host->rrdpush_seconds_to_replicate > host->rrd_history_entries * host->rrd_update_every)
  246. host->rrdpush_seconds_to_replicate = host->rrd_history_entries * host->rrd_update_every;
  247. break;
  248. case RRD_MEMORY_MODE_DBENGINE:
  249. break;
  250. }
  251. netdata_mutex_init(&host->aclk_state_lock);
  252. netdata_mutex_init(&host->receiver_lock);
  253. host->system_info = system_info;
  254. rrdset_index_init(host);
  255. if(config_get_boolean(CONFIG_SECTION_DB, "delete obsolete charts files", 1))
  256. rrdhost_option_set(host, RRDHOST_OPTION_DELETE_OBSOLETE_CHARTS);
  257. if(config_get_boolean(CONFIG_SECTION_DB, "delete orphan hosts files", 1) && !is_localhost)
  258. rrdhost_option_set(host, RRDHOST_OPTION_DELETE_ORPHAN_HOST);
  259. char filename[FILENAME_MAX + 1];
  260. if(is_localhost)
  261. host->cache_dir = strdupz(netdata_configured_cache_dir);
  262. else {
  263. // this is not localhost - append our GUID to localhost path
  264. if (is_in_multihost) { // don't append to cache dir in multihost
  265. host->cache_dir = strdupz(netdata_configured_cache_dir);
  266. }
  267. else {
  268. snprintfz(filename, FILENAME_MAX, "%s/%s", netdata_configured_cache_dir, host->machine_guid);
  269. host->cache_dir = strdupz(filename);
  270. }
  271. if((host->rrd_memory_mode == RRD_MEMORY_MODE_MAP || host->rrd_memory_mode == RRD_MEMORY_MODE_SAVE ||
  272. (host->rrd_memory_mode == RRD_MEMORY_MODE_DBENGINE && is_legacy))) {
  273. int r = mkdir(host->cache_dir, 0775);
  274. if(r != 0 && errno != EEXIST)
  275. error("Host '%s': cannot create directory '%s'", rrdhost_hostname(host), host->cache_dir);
  276. }
  277. }
  278. // this is also needed for custom host variables - not only health
  279. if(!host->rrdvars)
  280. host->rrdvars = rrdvariables_create();
  281. if (likely(!uuid_parse(host->machine_guid, host->host_uuid)))
  282. sql_load_node_id(host);
  283. else
  284. error_report("Host machine GUID %s is not valid", host->machine_guid);
  285. rrdfamily_index_init(host);
  286. rrdcalctemplate_index_init(host);
  287. rrdcalc_rrdhost_index_init(host);
  288. if (host->rrd_memory_mode == RRD_MEMORY_MODE_DBENGINE) {
  289. #ifdef ENABLE_DBENGINE
  290. char dbenginepath[FILENAME_MAX + 1];
  291. int ret;
  292. snprintfz(dbenginepath, FILENAME_MAX, "%s/dbengine", host->cache_dir);
  293. ret = mkdir(dbenginepath, 0775);
  294. if (ret != 0 && errno != EEXIST)
  295. error("Host '%s': cannot create directory '%s'", rrdhost_hostname(host), dbenginepath);
  296. else
  297. ret = 0; // succeed
  298. if (is_legacy) {
  299. // initialize legacy dbengine instance as needed
  300. host->db[0].mode = RRD_MEMORY_MODE_DBENGINE;
  301. host->db[0].eng = storage_engine_get(host->db[0].mode);
  302. host->db[0].tier_grouping = get_tier_grouping(0);
  303. ret = rrdeng_init(
  304. (struct rrdengine_instance **)&host->db[0].instance,
  305. dbenginepath,
  306. default_rrdeng_disk_quota_mb,
  307. 0); // may fail here for legacy dbengine initialization
  308. if(ret == 0) {
  309. rrdeng_readiness_wait((struct rrdengine_instance *)host->db[0].instance);
  310. // assign the rest of the shared storage instances to it
  311. // to allow them collect its metrics too
  312. for(size_t tier = 1; tier < storage_tiers ; tier++) {
  313. host->db[tier].mode = RRD_MEMORY_MODE_DBENGINE;
  314. host->db[tier].eng = storage_engine_get(host->db[tier].mode);
  315. host->db[tier].instance = (STORAGE_INSTANCE *) multidb_ctx[tier];
  316. host->db[tier].tier_grouping = get_tier_grouping(tier);
  317. }
  318. }
  319. }
  320. else {
  321. for(size_t tier = 0; tier < storage_tiers ; tier++) {
  322. host->db[tier].mode = RRD_MEMORY_MODE_DBENGINE;
  323. host->db[tier].eng = storage_engine_get(host->db[tier].mode);
  324. host->db[tier].instance = (STORAGE_INSTANCE *)multidb_ctx[tier];
  325. host->db[tier].tier_grouping = get_tier_grouping(tier);
  326. }
  327. }
  328. if (ret) { // check legacy or multihost initialization success
  329. error(
  330. "Host '%s': cannot initialize host with machine guid '%s'. Failed to initialize DB engine at '%s'.",
  331. rrdhost_hostname(host), host->machine_guid, host->cache_dir);
  332. rrd_wrlock();
  333. rrdhost_free___while_having_rrd_wrlock(host, true);
  334. rrd_unlock();
  335. return NULL;
  336. }
  337. #else
  338. fatal("RRD_MEMORY_MODE_DBENGINE is not supported in this platform.");
  339. #endif
  340. }
  341. else {
  342. host->db[0].mode = host->rrd_memory_mode;
  343. host->db[0].eng = storage_engine_get(host->db[0].mode);
  344. host->db[0].instance = NULL;
  345. host->db[0].tier_grouping = get_tier_grouping(0);
  346. #ifdef ENABLE_DBENGINE
  347. // the first tier is reserved for the non-dbengine modes
  348. for(size_t tier = 1; tier < storage_tiers ; tier++) {
  349. host->db[tier].mode = RRD_MEMORY_MODE_DBENGINE;
  350. host->db[tier].eng = storage_engine_get(host->db[tier].mode);
  351. host->db[tier].instance = (STORAGE_INSTANCE *) multidb_ctx[tier];
  352. host->db[tier].tier_grouping = get_tier_grouping(tier);
  353. }
  354. #endif
  355. }
  356. // ------------------------------------------------------------------------
  357. // init new ML host and update system_info to let upstreams know
  358. // about ML functionality
  359. //
  360. if (is_localhost && host->system_info) {
  361. host->system_info->ml_capable = ml_capable();
  362. host->system_info->ml_enabled = ml_enabled(host);
  363. host->system_info->mc_version = enable_metric_correlations ? metric_correlations_version : 0;
  364. }
  365. // ------------------------------------------------------------------------
  366. // link it and add it to the index
  367. rrd_wrlock();
  368. RRDHOST *t = rrdhost_index_add_by_guid(host);
  369. if(t != host) {
  370. error("Host '%s': cannot add host with machine guid '%s' to index. It already exists as host '%s' with machine guid '%s'.", rrdhost_hostname(host), host->machine_guid, rrdhost_hostname(t), t->machine_guid);
  371. rrdhost_free___while_having_rrd_wrlock(host, true);
  372. rrd_unlock();
  373. return NULL;
  374. }
  375. rrdhost_index_add_hostname(host);
  376. if(is_localhost)
  377. DOUBLE_LINKED_LIST_PREPEND_ITEM_UNSAFE(localhost, host, prev, next);
  378. else
  379. DOUBLE_LINKED_LIST_APPEND_ITEM_UNSAFE(localhost, host, prev, next);
  380. rrd_unlock();
  381. // ------------------------------------------------------------------------
  382. info("Host '%s' (at registry as '%s') with guid '%s' initialized"
  383. ", os '%s'"
  384. ", timezone '%s'"
  385. ", tags '%s'"
  386. ", program_name '%s'"
  387. ", program_version '%s'"
  388. ", update every %d"
  389. ", memory mode %s"
  390. ", history entries %ld"
  391. ", streaming %s"
  392. " (to '%s' with api key '%s')"
  393. ", health %s"
  394. ", cache_dir '%s'"
  395. ", alarms default handler '%s'"
  396. ", alarms default recipient '%s'"
  397. , rrdhost_hostname(host)
  398. , rrdhost_registry_hostname(host)
  399. , host->machine_guid
  400. , rrdhost_os(host)
  401. , rrdhost_timezone(host)
  402. , rrdhost_tags(host)
  403. , rrdhost_program_name(host)
  404. , rrdhost_program_version(host)
  405. , host->rrd_update_every
  406. , rrd_memory_mode_name(host->rrd_memory_mode)
  407. , host->rrd_history_entries
  408. , rrdhost_has_rrdpush_sender_enabled(host)?"enabled":"disabled"
  409. , host->rrdpush_send_destination?host->rrdpush_send_destination:""
  410. , host->rrdpush_send_api_key?host->rrdpush_send_api_key:""
  411. , host->health.health_enabled?"enabled":"disabled"
  412. , host->cache_dir
  413. , string2str(host->health.health_default_exec)
  414. , string2str(host->health.health_default_recipient)
  415. );
  416. if(!archived) {
  417. metaqueue_host_update_info(host);
  418. rrdhost_load_rrdcontext_data(host);
  419. // rrdhost_flag_set(host, RRDHOST_FLAG_METADATA_INFO | RRDHOST_FLAG_METADATA_UPDATE);
  420. ml_host_new(host);
  421. ml_host_start_training_thread(host);
  422. } else
  423. rrdhost_flag_set(host, RRDHOST_FLAG_PENDING_CONTEXT_LOAD | RRDHOST_FLAG_ARCHIVED | RRDHOST_FLAG_ORPHAN);
  424. return host;
  425. }
  426. static void rrdhost_update(RRDHOST *host
  427. , const char *hostname
  428. , const char *registry_hostname
  429. , const char *guid
  430. , const char *os
  431. , const char *timezone
  432. , const char *abbrev_timezone
  433. , int32_t utc_offset
  434. , const char *tags
  435. , const char *program_name
  436. , const char *program_version
  437. , int update_every
  438. , long history
  439. , RRD_MEMORY_MODE mode
  440. , unsigned int health_enabled
  441. , unsigned int rrdpush_enabled
  442. , char *rrdpush_destination
  443. , char *rrdpush_api_key
  444. , char *rrdpush_send_charts_matching
  445. , bool rrdpush_enable_replication
  446. , time_t rrdpush_seconds_to_replicate
  447. , time_t rrdpush_replication_step
  448. , struct rrdhost_system_info *system_info
  449. )
  450. {
  451. UNUSED(guid);
  452. netdata_spinlock_lock(&host->rrdhost_update_lock);
  453. host->health.health_enabled = (mode == RRD_MEMORY_MODE_NONE) ? 0 : health_enabled;
  454. {
  455. struct rrdhost_system_info *old = host->system_info;
  456. host->system_info = system_info;
  457. rrdhost_flag_set(host, RRDHOST_FLAG_METADATA_INFO | RRDHOST_FLAG_METADATA_CLAIMID | RRDHOST_FLAG_METADATA_UPDATE);
  458. rrdhost_system_info_free(old);
  459. }
  460. rrdhost_init_os(host, os);
  461. rrdhost_init_timezone(host, timezone, abbrev_timezone, utc_offset);
  462. string_freez(host->registry_hostname);
  463. host->registry_hostname = string_strdupz((registry_hostname && *registry_hostname)?registry_hostname:hostname);
  464. if(strcmp(rrdhost_hostname(host), hostname) != 0) {
  465. info("Host '%s' has been renamed to '%s'. If this is not intentional it may mean multiple hosts are using the same machine_guid.", rrdhost_hostname(host), hostname);
  466. rrdhost_init_hostname(host, hostname, true);
  467. } else {
  468. rrdhost_index_add_hostname(host);
  469. }
  470. if(strcmp(rrdhost_program_name(host), program_name) != 0) {
  471. info("Host '%s' switched program name from '%s' to '%s'", rrdhost_hostname(host), rrdhost_program_name(host), program_name);
  472. STRING *t = host->program_name;
  473. host->program_name = string_strdupz(program_name);
  474. string_freez(t);
  475. }
  476. if(strcmp(rrdhost_program_version(host), program_version) != 0) {
  477. info("Host '%s' switched program version from '%s' to '%s'", rrdhost_hostname(host), rrdhost_program_version(host), program_version);
  478. STRING *t = host->program_version;
  479. host->program_version = string_strdupz(program_version);
  480. string_freez(t);
  481. }
  482. if(host->rrd_update_every != update_every)
  483. error("Host '%s' has an update frequency of %d seconds, but the wanted one is %d seconds. Restart netdata here to apply the new settings.", rrdhost_hostname(host), host->rrd_update_every, update_every);
  484. if(host->rrd_memory_mode != mode)
  485. error("Host '%s' has memory mode '%s', but the wanted one is '%s'. Restart netdata here to apply the new settings.", rrdhost_hostname(host), rrd_memory_mode_name(host->rrd_memory_mode), rrd_memory_mode_name(mode));
  486. else if(host->rrd_memory_mode != RRD_MEMORY_MODE_DBENGINE && host->rrd_history_entries < history)
  487. error("Host '%s' has history of %ld entries, but the wanted one is %ld entries. Restart netdata here to apply the new settings.", rrdhost_hostname(host), host->rrd_history_entries, history);
  488. // update host tags
  489. rrdhost_init_tags(host, tags);
  490. if(!host->rrdvars)
  491. host->rrdvars = rrdvariables_create();
  492. if (rrdhost_flag_check(host, RRDHOST_FLAG_ARCHIVED)) {
  493. rrdhost_flag_clear(host, RRDHOST_FLAG_ARCHIVED);
  494. rrdfunctions_init(host);
  495. if(!host->rrdlabels)
  496. host->rrdlabels = rrdlabels_create();
  497. if (!host->rrdset_root_index)
  498. rrdset_index_init(host);
  499. rrdhost_initialize_rrdpush_sender(host,
  500. rrdpush_enabled,
  501. rrdpush_destination,
  502. rrdpush_api_key,
  503. rrdpush_send_charts_matching);
  504. rrdfamily_index_init(host);
  505. rrdcalctemplate_index_init(host);
  506. rrdcalc_rrdhost_index_init(host);
  507. if(rrdpush_enable_replication)
  508. rrdhost_option_set(host, RRDHOST_OPTION_REPLICATION);
  509. else
  510. rrdhost_option_clear(host, RRDHOST_OPTION_REPLICATION);
  511. host->rrdpush_seconds_to_replicate = rrdpush_seconds_to_replicate;
  512. host->rrdpush_replication_step = rrdpush_replication_step;
  513. ml_host_new(host);
  514. ml_host_start_training_thread(host);
  515. rrdhost_load_rrdcontext_data(host);
  516. info("Host %s is not in archived mode anymore", rrdhost_hostname(host));
  517. }
  518. netdata_spinlock_unlock(&host->rrdhost_update_lock);
  519. }
  520. RRDHOST *rrdhost_find_or_create(
  521. const char *hostname
  522. , const char *registry_hostname
  523. , const char *guid
  524. , const char *os
  525. , const char *timezone
  526. , const char *abbrev_timezone
  527. , int32_t utc_offset
  528. , const char *tags
  529. , const char *program_name
  530. , const char *program_version
  531. , int update_every
  532. , long history
  533. , RRD_MEMORY_MODE mode
  534. , unsigned int health_enabled
  535. , unsigned int rrdpush_enabled
  536. , char *rrdpush_destination
  537. , char *rrdpush_api_key
  538. , char *rrdpush_send_charts_matching
  539. , bool rrdpush_enable_replication
  540. , time_t rrdpush_seconds_to_replicate
  541. , time_t rrdpush_replication_step
  542. , struct rrdhost_system_info *system_info
  543. , bool archived
  544. ) {
  545. debug(D_RRDHOST, "Searching for host '%s' with guid '%s'", hostname, guid);
  546. RRDHOST *host = rrdhost_find_by_guid(guid);
  547. if (unlikely(host && host->rrd_memory_mode != mode && rrdhost_flag_check(host, RRDHOST_FLAG_ARCHIVED))) {
  548. /* If a legacy memory mode instantiates all dbengine state must be discarded to avoid inconsistencies */
  549. error("Archived host '%s' has memory mode '%s', but the wanted one is '%s'. Discarding archived state.",
  550. rrdhost_hostname(host), rrd_memory_mode_name(host->rrd_memory_mode), rrd_memory_mode_name(mode));
  551. rrd_wrlock();
  552. rrdhost_free___while_having_rrd_wrlock(host, true);
  553. host = NULL;
  554. rrd_unlock();
  555. }
  556. if(!host) {
  557. host = rrdhost_create(
  558. hostname
  559. , registry_hostname
  560. , guid
  561. , os
  562. , timezone
  563. , abbrev_timezone
  564. , utc_offset
  565. , tags
  566. , program_name
  567. , program_version
  568. , update_every
  569. , history
  570. , mode
  571. , health_enabled
  572. , rrdpush_enabled
  573. , rrdpush_destination
  574. , rrdpush_api_key
  575. , rrdpush_send_charts_matching
  576. , rrdpush_enable_replication
  577. , rrdpush_seconds_to_replicate
  578. , rrdpush_replication_step
  579. , system_info
  580. , 0
  581. , archived
  582. );
  583. }
  584. else {
  585. if (likely(!rrdhost_flag_check(host, RRDHOST_FLAG_PENDING_CONTEXT_LOAD)))
  586. rrdhost_update(host
  587. , hostname
  588. , registry_hostname
  589. , guid
  590. , os
  591. , timezone
  592. , abbrev_timezone
  593. , utc_offset
  594. , tags
  595. , program_name
  596. , program_version
  597. , update_every
  598. , history
  599. , mode
  600. , health_enabled
  601. , rrdpush_enabled
  602. , rrdpush_destination
  603. , rrdpush_api_key
  604. , rrdpush_send_charts_matching
  605. , rrdpush_enable_replication
  606. , rrdpush_seconds_to_replicate
  607. , rrdpush_replication_step
  608. , system_info);
  609. }
  610. return host;
  611. }
  612. inline int rrdhost_should_be_removed(RRDHOST *host, RRDHOST *protected_host, time_t now_s) {
  613. if(host != protected_host
  614. && host != localhost
  615. && rrdhost_receiver_replicating_charts(host) == 0
  616. && rrdhost_sender_replicating_charts(host) == 0
  617. && rrdhost_flag_check(host, RRDHOST_FLAG_ORPHAN)
  618. && !rrdhost_flag_check(host, RRDHOST_FLAG_ARCHIVED)
  619. && !host->receiver
  620. && host->child_disconnected_time
  621. && host->child_disconnected_time + rrdhost_free_orphan_time_s < now_s)
  622. return 1;
  623. return 0;
  624. }
  625. // ----------------------------------------------------------------------------
  626. // RRDHOST global / startup initialization
  627. #ifdef ENABLE_DBENGINE
  628. struct dbengine_initialization {
  629. netdata_thread_t thread;
  630. char path[FILENAME_MAX + 1];
  631. int disk_space_mb;
  632. size_t tier;
  633. int ret;
  634. };
  635. void *dbengine_tier_init(void *ptr) {
  636. struct dbengine_initialization *dbi = ptr;
  637. dbi->ret = rrdeng_init(NULL, dbi->path, dbi->disk_space_mb, dbi->tier);
  638. return ptr;
  639. }
  640. #endif
  641. void dbengine_init(char *hostname) {
  642. #ifdef ENABLE_DBENGINE
  643. use_direct_io = config_get_boolean(CONFIG_SECTION_DB, "dbengine use direct io", use_direct_io);
  644. unsigned read_num = (unsigned)config_get_number(CONFIG_SECTION_DB, "dbengine pages per extent", MAX_PAGES_PER_EXTENT);
  645. if (read_num > 0 && read_num <= MAX_PAGES_PER_EXTENT)
  646. rrdeng_pages_per_extent = read_num;
  647. else {
  648. error("Invalid dbengine pages per extent %u given. Using %u.", read_num, rrdeng_pages_per_extent);
  649. config_set_number(CONFIG_SECTION_DB, "dbengine pages per extent", rrdeng_pages_per_extent);
  650. }
  651. storage_tiers = config_get_number(CONFIG_SECTION_DB, "storage tiers", storage_tiers);
  652. if(storage_tiers < 1) {
  653. error("At least 1 storage tier is required. Assuming 1.");
  654. storage_tiers = 1;
  655. config_set_number(CONFIG_SECTION_DB, "storage tiers", storage_tiers);
  656. }
  657. if(storage_tiers > RRD_STORAGE_TIERS) {
  658. error("Up to %d storage tier are supported. Assuming %d.", RRD_STORAGE_TIERS, RRD_STORAGE_TIERS);
  659. storage_tiers = RRD_STORAGE_TIERS;
  660. config_set_number(CONFIG_SECTION_DB, "storage tiers", storage_tiers);
  661. }
  662. bool parallel_initialization = (storage_tiers <= (size_t)get_netdata_cpus()) ? true : false;
  663. parallel_initialization = config_get_boolean(CONFIG_SECTION_DB, "dbengine parallel initialization", parallel_initialization);
  664. struct dbengine_initialization tiers_init[RRD_STORAGE_TIERS] = {};
  665. size_t created_tiers = 0;
  666. char dbenginepath[FILENAME_MAX + 1];
  667. char dbengineconfig[200 + 1];
  668. int divisor = 1;
  669. for(size_t tier = 0; tier < storage_tiers ;tier++) {
  670. if(tier == 0)
  671. snprintfz(dbenginepath, FILENAME_MAX, "%s/dbengine", netdata_configured_cache_dir);
  672. else
  673. snprintfz(dbenginepath, FILENAME_MAX, "%s/dbengine-tier%zu", netdata_configured_cache_dir, tier);
  674. int ret = mkdir(dbenginepath, 0775);
  675. if (ret != 0 && errno != EEXIST) {
  676. error("DBENGINE on '%s': cannot create directory '%s'", hostname, dbenginepath);
  677. break;
  678. }
  679. if(tier > 0)
  680. divisor *= 2;
  681. int disk_space_mb = default_multidb_disk_quota_mb / divisor;
  682. size_t grouping_iterations = storage_tiers_grouping_iterations[tier];
  683. RRD_BACKFILL backfill = storage_tiers_backfill[tier];
  684. if(tier > 0) {
  685. snprintfz(dbengineconfig, 200, "dbengine tier %zu multihost disk space MB", tier);
  686. disk_space_mb = config_get_number(CONFIG_SECTION_DB, dbengineconfig, disk_space_mb);
  687. snprintfz(dbengineconfig, 200, "dbengine tier %zu update every iterations", tier);
  688. grouping_iterations = config_get_number(CONFIG_SECTION_DB, dbengineconfig, grouping_iterations);
  689. if(grouping_iterations < 2) {
  690. grouping_iterations = 2;
  691. config_set_number(CONFIG_SECTION_DB, dbengineconfig, grouping_iterations);
  692. error("DBENGINE on '%s': 'dbegnine tier %zu update every iterations' cannot be less than 2. Assuming 2.", hostname, tier);
  693. }
  694. snprintfz(dbengineconfig, 200, "dbengine tier %zu backfill", tier);
  695. const char *bf = config_get(CONFIG_SECTION_DB, dbengineconfig, backfill == RRD_BACKFILL_NEW ? "new" : backfill == RRD_BACKFILL_FULL ? "full" : "none");
  696. if(strcmp(bf, "new") == 0) backfill = RRD_BACKFILL_NEW;
  697. else if(strcmp(bf, "full") == 0) backfill = RRD_BACKFILL_FULL;
  698. else if(strcmp(bf, "none") == 0) backfill = RRD_BACKFILL_NONE;
  699. else {
  700. error("DBENGINE: unknown backfill value '%s', assuming 'new'", bf);
  701. config_set(CONFIG_SECTION_DB, dbengineconfig, "new");
  702. backfill = RRD_BACKFILL_NEW;
  703. }
  704. }
  705. storage_tiers_grouping_iterations[tier] = grouping_iterations;
  706. storage_tiers_backfill[tier] = backfill;
  707. if(tier > 0 && get_tier_grouping(tier) > 65535) {
  708. storage_tiers_grouping_iterations[tier] = 1;
  709. error("DBENGINE on '%s': dbengine tier %zu gives aggregation of more than 65535 points of tier 0. Disabling tiers above %zu", hostname, tier, tier);
  710. break;
  711. }
  712. internal_error(true, "DBENGINE tier %zu grouping iterations is set to %zu", tier, storage_tiers_grouping_iterations[tier]);
  713. tiers_init[tier].disk_space_mb = disk_space_mb;
  714. tiers_init[tier].tier = tier;
  715. strncpyz(tiers_init[tier].path, dbenginepath, FILENAME_MAX);
  716. tiers_init[tier].ret = 0;
  717. if(parallel_initialization)
  718. netdata_thread_create(&tiers_init[tier].thread, "DBENGINE_INIT", NETDATA_THREAD_OPTION_JOINABLE,
  719. dbengine_tier_init, &tiers_init[tier]);
  720. else
  721. dbengine_tier_init(&tiers_init[tier]);
  722. }
  723. for(size_t tier = 0; tier < storage_tiers ;tier++) {
  724. void *ptr;
  725. if(parallel_initialization)
  726. netdata_thread_join(tiers_init[tier].thread, &ptr);
  727. if(tiers_init[tier].ret != 0) {
  728. error("DBENGINE on '%s': Failed to initialize multi-host database tier %zu on path '%s'",
  729. hostname, tiers_init[tier].tier, tiers_init[tier].path);
  730. }
  731. else if(created_tiers == tier)
  732. created_tiers++;
  733. }
  734. if(created_tiers && created_tiers < storage_tiers) {
  735. error("DBENGINE on '%s': Managed to create %zu tiers instead of %zu. Continuing with %zu available.",
  736. hostname, created_tiers, storage_tiers, created_tiers);
  737. storage_tiers = created_tiers;
  738. }
  739. else if(!created_tiers)
  740. fatal("DBENGINE on '%s', failed to initialize databases at '%s'.", hostname, netdata_configured_cache_dir);
  741. for(size_t tier = 0; tier < storage_tiers ;tier++)
  742. rrdeng_readiness_wait(multidb_ctx[tier]);
  743. dbengine_enabled = true;
  744. #else
  745. storage_tiers = config_get_number(CONFIG_SECTION_DB, "storage tiers", 1);
  746. if(storage_tiers != 1) {
  747. error("DBENGINE is not available on '%s', so only 1 database tier can be supported.", hostname);
  748. storage_tiers = 1;
  749. config_set_number(CONFIG_SECTION_DB, "storage tiers", storage_tiers);
  750. }
  751. dbengine_enabled = false;
  752. #endif
  753. }
  754. int rrd_init(char *hostname, struct rrdhost_system_info *system_info, bool unittest) {
  755. rrdhost_init();
  756. if (unlikely(sql_init_database(DB_CHECK_NONE, system_info ? 0 : 1))) {
  757. if (default_rrd_memory_mode == RRD_MEMORY_MODE_DBENGINE)
  758. fatal("Failed to initialize SQLite");
  759. info("Skipping SQLITE metadata initialization since memory mode is not dbengine");
  760. }
  761. if (unlikely(sql_init_context_database(system_info ? 0 : 1))) {
  762. error_report("Failed to initialize context metadata database");
  763. }
  764. if (unlikely(unittest)) {
  765. dbengine_enabled = true;
  766. }
  767. else {
  768. health_init();
  769. rrdpush_init();
  770. if (default_rrd_memory_mode == RRD_MEMORY_MODE_DBENGINE || rrdpush_receiver_needs_dbengine()) {
  771. info("DBENGINE: Initializing ...");
  772. dbengine_init(hostname);
  773. }
  774. else {
  775. info("DBENGINE: Not initializing ...");
  776. storage_tiers = 1;
  777. }
  778. if (!dbengine_enabled) {
  779. if (storage_tiers > 1) {
  780. error("dbengine is not enabled, but %zu tiers have been requested. Resetting tiers to 1",
  781. storage_tiers);
  782. storage_tiers = 1;
  783. }
  784. if (default_rrd_memory_mode == RRD_MEMORY_MODE_DBENGINE) {
  785. error("dbengine is not enabled, but it has been given as the default db mode. Resetting db mode to alloc");
  786. default_rrd_memory_mode = RRD_MEMORY_MODE_ALLOC;
  787. }
  788. }
  789. }
  790. if(!unittest)
  791. metadata_sync_init();
  792. debug(D_RRDHOST, "Initializing localhost with hostname '%s'", hostname);
  793. localhost = rrdhost_create(
  794. hostname
  795. , registry_get_this_machine_hostname()
  796. , registry_get_this_machine_guid()
  797. , os_type
  798. , netdata_configured_timezone
  799. , netdata_configured_abbrev_timezone
  800. , netdata_configured_utc_offset
  801. , ""
  802. , program_name
  803. , program_version
  804. , default_rrd_update_every
  805. , default_rrd_history_entries
  806. , default_rrd_memory_mode
  807. , default_health_enabled
  808. , default_rrdpush_enabled
  809. , default_rrdpush_destination
  810. , default_rrdpush_api_key
  811. , default_rrdpush_send_charts_matching
  812. , default_rrdpush_enable_replication
  813. , default_rrdpush_seconds_to_replicate
  814. , default_rrdpush_replication_step
  815. , system_info
  816. , 1
  817. , 0
  818. );
  819. if (unlikely(!localhost)) {
  820. return 1;
  821. }
  822. if (likely(system_info)) {
  823. migrate_localhost(&localhost->host_uuid);
  824. sql_aclk_sync_init();
  825. web_client_api_v1_management_init();
  826. }
  827. return localhost==NULL;
  828. }
  829. // ----------------------------------------------------------------------------
  830. // RRDHOST - free
  831. void rrdhost_system_info_free(struct rrdhost_system_info *system_info) {
  832. if(likely(system_info)) {
  833. __atomic_sub_fetch(&netdata_buffers_statistics.rrdhost_allocations_size, sizeof(struct rrdhost_system_info), __ATOMIC_RELAXED);
  834. freez(system_info->cloud_provider_type);
  835. freez(system_info->cloud_instance_type);
  836. freez(system_info->cloud_instance_region);
  837. freez(system_info->host_os_name);
  838. freez(system_info->host_os_id);
  839. freez(system_info->host_os_id_like);
  840. freez(system_info->host_os_version);
  841. freez(system_info->host_os_version_id);
  842. freez(system_info->host_os_detection);
  843. freez(system_info->host_cores);
  844. freez(system_info->host_cpu_freq);
  845. freez(system_info->host_ram_total);
  846. freez(system_info->host_disk_space);
  847. freez(system_info->container_os_name);
  848. freez(system_info->container_os_id);
  849. freez(system_info->container_os_id_like);
  850. freez(system_info->container_os_version);
  851. freez(system_info->container_os_version_id);
  852. freez(system_info->container_os_detection);
  853. freez(system_info->kernel_name);
  854. freez(system_info->kernel_version);
  855. freez(system_info->architecture);
  856. freez(system_info->virtualization);
  857. freez(system_info->virt_detection);
  858. freez(system_info->container);
  859. freez(system_info->container_detection);
  860. freez(system_info->is_k8s_node);
  861. freez(system_info->install_type);
  862. freez(system_info->prebuilt_arch);
  863. freez(system_info->prebuilt_dist);
  864. freez(system_info);
  865. }
  866. }
  867. static void rrdhost_streaming_sender_structures_init(RRDHOST *host)
  868. {
  869. if (host->sender)
  870. return;
  871. host->sender = callocz(1, sizeof(*host->sender));
  872. __atomic_add_fetch(&netdata_buffers_statistics.rrdhost_senders, sizeof(*host->sender), __ATOMIC_RELAXED);
  873. host->sender->host = host;
  874. host->sender->buffer = cbuffer_new(CBUFFER_INITIAL_SIZE, 1024 * 1024, &netdata_buffers_statistics.cbuffers_streaming);
  875. host->sender->capabilities = stream_our_capabilities();
  876. host->sender->rrdpush_sender_pipe[PIPE_READ] = -1;
  877. host->sender->rrdpush_sender_pipe[PIPE_WRITE] = -1;
  878. host->sender->rrdpush_sender_socket = -1;
  879. #ifdef ENABLE_COMPRESSION
  880. if(default_compression_enabled) {
  881. host->sender->flags |= SENDER_FLAG_COMPRESSION;
  882. host->sender->compressor = create_compressor();
  883. }
  884. else
  885. host->sender->flags &= ~SENDER_FLAG_COMPRESSION;
  886. #endif
  887. netdata_mutex_init(&host->sender->mutex);
  888. replication_init_sender(host->sender);
  889. }
  890. static void rrdhost_streaming_sender_structures_free(RRDHOST *host)
  891. {
  892. rrdhost_option_clear(host, RRDHOST_OPTION_SENDER_ENABLED);
  893. if (unlikely(!host->sender))
  894. return;
  895. rrdpush_sender_thread_stop(host, "HOST CLEANUP", true); // stop a possibly running thread
  896. cbuffer_free(host->sender->buffer);
  897. #ifdef ENABLE_COMPRESSION
  898. if (host->sender->compressor)
  899. host->sender->compressor->destroy(&host->sender->compressor);
  900. #endif
  901. replication_cleanup_sender(host->sender);
  902. __atomic_sub_fetch(&netdata_buffers_statistics.rrdhost_senders, sizeof(*host->sender), __ATOMIC_RELAXED);
  903. freez(host->sender);
  904. host->sender = NULL;
  905. rrdhost_flag_clear(host, RRDHOST_FLAG_RRDPUSH_SENDER_INITIALIZED);
  906. }
  907. void rrdhost_free___while_having_rrd_wrlock(RRDHOST *host, bool force) {
  908. if(!host) return;
  909. if (netdata_exit || force) {
  910. info("RRD: 'host:%s' freeing memory...", rrdhost_hostname(host));
  911. // ------------------------------------------------------------------------
  912. // first remove it from the indexes, so that it will not be discoverable
  913. rrdhost_index_del_hostname(host);
  914. rrdhost_index_del_by_guid(host);
  915. if (host->prev)
  916. DOUBLE_LINKED_LIST_REMOVE_ITEM_UNSAFE(localhost, host, prev, next);
  917. }
  918. // ------------------------------------------------------------------------
  919. // clean up streaming
  920. rrdhost_streaming_sender_structures_free(host);
  921. if (netdata_exit || force)
  922. stop_streaming_receiver(host, "HOST CLEANUP");
  923. // ------------------------------------------------------------------------
  924. // clean up alarms
  925. rrdcalc_delete_all(host);
  926. // ------------------------------------------------------------------------
  927. // release its children resources
  928. #ifdef ENABLE_DBENGINE
  929. for(size_t tier = 0; tier < storage_tiers ;tier++) {
  930. if(host->db[tier].mode == RRD_MEMORY_MODE_DBENGINE
  931. && host->db[tier].instance
  932. && !is_storage_engine_shared(host->db[tier].instance))
  933. rrdeng_prepare_exit((struct rrdengine_instance *)host->db[tier].instance);
  934. }
  935. #endif
  936. // delete all the RRDSETs of the host
  937. rrdset_index_destroy(host);
  938. rrdcalc_rrdhost_index_destroy(host);
  939. rrdcalctemplate_index_destroy(host);
  940. // cleanup ML resources
  941. ml_host_stop_training_thread(host);
  942. ml_host_delete(host);
  943. freez(host->exporting_flags);
  944. health_alarm_log_free(host);
  945. #ifdef ENABLE_DBENGINE
  946. for(size_t tier = 0; tier < storage_tiers ;tier++) {
  947. if(host->db[tier].mode == RRD_MEMORY_MODE_DBENGINE
  948. && host->db[tier].instance
  949. && !is_storage_engine_shared(host->db[tier].instance))
  950. rrdeng_exit((struct rrdengine_instance *)host->db[tier].instance);
  951. }
  952. #endif
  953. if (!netdata_exit && !force) {
  954. info("RRD: 'host:%s' is now in archive mode...", rrdhost_hostname(host));
  955. rrdhost_flag_set(host, RRDHOST_FLAG_ARCHIVED | RRDHOST_FLAG_ORPHAN);
  956. return;
  957. }
  958. // ------------------------------------------------------------------------
  959. // free it
  960. pthread_mutex_destroy(&host->aclk_state_lock);
  961. freez(host->aclk_state.claimed_id);
  962. freez(host->aclk_state.prev_claimed_id);
  963. string_freez(host->tags);
  964. rrdlabels_destroy(host->rrdlabels);
  965. string_freez(host->os);
  966. string_freez(host->timezone);
  967. string_freez(host->abbrev_timezone);
  968. string_freez(host->program_name);
  969. string_freez(host->program_version);
  970. rrdhost_system_info_free(host->system_info);
  971. freez(host->cache_dir);
  972. freez(host->rrdpush_send_api_key);
  973. freez(host->rrdpush_send_destination);
  974. rrdpush_destinations_free(host);
  975. string_freez(host->health.health_default_exec);
  976. string_freez(host->health.health_default_recipient);
  977. string_freez(host->registry_hostname);
  978. simple_pattern_free(host->rrdpush_send_charts_matching);
  979. netdata_rwlock_destroy(&host->health_log.alarm_log_rwlock);
  980. freez(host->node_id);
  981. rrdfamily_index_destroy(host);
  982. rrdfunctions_destroy(host);
  983. rrdvariables_destroy(host->rrdvars);
  984. if (host == localhost)
  985. rrdvariables_destroy(health_rrdvars);
  986. rrdhost_destroy_rrdcontexts(host);
  987. string_freez(host->hostname);
  988. __atomic_sub_fetch(&netdata_buffers_statistics.rrdhost_allocations_size, sizeof(RRDHOST), __ATOMIC_RELAXED);
  989. freez(host);
  990. }
  991. void rrdhost_free_all(void) {
  992. rrd_wrlock();
  993. /* Make sure child-hosts are released before the localhost. */
  994. while(localhost && localhost->next)
  995. rrdhost_free___while_having_rrd_wrlock(localhost->next, true);
  996. if(localhost)
  997. rrdhost_free___while_having_rrd_wrlock(localhost, true);
  998. rrd_unlock();
  999. }
  1000. void rrd_finalize_collection_for_all_hosts(void) {
  1001. RRDHOST *host;
  1002. dfe_start_reentrant(rrdhost_root_index, host) {
  1003. rrdhost_finalize_collection(host);
  1004. }
  1005. dfe_done(host);
  1006. }
  1007. // ----------------------------------------------------------------------------
  1008. // RRDHOST - save host files
  1009. void rrdhost_save_charts(RRDHOST *host) {
  1010. if(!host) return;
  1011. info("RRD: 'host:%s' saving / closing database...", rrdhost_hostname(host));
  1012. RRDSET *st;
  1013. // we get a write lock
  1014. // to ensure only one thread is saving the database
  1015. rrdset_foreach_write(st, host) {
  1016. rrdset_save(st);
  1017. }
  1018. rrdset_foreach_done(st);
  1019. }
  1020. struct rrdhost_system_info *rrdhost_labels_to_system_info(DICTIONARY *labels) {
  1021. struct rrdhost_system_info *info = callocz(1, sizeof(struct rrdhost_system_info));
  1022. info->hops = 1;
  1023. rrdlabels_get_value_strdup_or_null(labels, &info->cloud_provider_type, "_cloud_provider_type");
  1024. rrdlabels_get_value_strdup_or_null(labels, &info->cloud_instance_type, "_cloud_instance_type");
  1025. rrdlabels_get_value_strdup_or_null(labels, &info->cloud_instance_region, "_cloud_instance_region");
  1026. rrdlabels_get_value_strdup_or_null(labels, &info->host_os_name, "_os_name");
  1027. rrdlabels_get_value_strdup_or_null(labels, &info->host_os_version, "_os_version");
  1028. rrdlabels_get_value_strdup_or_null(labels, &info->kernel_version, "_kernel_version");
  1029. rrdlabels_get_value_strdup_or_null(labels, &info->host_cores, "_system_cores");
  1030. rrdlabels_get_value_strdup_or_null(labels, &info->host_cpu_freq, "_system_cpu_freq");
  1031. rrdlabels_get_value_strdup_or_null(labels, &info->host_ram_total, "_system_ram_total");
  1032. rrdlabels_get_value_strdup_or_null(labels, &info->host_disk_space, "_system_disk_space");
  1033. rrdlabels_get_value_strdup_or_null(labels, &info->architecture, "_architecture");
  1034. rrdlabels_get_value_strdup_or_null(labels, &info->virtualization, "_virtualization");
  1035. rrdlabels_get_value_strdup_or_null(labels, &info->container, "_container");
  1036. rrdlabels_get_value_strdup_or_null(labels, &info->container_detection, "_container_detection");
  1037. rrdlabels_get_value_strdup_or_null(labels, &info->virt_detection, "_virt_detection");
  1038. rrdlabels_get_value_strdup_or_null(labels, &info->is_k8s_node, "_is_k8s_node");
  1039. rrdlabels_get_value_strdup_or_null(labels, &info->install_type, "_install_type");
  1040. rrdlabels_get_value_strdup_or_null(labels, &info->prebuilt_arch, "_prebuilt_arch");
  1041. rrdlabels_get_value_strdup_or_null(labels, &info->prebuilt_dist, "_prebuilt_dist");
  1042. return info;
  1043. }
  1044. static void rrdhost_load_auto_labels(void) {
  1045. DICTIONARY *labels = localhost->rrdlabels;
  1046. if (localhost->system_info->cloud_provider_type)
  1047. rrdlabels_add(labels, "_cloud_provider_type", localhost->system_info->cloud_provider_type, RRDLABEL_SRC_AUTO);
  1048. if (localhost->system_info->cloud_instance_type)
  1049. rrdlabels_add(labels, "_cloud_instance_type", localhost->system_info->cloud_instance_type, RRDLABEL_SRC_AUTO);
  1050. if (localhost->system_info->cloud_instance_region)
  1051. rrdlabels_add(labels, "_cloud_instance_region", localhost->system_info->cloud_instance_region, RRDLABEL_SRC_AUTO);
  1052. if (localhost->system_info->host_os_name)
  1053. rrdlabels_add(labels, "_os_name", localhost->system_info->host_os_name, RRDLABEL_SRC_AUTO);
  1054. if (localhost->system_info->host_os_version)
  1055. rrdlabels_add(labels, "_os_version", localhost->system_info->host_os_version, RRDLABEL_SRC_AUTO);
  1056. if (localhost->system_info->kernel_version)
  1057. rrdlabels_add(labels, "_kernel_version", localhost->system_info->kernel_version, RRDLABEL_SRC_AUTO);
  1058. if (localhost->system_info->host_cores)
  1059. rrdlabels_add(labels, "_system_cores", localhost->system_info->host_cores, RRDLABEL_SRC_AUTO);
  1060. if (localhost->system_info->host_cpu_freq)
  1061. rrdlabels_add(labels, "_system_cpu_freq", localhost->system_info->host_cpu_freq, RRDLABEL_SRC_AUTO);
  1062. if (localhost->system_info->host_ram_total)
  1063. rrdlabels_add(labels, "_system_ram_total", localhost->system_info->host_ram_total, RRDLABEL_SRC_AUTO);
  1064. if (localhost->system_info->host_disk_space)
  1065. rrdlabels_add(labels, "_system_disk_space", localhost->system_info->host_disk_space, RRDLABEL_SRC_AUTO);
  1066. if (localhost->system_info->architecture)
  1067. rrdlabels_add(labels, "_architecture", localhost->system_info->architecture, RRDLABEL_SRC_AUTO);
  1068. if (localhost->system_info->virtualization)
  1069. rrdlabels_add(labels, "_virtualization", localhost->system_info->virtualization, RRDLABEL_SRC_AUTO);
  1070. if (localhost->system_info->container)
  1071. rrdlabels_add(labels, "_container", localhost->system_info->container, RRDLABEL_SRC_AUTO);
  1072. if (localhost->system_info->container_detection)
  1073. rrdlabels_add(labels, "_container_detection", localhost->system_info->container_detection, RRDLABEL_SRC_AUTO);
  1074. if (localhost->system_info->virt_detection)
  1075. rrdlabels_add(labels, "_virt_detection", localhost->system_info->virt_detection, RRDLABEL_SRC_AUTO);
  1076. if (localhost->system_info->is_k8s_node)
  1077. rrdlabels_add(labels, "_is_k8s_node", localhost->system_info->is_k8s_node, RRDLABEL_SRC_AUTO);
  1078. if (localhost->system_info->install_type)
  1079. rrdlabels_add(labels, "_install_type", localhost->system_info->install_type, RRDLABEL_SRC_AUTO);
  1080. if (localhost->system_info->prebuilt_arch)
  1081. rrdlabels_add(labels, "_prebuilt_arch", localhost->system_info->prebuilt_arch, RRDLABEL_SRC_AUTO);
  1082. if (localhost->system_info->prebuilt_dist)
  1083. rrdlabels_add(labels, "_prebuilt_dist", localhost->system_info->prebuilt_dist, RRDLABEL_SRC_AUTO);
  1084. add_aclk_host_labels();
  1085. health_add_host_labels();
  1086. rrdlabels_add(labels, "_is_parent", (localhost->connected_children_count > 0) ? "true" : "false", RRDLABEL_SRC_AUTO);
  1087. if (localhost->rrdpush_send_destination)
  1088. rrdlabels_add(labels, "_streams_to", localhost->rrdpush_send_destination, RRDLABEL_SRC_AUTO);
  1089. }
  1090. void rrdhost_set_is_parent_label(int count) {
  1091. DICTIONARY *labels = localhost->rrdlabels;
  1092. if (count == 0 || count == 1) {
  1093. rrdlabels_add(labels, "_is_parent", (count) ? "true" : "false", RRDLABEL_SRC_AUTO);
  1094. //queue a node info
  1095. #ifdef ENABLE_ACLK
  1096. if (netdata_cloud_setting) {
  1097. aclk_queue_node_info(localhost, false);
  1098. }
  1099. #endif
  1100. }
  1101. }
  1102. static void rrdhost_load_config_labels(void) {
  1103. int status = config_load(NULL, 1, CONFIG_SECTION_HOST_LABEL);
  1104. if(!status) {
  1105. char *filename = CONFIG_DIR "/" CONFIG_FILENAME;
  1106. error("RRDLABEL: Cannot reload the configuration file '%s', using labels in memory", filename);
  1107. }
  1108. struct section *co = appconfig_get_section(&netdata_config, CONFIG_SECTION_HOST_LABEL);
  1109. if(co) {
  1110. config_section_wrlock(co);
  1111. struct config_option *cv;
  1112. for(cv = co->values; cv ; cv = cv->next) {
  1113. rrdlabels_add(localhost->rrdlabels, cv->name, cv->value, RRDLABEL_SRC_CONFIG);
  1114. cv->flags |= CONFIG_VALUE_USED;
  1115. }
  1116. config_section_unlock(co);
  1117. }
  1118. }
  1119. static void rrdhost_load_kubernetes_labels(void) {
  1120. char label_script[sizeof(char) * (strlen(netdata_configured_primary_plugins_dir) + strlen("get-kubernetes-labels.sh") + 2)];
  1121. sprintf(label_script, "%s/%s", netdata_configured_primary_plugins_dir, "get-kubernetes-labels.sh");
  1122. if (unlikely(access(label_script, R_OK) != 0)) {
  1123. error("Kubernetes pod label fetching script %s not found.",label_script);
  1124. return;
  1125. }
  1126. debug(D_RRDHOST, "Attempting to fetch external labels via %s", label_script);
  1127. pid_t pid;
  1128. FILE *fp_child_input;
  1129. FILE *fp_child_output = netdata_popen(label_script, &pid, &fp_child_input);
  1130. if(!fp_child_output) return;
  1131. char buffer[1000 + 1];
  1132. while (fgets(buffer, 1000, fp_child_output) != NULL)
  1133. rrdlabels_add_pair(localhost->rrdlabels, buffer, RRDLABEL_SRC_AUTO|RRDLABEL_SRC_K8S);
  1134. // Non-zero exit code means that all the script output is error messages. We've shown already any message that didn't include a ':'
  1135. // Here we'll inform with an ERROR that the script failed, show whatever (if anything) was added to the list of labels, free the memory and set the return to null
  1136. int rc = netdata_pclose(fp_child_input, fp_child_output, pid);
  1137. if(rc) error("%s exited abnormally. Failed to get kubernetes labels.", label_script);
  1138. }
  1139. void reload_host_labels(void) {
  1140. if(!localhost->rrdlabels)
  1141. localhost->rrdlabels = rrdlabels_create();
  1142. rrdlabels_unmark_all(localhost->rrdlabels);
  1143. // priority is important here
  1144. rrdhost_load_config_labels();
  1145. rrdhost_load_kubernetes_labels();
  1146. rrdhost_load_auto_labels();
  1147. rrdhost_flag_set(localhost,RRDHOST_FLAG_METADATA_LABELS | RRDHOST_FLAG_METADATA_UPDATE);
  1148. rrdpush_send_host_labels(localhost);
  1149. }
  1150. void rrdhost_finalize_collection(RRDHOST *host) {
  1151. info("RRD: 'host:%s' stopping data collection...", rrdhost_hostname(host));
  1152. RRDSET *st;
  1153. rrdset_foreach_read(st, host)
  1154. rrdset_finalize_collection(st, true);
  1155. rrdset_foreach_done(st);
  1156. }
  1157. // ----------------------------------------------------------------------------
  1158. // RRDHOST - delete host files
  1159. void rrdhost_delete_charts(RRDHOST *host) {
  1160. if(!host) return;
  1161. info("RRD: 'host:%s' deleting disk files...", rrdhost_hostname(host));
  1162. RRDSET *st;
  1163. if(host->rrd_memory_mode == RRD_MEMORY_MODE_SAVE || host->rrd_memory_mode == RRD_MEMORY_MODE_MAP) {
  1164. // we get a write lock
  1165. // to ensure only one thread is saving the database
  1166. rrdset_foreach_write(st, host){
  1167. rrdset_delete_files(st);
  1168. }
  1169. rrdset_foreach_done(st);
  1170. }
  1171. recursively_delete_dir(host->cache_dir, "left over host");
  1172. }
  1173. // ----------------------------------------------------------------------------
  1174. // RRDHOST - cleanup host files
  1175. void rrdhost_cleanup_charts(RRDHOST *host) {
  1176. if(!host) return;
  1177. info("RRD: 'host:%s' cleaning up disk files...", rrdhost_hostname(host));
  1178. RRDSET *st;
  1179. uint32_t rrdhost_delete_obsolete_charts = rrdhost_option_check(host, RRDHOST_OPTION_DELETE_OBSOLETE_CHARTS);
  1180. // we get a write lock
  1181. // to ensure only one thread is saving the database
  1182. rrdset_foreach_write(st, host) {
  1183. if(rrdhost_delete_obsolete_charts && rrdset_flag_check(st, RRDSET_FLAG_OBSOLETE))
  1184. rrdset_delete_files(st);
  1185. else if(rrdhost_delete_obsolete_charts && rrdset_flag_check(st, RRDSET_FLAG_OBSOLETE_DIMENSIONS))
  1186. rrdset_delete_obsolete_dimensions(st);
  1187. else
  1188. rrdset_save(st);
  1189. }
  1190. rrdset_foreach_done(st);
  1191. }
  1192. // ----------------------------------------------------------------------------
  1193. // RRDHOST - save all hosts to disk
  1194. void rrdhost_save_all(void) {
  1195. info("RRD: saving databases [%zu hosts(s)]...", rrdhost_hosts_available());
  1196. rrd_rdlock();
  1197. RRDHOST *host;
  1198. rrdhost_foreach_read(host)
  1199. rrdhost_save_charts(host);
  1200. rrd_unlock();
  1201. }
  1202. // ----------------------------------------------------------------------------
  1203. // RRDHOST - save or delete all hosts from disk
  1204. void rrdhost_cleanup_all(void) {
  1205. info("RRD: cleaning up database [%zu hosts(s)]...", rrdhost_hosts_available());
  1206. rrd_rdlock();
  1207. RRDHOST *host;
  1208. rrdhost_foreach_read(host) {
  1209. if (host != localhost && rrdhost_option_check(host, RRDHOST_OPTION_DELETE_ORPHAN_HOST) && !host->receiver
  1210. /* don't delete multi-host DB host files */
  1211. && !(host->rrd_memory_mode == RRD_MEMORY_MODE_DBENGINE && is_storage_engine_shared(host->db[0].instance))
  1212. )
  1213. rrdhost_delete_charts(host);
  1214. else
  1215. rrdhost_cleanup_charts(host);
  1216. }
  1217. rrd_unlock();
  1218. }
  1219. // ----------------------------------------------------------------------------
  1220. // RRDHOST - set system info from environment variables
  1221. // system_info fields must be heap allocated or NULL
  1222. int rrdhost_set_system_info_variable(struct rrdhost_system_info *system_info, char *name, char *value) {
  1223. int res = 0;
  1224. if (!strcmp(name, "NETDATA_PROTOCOL_VERSION"))
  1225. return res;
  1226. else if(!strcmp(name, "NETDATA_INSTANCE_CLOUD_TYPE")){
  1227. freez(system_info->cloud_provider_type);
  1228. system_info->cloud_provider_type = strdupz(value);
  1229. }
  1230. else if(!strcmp(name, "NETDATA_INSTANCE_CLOUD_INSTANCE_TYPE")){
  1231. freez(system_info->cloud_instance_type);
  1232. system_info->cloud_instance_type = strdupz(value);
  1233. }
  1234. else if(!strcmp(name, "NETDATA_INSTANCE_CLOUD_INSTANCE_REGION")){
  1235. freez(system_info->cloud_instance_region);
  1236. system_info->cloud_instance_region = strdupz(value);
  1237. }
  1238. else if(!strcmp(name, "NETDATA_CONTAINER_OS_NAME")){
  1239. freez(system_info->container_os_name);
  1240. system_info->container_os_name = strdupz(value);
  1241. }
  1242. else if(!strcmp(name, "NETDATA_CONTAINER_OS_ID")){
  1243. freez(system_info->container_os_id);
  1244. system_info->container_os_id = strdupz(value);
  1245. }
  1246. else if(!strcmp(name, "NETDATA_CONTAINER_OS_ID_LIKE")){
  1247. freez(system_info->container_os_id_like);
  1248. system_info->container_os_id_like = strdupz(value);
  1249. }
  1250. else if(!strcmp(name, "NETDATA_CONTAINER_OS_VERSION")){
  1251. freez(system_info->container_os_version);
  1252. system_info->container_os_version = strdupz(value);
  1253. }
  1254. else if(!strcmp(name, "NETDATA_CONTAINER_OS_VERSION_ID")){
  1255. freez(system_info->container_os_version_id);
  1256. system_info->container_os_version_id = strdupz(value);
  1257. }
  1258. else if(!strcmp(name, "NETDATA_CONTAINER_OS_DETECTION")){
  1259. freez(system_info->container_os_detection);
  1260. system_info->container_os_detection = strdupz(value);
  1261. }
  1262. else if(!strcmp(name, "NETDATA_HOST_OS_NAME")){
  1263. freez(system_info->host_os_name);
  1264. system_info->host_os_name = strdupz(value);
  1265. json_fix_string(system_info->host_os_name);
  1266. }
  1267. else if(!strcmp(name, "NETDATA_HOST_OS_ID")){
  1268. freez(system_info->host_os_id);
  1269. system_info->host_os_id = strdupz(value);
  1270. }
  1271. else if(!strcmp(name, "NETDATA_HOST_OS_ID_LIKE")){
  1272. freez(system_info->host_os_id_like);
  1273. system_info->host_os_id_like = strdupz(value);
  1274. }
  1275. else if(!strcmp(name, "NETDATA_HOST_OS_VERSION")){
  1276. freez(system_info->host_os_version);
  1277. system_info->host_os_version = strdupz(value);
  1278. }
  1279. else if(!strcmp(name, "NETDATA_HOST_OS_VERSION_ID")){
  1280. freez(system_info->host_os_version_id);
  1281. system_info->host_os_version_id = strdupz(value);
  1282. }
  1283. else if(!strcmp(name, "NETDATA_HOST_OS_DETECTION")){
  1284. freez(system_info->host_os_detection);
  1285. system_info->host_os_detection = strdupz(value);
  1286. }
  1287. else if(!strcmp(name, "NETDATA_SYSTEM_KERNEL_NAME")){
  1288. freez(system_info->kernel_name);
  1289. system_info->kernel_name = strdupz(value);
  1290. }
  1291. else if(!strcmp(name, "NETDATA_SYSTEM_CPU_LOGICAL_CPU_COUNT")){
  1292. freez(system_info->host_cores);
  1293. system_info->host_cores = strdupz(value);
  1294. }
  1295. else if(!strcmp(name, "NETDATA_SYSTEM_CPU_FREQ")){
  1296. freez(system_info->host_cpu_freq);
  1297. system_info->host_cpu_freq = strdupz(value);
  1298. }
  1299. else if(!strcmp(name, "NETDATA_SYSTEM_TOTAL_RAM")){
  1300. freez(system_info->host_ram_total);
  1301. system_info->host_ram_total = strdupz(value);
  1302. }
  1303. else if(!strcmp(name, "NETDATA_SYSTEM_TOTAL_DISK_SIZE")){
  1304. freez(system_info->host_disk_space);
  1305. system_info->host_disk_space = strdupz(value);
  1306. }
  1307. else if(!strcmp(name, "NETDATA_SYSTEM_KERNEL_VERSION")){
  1308. freez(system_info->kernel_version);
  1309. system_info->kernel_version = strdupz(value);
  1310. }
  1311. else if(!strcmp(name, "NETDATA_SYSTEM_ARCHITECTURE")){
  1312. freez(system_info->architecture);
  1313. system_info->architecture = strdupz(value);
  1314. }
  1315. else if(!strcmp(name, "NETDATA_SYSTEM_VIRTUALIZATION")){
  1316. freez(system_info->virtualization);
  1317. system_info->virtualization = strdupz(value);
  1318. }
  1319. else if(!strcmp(name, "NETDATA_SYSTEM_VIRT_DETECTION")){
  1320. freez(system_info->virt_detection);
  1321. system_info->virt_detection = strdupz(value);
  1322. }
  1323. else if(!strcmp(name, "NETDATA_SYSTEM_CONTAINER")){
  1324. freez(system_info->container);
  1325. system_info->container = strdupz(value);
  1326. }
  1327. else if(!strcmp(name, "NETDATA_SYSTEM_CONTAINER_DETECTION")){
  1328. freez(system_info->container_detection);
  1329. system_info->container_detection = strdupz(value);
  1330. }
  1331. else if(!strcmp(name, "NETDATA_HOST_IS_K8S_NODE")){
  1332. freez(system_info->is_k8s_node);
  1333. system_info->is_k8s_node = strdupz(value);
  1334. }
  1335. else if (!strcmp(name, "NETDATA_SYSTEM_CPU_VENDOR"))
  1336. return res;
  1337. else if (!strcmp(name, "NETDATA_SYSTEM_CPU_MODEL"))
  1338. return res;
  1339. else if (!strcmp(name, "NETDATA_SYSTEM_CPU_DETECTION"))
  1340. return res;
  1341. else if (!strcmp(name, "NETDATA_SYSTEM_RAM_DETECTION"))
  1342. return res;
  1343. else if (!strcmp(name, "NETDATA_SYSTEM_DISK_DETECTION"))
  1344. return res;
  1345. else if (!strcmp(name, "NETDATA_CONTAINER_IS_OFFICIAL_IMAGE"))
  1346. return res;
  1347. else {
  1348. res = 1;
  1349. }
  1350. return res;
  1351. }