rrdhost.c 63 KB

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