rrdhost.c 74 KB

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