rrdhost.c 72 KB

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