rrdhost.c 67 KB

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