rrdhost.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #define NETDATA_RRD_INTERNALS
  3. #include "rrd.h"
  4. RRDHOST *localhost = NULL;
  5. size_t rrd_hosts_available = 0;
  6. netdata_rwlock_t rrd_rwlock = NETDATA_RWLOCK_INITIALIZER;
  7. time_t rrdset_free_obsolete_time = 3600;
  8. time_t rrdhost_free_orphan_time = 3600;
  9. // ----------------------------------------------------------------------------
  10. // RRDHOST index
  11. int rrdhost_compare(void* a, void* b) {
  12. if(((RRDHOST *)a)->hash_machine_guid < ((RRDHOST *)b)->hash_machine_guid) return -1;
  13. else if(((RRDHOST *)a)->hash_machine_guid > ((RRDHOST *)b)->hash_machine_guid) return 1;
  14. else return strcmp(((RRDHOST *)a)->machine_guid, ((RRDHOST *)b)->machine_guid);
  15. }
  16. avl_tree_lock rrdhost_root_index = {
  17. .avl_tree = { NULL, rrdhost_compare },
  18. .rwlock = AVL_LOCK_INITIALIZER
  19. };
  20. RRDHOST *rrdhost_find_by_guid(const char *guid, uint32_t hash) {
  21. debug(D_RRDHOST, "Searching in index for host with guid '%s'", guid);
  22. RRDHOST tmp;
  23. strncpyz(tmp.machine_guid, guid, GUID_LEN);
  24. tmp.hash_machine_guid = (hash)?hash:simple_hash(tmp.machine_guid);
  25. return (RRDHOST *)avl_search_lock(&(rrdhost_root_index), (avl *) &tmp);
  26. }
  27. RRDHOST *rrdhost_find_by_hostname(const char *hostname, uint32_t hash) {
  28. if(unlikely(!strcmp(hostname, "localhost")))
  29. return localhost;
  30. if(unlikely(!hash)) hash = simple_hash(hostname);
  31. rrd_rdlock();
  32. RRDHOST *host;
  33. rrdhost_foreach_read(host) {
  34. if(unlikely((hash == host->hash_hostname && !strcmp(hostname, host->hostname)))) {
  35. rrd_unlock();
  36. return host;
  37. }
  38. }
  39. rrd_unlock();
  40. return NULL;
  41. }
  42. #define rrdhost_index_add(rrdhost) (RRDHOST *)avl_insert_lock(&(rrdhost_root_index), (avl *)(rrdhost))
  43. #define rrdhost_index_del(rrdhost) (RRDHOST *)avl_remove_lock(&(rrdhost_root_index), (avl *)(rrdhost))
  44. // ----------------------------------------------------------------------------
  45. // RRDHOST - internal helpers
  46. static inline void rrdhost_init_tags(RRDHOST *host, const char *tags) {
  47. if(host->tags && tags && !strcmp(host->tags, tags))
  48. return;
  49. void *old = (void *)host->tags;
  50. host->tags = (tags && *tags)?strdupz(tags):NULL;
  51. freez(old);
  52. }
  53. static inline void rrdhost_init_hostname(RRDHOST *host, const char *hostname) {
  54. if(host->hostname && hostname && !strcmp(host->hostname, hostname))
  55. return;
  56. void *old = host->hostname;
  57. host->hostname = strdupz(hostname?hostname:"localhost");
  58. host->hash_hostname = simple_hash(host->hostname);
  59. freez(old);
  60. }
  61. static inline void rrdhost_init_os(RRDHOST *host, const char *os) {
  62. if(host->os && os && !strcmp(host->os, os))
  63. return;
  64. void *old = (void *)host->os;
  65. host->os = strdupz(os?os:"unknown");
  66. freez(old);
  67. }
  68. static inline void rrdhost_init_timezone(RRDHOST *host, const char *timezone) {
  69. if(host->timezone && timezone && !strcmp(host->timezone, timezone))
  70. return;
  71. void *old = (void *)host->timezone;
  72. host->timezone = strdupz((timezone && *timezone)?timezone:"unknown");
  73. freez(old);
  74. }
  75. static inline void rrdhost_init_machine_guid(RRDHOST *host, const char *machine_guid) {
  76. strncpy(host->machine_guid, machine_guid, GUID_LEN);
  77. host->machine_guid[GUID_LEN] = '\0';
  78. host->hash_machine_guid = simple_hash(host->machine_guid);
  79. }
  80. // ----------------------------------------------------------------------------
  81. // RRDHOST - add a host
  82. RRDHOST *rrdhost_create(const char *hostname,
  83. const char *registry_hostname,
  84. const char *guid,
  85. const char *os,
  86. const char *timezone,
  87. const char *tags,
  88. const char *program_name,
  89. const char *program_version,
  90. int update_every,
  91. long entries,
  92. RRD_MEMORY_MODE memory_mode,
  93. unsigned int health_enabled,
  94. unsigned int rrdpush_enabled,
  95. char *rrdpush_destination,
  96. char *rrdpush_api_key,
  97. char *rrdpush_send_charts_matching,
  98. struct rrdhost_system_info *system_info,
  99. int is_localhost
  100. ) {
  101. debug(D_RRDHOST, "Host '%s': adding with guid '%s'", hostname, guid);
  102. rrd_check_wrlock();
  103. RRDHOST *host = callocz(1, sizeof(RRDHOST));
  104. host->rrd_update_every = (update_every > 0)?update_every:1;
  105. host->rrd_history_entries = align_entries_to_pagesize(memory_mode, entries);
  106. host->rrd_memory_mode = memory_mode;
  107. #ifdef ENABLE_DBENGINE
  108. host->page_cache_mb = default_rrdeng_page_cache_mb;
  109. host->disk_space_mb = default_rrdeng_disk_quota_mb;
  110. #endif
  111. host->health_enabled = (memory_mode == RRD_MEMORY_MODE_NONE)? 0 : health_enabled;
  112. host->rrdpush_send_enabled = (rrdpush_enabled && rrdpush_destination && *rrdpush_destination && rrdpush_api_key && *rrdpush_api_key) ? 1 : 0;
  113. host->rrdpush_send_destination = (host->rrdpush_send_enabled)?strdupz(rrdpush_destination):NULL;
  114. host->rrdpush_send_api_key = (host->rrdpush_send_enabled)?strdupz(rrdpush_api_key):NULL;
  115. host->rrdpush_send_charts_matching = simple_pattern_create(rrdpush_send_charts_matching, NULL, SIMPLE_PATTERN_EXACT);
  116. host->rrdpush_sender_pipe[0] = -1;
  117. host->rrdpush_sender_pipe[1] = -1;
  118. host->rrdpush_sender_socket = -1;
  119. host->stream_version = STREAMING_PROTOCOL_CURRENT_VERSION;
  120. #ifdef ENABLE_HTTPS
  121. host->ssl.conn = NULL;
  122. host->ssl.flags = NETDATA_SSL_START;
  123. host->stream_ssl.conn = NULL;
  124. host->stream_ssl.flags = NETDATA_SSL_START;
  125. #endif
  126. netdata_mutex_init(&host->rrdpush_sender_buffer_mutex);
  127. netdata_rwlock_init(&host->rrdhost_rwlock);
  128. netdata_rwlock_init(&host->labels_rwlock);
  129. rrdhost_init_hostname(host, hostname);
  130. rrdhost_init_machine_guid(host, guid);
  131. rrdhost_init_os(host, os);
  132. rrdhost_init_timezone(host, timezone);
  133. rrdhost_init_tags(host, tags);
  134. host->program_name = strdupz((program_name && *program_name)?program_name:"unknown");
  135. host->program_version = strdupz((program_version && *program_version)?program_version:"unknown");
  136. host->registry_hostname = strdupz((registry_hostname && *registry_hostname)?registry_hostname:hostname);
  137. host->system_info = system_info;
  138. avl_init_lock(&(host->rrdset_root_index), rrdset_compare);
  139. avl_init_lock(&(host->rrdset_root_index_name), rrdset_compare_name);
  140. avl_init_lock(&(host->rrdfamily_root_index), rrdfamily_compare);
  141. avl_init_lock(&(host->rrdvar_root_index), rrdvar_compare);
  142. if(config_get_boolean(CONFIG_SECTION_GLOBAL, "delete obsolete charts files", 1))
  143. rrdhost_flag_set(host, RRDHOST_FLAG_DELETE_OBSOLETE_CHARTS);
  144. if(config_get_boolean(CONFIG_SECTION_GLOBAL, "delete orphan hosts files", 1) && !is_localhost)
  145. rrdhost_flag_set(host, RRDHOST_FLAG_DELETE_ORPHAN_HOST);
  146. host->health_default_warn_repeat_every = config_get_duration(CONFIG_SECTION_HEALTH, "default repeat warning", "never");
  147. host->health_default_crit_repeat_every = config_get_duration(CONFIG_SECTION_HEALTH, "default repeat critical", "never");
  148. avl_init_lock(&(host->alarms_idx_health_log), alarm_compare_id);
  149. avl_init_lock(&(host->alarms_idx_name), alarm_compare_name);
  150. // ------------------------------------------------------------------------
  151. // initialize health variables
  152. host->health_log.next_log_id = 1;
  153. host->health_log.next_alarm_id = 1;
  154. host->health_log.max = 1000;
  155. host->health_log.next_log_id =
  156. host->health_log.next_alarm_id = (uint32_t)now_realtime_sec();
  157. long n = config_get_number(CONFIG_SECTION_HEALTH, "in memory max health log entries", host->health_log.max);
  158. if(n < 10) {
  159. error("Host '%s': health configuration has invalid max log entries %ld. Using default %u", host->hostname, n, host->health_log.max);
  160. config_set_number(CONFIG_SECTION_HEALTH, "in memory max health log entries", (long)host->health_log.max);
  161. }
  162. else
  163. host->health_log.max = (unsigned int)n;
  164. netdata_rwlock_init(&host->health_log.alarm_log_rwlock);
  165. char filename[FILENAME_MAX + 1];
  166. if(is_localhost) {
  167. host->cache_dir = strdupz(netdata_configured_cache_dir);
  168. host->varlib_dir = strdupz(netdata_configured_varlib_dir);
  169. }
  170. else {
  171. // this is not localhost - append our GUID to localhost path
  172. snprintfz(filename, FILENAME_MAX, "%s/%s", netdata_configured_cache_dir, host->machine_guid);
  173. host->cache_dir = strdupz(filename);
  174. if(host->rrd_memory_mode == RRD_MEMORY_MODE_MAP || host->rrd_memory_mode == RRD_MEMORY_MODE_SAVE ||
  175. host->rrd_memory_mode == RRD_MEMORY_MODE_DBENGINE) {
  176. int r = mkdir(host->cache_dir, 0775);
  177. if(r != 0 && errno != EEXIST)
  178. error("Host '%s': cannot create directory '%s'", host->hostname, host->cache_dir);
  179. }
  180. snprintfz(filename, FILENAME_MAX, "%s/%s", netdata_configured_varlib_dir, host->machine_guid);
  181. host->varlib_dir = strdupz(filename);
  182. if(host->health_enabled) {
  183. int r = mkdir(host->varlib_dir, 0775);
  184. if(r != 0 && errno != EEXIST)
  185. error("Host '%s': cannot create directory '%s'", host->hostname, host->varlib_dir);
  186. }
  187. }
  188. if (host->rrd_memory_mode == RRD_MEMORY_MODE_DBENGINE) {
  189. #ifdef ENABLE_DBENGINE
  190. char dbenginepath[FILENAME_MAX + 1];
  191. int ret;
  192. snprintfz(dbenginepath, FILENAME_MAX, "%s/dbengine", host->cache_dir);
  193. ret = mkdir(dbenginepath, 0775);
  194. if(ret != 0 && errno != EEXIST)
  195. error("Host '%s': cannot create directory '%s'", host->hostname, dbenginepath);
  196. else
  197. ret = rrdeng_init(&host->rrdeng_ctx, dbenginepath, host->page_cache_mb, host->disk_space_mb);
  198. if(ret) {
  199. error("Host '%s': cannot initialize host with machine guid '%s'. Failed to initialize DB engine at '%s'.",
  200. host->hostname, host->machine_guid, host->cache_dir);
  201. rrdhost_free(host);
  202. host = NULL;
  203. //rrd_hosts_available++; //TODO: maybe we want this?
  204. return host;
  205. }
  206. #else
  207. fatal("RRD_MEMORY_MODE_DBENGINE is not supported in this platform.");
  208. #endif
  209. }
  210. if(host->health_enabled) {
  211. snprintfz(filename, FILENAME_MAX, "%s/health", host->varlib_dir);
  212. int r = mkdir(filename, 0775);
  213. if(r != 0 && errno != EEXIST)
  214. error("Host '%s': cannot create directory '%s'", host->hostname, filename);
  215. }
  216. snprintfz(filename, FILENAME_MAX, "%s/health/health-log.db", host->varlib_dir);
  217. host->health_log_filename = strdupz(filename);
  218. snprintfz(filename, FILENAME_MAX, "%s/alarm-notify.sh", netdata_configured_primary_plugins_dir);
  219. host->health_default_exec = strdupz(config_get(CONFIG_SECTION_HEALTH, "script to execute on alarm", filename));
  220. host->health_default_recipient = strdupz("root");
  221. // ------------------------------------------------------------------------
  222. // load health configuration
  223. if(host->health_enabled) {
  224. rrdhost_wrlock(host);
  225. health_readdir(host, health_user_config_dir(), health_stock_config_dir(), NULL);
  226. rrdhost_unlock(host);
  227. health_alarm_log_load(host);
  228. health_alarm_log_open(host);
  229. }
  230. // ------------------------------------------------------------------------
  231. // link it and add it to the index
  232. if(is_localhost) {
  233. host->next = localhost;
  234. localhost = host;
  235. }
  236. else {
  237. if(localhost) {
  238. host->next = localhost->next;
  239. localhost->next = host;
  240. }
  241. else localhost = host;
  242. }
  243. RRDHOST *t = rrdhost_index_add(host);
  244. if(t != host) {
  245. 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);
  246. rrdhost_free(host);
  247. host = NULL;
  248. }
  249. else {
  250. info("Host '%s' (at registry as '%s') with guid '%s' initialized"
  251. ", os '%s'"
  252. ", timezone '%s'"
  253. ", tags '%s'"
  254. ", program_name '%s'"
  255. ", program_version '%s'"
  256. ", update every %d"
  257. ", memory mode %s"
  258. ", history entries %ld"
  259. ", streaming %s"
  260. " (to '%s' with api key '%s')"
  261. ", health %s"
  262. ", cache_dir '%s'"
  263. ", varlib_dir '%s'"
  264. ", health_log '%s'"
  265. ", alarms default handler '%s'"
  266. ", alarms default recipient '%s'"
  267. , host->hostname
  268. , host->registry_hostname
  269. , host->machine_guid
  270. , host->os
  271. , host->timezone
  272. , (host->tags)?host->tags:""
  273. , host->program_name
  274. , host->program_version
  275. , host->rrd_update_every
  276. , rrd_memory_mode_name(host->rrd_memory_mode)
  277. , host->rrd_history_entries
  278. , host->rrdpush_send_enabled?"enabled":"disabled"
  279. , host->rrdpush_send_destination?host->rrdpush_send_destination:""
  280. , host->rrdpush_send_api_key?host->rrdpush_send_api_key:""
  281. , host->health_enabled?"enabled":"disabled"
  282. , host->cache_dir
  283. , host->varlib_dir
  284. , host->health_log_filename
  285. , host->health_default_exec
  286. , host->health_default_recipient
  287. );
  288. }
  289. rrd_hosts_available++;
  290. return host;
  291. }
  292. RRDHOST *rrdhost_find_or_create(
  293. const char *hostname
  294. , const char *registry_hostname
  295. , const char *guid
  296. , const char *os
  297. , const char *timezone
  298. , const char *tags
  299. , const char *program_name
  300. , const char *program_version
  301. , int update_every
  302. , long history
  303. , RRD_MEMORY_MODE mode
  304. , unsigned int health_enabled
  305. , unsigned int rrdpush_enabled
  306. , char *rrdpush_destination
  307. , char *rrdpush_api_key
  308. , char *rrdpush_send_charts_matching
  309. , struct rrdhost_system_info *system_info
  310. ) {
  311. debug(D_RRDHOST, "Searching for host '%s' with guid '%s'", hostname, guid);
  312. rrd_wrlock();
  313. RRDHOST *host = rrdhost_find_by_guid(guid, 0);
  314. if(!host) {
  315. host = rrdhost_create(
  316. hostname
  317. , registry_hostname
  318. , guid
  319. , os
  320. , timezone
  321. , tags
  322. , program_name
  323. , program_version
  324. , update_every
  325. , history
  326. , mode
  327. , health_enabled
  328. , rrdpush_enabled
  329. , rrdpush_destination
  330. , rrdpush_api_key
  331. , rrdpush_send_charts_matching
  332. , system_info
  333. , 0
  334. );
  335. }
  336. else {
  337. host->health_enabled = health_enabled;
  338. host->stream_version = STREAMING_PROTOCOL_CURRENT_VERSION;
  339. if(strcmp(host->hostname, hostname) != 0) {
  340. 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);
  341. char *t = host->hostname;
  342. host->hostname = strdupz(hostname);
  343. host->hash_hostname = simple_hash(host->hostname);
  344. freez(t);
  345. }
  346. if(strcmp(host->program_name, program_name) != 0) {
  347. info("Host '%s' switched program name from '%s' to '%s'", host->hostname, host->program_name, program_name);
  348. char *t = host->program_name;
  349. host->program_name = strdupz(program_name);
  350. freez(t);
  351. }
  352. if(strcmp(host->program_version, program_version) != 0) {
  353. info("Host '%s' switched program version from '%s' to '%s'", host->hostname, host->program_version, program_version);
  354. char *t = host->program_version;
  355. host->program_version = strdupz(program_version);
  356. freez(t);
  357. }
  358. if(host->rrd_update_every != update_every)
  359. 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);
  360. if(host->rrd_history_entries < history)
  361. 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);
  362. if(host->rrd_memory_mode != mode)
  363. 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));
  364. // update host tags
  365. rrdhost_init_tags(host, tags);
  366. }
  367. rrdhost_cleanup_orphan_hosts_nolock(host);
  368. rrd_unlock();
  369. return host;
  370. }
  371. inline int rrdhost_should_be_removed(RRDHOST *host, RRDHOST *protected, time_t now) {
  372. if(host != protected
  373. && host != localhost
  374. && rrdhost_flag_check(host, RRDHOST_FLAG_ORPHAN)
  375. && !host->connected_senders
  376. && host->senders_disconnected_time
  377. && host->senders_disconnected_time + rrdhost_free_orphan_time < now)
  378. return 1;
  379. return 0;
  380. }
  381. void rrdhost_cleanup_orphan_hosts_nolock(RRDHOST *protected) {
  382. time_t now = now_realtime_sec();
  383. RRDHOST *host;
  384. restart_after_removal:
  385. rrdhost_foreach_write(host) {
  386. if(rrdhost_should_be_removed(host, protected, now)) {
  387. info("Host '%s' with machine guid '%s' is obsolete - cleaning up.", host->hostname, host->machine_guid);
  388. if(rrdhost_flag_check(host, RRDHOST_FLAG_DELETE_ORPHAN_HOST))
  389. rrdhost_delete_charts(host);
  390. else
  391. rrdhost_save_charts(host);
  392. rrdhost_free(host);
  393. goto restart_after_removal;
  394. }
  395. }
  396. }
  397. // ----------------------------------------------------------------------------
  398. // RRDHOST global / startup initialization
  399. int rrd_init(char *hostname, struct rrdhost_system_info *system_info) {
  400. rrdset_free_obsolete_time = config_get_number(CONFIG_SECTION_GLOBAL, "cleanup obsolete charts after seconds", rrdset_free_obsolete_time);
  401. gap_when_lost_iterations_above = (int)config_get_number(CONFIG_SECTION_GLOBAL, "gap when lost iterations above", gap_when_lost_iterations_above);
  402. if (gap_when_lost_iterations_above < 1)
  403. gap_when_lost_iterations_above = 1;
  404. health_init();
  405. registry_init();
  406. rrdpush_init();
  407. debug(D_RRDHOST, "Initializing localhost with hostname '%s'", hostname);
  408. rrd_wrlock();
  409. localhost = rrdhost_create(
  410. hostname
  411. , registry_get_this_machine_hostname()
  412. , registry_get_this_machine_guid()
  413. , os_type
  414. , netdata_configured_timezone
  415. , config_get(CONFIG_SECTION_BACKEND, "host tags", "")
  416. , program_name
  417. , program_version
  418. , default_rrd_update_every
  419. , default_rrd_history_entries
  420. , default_rrd_memory_mode
  421. , default_health_enabled
  422. , default_rrdpush_enabled
  423. , default_rrdpush_destination
  424. , default_rrdpush_api_key
  425. , default_rrdpush_send_charts_matching
  426. , system_info
  427. , 1
  428. );
  429. rrd_unlock();
  430. web_client_api_v1_management_init();
  431. return localhost==NULL;
  432. }
  433. // ----------------------------------------------------------------------------
  434. // RRDHOST - lock validations
  435. // there are only used when NETDATA_INTERNAL_CHECKS is set
  436. void __rrdhost_check_rdlock(RRDHOST *host, const char *file, const char *function, const unsigned long line) {
  437. debug(D_RRDHOST, "Checking read lock on host '%s'", host->hostname);
  438. int ret = netdata_rwlock_trywrlock(&host->rrdhost_rwlock);
  439. if(ret == 0)
  440. 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);
  441. }
  442. void __rrdhost_check_wrlock(RRDHOST *host, const char *file, const char *function, const unsigned long line) {
  443. debug(D_RRDHOST, "Checking write lock on host '%s'", host->hostname);
  444. int ret = netdata_rwlock_tryrdlock(&host->rrdhost_rwlock);
  445. if(ret == 0)
  446. 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);
  447. }
  448. void __rrd_check_rdlock(const char *file, const char *function, const unsigned long line) {
  449. debug(D_RRDHOST, "Checking read lock on all RRDs");
  450. int ret = netdata_rwlock_trywrlock(&rrd_rwlock);
  451. if(ret == 0)
  452. fatal("RRDs should be read-locked, but it are not, at function %s() at line %lu of file '%s'", function, line, file);
  453. }
  454. void __rrd_check_wrlock(const char *file, const char *function, const unsigned long line) {
  455. debug(D_RRDHOST, "Checking write lock on all RRDs");
  456. int ret = netdata_rwlock_tryrdlock(&rrd_rwlock);
  457. if(ret == 0)
  458. fatal("RRDs should be write-locked, but it are not, at function %s() at line %lu of file '%s'", function, line, file);
  459. }
  460. // ----------------------------------------------------------------------------
  461. // RRDHOST - free
  462. void rrdhost_system_info_free(struct rrdhost_system_info *system_info) {
  463. info("SYSTEM_INFO: free %p", system_info);
  464. if(likely(system_info)) {
  465. freez(system_info->host_os_name);
  466. freez(system_info->host_os_id);
  467. freez(system_info->host_os_id_like);
  468. freez(system_info->host_os_version);
  469. freez(system_info->host_os_version_id);
  470. freez(system_info->host_os_detection);
  471. freez(system_info->host_cores);
  472. freez(system_info->host_cpu_freq);
  473. freez(system_info->host_ram_total);
  474. freez(system_info->host_disk_space);
  475. freez(system_info->container_os_name);
  476. freez(system_info->container_os_id);
  477. freez(system_info->container_os_id_like);
  478. freez(system_info->container_os_version);
  479. freez(system_info->container_os_version_id);
  480. freez(system_info->container_os_detection);
  481. freez(system_info->kernel_name);
  482. freez(system_info->kernel_version);
  483. freez(system_info->architecture);
  484. freez(system_info->virtualization);
  485. freez(system_info->virt_detection);
  486. freez(system_info->container);
  487. freez(system_info->container_detection);
  488. freez(system_info);
  489. }
  490. }
  491. void rrdhost_free(RRDHOST *host) {
  492. if(!host) return;
  493. info("Freeing all memory for host '%s'...", host->hostname);
  494. rrd_check_wrlock(); // make sure the RRDs are write locked
  495. // stop a possibly running thread
  496. rrdpush_sender_thread_stop(host);
  497. rrdhost_wrlock(host); // lock this RRDHOST
  498. // ------------------------------------------------------------------------
  499. // release its children resources
  500. while(host->rrdset_root)
  501. rrdset_free(host->rrdset_root);
  502. freez(host->exporting_flags);
  503. while(host->alarms)
  504. rrdcalc_unlink_and_free(host, host->alarms);
  505. RRDCALC *rc,*nc;
  506. for(rc = host->alarms_with_foreach; rc ; rc = nc) {
  507. nc = rc->next;
  508. rrdcalc_free(rc);
  509. }
  510. host->alarms_with_foreach = NULL;
  511. while(host->templates)
  512. rrdcalctemplate_unlink_and_free(host, host->templates);
  513. RRDCALCTEMPLATE *rt,*next;
  514. for(rt = host->alarms_template_with_foreach; rt ; rt = next) {
  515. next = rt->next;
  516. rrdcalctemplate_free(rt);
  517. }
  518. host->alarms_template_with_foreach = NULL;
  519. debug(D_RRD_CALLS, "RRDHOST: Cleaning up remaining host variables for host '%s'", host->hostname);
  520. rrdvar_free_remaining_variables(host, &host->rrdvar_root_index);
  521. health_alarm_log_free(host);
  522. if (host->rrd_memory_mode == RRD_MEMORY_MODE_DBENGINE) {
  523. #ifdef ENABLE_DBENGINE
  524. rrdeng_exit(host->rrdeng_ctx);
  525. #endif
  526. }
  527. // ------------------------------------------------------------------------
  528. // remove it from the indexes
  529. if(rrdhost_index_del(host) != host)
  530. error("RRDHOST '%s' removed from index, deleted the wrong entry.", host->hostname);
  531. // ------------------------------------------------------------------------
  532. // unlink it from the host
  533. if(host == localhost) {
  534. localhost = host->next;
  535. }
  536. else {
  537. // find the previous one
  538. RRDHOST *h;
  539. for(h = localhost; h && h->next != host ; h = h->next) ;
  540. // bypass it
  541. if(h) h->next = host->next;
  542. else error("Request to free RRDHOST '%s': cannot find it", host->hostname);
  543. }
  544. // ------------------------------------------------------------------------
  545. // free it
  546. freez((void *)host->tags);
  547. free_host_labels(host->labels);
  548. freez((void *)host->os);
  549. freez((void *)host->timezone);
  550. freez(host->program_version);
  551. freez(host->program_name);
  552. rrdhost_system_info_free(host->system_info);
  553. freez(host->cache_dir);
  554. freez(host->varlib_dir);
  555. freez(host->rrdpush_send_api_key);
  556. freez(host->rrdpush_send_destination);
  557. freez(host->health_default_exec);
  558. freez(host->health_default_recipient);
  559. freez(host->health_log_filename);
  560. freez(host->hostname);
  561. freez(host->registry_hostname);
  562. simple_pattern_free(host->rrdpush_send_charts_matching);
  563. rrdhost_unlock(host);
  564. netdata_rwlock_destroy(&host->labels_rwlock);
  565. netdata_rwlock_destroy(&host->health_log.alarm_log_rwlock);
  566. netdata_rwlock_destroy(&host->rrdhost_rwlock);
  567. freez(host);
  568. rrd_hosts_available--;
  569. }
  570. void rrdhost_free_all(void) {
  571. rrd_wrlock();
  572. while(localhost) rrdhost_free(localhost);
  573. rrd_unlock();
  574. }
  575. // ----------------------------------------------------------------------------
  576. // RRDHOST - save host files
  577. void rrdhost_save_charts(RRDHOST *host) {
  578. if(!host) return;
  579. info("Saving/Closing database of host '%s'...", host->hostname);
  580. RRDSET *st;
  581. // we get a write lock
  582. // to ensure only one thread is saving the database
  583. rrdhost_wrlock(host);
  584. rrdset_foreach_write(st, host) {
  585. rrdset_rdlock(st);
  586. rrdset_save(st);
  587. rrdset_unlock(st);
  588. }
  589. rrdhost_unlock(host);
  590. }
  591. static int is_valid_label_value(char *value) {
  592. while(*value) {
  593. if(*value == '"' || *value == '\'' || *value == '*' || *value == '!') {
  594. return 0;
  595. }
  596. value++;
  597. }
  598. return 1;
  599. }
  600. static int is_valid_label_key(char *key) {
  601. //Prometheus exporter
  602. if(!strcmp(key, "chart") || !strcmp(key, "family") || !strcmp(key, "dimension"))
  603. return 0;
  604. //Netdata and Prometheus internal
  605. if (*key == '_')
  606. return 0;
  607. while(*key) {
  608. if(!(isdigit(*key) || isalpha(*key) || *key == '.' || *key == '_' || *key == '-'))
  609. return 0;
  610. key++;
  611. }
  612. return 1;
  613. }
  614. char *translate_label_source(LABEL_SOURCE l) {
  615. switch (l) {
  616. case LABEL_SOURCE_AUTO:
  617. return "AUTO";
  618. case LABEL_SOURCE_NETDATA_CONF:
  619. return "NETDATA.CONF";
  620. case LABEL_SOURCE_DOCKER :
  621. return "DOCKER";
  622. case LABEL_SOURCE_ENVIRONMENT :
  623. return "ENVIRONMENT";
  624. case LABEL_SOURCE_KUBERNETES :
  625. return "KUBERNETES";
  626. default:
  627. return "Invalid label source";
  628. }
  629. }
  630. struct label *load_auto_labels()
  631. {
  632. struct label *label_list = NULL;
  633. if (localhost->system_info->host_os_name)
  634. label_list =
  635. add_label_to_list(label_list, "_os_name", localhost->system_info->host_os_name, LABEL_SOURCE_AUTO);
  636. if (localhost->system_info->host_os_version)
  637. label_list =
  638. add_label_to_list(label_list, "_os_version", localhost->system_info->host_os_version, LABEL_SOURCE_AUTO);
  639. if (localhost->system_info->kernel_version)
  640. label_list =
  641. add_label_to_list(label_list, "_kernel_version", localhost->system_info->kernel_version, LABEL_SOURCE_AUTO);
  642. if (localhost->system_info->host_cores)
  643. label_list =
  644. add_label_to_list(label_list, "_system_cores", localhost->system_info->host_cores, LABEL_SOURCE_AUTO);
  645. if (localhost->system_info->host_cpu_freq)
  646. label_list =
  647. add_label_to_list(label_list, "_system_cpu_freq", localhost->system_info->host_cpu_freq, LABEL_SOURCE_AUTO);
  648. if (localhost->system_info->host_ram_total)
  649. label_list =
  650. add_label_to_list(label_list, "_system_ram_total", localhost->system_info->host_ram_total, LABEL_SOURCE_AUTO);
  651. if (localhost->system_info->host_disk_space)
  652. label_list =
  653. add_label_to_list(label_list, "_system_disk_space", localhost->system_info->host_disk_space, LABEL_SOURCE_AUTO);
  654. if (localhost->system_info->architecture)
  655. label_list =
  656. add_label_to_list(label_list, "_architecture", localhost->system_info->architecture, LABEL_SOURCE_AUTO);
  657. if (localhost->system_info->virtualization)
  658. label_list =
  659. add_label_to_list(label_list, "_virtualization", localhost->system_info->virtualization, LABEL_SOURCE_AUTO);
  660. if (localhost->system_info->container)
  661. label_list =
  662. add_label_to_list(label_list, "_container", localhost->system_info->container, LABEL_SOURCE_AUTO);
  663. if (localhost->system_info->container_detection)
  664. label_list =
  665. add_label_to_list(label_list, "_container_detection", localhost->system_info->container_detection, LABEL_SOURCE_AUTO);
  666. if (localhost->system_info->virt_detection)
  667. label_list =
  668. add_label_to_list(label_list, "_virt_detection", localhost->system_info->virt_detection, LABEL_SOURCE_AUTO);
  669. label_list = add_label_to_list(
  670. label_list, "_is_master", (localhost->next || configured_as_master()) ? "true" : "false", LABEL_SOURCE_AUTO);
  671. if (localhost->rrdpush_send_destination)
  672. label_list =
  673. add_label_to_list(label_list, "_streams_to", localhost->rrdpush_send_destination, LABEL_SOURCE_AUTO);
  674. return label_list;
  675. }
  676. static inline int is_valid_label_config_option(char *name, char *value) {
  677. return (is_valid_label_key(name) && is_valid_label_value(value) && strcmp(name, "from environment") && strcmp(name, "from kubernetes pods") );
  678. }
  679. struct label *load_config_labels()
  680. {
  681. int status = config_load(NULL, 1, CONFIG_SECTION_HOST_LABEL);
  682. if(!status) {
  683. char *filename = CONFIG_DIR "/" CONFIG_FILENAME;
  684. error("LABEL: Cannot reload the configuration file '%s', using labels in memory", filename);
  685. }
  686. struct label *l = NULL;
  687. struct section *co = appconfig_get_section(&netdata_config, CONFIG_SECTION_HOST_LABEL);
  688. if(co) {
  689. config_section_wrlock(co);
  690. struct config_option *cv;
  691. for(cv = co->values; cv ; cv = cv->next) {
  692. if( is_valid_label_config_option(cv->name, cv->value)) {
  693. l = add_label_to_list(l, cv->name, cv->value, LABEL_SOURCE_NETDATA_CONF);
  694. cv->flags |= CONFIG_VALUE_USED;
  695. } else {
  696. error("LABELS: It was not possible to create the label '%s' because it contains invalid character(s) or values."
  697. , cv->name);
  698. }
  699. }
  700. config_section_unlock(co);
  701. }
  702. return l;
  703. }
  704. typedef enum strip_quotes {
  705. DO_NOT_STRIP_QUOTES,
  706. STRIP_QUOTES
  707. } STRIP_QUOTES_OPTION;
  708. typedef enum skip_escaped_characters {
  709. DO_NOT_SKIP_ESCAPED_CHARACTERS,
  710. SKIP_ESCAPED_CHARACTERS
  711. } SKIP_ESCAPED_CHARACTERS_OPTION;
  712. static inline void strip_last_symbol(
  713. char *str,
  714. char symbol,
  715. SKIP_ESCAPED_CHARACTERS_OPTION skip_escaped_characters)
  716. {
  717. char *end = str;
  718. while (*end && *end != symbol) {
  719. if (unlikely(skip_escaped_characters && *end == '\\')) {
  720. end++;
  721. if (unlikely(!*end))
  722. break;
  723. }
  724. end++;
  725. }
  726. if (likely(*end == symbol))
  727. *end = '\0';
  728. }
  729. static inline char *strip_double_quotes(char *str, SKIP_ESCAPED_CHARACTERS_OPTION skip_escaped_characters)
  730. {
  731. if (*str == '"') {
  732. str++;
  733. strip_last_symbol(str, '"', skip_escaped_characters);
  734. }
  735. return str;
  736. }
  737. struct label *parse_simple_tags(
  738. struct label *label_list,
  739. const char *tags,
  740. char key_value_separator,
  741. char label_separator,
  742. STRIP_QUOTES_OPTION strip_quotes_from_key,
  743. STRIP_QUOTES_OPTION strip_quotes_from_value,
  744. SKIP_ESCAPED_CHARACTERS_OPTION skip_escaped_characters)
  745. {
  746. const char *end = tags;
  747. while (*end) {
  748. const char *start = end;
  749. char key[CONFIG_MAX_VALUE + 1];
  750. char value[CONFIG_MAX_VALUE + 1];
  751. while (*end && *end != key_value_separator)
  752. end++;
  753. strncpyz(key, start, end - start);
  754. if (*end)
  755. start = ++end;
  756. while (*end && *end != label_separator)
  757. end++;
  758. strncpyz(value, start, end - start);
  759. label_list = add_label_to_list(
  760. label_list,
  761. strip_quotes_from_key ? strip_double_quotes(trim(key), skip_escaped_characters) : trim(key),
  762. strip_quotes_from_value ? strip_double_quotes(trim(value), skip_escaped_characters) : trim(value),
  763. LABEL_SOURCE_NETDATA_CONF);
  764. if (*end)
  765. end++;
  766. }
  767. return label_list;
  768. }
  769. struct label *parse_json_tags(struct label *label_list, const char *tags)
  770. {
  771. char tags_buf[CONFIG_MAX_VALUE + 1];
  772. strncpy(tags_buf, tags, CONFIG_MAX_VALUE);
  773. char *str = tags_buf;
  774. switch (*str) {
  775. case '{':
  776. str++;
  777. strip_last_symbol(str, '}', SKIP_ESCAPED_CHARACTERS);
  778. label_list = parse_simple_tags(label_list, str, ':', ',', STRIP_QUOTES, STRIP_QUOTES, SKIP_ESCAPED_CHARACTERS);
  779. break;
  780. case '[':
  781. str++;
  782. strip_last_symbol(str, ']', SKIP_ESCAPED_CHARACTERS);
  783. char *end = str + strlen(str);
  784. size_t i = 0;
  785. while (str < end) {
  786. char key[CONFIG_MAX_VALUE + 1];
  787. snprintfz(key, CONFIG_MAX_VALUE, "host_tag%zu", i);
  788. str = strip_double_quotes(trim(str), SKIP_ESCAPED_CHARACTERS);
  789. label_list = add_label_to_list(label_list, key, str, LABEL_SOURCE_NETDATA_CONF);
  790. // skip to the next element in the array
  791. str += strlen(str) + 1;
  792. while (*str && *str != ',')
  793. str++;
  794. str++;
  795. i++;
  796. }
  797. break;
  798. case '"':
  799. label_list = add_label_to_list(
  800. label_list, "host_tag", strip_double_quotes(str, SKIP_ESCAPED_CHARACTERS), LABEL_SOURCE_NETDATA_CONF);
  801. break;
  802. default:
  803. label_list = add_label_to_list(label_list, "host_tag", str, LABEL_SOURCE_NETDATA_CONF);
  804. break;
  805. }
  806. return label_list;
  807. }
  808. struct label *load_labels_from_tags()
  809. {
  810. if (!localhost->tags)
  811. return NULL;
  812. struct label *label_list = NULL;
  813. BACKEND_TYPE type = BACKEND_TYPE_UNKNOWN;
  814. if (config_exists(CONFIG_SECTION_BACKEND, "enabled")) {
  815. if (config_get_boolean(CONFIG_SECTION_BACKEND, "enabled", CONFIG_BOOLEAN_NO) != CONFIG_BOOLEAN_NO) {
  816. const char *type_name = config_get(CONFIG_SECTION_BACKEND, "type", "graphite");
  817. type = backend_select_type(type_name);
  818. }
  819. }
  820. switch (type) {
  821. case BACKEND_TYPE_GRAPHITE:
  822. label_list = parse_simple_tags(
  823. label_list, localhost->tags, '=', ';', DO_NOT_STRIP_QUOTES, DO_NOT_STRIP_QUOTES,
  824. DO_NOT_SKIP_ESCAPED_CHARACTERS);
  825. break;
  826. case BACKEND_TYPE_OPENTSDB_USING_TELNET:
  827. label_list = parse_simple_tags(
  828. label_list, localhost->tags, '=', ' ', DO_NOT_STRIP_QUOTES, DO_NOT_STRIP_QUOTES,
  829. DO_NOT_SKIP_ESCAPED_CHARACTERS);
  830. break;
  831. case BACKEND_TYPE_OPENTSDB_USING_HTTP:
  832. label_list = parse_simple_tags(
  833. label_list, localhost->tags, ':', ',', STRIP_QUOTES, STRIP_QUOTES,
  834. DO_NOT_SKIP_ESCAPED_CHARACTERS);
  835. break;
  836. case BACKEND_TYPE_JSON:
  837. label_list = parse_json_tags(label_list, localhost->tags);
  838. break;
  839. default:
  840. label_list = parse_simple_tags(
  841. label_list, localhost->tags, '=', ',', DO_NOT_STRIP_QUOTES, STRIP_QUOTES,
  842. DO_NOT_SKIP_ESCAPED_CHARACTERS);
  843. break;
  844. }
  845. return label_list;
  846. }
  847. struct label *load_kubernetes_labels()
  848. {
  849. struct label *l=NULL;
  850. char *label_script = mallocz(sizeof(char) * (strlen(netdata_configured_primary_plugins_dir) + strlen("get-kubernetes-labels.sh") + 2));
  851. sprintf(label_script, "%s/%s", netdata_configured_primary_plugins_dir, "get-kubernetes-labels.sh");
  852. if (unlikely(access(label_script, R_OK) != 0)) {
  853. error("Kubernetes pod label fetching script %s not found.",label_script);
  854. freez(label_script);
  855. } else {
  856. pid_t command_pid;
  857. debug(D_RRDHOST, "Attempting to fetch external labels via %s", label_script);
  858. FILE *fp = mypopen(label_script, &command_pid);
  859. if(fp) {
  860. int MAX_LINE_SIZE=300;
  861. char buffer[MAX_LINE_SIZE + 1];
  862. while (fgets(buffer, MAX_LINE_SIZE, fp) != NULL) {
  863. char *name=buffer;
  864. char *value=buffer;
  865. while (*value && *value != ':') value++;
  866. if (*value == ':') {
  867. *value = '\0';
  868. value++;
  869. }
  870. char *eos=value;
  871. while (*eos && *eos != '\n') eos++;
  872. if (*eos == '\n') *eos = '\0';
  873. if (strlen(value)>0) {
  874. if (is_valid_label_key(name)){
  875. l = add_label_to_list(l, name, value, LABEL_SOURCE_KUBERNETES);
  876. } else {
  877. info("Ignoring invalid label name '%s'", name);
  878. }
  879. } else {
  880. error("%s outputted unexpected result: '%s'", label_script, name);
  881. }
  882. };
  883. // Non-zero exit code means that all the script output is error messages. We've shown already any message that didn't include a ':'
  884. // 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
  885. int retcode=mypclose(fp, command_pid);
  886. if (retcode) {
  887. error("%s exited abnormally. No kubernetes labels will be added to the host.", label_script);
  888. struct label *ll=l;
  889. while (ll != NULL) {
  890. info("Ignoring Label [source id=%s]: \"%s\" -> \"%s\"\n", translate_label_source(ll->label_source), ll->key, ll->value);
  891. ll = ll->next;
  892. freez(l);
  893. l=ll;
  894. }
  895. }
  896. }
  897. freez(label_script);
  898. }
  899. return l;
  900. }
  901. struct label *create_label(char *key, char *value, LABEL_SOURCE label_source)
  902. {
  903. size_t key_len = strlen(key), value_len = strlen(value);
  904. size_t n = sizeof(struct label) + key_len + 1 + value_len + 1;
  905. struct label *result = callocz(1,n);
  906. if (result != NULL) {
  907. char *c = (char *)result;
  908. c += sizeof(struct label);
  909. strcpy(c, key);
  910. result->key = c;
  911. c += key_len + 1;
  912. strcpy(c, value);
  913. result->value = c;
  914. result->label_source = label_source;
  915. result->key_hash = simple_hash(result->key);
  916. }
  917. return result;
  918. }
  919. void free_host_labels(struct label *labels)
  920. {
  921. while (labels != NULL)
  922. {
  923. struct label *current = labels;
  924. labels = labels->next;
  925. freez(current);
  926. }
  927. }
  928. void replace_label_list(RRDHOST *host, struct label *new_labels)
  929. {
  930. rrdhost_check_rdlock(host);
  931. netdata_rwlock_wrlock(&host->labels_rwlock);
  932. struct label *old_labels = host->labels;
  933. host->labels = new_labels;
  934. netdata_rwlock_unlock(&host->labels_rwlock);
  935. free_host_labels(old_labels);
  936. }
  937. struct label *add_label_to_list(struct label *l, char *key, char *value, LABEL_SOURCE label_source)
  938. {
  939. struct label *lab = create_label(key, value, label_source);
  940. lab->next = l;
  941. return lab;
  942. }
  943. int label_list_contains(struct label *head, struct label *check)
  944. {
  945. while (head != NULL)
  946. {
  947. if (head->key_hash == check->key_hash && !strcmp(head->key, check->key))
  948. return 1;
  949. head = head->next;
  950. }
  951. return 0;
  952. }
  953. /* Create a list with entries from both lists.
  954. If any entry in the low priority list is masked by an entry in the high priorty list then delete it.
  955. */
  956. struct label *merge_label_lists(struct label *lo_pri, struct label *hi_pri)
  957. {
  958. struct label *result = hi_pri;
  959. while (lo_pri != NULL)
  960. {
  961. struct label *current = lo_pri;
  962. lo_pri = lo_pri->next;
  963. if (!label_list_contains(result, current)) {
  964. current->next = result;
  965. result = current;
  966. }
  967. else
  968. freez(current);
  969. }
  970. return result;
  971. }
  972. void reload_host_labels()
  973. {
  974. struct label *from_auto = load_auto_labels();
  975. struct label *from_k8s = load_kubernetes_labels();
  976. struct label *from_config = load_config_labels();
  977. struct label *from_tags = load_labels_from_tags();
  978. struct label *new_labels = merge_label_lists(from_auto, from_k8s);
  979. new_labels = merge_label_lists(new_labels, from_tags);
  980. new_labels = merge_label_lists(new_labels, from_config);
  981. rrdhost_rdlock(localhost);
  982. replace_label_list(localhost, new_labels);
  983. health_label_log_save(localhost);
  984. rrdhost_unlock(localhost);
  985. if(localhost->rrdpush_send_enabled && localhost->rrdpush_sender_buffer){
  986. localhost->labels_flag |= LABEL_FLAG_UPDATE_STREAM;
  987. rrdpush_send_labels(localhost);
  988. }
  989. health_reload();
  990. }
  991. // ----------------------------------------------------------------------------
  992. // RRDHOST - delete host files
  993. void rrdhost_delete_charts(RRDHOST *host) {
  994. if(!host) return;
  995. info("Deleting database of host '%s'...", host->hostname);
  996. RRDSET *st;
  997. // we get a write lock
  998. // to ensure only one thread is saving the database
  999. rrdhost_wrlock(host);
  1000. rrdset_foreach_write(st, host) {
  1001. rrdset_rdlock(st);
  1002. rrdset_delete(st);
  1003. rrdset_unlock(st);
  1004. }
  1005. recursively_delete_dir(host->cache_dir, "left over host");
  1006. rrdhost_unlock(host);
  1007. }
  1008. // ----------------------------------------------------------------------------
  1009. // RRDHOST - cleanup host files
  1010. void rrdhost_cleanup_charts(RRDHOST *host) {
  1011. if(!host) return;
  1012. info("Cleaning up database of host '%s'...", host->hostname);
  1013. RRDSET *st;
  1014. uint32_t rrdhost_delete_obsolete_charts = rrdhost_flag_check(host, RRDHOST_FLAG_DELETE_OBSOLETE_CHARTS);
  1015. // we get a write lock
  1016. // to ensure only one thread is saving the database
  1017. rrdhost_wrlock(host);
  1018. rrdset_foreach_write(st, host) {
  1019. rrdset_rdlock(st);
  1020. if(rrdhost_delete_obsolete_charts && rrdset_flag_check(st, RRDSET_FLAG_OBSOLETE))
  1021. rrdset_delete(st);
  1022. else if(rrdhost_delete_obsolete_charts && rrdset_flag_check(st, RRDSET_FLAG_OBSOLETE_DIMENSIONS))
  1023. rrdset_delete_obsolete_dimensions(st);
  1024. else
  1025. rrdset_save(st);
  1026. rrdset_unlock(st);
  1027. }
  1028. rrdhost_unlock(host);
  1029. }
  1030. // ----------------------------------------------------------------------------
  1031. // RRDHOST - save all hosts to disk
  1032. void rrdhost_save_all(void) {
  1033. info("Saving database [%zu hosts(s)]...", rrd_hosts_available);
  1034. rrd_rdlock();
  1035. RRDHOST *host;
  1036. rrdhost_foreach_read(host)
  1037. rrdhost_save_charts(host);
  1038. rrd_unlock();
  1039. }
  1040. // ----------------------------------------------------------------------------
  1041. // RRDHOST - save or delete all hosts from disk
  1042. void rrdhost_cleanup_all(void) {
  1043. info("Cleaning up database [%zu hosts(s)]...", rrd_hosts_available);
  1044. rrd_rdlock();
  1045. RRDHOST *host;
  1046. rrdhost_foreach_read(host) {
  1047. if(host != localhost && rrdhost_flag_check(host, RRDHOST_FLAG_DELETE_OBSOLETE_CHARTS) && !host->connected_senders)
  1048. rrdhost_delete_charts(host);
  1049. else
  1050. rrdhost_cleanup_charts(host);
  1051. }
  1052. rrd_unlock();
  1053. }
  1054. // ----------------------------------------------------------------------------
  1055. // RRDHOST - save or delete all the host charts from disk
  1056. void rrdhost_cleanup_obsolete_charts(RRDHOST *host) {
  1057. time_t now = now_realtime_sec();
  1058. RRDSET *st;
  1059. uint32_t rrdhost_delete_obsolete_charts = rrdhost_flag_check(host, RRDHOST_FLAG_DELETE_OBSOLETE_CHARTS);
  1060. restart_after_removal:
  1061. rrdset_foreach_write(st, host) {
  1062. if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_OBSOLETE)
  1063. && st->last_accessed_time + rrdset_free_obsolete_time < now
  1064. && st->last_updated.tv_sec + rrdset_free_obsolete_time < now
  1065. && st->last_collected_time.tv_sec + rrdset_free_obsolete_time < now
  1066. )) {
  1067. rrdset_rdlock(st);
  1068. if(rrdhost_delete_obsolete_charts)
  1069. rrdset_delete(st);
  1070. else
  1071. rrdset_save(st);
  1072. rrdset_unlock(st);
  1073. rrdset_free(st);
  1074. goto restart_after_removal;
  1075. }
  1076. }
  1077. }
  1078. // ----------------------------------------------------------------------------
  1079. // RRDHOST - set system info from environment variables
  1080. // system_info fields must be heap allocated or NULL
  1081. int rrdhost_set_system_info_variable(struct rrdhost_system_info *system_info, char *name, char *value) {
  1082. int res = 0;
  1083. if (!strcmp(name, "NETDATA_PROTOCOL_VERSION"))
  1084. return res;
  1085. else if(!strcmp(name, "NETDATA_CONTAINER_OS_NAME")){
  1086. freez(system_info->container_os_name);
  1087. system_info->container_os_name = strdupz(value);
  1088. }
  1089. else if(!strcmp(name, "NETDATA_CONTAINER_OS_ID")){
  1090. freez(system_info->container_os_id);
  1091. system_info->container_os_id = strdupz(value);
  1092. }
  1093. else if(!strcmp(name, "NETDATA_CONTAINER_OS_ID_LIKE")){
  1094. freez(system_info->container_os_id_like);
  1095. system_info->container_os_id_like = strdupz(value);
  1096. }
  1097. else if(!strcmp(name, "NETDATA_CONTAINER_OS_VERSION")){
  1098. freez(system_info->container_os_version);
  1099. system_info->container_os_version = strdupz(value);
  1100. }
  1101. else if(!strcmp(name, "NETDATA_CONTAINER_OS_VERSION_ID")){
  1102. freez(system_info->container_os_version_id);
  1103. system_info->container_os_version_id = strdupz(value);
  1104. }
  1105. else if(!strcmp(name, "NETDATA_CONTAINER_OS_DETECTION")){
  1106. freez(system_info->host_os_detection);
  1107. system_info->host_os_detection = strdupz(value);
  1108. }
  1109. else if(!strcmp(name, "NETDATA_HOST_OS_NAME")){
  1110. freez(system_info->host_os_name);
  1111. system_info->host_os_name = strdupz(value);
  1112. }
  1113. else if(!strcmp(name, "NETDATA_HOST_OS_ID")){
  1114. freez(system_info->host_os_id);
  1115. system_info->host_os_id = strdupz(value);
  1116. }
  1117. else if(!strcmp(name, "NETDATA_HOST_OS_ID_LIKE")){
  1118. freez(system_info->host_os_id_like);
  1119. system_info->host_os_id_like = strdupz(value);
  1120. }
  1121. else if(!strcmp(name, "NETDATA_HOST_OS_VERSION")){
  1122. freez(system_info->host_os_version);
  1123. system_info->host_os_version = strdupz(value);
  1124. }
  1125. else if(!strcmp(name, "NETDATA_HOST_OS_VERSION_ID")){
  1126. freez(system_info->host_os_version_id);
  1127. system_info->host_os_version_id = strdupz(value);
  1128. }
  1129. else if(!strcmp(name, "NETDATA_HOST_OS_DETECTION")){
  1130. freez(system_info->host_os_detection);
  1131. system_info->host_os_detection = strdupz(value);
  1132. }
  1133. else if(!strcmp(name, "NETDATA_SYSTEM_KERNEL_NAME")){
  1134. freez(system_info->kernel_name);
  1135. system_info->kernel_name = strdupz(value);
  1136. }
  1137. else if(!strcmp(name, "NETDATA_SYSTEM_CPU_LOGICAL_CPU_COUNT")){
  1138. freez(system_info->host_cores);
  1139. system_info->host_cores = strdupz(value);
  1140. }
  1141. else if(!strcmp(name, "NETDATA_SYSTEM_CPU_FREQ")){
  1142. freez(system_info->host_cpu_freq);
  1143. system_info->host_cpu_freq = strdupz(value);
  1144. }
  1145. else if(!strcmp(name, "NETDATA_SYSTEM_TOTAL_RAM")){
  1146. freez(system_info->host_ram_total);
  1147. system_info->host_ram_total = strdupz(value);
  1148. }
  1149. else if(!strcmp(name, "NETDATA_SYSTEM_TOTAL_DISK_SIZE")){
  1150. freez(system_info->host_disk_space);
  1151. system_info->host_disk_space = strdupz(value);
  1152. }
  1153. else if(!strcmp(name, "NETDATA_SYSTEM_KERNEL_VERSION")){
  1154. freez(system_info->kernel_version);
  1155. system_info->kernel_version = strdupz(value);
  1156. }
  1157. else if(!strcmp(name, "NETDATA_SYSTEM_ARCHITECTURE")){
  1158. freez(system_info->architecture);
  1159. system_info->architecture = strdupz(value);
  1160. }
  1161. else if(!strcmp(name, "NETDATA_SYSTEM_VIRTUALIZATION")){
  1162. freez(system_info->virtualization);
  1163. system_info->virtualization = strdupz(value);
  1164. }
  1165. else if(!strcmp(name, "NETDATA_SYSTEM_VIRT_DETECTION")){
  1166. freez(system_info->virt_detection);
  1167. system_info->virt_detection = strdupz(value);
  1168. }
  1169. else if(!strcmp(name, "NETDATA_SYSTEM_CONTAINER")){
  1170. freez(system_info->container);
  1171. system_info->container = strdupz(value);
  1172. }
  1173. else if(!strcmp(name, "NETDATA_SYSTEM_CONTAINER_DETECTION")){
  1174. freez(system_info->container_detection);
  1175. system_info->container_detection = strdupz(value);
  1176. }
  1177. else if (!strcmp(name, "NETDATA_SYSTEM_CPU_VENDOR"))
  1178. return res;
  1179. else if (!strcmp(name, "NETDATA_SYSTEM_CPU_MODEL"))
  1180. return res;
  1181. else if (!strcmp(name, "NETDATA_SYSTEM_CPU_DETECTION"))
  1182. return res;
  1183. else if (!strcmp(name, "NETDATA_SYSTEM_RAM_DETECTION"))
  1184. return res;
  1185. else if (!strcmp(name, "NETDATA_SYSTEM_DISK_DETECTION"))
  1186. return res;
  1187. else {
  1188. res = 1;
  1189. }
  1190. return res;
  1191. }
  1192. /**
  1193. * Alarm Compare ID
  1194. *
  1195. * Callback function used with the binary trees to compare the id of RRDCALC
  1196. *
  1197. * @param a a pointer to the RRDCAL item to insert,compare or update the binary tree
  1198. * @param b the pointer to the binary tree.
  1199. *
  1200. * @return It returns 0 case the values are equal, 1 case a is bigger than b and -1 case a is smaller than b.
  1201. */
  1202. int alarm_compare_id(void *a, void *b) {
  1203. register uint32_t hash1 = ((RRDCALC *)a)->id;
  1204. register uint32_t hash2 = ((RRDCALC *)b)->id;
  1205. if(hash1 < hash2) return -1;
  1206. else if(hash1 > hash2) return 1;
  1207. return 0;
  1208. }
  1209. /**
  1210. * Alarm Compare NAME
  1211. *
  1212. * Callback function used with the binary trees to compare the name of RRDCALC
  1213. *
  1214. * @param a a pointer to the RRDCAL item to insert,compare or update the binary tree
  1215. * @param b the pointer to the binary tree.
  1216. *
  1217. * @return It returns 0 case the values are equal, 1 case a is bigger than b and -1 case a is smaller than b.
  1218. */
  1219. int alarm_compare_name(void *a, void *b) {
  1220. RRDCALC *in1 = (RRDCALC *)a;
  1221. RRDCALC *in2 = (RRDCALC *)b;
  1222. if(in1->hash < in2->hash) return -1;
  1223. else if(in1->hash > in2->hash) return 1;
  1224. return strcmp(in1->name,in2->name);
  1225. }