rrdhost.c 74 KB

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