rrdhost.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  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. int is_localhost
  99. ) {
  100. debug(D_RRDHOST, "Host '%s': adding with guid '%s'", hostname, guid);
  101. rrd_check_wrlock();
  102. RRDHOST *host = callocz(1, sizeof(RRDHOST));
  103. host->rrd_update_every = (update_every > 0)?update_every:1;
  104. host->rrd_history_entries = align_entries_to_pagesize(memory_mode, entries);
  105. host->rrd_memory_mode = memory_mode;
  106. host->health_enabled = (memory_mode == RRD_MEMORY_MODE_NONE)? 0 : health_enabled;
  107. host->rrdpush_send_enabled = (rrdpush_enabled && rrdpush_destination && *rrdpush_destination && rrdpush_api_key && *rrdpush_api_key) ? 1 : 0;
  108. host->rrdpush_send_destination = (host->rrdpush_send_enabled)?strdupz(rrdpush_destination):NULL;
  109. host->rrdpush_send_api_key = (host->rrdpush_send_enabled)?strdupz(rrdpush_api_key):NULL;
  110. host->rrdpush_send_charts_matching = simple_pattern_create(rrdpush_send_charts_matching, NULL, SIMPLE_PATTERN_EXACT);
  111. host->rrdpush_sender_pipe[0] = -1;
  112. host->rrdpush_sender_pipe[1] = -1;
  113. host->rrdpush_sender_socket = -1;
  114. netdata_mutex_init(&host->rrdpush_sender_buffer_mutex);
  115. netdata_rwlock_init(&host->rrdhost_rwlock);
  116. rrdhost_init_hostname(host, hostname);
  117. rrdhost_init_machine_guid(host, guid);
  118. rrdhost_init_os(host, os);
  119. rrdhost_init_timezone(host, timezone);
  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:hostname);
  124. avl_init_lock(&(host->rrdset_root_index), rrdset_compare);
  125. avl_init_lock(&(host->rrdset_root_index_name), rrdset_compare_name);
  126. avl_init_lock(&(host->rrdfamily_root_index), rrdfamily_compare);
  127. avl_init_lock(&(host->rrdvar_root_index), rrdvar_compare);
  128. if(config_get_boolean(CONFIG_SECTION_GLOBAL, "delete obsolete charts files", 1))
  129. rrdhost_flag_set(host, RRDHOST_FLAG_DELETE_OBSOLETE_CHARTS);
  130. if(config_get_boolean(CONFIG_SECTION_GLOBAL, "delete orphan hosts files", 1) && !is_localhost)
  131. rrdhost_flag_set(host, RRDHOST_FLAG_DELETE_ORPHAN_HOST);
  132. // ------------------------------------------------------------------------
  133. // initialize health variables
  134. host->health_log.next_log_id = 1;
  135. host->health_log.next_alarm_id = 1;
  136. host->health_log.max = 1000;
  137. host->health_log.next_log_id =
  138. host->health_log.next_alarm_id = (uint32_t)now_realtime_sec();
  139. long n = config_get_number(CONFIG_SECTION_HEALTH, "in memory max health log entries", host->health_log.max);
  140. if(n < 10) {
  141. error("Host '%s': health configuration has invalid max log entries %ld. Using default %u", host->hostname, n, host->health_log.max);
  142. config_set_number(CONFIG_SECTION_HEALTH, "in memory max health log entries", (long)host->health_log.max);
  143. }
  144. else
  145. host->health_log.max = (unsigned int)n;
  146. netdata_rwlock_init(&host->health_log.alarm_log_rwlock);
  147. char filename[FILENAME_MAX + 1];
  148. if(is_localhost) {
  149. host->cache_dir = strdupz(netdata_configured_cache_dir);
  150. host->varlib_dir = strdupz(netdata_configured_varlib_dir);
  151. }
  152. else {
  153. // this is not localhost - append our GUID to localhost path
  154. snprintfz(filename, FILENAME_MAX, "%s/%s", netdata_configured_cache_dir, host->machine_guid);
  155. host->cache_dir = strdupz(filename);
  156. if(host->rrd_memory_mode == RRD_MEMORY_MODE_MAP || host->rrd_memory_mode == RRD_MEMORY_MODE_SAVE) {
  157. int r = mkdir(host->cache_dir, 0775);
  158. if(r != 0 && errno != EEXIST)
  159. error("Host '%s': cannot create directory '%s'", host->hostname, host->cache_dir);
  160. }
  161. snprintfz(filename, FILENAME_MAX, "%s/%s", netdata_configured_varlib_dir, host->machine_guid);
  162. host->varlib_dir = strdupz(filename);
  163. if(host->health_enabled) {
  164. int r = mkdir(host->varlib_dir, 0775);
  165. if(r != 0 && errno != EEXIST)
  166. error("Host '%s': cannot create directory '%s'", host->hostname, host->varlib_dir);
  167. }
  168. }
  169. if(host->health_enabled) {
  170. snprintfz(filename, FILENAME_MAX, "%s/health", host->varlib_dir);
  171. int r = mkdir(filename, 0775);
  172. if(r != 0 && errno != EEXIST)
  173. error("Host '%s': cannot create directory '%s'", host->hostname, filename);
  174. }
  175. snprintfz(filename, FILENAME_MAX, "%s/health/health-log.db", host->varlib_dir);
  176. host->health_log_filename = strdupz(filename);
  177. snprintfz(filename, FILENAME_MAX, "%s/alarm-notify.sh", netdata_configured_primary_plugins_dir);
  178. host->health_default_exec = strdupz(config_get(CONFIG_SECTION_HEALTH, "script to execute on alarm", filename));
  179. host->health_default_recipient = strdupz("root");
  180. // ------------------------------------------------------------------------
  181. // load health configuration
  182. if(host->health_enabled) {
  183. health_alarm_log_load(host);
  184. health_alarm_log_open(host);
  185. rrdhost_wrlock(host);
  186. health_readdir(host, health_user_config_dir(), health_stock_config_dir(), NULL);
  187. rrdhost_unlock(host);
  188. }
  189. // ------------------------------------------------------------------------
  190. // link it and add it to the index
  191. if(is_localhost) {
  192. host->next = localhost;
  193. localhost = host;
  194. }
  195. else {
  196. if(localhost) {
  197. host->next = localhost->next;
  198. localhost->next = host;
  199. }
  200. else localhost = host;
  201. }
  202. RRDHOST *t = rrdhost_index_add(host);
  203. if(t != host) {
  204. 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);
  205. rrdhost_free(host);
  206. host = NULL;
  207. }
  208. else {
  209. info("Host '%s' (at registry as '%s') with guid '%s' initialized"
  210. ", os '%s'"
  211. ", timezone '%s'"
  212. ", tags '%s'"
  213. ", program_name '%s'"
  214. ", program_version '%s'"
  215. ", update every %d"
  216. ", memory mode %s"
  217. ", history entries %ld"
  218. ", streaming %s"
  219. " (to '%s' with api key '%s')"
  220. ", health %s"
  221. ", cache_dir '%s'"
  222. ", varlib_dir '%s'"
  223. ", health_log '%s'"
  224. ", alarms default handler '%s'"
  225. ", alarms default recipient '%s'"
  226. , host->hostname
  227. , host->registry_hostname
  228. , host->machine_guid
  229. , host->os
  230. , host->timezone
  231. , (host->tags)?host->tags:""
  232. , host->program_name
  233. , host->program_version
  234. , host->rrd_update_every
  235. , rrd_memory_mode_name(host->rrd_memory_mode)
  236. , host->rrd_history_entries
  237. , host->rrdpush_send_enabled?"enabled":"disabled"
  238. , host->rrdpush_send_destination?host->rrdpush_send_destination:""
  239. , host->rrdpush_send_api_key?host->rrdpush_send_api_key:""
  240. , host->health_enabled?"enabled":"disabled"
  241. , host->cache_dir
  242. , host->varlib_dir
  243. , host->health_log_filename
  244. , host->health_default_exec
  245. , host->health_default_recipient
  246. );
  247. }
  248. rrd_hosts_available++;
  249. return host;
  250. }
  251. RRDHOST *rrdhost_find_or_create(
  252. const char *hostname
  253. , const char *registry_hostname
  254. , const char *guid
  255. , const char *os
  256. , const char *timezone
  257. , const char *tags
  258. , const char *program_name
  259. , const char *program_version
  260. , int update_every
  261. , long history
  262. , RRD_MEMORY_MODE mode
  263. , unsigned int health_enabled
  264. , unsigned int rrdpush_enabled
  265. , char *rrdpush_destination
  266. , char *rrdpush_api_key
  267. , char *rrdpush_send_charts_matching
  268. ) {
  269. debug(D_RRDHOST, "Searching for host '%s' with guid '%s'", hostname, guid);
  270. rrd_wrlock();
  271. RRDHOST *host = rrdhost_find_by_guid(guid, 0);
  272. if(!host) {
  273. host = rrdhost_create(
  274. hostname
  275. , registry_hostname
  276. , guid
  277. , os
  278. , timezone
  279. , tags
  280. , program_name
  281. , program_version
  282. , update_every
  283. , history
  284. , mode
  285. , health_enabled
  286. , rrdpush_enabled
  287. , rrdpush_destination
  288. , rrdpush_api_key
  289. , rrdpush_send_charts_matching
  290. , 0
  291. );
  292. }
  293. else {
  294. host->health_enabled = health_enabled;
  295. if(strcmp(host->hostname, hostname) != 0) {
  296. 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);
  297. char *t = host->hostname;
  298. host->hostname = strdupz(hostname);
  299. host->hash_hostname = simple_hash(host->hostname);
  300. freez(t);
  301. }
  302. if(strcmp(host->program_name, program_name) != 0) {
  303. info("Host '%s' switched program name from '%s' to '%s'", host->hostname, host->program_name, program_name);
  304. char *t = host->program_name;
  305. host->program_name = strdupz(program_name);
  306. freez(t);
  307. }
  308. if(strcmp(host->program_version, program_version) != 0) {
  309. info("Host '%s' switched program version from '%s' to '%s'", host->hostname, host->program_version, program_version);
  310. char *t = host->program_version;
  311. host->program_version = strdupz(program_version);
  312. freez(t);
  313. }
  314. if(host->rrd_update_every != update_every)
  315. 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);
  316. if(host->rrd_history_entries < history)
  317. 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);
  318. if(host->rrd_memory_mode != mode)
  319. 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));
  320. // update host tags
  321. rrdhost_init_tags(host, tags);
  322. }
  323. rrdhost_cleanup_orphan_hosts_nolock(host);
  324. rrd_unlock();
  325. return host;
  326. }
  327. inline int rrdhost_should_be_removed(RRDHOST *host, RRDHOST *protected, time_t now) {
  328. if(host != protected
  329. && host != localhost
  330. && rrdhost_flag_check(host, RRDHOST_FLAG_ORPHAN)
  331. && !host->connected_senders
  332. && host->senders_disconnected_time
  333. && host->senders_disconnected_time + rrdhost_free_orphan_time < now)
  334. return 1;
  335. return 0;
  336. }
  337. void rrdhost_cleanup_orphan_hosts_nolock(RRDHOST *protected) {
  338. time_t now = now_realtime_sec();
  339. RRDHOST *host;
  340. restart_after_removal:
  341. rrdhost_foreach_write(host) {
  342. if(rrdhost_should_be_removed(host, protected, now)) {
  343. info("Host '%s' with machine guid '%s' is obsolete - cleaning up.", host->hostname, host->machine_guid);
  344. if(rrdhost_flag_check(host, RRDHOST_FLAG_DELETE_ORPHAN_HOST))
  345. rrdhost_delete_charts(host);
  346. else
  347. rrdhost_save_charts(host);
  348. rrdhost_free(host);
  349. goto restart_after_removal;
  350. }
  351. }
  352. }
  353. // ----------------------------------------------------------------------------
  354. // RRDHOST global / startup initialization
  355. void rrd_init(char *hostname) {
  356. rrdset_free_obsolete_time = config_get_number(CONFIG_SECTION_GLOBAL, "cleanup obsolete charts after seconds", rrdset_free_obsolete_time);
  357. gap_when_lost_iterations_above = (int)config_get_number(CONFIG_SECTION_GLOBAL, "gap when lost iterations above", gap_when_lost_iterations_above);
  358. if (gap_when_lost_iterations_above < 1)
  359. gap_when_lost_iterations_above = 1;
  360. health_init();
  361. registry_init();
  362. rrdpush_init();
  363. debug(D_RRDHOST, "Initializing localhost with hostname '%s'", hostname);
  364. rrd_wrlock();
  365. localhost = rrdhost_create(
  366. hostname
  367. , registry_get_this_machine_hostname()
  368. , registry_get_this_machine_guid()
  369. , os_type
  370. , netdata_configured_timezone
  371. , config_get(CONFIG_SECTION_BACKEND, "host tags", "")
  372. , program_name
  373. , program_version
  374. , default_rrd_update_every
  375. , default_rrd_history_entries
  376. , default_rrd_memory_mode
  377. , default_health_enabled
  378. , default_rrdpush_enabled
  379. , default_rrdpush_destination
  380. , default_rrdpush_api_key
  381. , default_rrdpush_send_charts_matching
  382. , 1
  383. );
  384. rrd_unlock();
  385. web_client_api_v1_management_init();
  386. }
  387. // ----------------------------------------------------------------------------
  388. // RRDHOST - lock validations
  389. // there are only used when NETDATA_INTERNAL_CHECKS is set
  390. void __rrdhost_check_rdlock(RRDHOST *host, const char *file, const char *function, const unsigned long line) {
  391. debug(D_RRDHOST, "Checking read lock on host '%s'", host->hostname);
  392. int ret = netdata_rwlock_trywrlock(&host->rrdhost_rwlock);
  393. if(ret == 0)
  394. 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);
  395. }
  396. void __rrdhost_check_wrlock(RRDHOST *host, const char *file, const char *function, const unsigned long line) {
  397. debug(D_RRDHOST, "Checking write lock on host '%s'", host->hostname);
  398. int ret = netdata_rwlock_tryrdlock(&host->rrdhost_rwlock);
  399. if(ret == 0)
  400. 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);
  401. }
  402. void __rrd_check_rdlock(const char *file, const char *function, const unsigned long line) {
  403. debug(D_RRDHOST, "Checking read lock on all RRDs");
  404. int ret = netdata_rwlock_trywrlock(&rrd_rwlock);
  405. if(ret == 0)
  406. fatal("RRDs should be read-locked, but it are not, at function %s() at line %lu of file '%s'", function, line, file);
  407. }
  408. void __rrd_check_wrlock(const char *file, const char *function, const unsigned long line) {
  409. debug(D_RRDHOST, "Checking write lock on all RRDs");
  410. int ret = netdata_rwlock_tryrdlock(&rrd_rwlock);
  411. if(ret == 0)
  412. fatal("RRDs should be write-locked, but it are not, at function %s() at line %lu of file '%s'", function, line, file);
  413. }
  414. // ----------------------------------------------------------------------------
  415. // RRDHOST - free
  416. void rrdhost_free(RRDHOST *host) {
  417. if(!host) return;
  418. info("Freeing all memory for host '%s'...", host->hostname);
  419. rrd_check_wrlock(); // make sure the RRDs are write locked
  420. // stop a possibly running thread
  421. rrdpush_sender_thread_stop(host);
  422. rrdhost_wrlock(host); // lock this RRDHOST
  423. // ------------------------------------------------------------------------
  424. // release its children resources
  425. while(host->rrdset_root)
  426. rrdset_free(host->rrdset_root);
  427. while(host->alarms)
  428. rrdcalc_unlink_and_free(host, host->alarms);
  429. while(host->templates)
  430. rrdcalctemplate_unlink_and_free(host, host->templates);
  431. debug(D_RRD_CALLS, "RRDHOST: Cleaning up remaining host variables for host '%s'", host->hostname);
  432. rrdvar_free_remaining_variables(host, &host->rrdvar_root_index);
  433. health_alarm_log_free(host);
  434. // ------------------------------------------------------------------------
  435. // remove it from the indexes
  436. if(rrdhost_index_del(host) != host)
  437. error("RRDHOST '%s' removed from index, deleted the wrong entry.", host->hostname);
  438. // ------------------------------------------------------------------------
  439. // unlink it from the host
  440. if(host == localhost) {
  441. localhost = host->next;
  442. }
  443. else {
  444. // find the previous one
  445. RRDHOST *h;
  446. for(h = localhost; h && h->next != host ; h = h->next) ;
  447. // bypass it
  448. if(h) h->next = host->next;
  449. else error("Request to free RRDHOST '%s': cannot find it", host->hostname);
  450. }
  451. // ------------------------------------------------------------------------
  452. // free it
  453. freez((void *)host->tags);
  454. freez((void *)host->os);
  455. freez((void *)host->timezone);
  456. freez(host->program_version);
  457. freez(host->program_name);
  458. freez(host->cache_dir);
  459. freez(host->varlib_dir);
  460. freez(host->rrdpush_send_api_key);
  461. freez(host->rrdpush_send_destination);
  462. freez(host->health_default_exec);
  463. freez(host->health_default_recipient);
  464. freez(host->health_log_filename);
  465. freez(host->hostname);
  466. freez(host->registry_hostname);
  467. simple_pattern_free(host->rrdpush_send_charts_matching);
  468. rrdhost_unlock(host);
  469. netdata_rwlock_destroy(&host->health_log.alarm_log_rwlock);
  470. netdata_rwlock_destroy(&host->rrdhost_rwlock);
  471. freez(host);
  472. rrd_hosts_available--;
  473. }
  474. void rrdhost_free_all(void) {
  475. rrd_wrlock();
  476. while(localhost) rrdhost_free(localhost);
  477. rrd_unlock();
  478. }
  479. // ----------------------------------------------------------------------------
  480. // RRDHOST - save host files
  481. void rrdhost_save_charts(RRDHOST *host) {
  482. if(!host) return;
  483. info("Saving/Closing database of host '%s'...", host->hostname);
  484. RRDSET *st;
  485. // we get a write lock
  486. // to ensure only one thread is saving the database
  487. rrdhost_wrlock(host);
  488. rrdset_foreach_write(st, host) {
  489. rrdset_rdlock(st);
  490. rrdset_save(st);
  491. rrdset_unlock(st);
  492. }
  493. rrdhost_unlock(host);
  494. }
  495. // ----------------------------------------------------------------------------
  496. // RRDHOST - delete host files
  497. void rrdhost_delete_charts(RRDHOST *host) {
  498. if(!host) return;
  499. info("Deleting database of host '%s'...", host->hostname);
  500. RRDSET *st;
  501. // we get a write lock
  502. // to ensure only one thread is saving the database
  503. rrdhost_wrlock(host);
  504. rrdset_foreach_write(st, host) {
  505. rrdset_rdlock(st);
  506. rrdset_delete(st);
  507. rrdset_unlock(st);
  508. }
  509. recursively_delete_dir(host->cache_dir, "left over host");
  510. rrdhost_unlock(host);
  511. }
  512. // ----------------------------------------------------------------------------
  513. // RRDHOST - cleanup host files
  514. void rrdhost_cleanup_charts(RRDHOST *host) {
  515. if(!host) return;
  516. info("Cleaning up database of host '%s'...", host->hostname);
  517. RRDSET *st;
  518. uint32_t rrdhost_delete_obsolete_charts = rrdhost_flag_check(host, RRDHOST_FLAG_DELETE_OBSOLETE_CHARTS);
  519. // we get a write lock
  520. // to ensure only one thread is saving the database
  521. rrdhost_wrlock(host);
  522. rrdset_foreach_write(st, host) {
  523. rrdset_rdlock(st);
  524. if(rrdhost_delete_obsolete_charts && rrdset_flag_check(st, RRDSET_FLAG_OBSOLETE))
  525. rrdset_delete(st);
  526. else if(rrdhost_delete_obsolete_charts && rrdset_flag_check(st, RRDSET_FLAG_OBSOLETE_DIMENSIONS))
  527. rrdset_delete_obsolete_dimensions(st);
  528. else
  529. rrdset_save(st);
  530. rrdset_unlock(st);
  531. }
  532. rrdhost_unlock(host);
  533. }
  534. // ----------------------------------------------------------------------------
  535. // RRDHOST - save all hosts to disk
  536. void rrdhost_save_all(void) {
  537. info("Saving database [%zu hosts(s)]...", rrd_hosts_available);
  538. rrd_rdlock();
  539. RRDHOST *host;
  540. rrdhost_foreach_read(host)
  541. rrdhost_save_charts(host);
  542. rrd_unlock();
  543. }
  544. // ----------------------------------------------------------------------------
  545. // RRDHOST - save or delete all hosts from disk
  546. void rrdhost_cleanup_all(void) {
  547. info("Cleaning up database [%zu hosts(s)]...", rrd_hosts_available);
  548. rrd_rdlock();
  549. RRDHOST *host;
  550. rrdhost_foreach_read(host) {
  551. if(host != localhost && rrdhost_flag_check(host, RRDHOST_FLAG_DELETE_OBSOLETE_CHARTS) && !host->connected_senders)
  552. rrdhost_delete_charts(host);
  553. else
  554. rrdhost_cleanup_charts(host);
  555. }
  556. rrd_unlock();
  557. }
  558. // ----------------------------------------------------------------------------
  559. // RRDHOST - save or delete all the host charts from disk
  560. void rrdhost_cleanup_obsolete_charts(RRDHOST *host) {
  561. time_t now = now_realtime_sec();
  562. RRDSET *st;
  563. uint32_t rrdhost_delete_obsolete_charts = rrdhost_flag_check(host, RRDHOST_FLAG_DELETE_OBSOLETE_CHARTS);
  564. restart_after_removal:
  565. rrdset_foreach_write(st, host) {
  566. if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_OBSOLETE)
  567. && st->last_accessed_time + rrdset_free_obsolete_time < now
  568. && st->last_updated.tv_sec + rrdset_free_obsolete_time < now
  569. && st->last_collected_time.tv_sec + rrdset_free_obsolete_time < now
  570. )) {
  571. rrdset_rdlock(st);
  572. if(rrdhost_delete_obsolete_charts)
  573. rrdset_delete(st);
  574. else
  575. rrdset_save(st);
  576. rrdset_unlock(st);
  577. rrdset_free(st);
  578. goto restart_after_removal;
  579. }
  580. }
  581. }