1 #define NETDATA_RRD_INTERNALS 1
4 RRDHOST *localhost = NULL;
5 size_t rrd_hosts_available = 0;
6 pthread_rwlock_t rrd_rwlock = PTHREAD_RWLOCK_INITIALIZER;
8 time_t rrdset_free_obsolete_time = 3600;
9 time_t rrdhost_free_orphan_time = 3600;
11 // ----------------------------------------------------------------------------
14 int rrdhost_compare(void* a, void* b) {
15 if(((RRDHOST *)a)->hash_machine_guid < ((RRDHOST *)b)->hash_machine_guid) return -1;
16 else if(((RRDHOST *)a)->hash_machine_guid > ((RRDHOST *)b)->hash_machine_guid) return 1;
17 else return strcmp(((RRDHOST *)a)->machine_guid, ((RRDHOST *)b)->machine_guid);
20 avl_tree_lock rrdhost_root_index = {
21 .avl_tree = { NULL, rrdhost_compare },
22 .rwlock = AVL_LOCK_INITIALIZER
25 RRDHOST *rrdhost_find_by_guid(const char *guid, uint32_t hash) {
26 debug(D_RRDHOST, "Searching in index for host with guid '%s'", guid);
29 strncpyz(tmp.machine_guid, guid, GUID_LEN);
30 tmp.hash_machine_guid = (hash)?hash:simple_hash(tmp.machine_guid);
32 return (RRDHOST *)avl_search_lock(&(rrdhost_root_index), (avl *) &tmp);
35 RRDHOST *rrdhost_find_by_hostname(const char *hostname, uint32_t hash) {
36 if(unlikely(!strcmp(hostname, "localhost")))
39 if(unlikely(!hash)) hash = simple_hash(hostname);
43 rrdhost_foreach_read(host) {
44 if(unlikely((hash == host->hash_hostname && !strcmp(hostname, host->hostname)))) {
54 #define rrdhost_index_add(rrdhost) (RRDHOST *)avl_insert_lock(&(rrdhost_root_index), (avl *)(rrdhost))
55 #define rrdhost_index_del(rrdhost) (RRDHOST *)avl_remove_lock(&(rrdhost_root_index), (avl *)(rrdhost))
58 // ----------------------------------------------------------------------------
59 // RRDHOST - internal helpers
61 static inline void rrdhost_init_hostname(RRDHOST *host, const char *hostname) {
62 freez(host->hostname);
63 host->hostname = strdupz(hostname);
64 host->hash_hostname = simple_hash(host->hostname);
67 static inline void rrdhost_init_os(RRDHOST *host, const char *os) {
69 host->os = strdupz(os?os:"unknown");
72 static inline void rrdhost_init_machine_guid(RRDHOST *host, const char *machine_guid) {
73 strncpy(host->machine_guid, machine_guid, GUID_LEN);
74 host->machine_guid[GUID_LEN] = '\0';
75 host->hash_machine_guid = simple_hash(host->machine_guid);
79 // ----------------------------------------------------------------------------
80 // RRDHOST - add a host
82 RRDHOST *rrdhost_create(const char *hostname,
87 RRD_MEMORY_MODE memory_mode,
90 char *rrdpush_destination,
91 char *rrdpush_api_key,
95 debug(D_RRDHOST, "Host '%s': adding with guid '%s'", hostname, guid);
97 RRDHOST *host = callocz(1, sizeof(RRDHOST));
99 host->rrd_update_every = update_every;
100 host->rrd_history_entries = entries;
101 host->rrd_memory_mode = memory_mode;
102 host->health_enabled = (memory_mode == RRD_MEMORY_MODE_NONE)? 0 : health_enabled;
103 host->rrdpush_enabled = (rrdpush_enabled && rrdpush_destination && *rrdpush_destination && rrdpush_api_key && *rrdpush_api_key);
104 host->rrdpush_destination = (host->rrdpush_enabled)?strdupz(rrdpush_destination):NULL;
105 host->rrdpush_api_key = (host->rrdpush_enabled)?strdupz(rrdpush_api_key):NULL;
107 host->rrdpush_pipe[0] = -1;
108 host->rrdpush_pipe[1] = -1;
109 host->rrdpush_socket = -1;
111 pthread_mutex_init(&host->rrdpush_mutex, NULL);
112 pthread_rwlock_init(&host->rrdhost_rwlock, NULL);
114 rrdhost_init_hostname(host, hostname);
115 rrdhost_init_machine_guid(host, guid);
116 rrdhost_init_os(host, os);
118 avl_init_lock(&(host->rrdset_root_index), rrdset_compare);
119 avl_init_lock(&(host->rrdset_root_index_name), rrdset_compare_name);
120 avl_init_lock(&(host->rrdfamily_root_index), rrdfamily_compare);
121 avl_init_lock(&(host->variables_root_index), rrdvar_compare);
123 if(config_get_boolean(CONFIG_SECTION_GLOBAL, "delete obsolete charts files", 1))
124 rrdhost_flag_set(host, RRDHOST_DELETE_OBSOLETE_FILES);
126 if(config_get_boolean(CONFIG_SECTION_GLOBAL, "delete orphan hosts files", 1) && !is_localhost)
127 rrdhost_flag_set(host, RRDHOST_DELETE_ORPHAN_FILES);
130 // ------------------------------------------------------------------------
131 // initialize health variables
133 host->health_log.next_log_id = 1;
134 host->health_log.next_alarm_id = 1;
135 host->health_log.max = 1000;
136 host->health_log.next_log_id =
137 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);
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);
145 host->health_log.max = (unsigned int)n;
147 pthread_rwlock_init(&(host->health_log.alarm_log_rwlock), NULL);
149 char filename[FILENAME_MAX + 1];
153 host->cache_dir = strdupz(netdata_configured_cache_dir);
154 host->varlib_dir = strdupz(netdata_configured_varlib_dir);
158 // this is not localhost - append our GUID to localhost path
160 snprintfz(filename, FILENAME_MAX, "%s/%s", netdata_configured_cache_dir, host->machine_guid);
161 host->cache_dir = strdupz(filename);
163 if(host->rrd_memory_mode == RRD_MEMORY_MODE_MAP || host->rrd_memory_mode == RRD_MEMORY_MODE_SAVE) {
164 int r = mkdir(host->cache_dir, 0775);
165 if(r != 0 && errno != EEXIST)
166 error("Host '%s': cannot create directory '%s'", host->hostname, host->cache_dir);
169 snprintfz(filename, FILENAME_MAX, "%s/%s", netdata_configured_varlib_dir, host->machine_guid);
170 host->varlib_dir = strdupz(filename);
172 if(host->health_enabled) {
173 int r = mkdir(host->varlib_dir, 0775);
174 if(r != 0 && errno != EEXIST)
175 error("Host '%s': cannot create directory '%s'", host->hostname, host->varlib_dir);
180 if(host->health_enabled) {
181 snprintfz(filename, FILENAME_MAX, "%s/health", host->varlib_dir);
182 int r = mkdir(filename, 0775);
183 if(r != 0 && errno != EEXIST)
184 error("Host '%s': cannot create directory '%s'", host->hostname, filename);
187 snprintfz(filename, FILENAME_MAX, "%s/health/health-log.db", host->varlib_dir);
188 host->health_log_filename = strdupz(filename);
190 snprintfz(filename, FILENAME_MAX, "%s/alarm-notify.sh", netdata_configured_plugins_dir);
191 host->health_default_exec = strdupz(config_get(CONFIG_SECTION_HEALTH, "script to execute on alarm", filename));
192 host->health_default_recipient = strdup("root");
195 // ------------------------------------------------------------------------
196 // load health configuration
198 if(host->health_enabled) {
199 health_alarm_log_load(host);
200 health_alarm_log_open(host);
202 rrdhost_wrlock(host);
203 health_readdir(host, health_config_dir());
204 rrdhost_unlock(host);
208 // ------------------------------------------------------------------------
209 // link it and add it to the index
214 host->next = localhost;
219 host->next = localhost->next;
220 localhost->next = host;
222 else localhost = host;
225 RRDHOST *t = rrdhost_index_add(host);
228 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);
233 info("Host '%s' with guid '%s' initialized"
237 ", history entries %d"
239 " (to '%s' with api key '%s')"
244 ", alarms default handler '%s'"
245 ", alarms default recipient '%s'"
249 , host->rrd_update_every
250 , rrd_memory_mode_name(host->rrd_memory_mode)
251 , host->rrd_history_entries
252 , host->rrdpush_enabled?"enabled":"disabled"
253 , host->rrdpush_destination?host->rrdpush_destination:""
254 , host->rrdpush_api_key?host->rrdpush_api_key:""
255 , host->health_enabled?"enabled":"disabled"
258 , host->health_log_filename
259 , host->health_default_exec
260 , host->health_default_recipient
264 rrd_hosts_available++;
270 RRDHOST *rrdhost_find_or_create(
276 , RRD_MEMORY_MODE mode
278 , int rrdpush_enabled
279 , char *rrdpush_destination
280 , char *rrdpush_api_key
282 debug(D_RRDHOST, "Searching for host '%s' with guid '%s'", hostname, guid);
284 RRDHOST *host = rrdhost_find_by_guid(guid, 0);
286 host = rrdhost_create(
295 , rrdpush_destination
301 host->health_enabled = health_enabled;
303 if(strcmp(host->hostname, hostname)) {
304 char *t = host->hostname;
305 char *n = strdupz(hostname);
310 if(host->rrd_update_every != update_every)
311 error("Host '%s' has an update frequency of %d seconds, but the wanted one is %d seconds.", host->hostname, host->rrd_update_every, update_every);
313 if(host->rrd_history_entries != history)
314 error("Host '%s' has history of %d entries, but the wanted one is %d entries.", host->hostname, host->rrd_history_entries, history);
316 if(host->rrd_memory_mode != mode)
317 error("Host '%s' has memory mode '%s', but the wanted one is '%s'.", host->hostname, rrd_memory_mode_name(host->rrd_memory_mode), rrd_memory_mode_name(mode));
320 rrdhost_cleanup_orphan(host);
325 void rrdhost_cleanup_orphan(RRDHOST *protected) {
326 time_t now = now_realtime_sec();
332 restart_after_removal:
333 rrdhost_foreach_write(host) {
336 && !host->connected_senders
337 && host->senders_disconnected_time + rrdhost_free_orphan_time < now) {
338 info("Host '%s' with machine guid '%s' is obsolete - cleaning up.", host->hostname, host->machine_guid);
340 if(rrdset_flag_check(host, RRDHOST_ORPHAN))
341 rrdhost_delete(host);
346 goto restart_after_removal;
353 // ----------------------------------------------------------------------------
354 // RRDHOST global / startup initialization
356 void rrd_init(char *hostname) {
357 rrdset_free_obsolete_time = config_get_number(CONFIG_SECTION_GLOBAL, "cleanup obsolete charts after seconds", rrdset_free_obsolete_time);
363 debug(D_RRDHOST, "Initializing localhost with hostname '%s'", hostname);
364 localhost = rrdhost_create(
366 , registry_get_this_machine_guid()
368 , default_rrd_update_every
369 , default_rrd_history_entries
370 , default_rrd_memory_mode
371 , default_health_enabled
372 , default_rrdpush_enabled
373 , default_rrdpush_destination
374 , default_rrdpush_api_key
379 // ----------------------------------------------------------------------------
380 // RRDHOST - lock validations
381 // there are only used when NETDATA_INTERNAL_CHECKS is set
383 void rrdhost_check_rdlock_int(RRDHOST *host, const char *file, const char *function, const unsigned long line) {
384 debug(D_RRDHOST, "Checking read lock on host '%s'", host->hostname);
386 int ret = pthread_rwlock_trywrlock(&host->rrdhost_rwlock);
388 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);
391 void rrdhost_check_wrlock_int(RRDHOST *host, const char *file, const char *function, const unsigned long line) {
392 debug(D_RRDHOST, "Checking write lock on host '%s'", host->hostname);
394 int ret = pthread_rwlock_tryrdlock(&host->rrdhost_rwlock);
396 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);
399 void rrd_check_rdlock_int(const char *file, const char *function, const unsigned long line) {
400 debug(D_RRDHOST, "Checking read lock on all RRDs");
402 int ret = pthread_rwlock_trywrlock(&rrd_rwlock);
404 fatal("RRDs should be read-locked, but it are not, at function %s() at line %lu of file '%s'", function, line, file);
407 void rrd_check_wrlock_int(const char *file, const char *function, const unsigned long line) {
408 debug(D_RRDHOST, "Checking write lock on all RRDs");
410 int ret = pthread_rwlock_tryrdlock(&rrd_rwlock);
412 fatal("RRDs should be write-locked, but it are not, at function %s() at line %lu of file '%s'", function, line, file);
415 // ----------------------------------------------------------------------------
418 void rrdhost_free(RRDHOST *host) {
421 info("Freeing all memory for host '%s'...", host->hostname);
423 rrd_check_wrlock(); // make sure the RRDs are write locked
425 // stop a possibly running thread
426 rrdpush_sender_thread_stop(host);
428 rrdhost_wrlock(host); // lock this RRDHOST
430 // ------------------------------------------------------------------------
431 // release its children resources
433 while(host->rrdset_root) rrdset_free(host->rrdset_root);
435 while(host->alarms) rrdcalc_free(host, host->alarms);
436 while(host->templates) rrdcalctemplate_free(host, host->templates);
437 health_alarm_log_free(host);
440 // ------------------------------------------------------------------------
441 // remove it from the indexes
443 if(rrdhost_index_del(host) != host)
444 error("RRDHOST '%s' removed from index, deleted the wrong entry.", host->hostname);
447 // ------------------------------------------------------------------------
448 // unlink it from the host
450 if(host == localhost) {
451 localhost = host->next;
454 // find the previous one
456 for(h = localhost; h && h->next != host ; h = h->next) ;
459 if(h) h->next = host->next;
460 else error("Request to free RRDHOST '%s': cannot find it", host->hostname);
463 // ------------------------------------------------------------------------
467 freez(host->cache_dir);
468 freez(host->varlib_dir);
469 freez(host->rrdpush_api_key);
470 freez(host->rrdpush_destination);
471 freez(host->health_default_exec);
472 freez(host->health_default_recipient);
473 freez(host->health_log_filename);
474 freez(host->hostname);
475 rrdhost_unlock(host);
478 rrd_hosts_available--;
481 void rrdhost_free_all(void) {
483 while(localhost) rrdhost_free(localhost);
487 // ----------------------------------------------------------------------------
490 void rrdhost_save(RRDHOST *host) {
493 info("Saving database of host '%s'...", host->hostname);
497 // we get a write lock
498 // to ensure only one thread is saving the database
499 rrdhost_wrlock(host);
501 rrdset_foreach_write(st, host) {
507 rrdhost_unlock(host);
510 // ----------------------------------------------------------------------------
511 // RRDHOST - delete files
513 void rrdhost_delete(RRDHOST *host) {
516 info("Deleting database of host '%s'...", host->hostname);
520 // we get a write lock
521 // to ensure only one thread is saving the database
522 rrdhost_wrlock(host);
524 rrdset_foreach_write(st, host) {
530 rrdhost_unlock(host);
533 void rrdhost_save_all(void) {
534 info("Saving database [%zu hosts(s)]...", rrd_hosts_available);
539 rrdhost_foreach_read(host)
545 void rrdhost_cleanup_obsolete(RRDHOST *host) {
546 time_t now = now_realtime_sec();
550 restart_after_removal:
551 rrdset_foreach_write(st, host) {
552 if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_OBSOLETE)
553 && st->last_accessed_time + rrdset_free_obsolete_time < now
554 && st->last_updated.tv_sec + rrdset_free_obsolete_time < now
555 && st->last_collected_time.tv_sec + rrdset_free_obsolete_time < now
560 if(rrdhost_flag_check(host, RRDHOST_DELETE_OBSOLETE_FILES))
568 goto restart_after_removal;