rrdhost.c 74 KB

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