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 rrdhost_free_orphan_time = 3600;
10 // ----------------------------------------------------------------------------
13 int rrdhost_compare(void* a, void* b) {
14 if(((RRDHOST *)a)->hash_machine_guid < ((RRDHOST *)b)->hash_machine_guid) return -1;
15 else if(((RRDHOST *)a)->hash_machine_guid > ((RRDHOST *)b)->hash_machine_guid) return 1;
16 else return strcmp(((RRDHOST *)a)->machine_guid, ((RRDHOST *)b)->machine_guid);
19 avl_tree_lock rrdhost_root_index = {
20 .avl_tree = { NULL, rrdhost_compare },
21 .rwlock = AVL_LOCK_INITIALIZER
24 RRDHOST *rrdhost_find_by_guid(const char *guid, uint32_t hash) {
25 debug(D_RRDHOST, "Searching in index for host with guid '%s'", guid);
28 strncpyz(tmp.machine_guid, guid, GUID_LEN);
29 tmp.hash_machine_guid = (hash)?hash:simple_hash(tmp.machine_guid);
31 return (RRDHOST *)avl_search_lock(&(rrdhost_root_index), (avl *) &tmp);
34 RRDHOST *rrdhost_find_by_hostname(const char *hostname, uint32_t hash) {
35 if(unlikely(!strcmp(hostname, "localhost")))
38 if(unlikely(!hash)) hash = simple_hash(hostname);
42 rrdhost_foreach_read(host) {
43 if(unlikely((hash == host->hash_hostname && !strcmp(hostname, host->hostname)))) {
53 #define rrdhost_index_add(rrdhost) (RRDHOST *)avl_insert_lock(&(rrdhost_root_index), (avl *)(rrdhost))
54 #define rrdhost_index_del(rrdhost) (RRDHOST *)avl_remove_lock(&(rrdhost_root_index), (avl *)(rrdhost))
57 // ----------------------------------------------------------------------------
58 // RRDHOST - internal helpers
60 static inline void rrdhost_init_hostname(RRDHOST *host, const char *hostname) {
61 freez(host->hostname);
62 host->hostname = strdupz(hostname);
63 host->hash_hostname = simple_hash(host->hostname);
66 static inline void rrdhost_init_os(RRDHOST *host, const char *os) {
68 host->os = strdupz(os?os:"unknown");
71 static inline void rrdhost_init_machine_guid(RRDHOST *host, const char *machine_guid) {
72 strncpy(host->machine_guid, machine_guid, GUID_LEN);
73 host->machine_guid[GUID_LEN] = '\0';
74 host->hash_machine_guid = simple_hash(host->machine_guid);
78 // ----------------------------------------------------------------------------
79 // RRDHOST - add a host
81 RRDHOST *rrdhost_create(const char *hostname,
86 RRD_MEMORY_MODE memory_mode,
89 char *rrdpush_destination,
90 char *rrdpush_api_key,
94 debug(D_RRDHOST, "Host '%s': adding with guid '%s'", hostname, guid);
96 RRDHOST *host = callocz(1, sizeof(RRDHOST));
98 host->rrd_update_every = update_every;
99 host->rrd_history_entries = entries;
100 host->rrd_memory_mode = memory_mode;
101 host->health_enabled = (memory_mode == RRD_MEMORY_MODE_NONE)? 0 : health_enabled;
102 host->rrdpush_enabled = (rrdpush_enabled && rrdpush_destination && *rrdpush_destination && rrdpush_api_key && *rrdpush_api_key);
103 host->rrdpush_destination = (host->rrdpush_enabled)?strdupz(rrdpush_destination):NULL;
104 host->rrdpush_api_key = (host->rrdpush_enabled)?strdupz(rrdpush_api_key):NULL;
106 host->rrdpush_pipe[0] = -1;
107 host->rrdpush_pipe[1] = -1;
108 host->rrdpush_socket = -1;
110 pthread_mutex_init(&host->rrdpush_mutex, NULL);
111 pthread_rwlock_init(&host->rrdhost_rwlock, NULL);
113 rrdhost_init_hostname(host, hostname);
114 rrdhost_init_machine_guid(host, guid);
115 rrdhost_init_os(host, os);
117 avl_init_lock(&(host->rrdset_root_index), rrdset_compare);
118 avl_init_lock(&(host->rrdset_root_index_name), rrdset_compare_name);
119 avl_init_lock(&(host->rrdfamily_root_index), rrdfamily_compare);
120 avl_init_lock(&(host->variables_root_index), rrdvar_compare);
122 // ------------------------------------------------------------------------
123 // initialize health variables
125 host->health_log.next_log_id = 1;
126 host->health_log.next_alarm_id = 1;
127 host->health_log.max = 1000;
128 host->health_log.next_log_id =
129 host->health_log.next_alarm_id = (uint32_t)now_realtime_sec();
131 long n = config_get_number(CONFIG_SECTION_HEALTH, "in memory max health log entries", host->health_log.max);
133 error("Host '%s': health configuration has invalid max log entries %ld. Using default %u", host->hostname, n, host->health_log.max);
134 config_set_number(CONFIG_SECTION_HEALTH, "in memory max health log entries", (long)host->health_log.max);
137 host->health_log.max = (unsigned int)n;
139 pthread_rwlock_init(&(host->health_log.alarm_log_rwlock), NULL);
141 char filename[FILENAME_MAX + 1];
145 host->cache_dir = strdupz(netdata_configured_cache_dir);
146 host->varlib_dir = strdupz(netdata_configured_varlib_dir);
150 // this is not localhost - append our GUID to localhost path
152 snprintfz(filename, FILENAME_MAX, "%s/%s", netdata_configured_cache_dir, host->machine_guid);
153 host->cache_dir = strdupz(filename);
155 if(host->rrd_memory_mode == RRD_MEMORY_MODE_MAP || host->rrd_memory_mode == RRD_MEMORY_MODE_SAVE) {
156 int r = mkdir(host->cache_dir, 0775);
157 if(r != 0 && errno != EEXIST)
158 error("Host '%s': cannot create directory '%s'", host->hostname, host->cache_dir);
161 snprintfz(filename, FILENAME_MAX, "%s/%s", netdata_configured_varlib_dir, host->machine_guid);
162 host->varlib_dir = strdupz(filename);
164 if(host->health_enabled) {
165 int r = mkdir(host->varlib_dir, 0775);
166 if(r != 0 && errno != EEXIST)
167 error("Host '%s': cannot create directory '%s'", host->hostname, host->varlib_dir);
169 snprintfz(filename, FILENAME_MAX, "%s/health", host->varlib_dir);
170 r = mkdir(filename, 0775);
171 if(r != 0 && errno != EEXIST)
172 error("Host '%s': cannot create directory '%s'", host->hostname, filename);
177 snprintfz(filename, FILENAME_MAX, "%s/health/health-log.db", host->varlib_dir);
178 host->health_log_filename = strdupz(filename);
180 snprintfz(filename, FILENAME_MAX, "%s/alarm-notify.sh", netdata_configured_plugins_dir);
181 host->health_default_exec = strdupz(config_get(CONFIG_SECTION_HEALTH, "script to execute on alarm", filename));
182 host->health_default_recipient = strdup("root");
185 // ------------------------------------------------------------------------
186 // load health configuration
188 if(host->health_enabled) {
189 health_alarm_log_load(host);
190 health_alarm_log_open(host);
192 rrdhost_wrlock(host);
193 health_readdir(host, health_config_dir());
194 rrdhost_unlock(host);
198 // ------------------------------------------------------------------------
199 // link it and add it to the index
204 host->next = localhost;
209 host->next = localhost->next;
210 localhost->next = host;
212 else localhost = host;
215 RRDHOST *t = rrdhost_index_add(host);
218 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);
223 info("Host '%s' with guid '%s' initialized"
227 ", history entries %d"
229 " (to '%s' with api key '%s')"
234 ", alarms default handler '%s'"
235 ", alarms default recipient '%s'"
239 , host->rrd_update_every
240 , rrd_memory_mode_name(host->rrd_memory_mode)
241 , host->rrd_history_entries
242 , host->rrdpush_enabled?"enabled":"disabled"
243 , host->rrdpush_destination?host->rrdpush_destination:""
244 , host->rrdpush_api_key?host->rrdpush_api_key:""
245 , host->health_enabled?"enabled":"disabled"
248 , host->health_log_filename
249 , host->health_default_exec
250 , host->health_default_recipient
254 rrd_hosts_available++;
260 RRDHOST *rrdhost_find_or_create(
266 , RRD_MEMORY_MODE mode
268 , int rrdpush_enabled
269 , char *rrdpush_destination
270 , char *rrdpush_api_key
272 debug(D_RRDHOST, "Searching for host '%s' with guid '%s'", hostname, guid);
274 RRDHOST *host = rrdhost_find_by_guid(guid, 0);
276 host = rrdhost_create(
285 , rrdpush_destination
291 host->health_enabled = health_enabled;
293 if(strcmp(host->hostname, hostname)) {
294 char *t = host->hostname;
295 char *n = strdupz(hostname);
300 if(host->rrd_update_every != update_every)
301 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);
303 if(host->rrd_history_entries != history)
304 error("Host '%s' has history of %d entries, but the wanted one is %d entries.", host->hostname, host->rrd_history_entries, history);
306 if(host->rrd_memory_mode != mode)
307 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));
310 rrdhost_cleanup_remote_stale(host);
315 void rrdhost_cleanup_remote_stale(RRDHOST *protected) {
319 rrdhost_foreach_write(h) {
322 && !h->connected_senders
323 && h->senders_disconnected_time + rrdhost_free_orphan_time > now_realtime_sec()) {
324 info("Host '%s' with machine guid '%s' is obsolete - cleaning up.", h->hostname, h->machine_guid);
334 // ----------------------------------------------------------------------------
335 // RRDHOST global / startup initialization
337 void rrd_init(char *hostname) {
342 debug(D_RRDHOST, "Initializing localhost with hostname '%s'", hostname);
343 localhost = rrdhost_create(
345 , registry_get_this_machine_guid()
347 , default_rrd_update_every
348 , default_rrd_history_entries
349 , default_rrd_memory_mode
350 , default_health_enabled
351 , default_rrdpush_enabled
352 , default_rrdpush_destination
353 , default_rrdpush_api_key
358 // ----------------------------------------------------------------------------
359 // RRDHOST - lock validations
360 // there are only used when NETDATA_INTERNAL_CHECKS is set
362 void rrdhost_check_rdlock_int(RRDHOST *host, const char *file, const char *function, const unsigned long line) {
363 debug(D_RRDHOST, "Checking read lock on host '%s'", host->hostname);
365 int ret = pthread_rwlock_trywrlock(&host->rrdhost_rwlock);
367 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);
370 void rrdhost_check_wrlock_int(RRDHOST *host, const char *file, const char *function, const unsigned long line) {
371 debug(D_RRDHOST, "Checking write lock on host '%s'", host->hostname);
373 int ret = pthread_rwlock_tryrdlock(&host->rrdhost_rwlock);
375 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);
378 void rrd_check_rdlock_int(const char *file, const char *function, const unsigned long line) {
379 debug(D_RRDHOST, "Checking read lock on all RRDs");
381 int ret = pthread_rwlock_trywrlock(&rrd_rwlock);
383 fatal("RRDs should be read-locked, but it are not, at function %s() at line %lu of file '%s'", function, line, file);
386 void rrd_check_wrlock_int(const char *file, const char *function, const unsigned long line) {
387 debug(D_RRDHOST, "Checking write lock on all RRDs");
389 int ret = pthread_rwlock_tryrdlock(&rrd_rwlock);
391 fatal("RRDs should be write-locked, but it are not, at function %s() at line %lu of file '%s'", function, line, file);
394 // ----------------------------------------------------------------------------
397 void rrdhost_free(RRDHOST *host) {
400 info("Freeing all memory for host '%s'...", host->hostname);
402 rrd_check_wrlock(); // make sure the RRDs are write locked
403 rrdhost_wrlock(host); // lock this RRDHOST
405 // ------------------------------------------------------------------------
406 // release its children resources
408 while(host->rrdset_root) rrdset_free(host->rrdset_root);
410 while(host->alarms) rrdcalc_free(host, host->alarms);
411 while(host->templates) rrdcalctemplate_free(host, host->templates);
412 health_alarm_log_free(host);
415 // ------------------------------------------------------------------------
416 // remove it from the indexes
418 if(rrdhost_index_del(host) != host)
419 error("RRDHOST '%s' removed from index, deleted the wrong entry.", host->hostname);
422 // ------------------------------------------------------------------------
423 // unlink it from the host
425 if(host == localhost) {
426 localhost = host->next;
429 // find the previous one
431 for(h = localhost; h && h->next != host ; h = h->next) ;
434 if(h) h->next = host->next;
435 else error("Request to free RRDHOST '%s': cannot find it", host->hostname);
438 // ------------------------------------------------------------------------
441 rrdpush_sender_thread_stop(host);
444 freez(host->cache_dir);
445 freez(host->varlib_dir);
446 freez(host->rrdpush_api_key);
447 freez(host->rrdpush_destination);
448 freez(host->health_default_exec);
449 freez(host->health_default_recipient);
450 freez(host->health_log_filename);
451 freez(host->hostname);
452 rrdhost_unlock(host);
455 rrd_hosts_available--;
458 void rrdhost_free_all(void) {
460 while(localhost) rrdhost_free(localhost);
464 // ----------------------------------------------------------------------------
467 void rrdhost_save(RRDHOST *host) {
470 info("Saving database of host '%s'...", host->hostname);
475 // we get a write lock
476 // to ensure only one thread is saving the database
477 rrdhost_wrlock(host);
479 rrdset_foreach_write(st, host) {
482 if(st->rrd_memory_mode == RRD_MEMORY_MODE_SAVE) {
483 debug(D_RRD_STATS, "Saving stats '%s' to '%s'.", st->name, st->cache_filename);
484 savememory(st->cache_filename, st, st->memsize);
487 rrddim_foreach_read(rd, st) {
488 if(likely(rd->rrd_memory_mode == RRD_MEMORY_MODE_SAVE)) {
489 debug(D_RRD_STATS, "Saving dimension '%s' to '%s'.", rd->name, rd->cache_filename);
490 savememory(rd->cache_filename, rd, rd->memsize);
497 rrdhost_unlock(host);
500 void rrdhost_save_all(void) {
501 info("Saving database [%zu hosts(s)]...", rrd_hosts_available);
506 rrdhost_foreach_read(host)