rrdhost.c 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #define NETDATA_RRD_INTERNALS
  3. #include "rrd.h"
  4. int storage_tiers = 1;
  5. int storage_tiers_grouping_iterations[RRD_STORAGE_TIERS] = { 1, 60, 60, 60, 60 };
  6. RRD_BACKFILL storage_tiers_backfill[RRD_STORAGE_TIERS] = { RRD_BACKFILL_NEW, RRD_BACKFILL_NEW, RRD_BACKFILL_NEW, RRD_BACKFILL_NEW, RRD_BACKFILL_NEW };
  7. #if RRD_STORAGE_TIERS != 5
  8. #error RRD_STORAGE_TIERS is not 5 - you need to update the grouping iterations per tier
  9. #endif
  10. int get_tier_grouping(int tier) {
  11. if(unlikely(tier >= storage_tiers)) tier = storage_tiers - 1;
  12. if(unlikely(tier < 0)) tier = 0;
  13. int grouping = 1;
  14. // first tier is always 1 iteration of whatever update every the chart has
  15. for(int i = 1; i <= tier ;i++)
  16. grouping *= storage_tiers_grouping_iterations[i];
  17. return grouping;
  18. }
  19. RRDHOST *localhost = NULL;
  20. size_t rrd_hosts_available = 0;
  21. netdata_rwlock_t rrd_rwlock = NETDATA_RWLOCK_INITIALIZER;
  22. time_t rrdset_free_obsolete_time = 3600;
  23. time_t rrdhost_free_orphan_time = 3600;
  24. bool is_storage_engine_shared(STORAGE_INSTANCE *engine) {
  25. #ifdef ENABLE_DBENGINE
  26. for(int tier = 0; tier < storage_tiers ;tier++) {
  27. if (engine == (STORAGE_INSTANCE *)multidb_ctx[tier])
  28. return true;
  29. }
  30. #endif
  31. return false;
  32. }
  33. // ----------------------------------------------------------------------------
  34. // RRDHOST index
  35. int rrdhost_compare(void* a, void* b) {
  36. if(((RRDHOST *)a)->hash_machine_guid < ((RRDHOST *)b)->hash_machine_guid) return -1;
  37. else if(((RRDHOST *)a)->hash_machine_guid > ((RRDHOST *)b)->hash_machine_guid) return 1;
  38. else return strcmp(((RRDHOST *)a)->machine_guid, ((RRDHOST *)b)->machine_guid);
  39. }
  40. avl_tree_lock rrdhost_root_index = {
  41. .avl_tree = { NULL, rrdhost_compare },
  42. .rwlock = AVL_LOCK_INITIALIZER
  43. };
  44. RRDHOST *rrdhost_find_by_guid(const char *guid, uint32_t hash) {
  45. debug(D_RRDHOST, "Searching in index for host with guid '%s'", guid);
  46. RRDHOST tmp;
  47. strncpyz(tmp.machine_guid, guid, GUID_LEN);
  48. tmp.hash_machine_guid = (hash)?hash:simple_hash(tmp.machine_guid);
  49. return (RRDHOST *)avl_search_lock(&(rrdhost_root_index), (avl_t *) &tmp);
  50. }
  51. RRDHOST *rrdhost_find_by_hostname(const char *hostname, uint32_t hash) {
  52. if(unlikely(!strcmp(hostname, "localhost")))
  53. return localhost;
  54. if(unlikely(!hash)) hash = simple_hash(hostname);
  55. rrd_rdlock();
  56. RRDHOST *host;
  57. rrdhost_foreach_read(host) {
  58. if(unlikely((hash == host->hash_hostname && !strcmp(hostname, host->hostname)))) {
  59. rrd_unlock();
  60. return host;
  61. }
  62. }
  63. rrd_unlock();
  64. return NULL;
  65. }
  66. #define rrdhost_index_add(rrdhost) (RRDHOST *)avl_insert_lock(&(rrdhost_root_index), (avl_t *)(rrdhost))
  67. #define rrdhost_index_del(rrdhost) (RRDHOST *)avl_remove_lock(&(rrdhost_root_index), (avl_t *)(rrdhost))
  68. // ----------------------------------------------------------------------------
  69. // RRDHOST - internal helpers
  70. static inline void rrdhost_init_tags(RRDHOST *host, const char *tags) {
  71. if(host->tags && tags && !strcmp(host->tags, tags))
  72. return;
  73. void *old = (void *)host->tags;
  74. host->tags = (tags && *tags)?strdupz(tags):NULL;
  75. freez(old);
  76. }
  77. static inline void rrdhost_init_hostname(RRDHOST *host, const char *hostname) {
  78. if(host->hostname && hostname && !strcmp(host->hostname, hostname))
  79. return;
  80. void *old = host->hostname;
  81. host->hostname = strdupz(hostname?hostname:"localhost");
  82. host->hash_hostname = simple_hash(host->hostname);
  83. freez(old);
  84. }
  85. static inline void rrdhost_init_os(RRDHOST *host, const char *os) {
  86. if(host->os && os && !strcmp(host->os, os))
  87. return;
  88. void *old = (void *)host->os;
  89. host->os = strdupz(os?os:"unknown");
  90. freez(old);
  91. }
  92. static inline void rrdhost_init_timezone(RRDHOST *host, const char *timezone, const char *abbrev_timezone, int32_t utc_offset) {
  93. if (host->timezone && timezone && !strcmp(host->timezone, timezone) && host->abbrev_timezone && abbrev_timezone &&
  94. !strcmp(host->abbrev_timezone, abbrev_timezone) && host->utc_offset == utc_offset)
  95. return;
  96. void *old = (void *)host->timezone;
  97. host->timezone = strdupz((timezone && *timezone)?timezone:"unknown");
  98. freez(old);
  99. old = (void *)host->abbrev_timezone;
  100. host->abbrev_timezone = strdupz((abbrev_timezone && *abbrev_timezone) ? abbrev_timezone : "UTC");
  101. freez(old);
  102. host->utc_offset = utc_offset;
  103. }
  104. static inline void rrdhost_init_machine_guid(RRDHOST *host, const char *machine_guid) {
  105. strncpy(host->machine_guid, machine_guid, GUID_LEN);
  106. host->machine_guid[GUID_LEN] = '\0';
  107. host->hash_machine_guid = simple_hash(host->machine_guid);
  108. }
  109. void set_host_properties(RRDHOST *host, int update_every, RRD_MEMORY_MODE memory_mode, const char *hostname,
  110. const char *registry_hostname, const char *guid, const char *os, const char *tags,
  111. const char *tzone, const char *abbrev_tzone, int32_t utc_offset, const char *program_name,
  112. const char *program_version)
  113. {
  114. host->rrd_update_every = update_every;
  115. host->rrd_memory_mode = memory_mode;
  116. rrdhost_init_hostname(host, hostname);
  117. rrdhost_init_machine_guid(host, guid);
  118. rrdhost_init_os(host, os);
  119. rrdhost_init_timezone(host, tzone, abbrev_tzone, utc_offset);
  120. rrdhost_init_tags(host, tags);
  121. host->program_name = strdupz((program_name && *program_name) ? program_name : "unknown");
  122. host->program_version = strdupz((program_version && *program_version) ? program_version : "unknown");
  123. host->registry_hostname = strdupz((registry_hostname && *registry_hostname) ? registry_hostname : host->hostname);
  124. }
  125. // ----------------------------------------------------------------------------
  126. // RRDHOST - add a host
  127. RRDHOST *rrdhost_create(const char *hostname,
  128. const char *registry_hostname,
  129. const char *guid,
  130. const char *os,
  131. const char *timezone,
  132. const char *abbrev_timezone,
  133. int32_t utc_offset,
  134. const char *tags,
  135. const char *program_name,
  136. const char *program_version,
  137. int update_every,
  138. long entries,
  139. RRD_MEMORY_MODE memory_mode,
  140. unsigned int health_enabled,
  141. unsigned int rrdpush_enabled,
  142. char *rrdpush_destination,
  143. char *rrdpush_api_key,
  144. char *rrdpush_send_charts_matching,
  145. struct rrdhost_system_info *system_info,
  146. int is_localhost
  147. ) {
  148. debug(D_RRDHOST, "Host '%s': adding with guid '%s'", hostname, guid);
  149. #ifdef ENABLE_DBENGINE
  150. int is_legacy = (memory_mode == RRD_MEMORY_MODE_DBENGINE) && is_legacy_child(guid);
  151. #else
  152. int is_legacy = 1;
  153. #endif
  154. rrd_check_wrlock();
  155. int is_in_multihost = (memory_mode == RRD_MEMORY_MODE_DBENGINE && !is_legacy);
  156. RRDHOST *host = callocz(1, sizeof(RRDHOST));
  157. set_host_properties(host, (update_every > 0)?update_every:1, memory_mode, hostname, registry_hostname, guid, os,
  158. tags, timezone, abbrev_timezone, utc_offset, program_name, program_version);
  159. host->rrd_history_entries = align_entries_to_pagesize(memory_mode, entries);
  160. host->health_enabled = ((memory_mode == RRD_MEMORY_MODE_NONE)) ? 0 : health_enabled;
  161. host->sender = mallocz(sizeof(*host->sender));
  162. sender_init(host->sender, host);
  163. netdata_mutex_init(&host->receiver_lock);
  164. host->rrdpush_send_enabled = (rrdpush_enabled && rrdpush_destination && *rrdpush_destination && rrdpush_api_key && *rrdpush_api_key) ? 1 : 0;
  165. host->rrdpush_send_destination = (host->rrdpush_send_enabled)?strdupz(rrdpush_destination):NULL;
  166. if (host->rrdpush_send_destination)
  167. host->destinations = destinations_init(host->rrdpush_send_destination);
  168. host->rrdpush_send_api_key = (host->rrdpush_send_enabled)?strdupz(rrdpush_api_key):NULL;
  169. host->rrdpush_send_charts_matching = simple_pattern_create(rrdpush_send_charts_matching, NULL, SIMPLE_PATTERN_EXACT);
  170. host->rrdpush_sender_pipe[0] = -1;
  171. host->rrdpush_sender_pipe[1] = -1;
  172. host->rrdpush_sender_socket = -1;
  173. //host->stream_version = STREAMING_PROTOCOL_CURRENT_VERSION; Unused?
  174. #ifdef ENABLE_HTTPS
  175. host->ssl.conn = NULL;
  176. host->ssl.flags = NETDATA_SSL_START;
  177. host->stream_ssl.conn = NULL;
  178. host->stream_ssl.flags = NETDATA_SSL_START;
  179. #endif
  180. netdata_rwlock_init(&host->rrdhost_rwlock);
  181. host->host_labels = rrdlabels_create();
  182. netdata_mutex_init(&host->aclk_state_lock);
  183. host->system_info = system_info;
  184. avl_init_lock(&(host->rrdset_root_index), rrdset_compare);
  185. avl_init_lock(&(host->rrdset_root_index_name), rrdset_compare_name);
  186. avl_init_lock(&(host->rrdfamily_root_index), rrdfamily_compare);
  187. avl_init_lock(&(host->rrdvar_root_index), rrdvar_compare);
  188. if(config_get_boolean(CONFIG_SECTION_DB, "delete obsolete charts files", 1))
  189. rrdhost_flag_set(host, RRDHOST_FLAG_DELETE_OBSOLETE_CHARTS);
  190. if(config_get_boolean(CONFIG_SECTION_DB, "delete orphan hosts files", 1) && !is_localhost)
  191. rrdhost_flag_set(host, RRDHOST_FLAG_DELETE_ORPHAN_HOST);
  192. host->health_default_warn_repeat_every = config_get_duration(CONFIG_SECTION_HEALTH, "default repeat warning", "never");
  193. host->health_default_crit_repeat_every = config_get_duration(CONFIG_SECTION_HEALTH, "default repeat critical", "never");
  194. avl_init_lock(&(host->alarms_idx_health_log), alarm_compare_id);
  195. avl_init_lock(&(host->alarms_idx_name), alarm_compare_name);
  196. // ------------------------------------------------------------------------
  197. // initialize health variables
  198. host->health_log.next_log_id = 1;
  199. host->health_log.next_alarm_id = 1;
  200. host->health_log.max = 1000;
  201. host->health_log.next_log_id = (uint32_t)now_realtime_sec();
  202. host->health_log.next_alarm_id = 0;
  203. long n = config_get_number(CONFIG_SECTION_HEALTH, "in memory max health log entries", host->health_log.max);
  204. if(n < 10) {
  205. error("Host '%s': health configuration has invalid max log entries %ld. Using default %u", host->hostname, n, host->health_log.max);
  206. config_set_number(CONFIG_SECTION_HEALTH, "in memory max health log entries", (long)host->health_log.max);
  207. }
  208. else
  209. host->health_log.max = (unsigned int)n;
  210. netdata_rwlock_init(&host->health_log.alarm_log_rwlock);
  211. char filename[FILENAME_MAX + 1];
  212. if(is_localhost) {
  213. host->cache_dir = strdupz(netdata_configured_cache_dir);
  214. host->varlib_dir = strdupz(netdata_configured_varlib_dir);
  215. }
  216. else {
  217. // this is not localhost - append our GUID to localhost path
  218. if (is_in_multihost) { // don't append to cache dir in multihost
  219. host->cache_dir = strdupz(netdata_configured_cache_dir);
  220. } else {
  221. snprintfz(filename, FILENAME_MAX, "%s/%s", netdata_configured_cache_dir, host->machine_guid);
  222. host->cache_dir = strdupz(filename);
  223. }
  224. if((host->rrd_memory_mode == RRD_MEMORY_MODE_MAP || host->rrd_memory_mode == RRD_MEMORY_MODE_SAVE || (
  225. host->rrd_memory_mode == RRD_MEMORY_MODE_DBENGINE && is_legacy))) {
  226. int r = mkdir(host->cache_dir, 0775);
  227. if(r != 0 && errno != EEXIST)
  228. error("Host '%s': cannot create directory '%s'", host->hostname, host->cache_dir);
  229. }
  230. snprintfz(filename, FILENAME_MAX, "%s/%s", netdata_configured_varlib_dir, host->machine_guid);
  231. host->varlib_dir = strdupz(filename);
  232. if(host->health_enabled) {
  233. int r = mkdir(host->varlib_dir, 0775);
  234. if(r != 0 && errno != EEXIST)
  235. error("Host '%s': cannot create directory '%s'", host->hostname, host->varlib_dir);
  236. }
  237. }
  238. if(host->health_enabled) {
  239. snprintfz(filename, FILENAME_MAX, "%s/health", host->varlib_dir);
  240. int r = mkdir(filename, 0775);
  241. if(r != 0 && errno != EEXIST)
  242. error("Host '%s': cannot create directory '%s'", host->hostname, filename);
  243. }
  244. snprintfz(filename, FILENAME_MAX, "%s/health/health-log.db", host->varlib_dir);
  245. host->health_log_filename = strdupz(filename);
  246. snprintfz(filename, FILENAME_MAX, "%s/alarm-notify.sh", netdata_configured_primary_plugins_dir);
  247. host->health_default_exec = strdupz(config_get(CONFIG_SECTION_HEALTH, "script to execute on alarm", filename));
  248. host->health_default_recipient = strdupz("root");
  249. // ------------------------------------------------------------------------
  250. // load health configuration
  251. if(host->health_enabled) {
  252. rrdhost_wrlock(host);
  253. health_readdir(host, health_user_config_dir(), health_stock_config_dir(), NULL);
  254. rrdhost_unlock(host);
  255. }
  256. RRDHOST *t = rrdhost_index_add(host);
  257. if(t != host) {
  258. error("Host '%s': cannot add host with machine guid '%s' to index. It already exists as host '%s' with machine guid '%s'.", host->hostname, host->machine_guid, t->hostname, t->machine_guid);
  259. rrdhost_free(host);
  260. return NULL;
  261. }
  262. if (likely(!uuid_parse(host->machine_guid, host->host_uuid))) {
  263. int rc = sql_store_host_info(host);
  264. if (unlikely(rc))
  265. error_report("Failed to store machine GUID to the database");
  266. sql_load_node_id(host);
  267. if (host->health_enabled) {
  268. if (!file_is_migrated(host->health_log_filename)) {
  269. int rc = sql_create_health_log_table(host);
  270. if (unlikely(rc)) {
  271. error_report("Failed to create health log table in the database");
  272. health_alarm_log_load(host);
  273. health_alarm_log_open(host);
  274. }
  275. else {
  276. health_alarm_log_load(host);
  277. add_migrated_file(host->health_log_filename, 0);
  278. }
  279. } else {
  280. sql_create_health_log_table(host);
  281. sql_health_alarm_log_load(host);
  282. }
  283. }
  284. }
  285. else
  286. error_report("Host machine GUID %s is not valid", host->machine_guid);
  287. if (host->rrd_memory_mode == RRD_MEMORY_MODE_DBENGINE) {
  288. #ifdef ENABLE_DBENGINE
  289. char dbenginepath[FILENAME_MAX + 1];
  290. int ret;
  291. snprintfz(dbenginepath, FILENAME_MAX, "%s/dbengine", host->cache_dir);
  292. ret = mkdir(dbenginepath, 0775);
  293. if (ret != 0 && errno != EEXIST)
  294. error("Host '%s': cannot create directory '%s'", host->hostname, dbenginepath);
  295. else ret = 0; // succeed
  296. if (is_legacy) {
  297. // initialize legacy dbengine instance as needed
  298. ret = rrdeng_init(
  299. host,
  300. (struct rrdengine_instance **)&host->storage_instance[0],
  301. dbenginepath,
  302. default_rrdeng_page_cache_mb,
  303. default_rrdeng_disk_quota_mb,
  304. 0); // may fail here for legacy dbengine initialization
  305. if(ret == 0) {
  306. // assign the rest of the shared storage instances to it
  307. // to allow them collect its metrics too
  308. for(int tier = 1; tier < storage_tiers ; tier++)
  309. host->storage_instance[tier] = (STORAGE_INSTANCE *)multidb_ctx[tier];
  310. }
  311. }
  312. else {
  313. for(int tier = 0; tier < storage_tiers ; tier++)
  314. host->storage_instance[tier] = (STORAGE_INSTANCE *)multidb_ctx[tier];
  315. }
  316. if (ret) { // check legacy or multihost initialization success
  317. error(
  318. "Host '%s': cannot initialize host with machine guid '%s'. Failed to initialize DB engine at '%s'.",
  319. host->hostname, host->machine_guid, host->cache_dir);
  320. rrdhost_free(host);
  321. host = NULL;
  322. //rrd_hosts_available++; //TODO: maybe we want this?
  323. return host;
  324. }
  325. #else
  326. fatal("RRD_MEMORY_MODE_DBENGINE is not supported in this platform.");
  327. #endif
  328. }
  329. else {
  330. #ifdef ENABLE_DBENGINE
  331. // the first tier is reserved for the non-dbengine modes
  332. for(int tier = 1; tier < storage_tiers ; tier++)
  333. host->storage_instance[tier] = (STORAGE_INSTANCE *)multidb_ctx[tier];
  334. #endif
  335. }
  336. // ------------------------------------------------------------------------
  337. // link it and add it to the index
  338. if(is_localhost) {
  339. host->next = localhost;
  340. localhost = host;
  341. }
  342. else {
  343. if(localhost) {
  344. host->next = localhost->next;
  345. localhost->next = host;
  346. }
  347. else localhost = host;
  348. }
  349. // ------------------------------------------------------------------------
  350. // init new ML host and update system_info to let upstreams know
  351. // about ML functionality
  352. //
  353. if (is_localhost && host->system_info) {
  354. host->system_info->ml_capable = ml_capable();
  355. host->system_info->ml_enabled = ml_enabled(host);
  356. host->system_info->mc_version = enable_metric_correlations ? metric_correlations_version : 0;
  357. }
  358. info("Host '%s' (at registry as '%s') with guid '%s' initialized"
  359. ", os '%s'"
  360. ", timezone '%s'"
  361. ", tags '%s'"
  362. ", program_name '%s'"
  363. ", program_version '%s'"
  364. ", update every %d"
  365. ", memory mode %s"
  366. ", history entries %ld"
  367. ", streaming %s"
  368. " (to '%s' with api key '%s')"
  369. ", health %s"
  370. ", cache_dir '%s'"
  371. ", varlib_dir '%s'"
  372. ", health_log '%s'"
  373. ", alarms default handler '%s'"
  374. ", alarms default recipient '%s'"
  375. , host->hostname
  376. , host->registry_hostname
  377. , host->machine_guid
  378. , host->os
  379. , host->timezone
  380. , (host->tags)?host->tags:""
  381. , host->program_name
  382. , host->program_version
  383. , host->rrd_update_every
  384. , rrd_memory_mode_name(host->rrd_memory_mode)
  385. , host->rrd_history_entries
  386. , host->rrdpush_send_enabled?"enabled":"disabled"
  387. , host->rrdpush_send_destination?host->rrdpush_send_destination:""
  388. , host->rrdpush_send_api_key?host->rrdpush_send_api_key:""
  389. , host->health_enabled?"enabled":"disabled"
  390. , host->cache_dir
  391. , host->varlib_dir
  392. , host->health_log_filename
  393. , host->health_default_exec
  394. , host->health_default_recipient
  395. );
  396. rrd_hosts_available++;
  397. rrdhost_load_rrdcontext_data(host);
  398. ml_new_host(host);
  399. return host;
  400. }
  401. void rrdhost_update(RRDHOST *host
  402. , const char *hostname
  403. , const char *registry_hostname
  404. , const char *guid
  405. , const char *os
  406. , const char *timezone
  407. , const char *abbrev_timezone
  408. , int32_t utc_offset
  409. , const char *tags
  410. , const char *program_name
  411. , const char *program_version
  412. , int update_every
  413. , long history
  414. , RRD_MEMORY_MODE mode
  415. , unsigned int health_enabled
  416. , unsigned int rrdpush_enabled
  417. , char *rrdpush_destination
  418. , char *rrdpush_api_key
  419. , char *rrdpush_send_charts_matching
  420. , struct rrdhost_system_info *system_info
  421. )
  422. {
  423. UNUSED(guid);
  424. UNUSED(rrdpush_enabled);
  425. UNUSED(rrdpush_destination);
  426. UNUSED(rrdpush_api_key);
  427. UNUSED(rrdpush_send_charts_matching);
  428. host->health_enabled = (mode == RRD_MEMORY_MODE_NONE) ? 0 : health_enabled;
  429. //host->stream_version = STREAMING_PROTOCOL_CURRENT_VERSION; Unused?
  430. rrdhost_system_info_free(host->system_info);
  431. host->system_info = system_info;
  432. rrdhost_init_os(host, os);
  433. rrdhost_init_timezone(host, timezone, abbrev_timezone, utc_offset);
  434. freez(host->registry_hostname);
  435. host->registry_hostname = strdupz((registry_hostname && *registry_hostname)?registry_hostname:hostname);
  436. if(strcmp(host->hostname, hostname) != 0) {
  437. info("Host '%s' has been renamed to '%s'. If this is not intentional it may mean multiple hosts are using the same machine_guid.", host->hostname, hostname);
  438. char *t = host->hostname;
  439. host->hostname = strdupz(hostname);
  440. host->hash_hostname = simple_hash(host->hostname);
  441. freez(t);
  442. }
  443. if(strcmp(host->program_name, program_name) != 0) {
  444. info("Host '%s' switched program name from '%s' to '%s'", host->hostname, host->program_name, program_name);
  445. char *t = host->program_name;
  446. host->program_name = strdupz(program_name);
  447. freez(t);
  448. }
  449. if(strcmp(host->program_version, program_version) != 0) {
  450. info("Host '%s' switched program version from '%s' to '%s'", host->hostname, host->program_version, program_version);
  451. char *t = host->program_version;
  452. host->program_version = strdupz(program_version);
  453. freez(t);
  454. }
  455. if(host->rrd_update_every != update_every)
  456. 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.", host->hostname, host->rrd_update_every, update_every);
  457. if(host->rrd_history_entries < history)
  458. error("Host '%s' has history of %ld entries, but the wanted one is %ld entries. Restart netdata here to apply the new settings.", host->hostname, host->rrd_history_entries, history);
  459. if(host->rrd_memory_mode != mode)
  460. error("Host '%s' has memory mode '%s', but the wanted one is '%s'. Restart netdata here to apply the new settings.", host->hostname, rrd_memory_mode_name(host->rrd_memory_mode), rrd_memory_mode_name(mode));
  461. // update host tags
  462. rrdhost_init_tags(host, tags);
  463. if (rrdhost_flag_check(host, RRDHOST_FLAG_ARCHIVED)) {
  464. rrdhost_flag_clear(host, RRDHOST_FLAG_ARCHIVED);
  465. if(host->health_enabled) {
  466. int r;
  467. char filename[FILENAME_MAX + 1];
  468. if (host != localhost) {
  469. r = mkdir(host->varlib_dir, 0775);
  470. if (r != 0 && errno != EEXIST)
  471. error("Host '%s': cannot create directory '%s'", host->hostname, host->varlib_dir);
  472. }
  473. snprintfz(filename, FILENAME_MAX, "%s/health", host->varlib_dir);
  474. r = mkdir(filename, 0775);
  475. if(r != 0 && errno != EEXIST)
  476. error("Host '%s': cannot create directory '%s'", host->hostname, filename);
  477. rrdhost_wrlock(host);
  478. health_readdir(host, health_user_config_dir(), health_stock_config_dir(), NULL);
  479. rrdhost_unlock(host);
  480. if (!file_is_migrated(host->health_log_filename)) {
  481. int rc = sql_create_health_log_table(host);
  482. if (unlikely(rc)) {
  483. error_report("Failed to create health log table in the database");
  484. health_alarm_log_load(host);
  485. health_alarm_log_open(host);
  486. } else {
  487. health_alarm_log_load(host);
  488. add_migrated_file(host->health_log_filename, 0);
  489. }
  490. } else {
  491. sql_create_health_log_table(host);
  492. sql_health_alarm_log_load(host);
  493. }
  494. }
  495. rrd_hosts_available++;
  496. info("Host %s is not in archived mode anymore", host->hostname);
  497. }
  498. return;
  499. }
  500. RRDHOST *rrdhost_find_or_create(
  501. const char *hostname
  502. , const char *registry_hostname
  503. , const char *guid
  504. , const char *os
  505. , const char *timezone
  506. , const char *abbrev_timezone
  507. , int32_t utc_offset
  508. , const char *tags
  509. , const char *program_name
  510. , const char *program_version
  511. , int update_every
  512. , long history
  513. , RRD_MEMORY_MODE mode
  514. , unsigned int health_enabled
  515. , unsigned int rrdpush_enabled
  516. , char *rrdpush_destination
  517. , char *rrdpush_api_key
  518. , char *rrdpush_send_charts_matching
  519. , struct rrdhost_system_info *system_info
  520. ) {
  521. debug(D_RRDHOST, "Searching for host '%s' with guid '%s'", hostname, guid);
  522. rrd_wrlock();
  523. RRDHOST *host = rrdhost_find_by_guid(guid, 0);
  524. if (unlikely(host && RRD_MEMORY_MODE_DBENGINE != mode && rrdhost_flag_check(host, RRDHOST_FLAG_ARCHIVED))) {
  525. /* If a legacy memory mode instantiates all dbengine state must be discarded to avoid inconsistencies */
  526. error("Archived host '%s' has memory mode '%s', but the wanted one is '%s'. Discarding archived state.",
  527. host->hostname, rrd_memory_mode_name(host->rrd_memory_mode), rrd_memory_mode_name(mode));
  528. rrdhost_free(host);
  529. host = NULL;
  530. }
  531. if(!host) {
  532. host = rrdhost_create(
  533. hostname
  534. , registry_hostname
  535. , guid
  536. , os
  537. , timezone
  538. , abbrev_timezone
  539. , utc_offset
  540. , tags
  541. , program_name
  542. , program_version
  543. , update_every
  544. , history
  545. , mode
  546. , health_enabled
  547. , rrdpush_enabled
  548. , rrdpush_destination
  549. , rrdpush_api_key
  550. , rrdpush_send_charts_matching
  551. , system_info
  552. , 0
  553. );
  554. }
  555. else {
  556. rrdhost_update(host
  557. , hostname
  558. , registry_hostname
  559. , guid
  560. , os
  561. , timezone
  562. , abbrev_timezone
  563. , utc_offset
  564. , tags
  565. , program_name
  566. , program_version
  567. , update_every
  568. , history
  569. , mode
  570. , health_enabled
  571. , rrdpush_enabled
  572. , rrdpush_destination
  573. , rrdpush_api_key
  574. , rrdpush_send_charts_matching
  575. , system_info);
  576. }
  577. if (host) {
  578. rrdhost_wrlock(host);
  579. rrdhost_flag_clear(host, RRDHOST_FLAG_ORPHAN);
  580. host->senders_disconnected_time = 0;
  581. rrdhost_load_rrdcontext_data(host);
  582. rrdhost_unlock(host);
  583. }
  584. rrd_unlock();
  585. return host;
  586. }
  587. inline int rrdhost_should_be_removed(RRDHOST *host, RRDHOST *protected_host, time_t now) {
  588. if(host != protected_host
  589. && host != localhost
  590. && rrdhost_flag_check(host, RRDHOST_FLAG_ORPHAN)
  591. && !host->receiver
  592. && host->senders_disconnected_time
  593. && host->senders_disconnected_time + rrdhost_free_orphan_time < now)
  594. return 1;
  595. return 0;
  596. }
  597. void rrdhost_cleanup_orphan_hosts_nolock(RRDHOST *protected_host) {
  598. time_t now = now_realtime_sec();
  599. RRDHOST *host;
  600. restart_after_removal:
  601. rrdhost_foreach_write(host) {
  602. if(rrdhost_should_be_removed(host, protected_host, now)) {
  603. info("Host '%s' with machine guid '%s' is obsolete - cleaning up.", host->hostname, host->machine_guid);
  604. if (rrdhost_flag_check(host, RRDHOST_FLAG_DELETE_ORPHAN_HOST)
  605. #ifdef ENABLE_DBENGINE
  606. /* don't delete multi-host DB host files */
  607. && !(host->rrd_memory_mode == RRD_MEMORY_MODE_DBENGINE && is_storage_engine_shared(host->storage_instance[0]))
  608. #endif
  609. )
  610. rrdhost_delete_charts(host);
  611. else
  612. rrdhost_save_charts(host);
  613. rrdhost_free(host);
  614. goto restart_after_removal;
  615. }
  616. }
  617. }
  618. // ----------------------------------------------------------------------------
  619. // RRDHOST global / startup initialization
  620. int rrd_init(char *hostname, struct rrdhost_system_info *system_info) {
  621. if (unlikely(sql_init_database(DB_CHECK_NONE, system_info ? 0 : 1))) {
  622. if (default_rrd_memory_mode == RRD_MEMORY_MODE_DBENGINE)
  623. fatal("Failed to initialize SQLite");
  624. info("Skipping SQLITE metadata initialization since memory mode is not dbengine");
  625. }
  626. if (unlikely(sql_init_context_database(0))) {
  627. error_report("Failed to initialize context metadata database");
  628. }
  629. #ifdef ENABLE_DBENGINE
  630. storage_tiers = config_get_number(CONFIG_SECTION_DB, "storage tiers", storage_tiers);
  631. if(storage_tiers < 1) {
  632. error("At least 1 storage tier is required. Assuming 1.");
  633. storage_tiers = 1;
  634. config_set_number(CONFIG_SECTION_DB, "storage tiers", storage_tiers);
  635. }
  636. if(storage_tiers > RRD_STORAGE_TIERS) {
  637. error("Up to %d storage tier are supported. Assuming %d.", RRD_STORAGE_TIERS, RRD_STORAGE_TIERS);
  638. storage_tiers = RRD_STORAGE_TIERS;
  639. config_set_number(CONFIG_SECTION_DB, "storage tiers", storage_tiers);
  640. }
  641. default_rrdeng_page_fetch_timeout = (int) config_get_number(CONFIG_SECTION_DB, "dbengine page fetch timeout secs", PAGE_CACHE_FETCH_WAIT_TIMEOUT);
  642. if (default_rrdeng_page_fetch_timeout < 1) {
  643. info("'dbengine page fetch timeout secs' cannot be %d, using 1", default_rrdeng_page_fetch_timeout);
  644. default_rrdeng_page_fetch_timeout = 1;
  645. config_set_number(CONFIG_SECTION_DB, "dbengine page fetch timeout secs", default_rrdeng_page_fetch_timeout);
  646. }
  647. default_rrdeng_page_fetch_retries = (int) config_get_number(CONFIG_SECTION_DB, "dbengine page fetch retries", MAX_PAGE_CACHE_FETCH_RETRIES);
  648. if (default_rrdeng_page_fetch_retries < 1) {
  649. info("\"dbengine page fetch retries\" found in netdata.conf cannot be %d, using 1", default_rrdeng_page_fetch_retries);
  650. default_rrdeng_page_fetch_retries = 1;
  651. config_set_number(CONFIG_SECTION_DB, "dbengine page fetch retries", default_rrdeng_page_fetch_retries);
  652. }
  653. if(config_get_boolean(CONFIG_SECTION_DB, "dbengine page descriptors in file mapped memory", rrdeng_page_descr_is_mmap()) == CONFIG_BOOLEAN_YES)
  654. rrdeng_page_descr_use_mmap();
  655. else
  656. rrdeng_page_descr_use_malloc();
  657. rrdeng_page_descr_aral_go_singlethreaded();
  658. int created_tiers = 0;
  659. char dbenginepath[FILENAME_MAX + 1];
  660. char dbengineconfig[200 + 1];
  661. for(int tier = 0; tier < storage_tiers ;tier++) {
  662. if(tier == 0)
  663. snprintfz(dbenginepath, FILENAME_MAX, "%s/dbengine", netdata_configured_cache_dir);
  664. else
  665. snprintfz(dbenginepath, FILENAME_MAX, "%s/dbengine-tier%d", netdata_configured_cache_dir, tier);
  666. int ret = mkdir(dbenginepath, 0775);
  667. if (ret != 0 && errno != EEXIST) {
  668. error("DBENGINE on '%s': cannot create directory '%s'", hostname, dbenginepath);
  669. break;
  670. }
  671. int page_cache_mb = default_rrdeng_page_cache_mb;
  672. int disk_space_mb = default_multidb_disk_quota_mb;
  673. int grouping_iterations = storage_tiers_grouping_iterations[tier];
  674. RRD_BACKFILL backfill = storage_tiers_backfill[tier];
  675. if(tier > 0) {
  676. snprintfz(dbengineconfig, 200, "dbengine tier %d page cache size MB", tier);
  677. page_cache_mb = config_get_number(CONFIG_SECTION_DB, dbengineconfig, page_cache_mb);
  678. snprintfz(dbengineconfig, 200, "dbengine tier %d multihost disk space MB", tier);
  679. disk_space_mb = config_get_number(CONFIG_SECTION_DB, dbengineconfig, disk_space_mb);
  680. snprintfz(dbengineconfig, 200, "dbengine tier %d update every iterations", tier);
  681. grouping_iterations = config_get_number(CONFIG_SECTION_DB, dbengineconfig, grouping_iterations);
  682. if(grouping_iterations < 2) {
  683. grouping_iterations = 2;
  684. config_set_number(CONFIG_SECTION_DB, dbengineconfig, grouping_iterations);
  685. error("DBENGINE on '%s': 'dbegnine tier %d update every iterations' cannot be less than 2. Assuming 2.", hostname, tier);
  686. }
  687. snprintfz(dbengineconfig, 200, "dbengine tier %d backfill", tier);
  688. const char *bf = config_get(CONFIG_SECTION_DB, dbengineconfig, backfill == RRD_BACKFILL_NEW ? "new" : backfill == RRD_BACKFILL_FULL ? "full" : "none");
  689. if(strcmp(bf, "new") == 0) backfill = RRD_BACKFILL_NEW;
  690. else if(strcmp(bf, "full") == 0) backfill = RRD_BACKFILL_FULL;
  691. else if(strcmp(bf, "none") == 0) backfill = RRD_BACKFILL_NONE;
  692. else {
  693. error("DBENGINE: unknown backfill value '%s', assuming 'new'", bf);
  694. config_set(CONFIG_SECTION_DB, dbengineconfig, "new");
  695. backfill = RRD_BACKFILL_NEW;
  696. }
  697. }
  698. storage_tiers_grouping_iterations[tier] = grouping_iterations;
  699. storage_tiers_backfill[tier] = backfill;
  700. if(tier > 0 && get_tier_grouping(tier) > 65535) {
  701. storage_tiers_grouping_iterations[tier] = 1;
  702. error("DBENGINE on '%s': dbengine tier %d gives aggregation of more than 65535 points of tier 0. Disabling tiers above %d", hostname, tier, tier);
  703. break;
  704. }
  705. internal_error(true, "DBENGINE tier %d grouping iterations is set to %d", tier, storage_tiers_grouping_iterations[tier]);
  706. ret = rrdeng_init(NULL, NULL, dbenginepath, page_cache_mb, disk_space_mb, tier);
  707. if(ret != 0) {
  708. error("DBENGINE on '%s': Failed to initialize multi-host database tier %d on path '%s'",
  709. hostname, tier, dbenginepath);
  710. break;
  711. }
  712. else
  713. created_tiers++;
  714. }
  715. if(created_tiers && created_tiers < storage_tiers) {
  716. error("DBENGINE on '%s': Managed to create %d tiers instead of %d. Continuing with %d available.",
  717. hostname, created_tiers, storage_tiers, created_tiers);
  718. storage_tiers = created_tiers;
  719. }
  720. else if(!created_tiers)
  721. fatal("DBENGINE on '%s', failed to initialize databases at '%s'.", hostname, netdata_configured_cache_dir);
  722. rrdeng_page_descr_aral_go_multithreaded();
  723. #else
  724. storage_tiers = config_get_number(CONFIG_SECTION_DB, "storage tiers", 1);
  725. if(storage_tiers != 1) {
  726. error("DBENGINE is not available on '%s', so only 1 database tier can be supported.", hostname);
  727. storage_tiers = 1;
  728. config_set_number(CONFIG_SECTION_DB, "storage tiers", storage_tiers);
  729. }
  730. #endif
  731. health_init();
  732. rrdpush_init();
  733. debug(D_RRDHOST, "Initializing localhost with hostname '%s'", hostname);
  734. rrd_wrlock();
  735. localhost = rrdhost_create(
  736. hostname
  737. , registry_get_this_machine_hostname()
  738. , registry_get_this_machine_guid()
  739. , os_type
  740. , netdata_configured_timezone
  741. , netdata_configured_abbrev_timezone
  742. , netdata_configured_utc_offset
  743. , ""
  744. , program_name
  745. , program_version
  746. , default_rrd_update_every
  747. , default_rrd_history_entries
  748. , default_rrd_memory_mode
  749. , default_health_enabled
  750. , default_rrdpush_enabled
  751. , default_rrdpush_destination
  752. , default_rrdpush_api_key
  753. , default_rrdpush_send_charts_matching
  754. , system_info
  755. , 1
  756. );
  757. if (unlikely(!localhost)) {
  758. rrd_unlock();
  759. return 1;
  760. }
  761. if (likely(system_info))
  762. migrate_localhost(&localhost->host_uuid);
  763. sql_aclk_sync_init();
  764. rrd_unlock();
  765. web_client_api_v1_management_init();
  766. return localhost==NULL;
  767. }
  768. // ----------------------------------------------------------------------------
  769. // RRDHOST - lock validations
  770. // there are only used when NETDATA_INTERNAL_CHECKS is set
  771. void __rrdhost_check_rdlock(RRDHOST *host, const char *file, const char *function, const unsigned long line) {
  772. debug(D_RRDHOST, "Checking read lock on host '%s'", host->hostname);
  773. int ret = netdata_rwlock_trywrlock(&host->rrdhost_rwlock);
  774. if(ret == 0)
  775. fatal("RRDHOST '%s' should be read-locked, but it is not, at function %s() at line %lu of file '%s'", host->hostname, function, line, file);
  776. }
  777. void __rrdhost_check_wrlock(RRDHOST *host, const char *file, const char *function, const unsigned long line) {
  778. debug(D_RRDHOST, "Checking write lock on host '%s'", host->hostname);
  779. int ret = netdata_rwlock_tryrdlock(&host->rrdhost_rwlock);
  780. if(ret == 0)
  781. fatal("RRDHOST '%s' should be write-locked, but it is not, at function %s() at line %lu of file '%s'", host->hostname, function, line, file);
  782. }
  783. void __rrd_check_rdlock(const char *file, const char *function, const unsigned long line) {
  784. debug(D_RRDHOST, "Checking read lock on all RRDs");
  785. int ret = netdata_rwlock_trywrlock(&rrd_rwlock);
  786. if(ret == 0)
  787. fatal("RRDs should be read-locked, but it are not, at function %s() at line %lu of file '%s'", function, line, file);
  788. }
  789. void __rrd_check_wrlock(const char *file, const char *function, const unsigned long line) {
  790. debug(D_RRDHOST, "Checking write lock on all RRDs");
  791. int ret = netdata_rwlock_tryrdlock(&rrd_rwlock);
  792. if(ret == 0)
  793. fatal("RRDs should be write-locked, but it are not, at function %s() at line %lu of file '%s'", function, line, file);
  794. }
  795. // ----------------------------------------------------------------------------
  796. // RRDHOST - free
  797. void rrdhost_system_info_free(struct rrdhost_system_info *system_info) {
  798. info("SYSTEM_INFO: free %p", system_info);
  799. if(likely(system_info)) {
  800. freez(system_info->cloud_provider_type);
  801. freez(system_info->cloud_instance_type);
  802. freez(system_info->cloud_instance_region);
  803. freez(system_info->host_os_name);
  804. freez(system_info->host_os_id);
  805. freez(system_info->host_os_id_like);
  806. freez(system_info->host_os_version);
  807. freez(system_info->host_os_version_id);
  808. freez(system_info->host_os_detection);
  809. freez(system_info->host_cores);
  810. freez(system_info->host_cpu_freq);
  811. freez(system_info->host_ram_total);
  812. freez(system_info->host_disk_space);
  813. freez(system_info->container_os_name);
  814. freez(system_info->container_os_id);
  815. freez(system_info->container_os_id_like);
  816. freez(system_info->container_os_version);
  817. freez(system_info->container_os_version_id);
  818. freez(system_info->container_os_detection);
  819. freez(system_info->kernel_name);
  820. freez(system_info->kernel_version);
  821. freez(system_info->architecture);
  822. freez(system_info->virtualization);
  823. freez(system_info->virt_detection);
  824. freez(system_info->container);
  825. freez(system_info->container_detection);
  826. freez(system_info->is_k8s_node);
  827. freez(system_info->install_type);
  828. freez(system_info->prebuilt_arch);
  829. freez(system_info->prebuilt_dist);
  830. freez(system_info);
  831. }
  832. }
  833. void destroy_receiver_state(struct receiver_state *rpt);
  834. void rrdhost_free(RRDHOST *host) {
  835. if(!host) return;
  836. info("Freeing all memory for host '%s'...", host->hostname);
  837. rrd_check_wrlock(); // make sure the RRDs are write locked
  838. rrdhost_wrlock(host);
  839. ml_delete_host(host);
  840. rrdhost_unlock(host);
  841. // ------------------------------------------------------------------------
  842. // clean up streaming
  843. rrdpush_sender_thread_stop(host); // stop a possibly running thread
  844. cbuffer_free(host->sender->buffer);
  845. buffer_free(host->sender->build);
  846. #ifdef ENABLE_COMPRESSION
  847. if (host->sender->compressor)
  848. host->sender->compressor->destroy(&host->sender->compressor);
  849. #endif
  850. freez(host->sender);
  851. host->sender = NULL;
  852. if (netdata_exit) {
  853. netdata_mutex_lock(&host->receiver_lock);
  854. if (host->receiver) {
  855. if (!host->receiver->exited)
  856. netdata_thread_cancel(host->receiver->thread);
  857. netdata_mutex_unlock(&host->receiver_lock);
  858. struct receiver_state *rpt = host->receiver;
  859. while (host->receiver && !rpt->exited)
  860. sleep_usec(50 * USEC_PER_MS);
  861. // If the receiver detached from the host then its thread will destroy the state
  862. if (host->receiver == rpt)
  863. destroy_receiver_state(host->receiver);
  864. }
  865. else
  866. netdata_mutex_unlock(&host->receiver_lock);
  867. }
  868. rrdhost_wrlock(host); // lock this RRDHOST
  869. #ifdef ENABLE_ACLK
  870. struct aclk_database_worker_config *wc = host->dbsync_worker;
  871. if (wc && !netdata_exit) {
  872. struct aclk_database_cmd cmd;
  873. memset(&cmd, 0, sizeof(cmd));
  874. cmd.opcode = ACLK_DATABASE_ORPHAN_HOST;
  875. struct aclk_completion compl ;
  876. init_aclk_completion(&compl );
  877. cmd.completion = &compl ;
  878. aclk_database_enq_cmd(wc, &cmd);
  879. wait_for_aclk_completion(&compl );
  880. destroy_aclk_completion(&compl );
  881. }
  882. #endif
  883. // ------------------------------------------------------------------------
  884. // release its children resources
  885. #ifdef ENABLE_DBENGINE
  886. for(int tier = 0; tier < storage_tiers ;tier++) {
  887. if(host->rrd_memory_mode == RRD_MEMORY_MODE_DBENGINE &&
  888. host->storage_instance[tier] &&
  889. !is_storage_engine_shared(host->storage_instance[tier]))
  890. rrdeng_prepare_exit((struct rrdengine_instance *)host->storage_instance[tier]);
  891. }
  892. #endif
  893. while(host->rrdset_root)
  894. rrdset_free(host->rrdset_root);
  895. freez(host->exporting_flags);
  896. while(host->alarms)
  897. rrdcalc_unlink_and_free(host, host->alarms);
  898. RRDCALC *rc,*nc;
  899. for(rc = host->alarms_with_foreach; rc ; rc = nc) {
  900. nc = rc->next;
  901. rrdcalc_free(rc);
  902. }
  903. host->alarms_with_foreach = NULL;
  904. while(host->templates)
  905. rrdcalctemplate_unlink_and_free(host, host->templates);
  906. RRDCALCTEMPLATE *rt,*next;
  907. for(rt = host->alarms_template_with_foreach; rt ; rt = next) {
  908. next = rt->next;
  909. rrdcalctemplate_free(rt);
  910. }
  911. host->alarms_template_with_foreach = NULL;
  912. debug(D_RRD_CALLS, "RRDHOST: Cleaning up remaining host variables for host '%s'", host->hostname);
  913. rrdvar_free_remaining_variables(host, &host->rrdvar_root_index);
  914. health_alarm_log_free(host);
  915. #ifdef ENABLE_DBENGINE
  916. for(int tier = 0; tier < storage_tiers ;tier++) {
  917. if(host->rrd_memory_mode == RRD_MEMORY_MODE_DBENGINE &&
  918. host->storage_instance[tier] &&
  919. !is_storage_engine_shared(host->storage_instance[tier]))
  920. rrdeng_exit((struct rrdengine_instance *)host->storage_instance[tier]);
  921. }
  922. #endif
  923. // ------------------------------------------------------------------------
  924. // remove it from the indexes
  925. if(rrdhost_index_del(host) != host)
  926. error("RRDHOST '%s' removed from index, deleted the wrong entry.", host->hostname);
  927. // ------------------------------------------------------------------------
  928. // unlink it from the host
  929. if(host == localhost) {
  930. localhost = host->next;
  931. }
  932. else {
  933. // find the previous one
  934. RRDHOST *h;
  935. for(h = localhost; h && h->next != host ; h = h->next) ;
  936. // bypass it
  937. if(h) h->next = host->next;
  938. else error("Request to free RRDHOST '%s': cannot find it", host->hostname);
  939. }
  940. // ------------------------------------------------------------------------
  941. // free it
  942. pthread_mutex_destroy(&host->aclk_state_lock);
  943. freez(host->aclk_state.claimed_id);
  944. freez(host->aclk_state.prev_claimed_id);
  945. freez((void *)host->tags);
  946. rrdlabels_destroy(host->host_labels);
  947. freez((void *)host->os);
  948. freez((void *)host->timezone);
  949. freez((void *)host->abbrev_timezone);
  950. freez(host->program_version);
  951. freez(host->program_name);
  952. rrdhost_system_info_free(host->system_info);
  953. freez(host->cache_dir);
  954. freez(host->varlib_dir);
  955. freez(host->rrdpush_send_api_key);
  956. freez(host->rrdpush_send_destination);
  957. freez(host->health_default_exec);
  958. freez(host->health_default_recipient);
  959. freez(host->health_log_filename);
  960. freez(host->hostname);
  961. freez(host->registry_hostname);
  962. simple_pattern_free(host->rrdpush_send_charts_matching);
  963. rrdhost_unlock(host);
  964. netdata_rwlock_destroy(&host->health_log.alarm_log_rwlock);
  965. netdata_rwlock_destroy(&host->rrdhost_rwlock);
  966. freez(host->node_id);
  967. rrdhost_destroy_rrdcontexts(host);
  968. freez(host);
  969. #ifdef ENABLE_ACLK
  970. if (wc)
  971. wc->is_orphan = 0;
  972. #endif
  973. rrd_hosts_available--;
  974. }
  975. void rrdhost_free_all(void) {
  976. rrd_wrlock();
  977. /* Make sure child-hosts are released before the localhost. */
  978. while(localhost->next) rrdhost_free(localhost->next);
  979. rrdhost_free(localhost);
  980. rrd_unlock();
  981. }
  982. // ----------------------------------------------------------------------------
  983. // RRDHOST - save host files
  984. void rrdhost_save_charts(RRDHOST *host) {
  985. if(!host) return;
  986. info("Saving/Closing database of host '%s'...", host->hostname);
  987. RRDSET *st;
  988. // we get a write lock
  989. // to ensure only one thread is saving the database
  990. rrdhost_wrlock(host);
  991. rrdset_foreach_write(st, host) {
  992. rrdset_rdlock(st);
  993. rrdset_save(st);
  994. rrdset_unlock(st);
  995. }
  996. rrdhost_unlock(host);
  997. }
  998. static void rrdhost_load_auto_labels(void) {
  999. DICTIONARY *labels = localhost->host_labels;
  1000. if (localhost->system_info->cloud_provider_type)
  1001. rrdlabels_add(labels, "_cloud_provider_type", localhost->system_info->cloud_provider_type, RRDLABEL_SRC_AUTO);
  1002. if (localhost->system_info->cloud_instance_type)
  1003. rrdlabels_add(labels, "_cloud_instance_type", localhost->system_info->cloud_instance_type, RRDLABEL_SRC_AUTO);
  1004. if (localhost->system_info->cloud_instance_region)
  1005. rrdlabels_add(
  1006. labels, "_cloud_instance_region", localhost->system_info->cloud_instance_region, RRDLABEL_SRC_AUTO);
  1007. if (localhost->system_info->host_os_name)
  1008. rrdlabels_add(labels, "_os_name", localhost->system_info->host_os_name, RRDLABEL_SRC_AUTO);
  1009. if (localhost->system_info->host_os_version)
  1010. rrdlabels_add(labels, "_os_version", localhost->system_info->host_os_version, RRDLABEL_SRC_AUTO);
  1011. if (localhost->system_info->kernel_version)
  1012. rrdlabels_add(labels, "_kernel_version", localhost->system_info->kernel_version, RRDLABEL_SRC_AUTO);
  1013. if (localhost->system_info->host_cores)
  1014. rrdlabels_add(labels, "_system_cores", localhost->system_info->host_cores, RRDLABEL_SRC_AUTO);
  1015. if (localhost->system_info->host_cpu_freq)
  1016. rrdlabels_add(labels, "_system_cpu_freq", localhost->system_info->host_cpu_freq, RRDLABEL_SRC_AUTO);
  1017. if (localhost->system_info->host_ram_total)
  1018. rrdlabels_add(labels, "_system_ram_total", localhost->system_info->host_ram_total, RRDLABEL_SRC_AUTO);
  1019. if (localhost->system_info->host_disk_space)
  1020. rrdlabels_add(labels, "_system_disk_space", localhost->system_info->host_disk_space, RRDLABEL_SRC_AUTO);
  1021. if (localhost->system_info->architecture)
  1022. rrdlabels_add(labels, "_architecture", localhost->system_info->architecture, RRDLABEL_SRC_AUTO);
  1023. if (localhost->system_info->virtualization)
  1024. rrdlabels_add(labels, "_virtualization", localhost->system_info->virtualization, RRDLABEL_SRC_AUTO);
  1025. if (localhost->system_info->container)
  1026. rrdlabels_add(labels, "_container", localhost->system_info->container, RRDLABEL_SRC_AUTO);
  1027. if (localhost->system_info->container_detection)
  1028. rrdlabels_add(labels, "_container_detection", localhost->system_info->container_detection, RRDLABEL_SRC_AUTO);
  1029. if (localhost->system_info->virt_detection)
  1030. rrdlabels_add(labels, "_virt_detection", localhost->system_info->virt_detection, RRDLABEL_SRC_AUTO);
  1031. if (localhost->system_info->is_k8s_node)
  1032. rrdlabels_add(labels, "_is_k8s_node", localhost->system_info->is_k8s_node, RRDLABEL_SRC_AUTO);
  1033. if (localhost->system_info->install_type)
  1034. rrdlabels_add(labels, "_install_type", localhost->system_info->install_type, RRDLABEL_SRC_AUTO);
  1035. if (localhost->system_info->prebuilt_arch)
  1036. rrdlabels_add(labels, "_prebuilt_arch", localhost->system_info->prebuilt_arch, RRDLABEL_SRC_AUTO);
  1037. if (localhost->system_info->prebuilt_dist)
  1038. rrdlabels_add(labels, "_prebuilt_dist", localhost->system_info->prebuilt_dist, RRDLABEL_SRC_AUTO);
  1039. add_aclk_host_labels();
  1040. rrdlabels_add(
  1041. labels, "_is_parent", (localhost->next || configured_as_parent()) ? "true" : "false", RRDLABEL_SRC_AUTO);
  1042. if (localhost->rrdpush_send_destination)
  1043. rrdlabels_add(labels, "_streams_to", localhost->rrdpush_send_destination, RRDLABEL_SRC_AUTO);
  1044. }
  1045. static void rrdhost_load_config_labels(void) {
  1046. int status = config_load(NULL, 1, CONFIG_SECTION_HOST_LABEL);
  1047. if(!status) {
  1048. char *filename = CONFIG_DIR "/" CONFIG_FILENAME;
  1049. error("RRDLABEL: Cannot reload the configuration file '%s', using labels in memory", filename);
  1050. }
  1051. struct section *co = appconfig_get_section(&netdata_config, CONFIG_SECTION_HOST_LABEL);
  1052. if(co) {
  1053. config_section_wrlock(co);
  1054. struct config_option *cv;
  1055. for(cv = co->values; cv ; cv = cv->next) {
  1056. rrdlabels_add(localhost->host_labels, cv->name, cv->value, RRDLABEL_SRC_CONFIG);
  1057. cv->flags |= CONFIG_VALUE_USED;
  1058. }
  1059. config_section_unlock(co);
  1060. }
  1061. }
  1062. static void rrdhost_load_kubernetes_labels(void) {
  1063. char label_script[sizeof(char) * (strlen(netdata_configured_primary_plugins_dir) + strlen("get-kubernetes-labels.sh") + 2)];
  1064. sprintf(label_script, "%s/%s", netdata_configured_primary_plugins_dir, "get-kubernetes-labels.sh");
  1065. if (unlikely(access(label_script, R_OK) != 0)) {
  1066. error("Kubernetes pod label fetching script %s not found.",label_script);
  1067. return;
  1068. }
  1069. debug(D_RRDHOST, "Attempting to fetch external labels via %s", label_script);
  1070. pid_t pid;
  1071. FILE *fp = mypopen(label_script, &pid);
  1072. if(!fp) return;
  1073. char buffer[1000 + 1];
  1074. while (fgets(buffer, 1000, fp) != NULL)
  1075. rrdlabels_add_pair(localhost->host_labels, buffer, RRDLABEL_SRC_AUTO|RRDLABEL_SRC_K8S);
  1076. // Non-zero exit code means that all the script output is error messages. We've shown already any message that didn't include a ':'
  1077. // 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
  1078. int rc = mypclose(fp, pid);
  1079. if(rc) error("%s exited abnormally. Failed to get kubernetes labels.", label_script);
  1080. }
  1081. void reload_host_labels(void) {
  1082. if(!localhost->host_labels)
  1083. localhost->host_labels = rrdlabels_create();
  1084. rrdlabels_unmark_all(localhost->host_labels);
  1085. // priority is important here
  1086. rrdhost_load_config_labels();
  1087. rrdhost_load_kubernetes_labels();
  1088. rrdhost_load_auto_labels();
  1089. rrdlabels_remove_all_unmarked(localhost->host_labels);
  1090. health_label_log_save(localhost);
  1091. /* TODO-GAPS - fix this so that it looks properly at the state and version of the sender
  1092. if(localhost->rrdpush_send_enabled && localhost->rrdpush_sender_buffer){
  1093. localhost->labels.labels_flag |= RRDHOST_FLAG_STREAM_LABELS_UPDATE;
  1094. rrdpush_send_labels(localhost);
  1095. }
  1096. */
  1097. health_reload();
  1098. }
  1099. // ----------------------------------------------------------------------------
  1100. // RRDHOST - delete host files
  1101. void rrdhost_delete_charts(RRDHOST *host) {
  1102. if(!host) return;
  1103. info("Deleting database of host '%s'...", host->hostname);
  1104. RRDSET *st;
  1105. // we get a write lock
  1106. // to ensure only one thread is saving the database
  1107. rrdhost_wrlock(host);
  1108. rrdset_foreach_write(st, host) {
  1109. rrdset_rdlock(st);
  1110. rrdset_delete_files(st);
  1111. rrdset_unlock(st);
  1112. }
  1113. recursively_delete_dir(host->cache_dir, "left over host");
  1114. rrdhost_unlock(host);
  1115. }
  1116. // ----------------------------------------------------------------------------
  1117. // RRDHOST - cleanup host files
  1118. void rrdhost_cleanup_charts(RRDHOST *host) {
  1119. if(!host) return;
  1120. info("Cleaning up database of host '%s'...", host->hostname);
  1121. RRDSET *st;
  1122. uint32_t rrdhost_delete_obsolete_charts = rrdhost_flag_check(host, RRDHOST_FLAG_DELETE_OBSOLETE_CHARTS);
  1123. // we get a write lock
  1124. // to ensure only one thread is saving the database
  1125. rrdhost_wrlock(host);
  1126. rrdset_foreach_write(st, host) {
  1127. rrdset_rdlock(st);
  1128. if(rrdhost_delete_obsolete_charts && rrdset_flag_check(st, RRDSET_FLAG_OBSOLETE))
  1129. rrdset_delete_files(st);
  1130. else if(rrdhost_delete_obsolete_charts && rrdset_flag_check(st, RRDSET_FLAG_OBSOLETE_DIMENSIONS))
  1131. rrdset_delete_obsolete_dimensions(st);
  1132. else
  1133. rrdset_save(st);
  1134. rrdset_unlock(st);
  1135. }
  1136. rrdhost_unlock(host);
  1137. }
  1138. // ----------------------------------------------------------------------------
  1139. // RRDHOST - save all hosts to disk
  1140. void rrdhost_save_all(void) {
  1141. info("Saving database [%zu hosts(s)]...", rrd_hosts_available);
  1142. rrd_rdlock();
  1143. RRDHOST *host;
  1144. rrdhost_foreach_read(host)
  1145. rrdhost_save_charts(host);
  1146. rrd_unlock();
  1147. }
  1148. // ----------------------------------------------------------------------------
  1149. // RRDHOST - save or delete all hosts from disk
  1150. void rrdhost_cleanup_all(void) {
  1151. info("Cleaning up database [%zu hosts(s)]...", rrd_hosts_available);
  1152. rrd_rdlock();
  1153. RRDHOST *host;
  1154. rrdhost_foreach_read(host) {
  1155. if (host != localhost && rrdhost_flag_check(host, RRDHOST_FLAG_DELETE_ORPHAN_HOST) && !host->receiver
  1156. #ifdef ENABLE_DBENGINE
  1157. /* don't delete multi-host DB host files */
  1158. && !(host->rrd_memory_mode == RRD_MEMORY_MODE_DBENGINE && is_storage_engine_shared(host->storage_instance[0]))
  1159. #endif
  1160. )
  1161. rrdhost_delete_charts(host);
  1162. else
  1163. rrdhost_cleanup_charts(host);
  1164. }
  1165. rrd_unlock();
  1166. }
  1167. // ----------------------------------------------------------------------------
  1168. // RRDHOST - save or delete all the host charts from disk
  1169. void rrdhost_cleanup_obsolete_charts(RRDHOST *host) {
  1170. time_t now = now_realtime_sec();
  1171. RRDSET *st;
  1172. uint32_t rrdhost_delete_obsolete_charts = rrdhost_flag_check(host, RRDHOST_FLAG_DELETE_OBSOLETE_CHARTS);
  1173. restart_after_removal:
  1174. rrdset_foreach_write(st, host) {
  1175. if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_OBSOLETE)
  1176. && st->last_accessed_time + rrdset_free_obsolete_time < now
  1177. && st->last_updated.tv_sec + rrdset_free_obsolete_time < now
  1178. && st->last_collected_time.tv_sec + rrdset_free_obsolete_time < now
  1179. )) {
  1180. st->rrdhost->obsolete_charts_count--;
  1181. #ifdef ENABLE_DBENGINE
  1182. if(st->rrd_memory_mode == RRD_MEMORY_MODE_DBENGINE) {
  1183. RRDDIM *rd, *last;
  1184. rrdset_flag_set(st, RRDSET_FLAG_ARCHIVED);
  1185. while (st->variables) rrdsetvar_free(st->variables);
  1186. while (st->alarms) rrdsetcalc_unlink(st->alarms);
  1187. rrdset_wrlock(st);
  1188. for (rd = st->dimensions, last = NULL ; likely(rd) ; ) {
  1189. if (rrddim_flag_check(rd, RRDDIM_FLAG_ARCHIVED)) {
  1190. last = rd;
  1191. rd = rd->next;
  1192. continue;
  1193. }
  1194. if (rrddim_flag_check(rd, RRDDIM_FLAG_ACLK)) {
  1195. last = rd;
  1196. rd = rd->next;
  1197. continue;
  1198. }
  1199. rrddim_flag_set(rd, RRDDIM_FLAG_ARCHIVED);
  1200. while (rd->variables)
  1201. rrddimvar_free(rd->variables);
  1202. if (rrddim_flag_check(rd, RRDDIM_FLAG_OBSOLETE)) {
  1203. rrddim_flag_clear(rd, RRDDIM_FLAG_OBSOLETE);
  1204. /* only a collector can mark a chart as obsolete, so we must remove the reference */
  1205. size_t tiers_available = 0, tiers_said_yes = 0;
  1206. for(int tier = 0; tier < storage_tiers ;tier++) {
  1207. if(rd->tiers[tier]) {
  1208. tiers_available++;
  1209. if(rd->tiers[tier]->collect_ops.finalize(rd->tiers[tier]->db_collection_handle))
  1210. tiers_said_yes++;
  1211. rd->tiers[tier]->db_collection_handle = NULL;
  1212. }
  1213. }
  1214. if (tiers_available == tiers_said_yes && tiers_said_yes) {
  1215. /* This metric has no data and no references */
  1216. delete_dimension_uuid(&rd->metric_uuid);
  1217. rrddim_free(st, rd);
  1218. if (unlikely(!last)) {
  1219. rd = st->dimensions;
  1220. }
  1221. else {
  1222. rd = last->next;
  1223. }
  1224. continue;
  1225. }
  1226. #ifdef ENABLE_ACLK
  1227. else
  1228. queue_dimension_to_aclk(rd, rd->last_collected_time.tv_sec);
  1229. #endif
  1230. }
  1231. last = rd;
  1232. rd = rd->next;
  1233. }
  1234. rrdset_unlock(st);
  1235. debug(D_RRD_CALLS, "RRDSET: Cleaning up remaining chart variables for host '%s', chart '%s'", host->hostname, st->id);
  1236. rrdvar_free_remaining_variables(host, &st->rrdvar_root_index);
  1237. rrdset_flag_clear(st, RRDSET_FLAG_OBSOLETE);
  1238. if (st->dimensions) {
  1239. /* If the chart still has dimensions don't delete it from the metadata log */
  1240. continue;
  1241. }
  1242. }
  1243. #endif
  1244. rrdset_rdlock(st);
  1245. if(rrdhost_delete_obsolete_charts)
  1246. rrdset_delete_files(st);
  1247. else
  1248. rrdset_save(st);
  1249. rrdset_unlock(st);
  1250. rrdset_free(st);
  1251. goto restart_after_removal;
  1252. }
  1253. #ifdef ENABLE_ACLK
  1254. else
  1255. sql_check_chart_liveness(st);
  1256. #endif
  1257. }
  1258. }
  1259. void rrdset_check_obsoletion(RRDHOST *host)
  1260. {
  1261. RRDSET *st;
  1262. time_t last_entry_t;
  1263. rrdset_foreach_read(st, host) {
  1264. last_entry_t = rrdset_last_entry_t(st);
  1265. if (last_entry_t && last_entry_t < host->senders_connect_time) {
  1266. rrdset_is_obsolete(st);
  1267. }
  1268. }
  1269. }
  1270. void rrd_cleanup_obsolete_charts()
  1271. {
  1272. rrd_rdlock();
  1273. RRDHOST *host;
  1274. rrdhost_foreach_read(host)
  1275. {
  1276. if (host->obsolete_charts_count) {
  1277. rrdhost_wrlock(host);
  1278. rrdhost_cleanup_obsolete_charts(host);
  1279. rrdhost_unlock(host);
  1280. }
  1281. if ( host != localhost &&
  1282. host->trigger_chart_obsoletion_check &&
  1283. ((host->senders_last_chart_command &&
  1284. host->senders_last_chart_command + host->health_delay_up_to < now_realtime_sec())
  1285. || (host->senders_connect_time + 300 < now_realtime_sec())) ) {
  1286. rrdhost_rdlock(host);
  1287. rrdset_check_obsoletion(host);
  1288. rrdhost_unlock(host);
  1289. host->trigger_chart_obsoletion_check = 0;
  1290. }
  1291. }
  1292. rrd_unlock();
  1293. }
  1294. // ----------------------------------------------------------------------------
  1295. // RRDHOST - set system info from environment variables
  1296. // system_info fields must be heap allocated or NULL
  1297. int rrdhost_set_system_info_variable(struct rrdhost_system_info *system_info, char *name, char *value) {
  1298. int res = 0;
  1299. if (!strcmp(name, "NETDATA_PROTOCOL_VERSION"))
  1300. return res;
  1301. else if(!strcmp(name, "NETDATA_INSTANCE_CLOUD_TYPE")){
  1302. freez(system_info->cloud_provider_type);
  1303. system_info->cloud_provider_type = strdupz(value);
  1304. }
  1305. else if(!strcmp(name, "NETDATA_INSTANCE_CLOUD_INSTANCE_TYPE")){
  1306. freez(system_info->cloud_instance_type);
  1307. system_info->cloud_instance_type = strdupz(value);
  1308. }
  1309. else if(!strcmp(name, "NETDATA_INSTANCE_CLOUD_INSTANCE_REGION")){
  1310. freez(system_info->cloud_instance_region);
  1311. system_info->cloud_instance_region = strdupz(value);
  1312. }
  1313. else if(!strcmp(name, "NETDATA_CONTAINER_OS_NAME")){
  1314. freez(system_info->container_os_name);
  1315. system_info->container_os_name = strdupz(value);
  1316. }
  1317. else if(!strcmp(name, "NETDATA_CONTAINER_OS_ID")){
  1318. freez(system_info->container_os_id);
  1319. system_info->container_os_id = strdupz(value);
  1320. }
  1321. else if(!strcmp(name, "NETDATA_CONTAINER_OS_ID_LIKE")){
  1322. freez(system_info->container_os_id_like);
  1323. system_info->container_os_id_like = strdupz(value);
  1324. }
  1325. else if(!strcmp(name, "NETDATA_CONTAINER_OS_VERSION")){
  1326. freez(system_info->container_os_version);
  1327. system_info->container_os_version = strdupz(value);
  1328. }
  1329. else if(!strcmp(name, "NETDATA_CONTAINER_OS_VERSION_ID")){
  1330. freez(system_info->container_os_version_id);
  1331. system_info->container_os_version_id = strdupz(value);
  1332. }
  1333. else if(!strcmp(name, "NETDATA_CONTAINER_OS_DETECTION")){
  1334. freez(system_info->container_os_detection);
  1335. system_info->container_os_detection = strdupz(value);
  1336. }
  1337. else if(!strcmp(name, "NETDATA_HOST_OS_NAME")){
  1338. freez(system_info->host_os_name);
  1339. system_info->host_os_name = strdupz(value);
  1340. json_fix_string(system_info->host_os_name);
  1341. }
  1342. else if(!strcmp(name, "NETDATA_HOST_OS_ID")){
  1343. freez(system_info->host_os_id);
  1344. system_info->host_os_id = strdupz(value);
  1345. }
  1346. else if(!strcmp(name, "NETDATA_HOST_OS_ID_LIKE")){
  1347. freez(system_info->host_os_id_like);
  1348. system_info->host_os_id_like = strdupz(value);
  1349. }
  1350. else if(!strcmp(name, "NETDATA_HOST_OS_VERSION")){
  1351. freez(system_info->host_os_version);
  1352. system_info->host_os_version = strdupz(value);
  1353. }
  1354. else if(!strcmp(name, "NETDATA_HOST_OS_VERSION_ID")){
  1355. freez(system_info->host_os_version_id);
  1356. system_info->host_os_version_id = strdupz(value);
  1357. }
  1358. else if(!strcmp(name, "NETDATA_HOST_OS_DETECTION")){
  1359. freez(system_info->host_os_detection);
  1360. system_info->host_os_detection = strdupz(value);
  1361. }
  1362. else if(!strcmp(name, "NETDATA_SYSTEM_KERNEL_NAME")){
  1363. freez(system_info->kernel_name);
  1364. system_info->kernel_name = strdupz(value);
  1365. }
  1366. else if(!strcmp(name, "NETDATA_SYSTEM_CPU_LOGICAL_CPU_COUNT")){
  1367. freez(system_info->host_cores);
  1368. system_info->host_cores = strdupz(value);
  1369. }
  1370. else if(!strcmp(name, "NETDATA_SYSTEM_CPU_FREQ")){
  1371. freez(system_info->host_cpu_freq);
  1372. system_info->host_cpu_freq = strdupz(value);
  1373. }
  1374. else if(!strcmp(name, "NETDATA_SYSTEM_TOTAL_RAM")){
  1375. freez(system_info->host_ram_total);
  1376. system_info->host_ram_total = strdupz(value);
  1377. }
  1378. else if(!strcmp(name, "NETDATA_SYSTEM_TOTAL_DISK_SIZE")){
  1379. freez(system_info->host_disk_space);
  1380. system_info->host_disk_space = strdupz(value);
  1381. }
  1382. else if(!strcmp(name, "NETDATA_SYSTEM_KERNEL_VERSION")){
  1383. freez(system_info->kernel_version);
  1384. system_info->kernel_version = strdupz(value);
  1385. }
  1386. else if(!strcmp(name, "NETDATA_SYSTEM_ARCHITECTURE")){
  1387. freez(system_info->architecture);
  1388. system_info->architecture = strdupz(value);
  1389. }
  1390. else if(!strcmp(name, "NETDATA_SYSTEM_VIRTUALIZATION")){
  1391. freez(system_info->virtualization);
  1392. system_info->virtualization = strdupz(value);
  1393. }
  1394. else if(!strcmp(name, "NETDATA_SYSTEM_VIRT_DETECTION")){
  1395. freez(system_info->virt_detection);
  1396. system_info->virt_detection = strdupz(value);
  1397. }
  1398. else if(!strcmp(name, "NETDATA_SYSTEM_CONTAINER")){
  1399. freez(system_info->container);
  1400. system_info->container = strdupz(value);
  1401. }
  1402. else if(!strcmp(name, "NETDATA_SYSTEM_CONTAINER_DETECTION")){
  1403. freez(system_info->container_detection);
  1404. system_info->container_detection = strdupz(value);
  1405. }
  1406. else if(!strcmp(name, "NETDATA_HOST_IS_K8S_NODE")){
  1407. freez(system_info->is_k8s_node);
  1408. system_info->is_k8s_node = strdupz(value);
  1409. }
  1410. else if (!strcmp(name, "NETDATA_SYSTEM_CPU_VENDOR"))
  1411. return res;
  1412. else if (!strcmp(name, "NETDATA_SYSTEM_CPU_MODEL"))
  1413. return res;
  1414. else if (!strcmp(name, "NETDATA_SYSTEM_CPU_DETECTION"))
  1415. return res;
  1416. else if (!strcmp(name, "NETDATA_SYSTEM_RAM_DETECTION"))
  1417. return res;
  1418. else if (!strcmp(name, "NETDATA_SYSTEM_DISK_DETECTION"))
  1419. return res;
  1420. else if (!strcmp(name, "NETDATA_CONTAINER_IS_OFFICIAL_IMAGE"))
  1421. return res;
  1422. else {
  1423. res = 1;
  1424. }
  1425. return res;
  1426. }
  1427. /**
  1428. * Alarm Compare ID
  1429. *
  1430. * Callback function used with the binary trees to compare the id of RRDCALC
  1431. *
  1432. * @param a a pointer to the RRDCAL item to insert,compare or update the binary tree
  1433. * @param b the pointer to the binary tree.
  1434. *
  1435. * @return It returns 0 case the values are equal, 1 case a is bigger than b and -1 case a is smaller than b.
  1436. */
  1437. int alarm_compare_id(void *a, void *b) {
  1438. register uint32_t hash1 = ((RRDCALC *)a)->id;
  1439. register uint32_t hash2 = ((RRDCALC *)b)->id;
  1440. if(hash1 < hash2) return -1;
  1441. else if(hash1 > hash2) return 1;
  1442. return 0;
  1443. }
  1444. /**
  1445. * Alarm Compare NAME
  1446. *
  1447. * Callback function used with the binary trees to compare the name of RRDCALC
  1448. *
  1449. * @param a a pointer to the RRDCAL item to insert,compare or update the binary tree
  1450. * @param b the pointer to the binary tree.
  1451. *
  1452. * @return It returns 0 case the values are equal, 1 case a is bigger than b and -1 case a is smaller than b.
  1453. */
  1454. int alarm_compare_name(void *a, void *b) {
  1455. RRDCALC *in1 = (RRDCALC *)a;
  1456. RRDCALC *in2 = (RRDCALC *)b;
  1457. if(in1->hash < in2->hash) return -1;
  1458. else if(in1->hash > in2->hash) return 1;
  1459. return strcmp(in1->name,in2->name);
  1460. }
  1461. // Added for gap-filling, if this proves to be a bottleneck in large-scale systems then we will need to cache
  1462. // the last entry times as the metric updates, but let's see if it is a problem first.
  1463. time_t rrdhost_last_entry_t(RRDHOST *h) {
  1464. rrdhost_rdlock(h);
  1465. RRDSET *st;
  1466. time_t result = 0;
  1467. rrdset_foreach_read(st, h) {
  1468. time_t st_last = rrdset_last_entry_t(st);
  1469. if (st_last > result)
  1470. result = st_last;
  1471. }
  1472. rrdhost_unlock(h);
  1473. return result;
  1474. }