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);
172 if(host->health_enabled) {
173 snprintfz(filename, FILENAME_MAX, "%s/health", host->varlib_dir);
174 int r = mkdir(filename, 0775);
175 if(r != 0 && errno != EEXIST)
176 error("Host '%s': cannot create directory '%s'", host->hostname, filename);
179 snprintfz(filename, FILENAME_MAX, "%s/health/health-log.db", host->varlib_dir);
180 host->health_log_filename = strdupz(filename);
182 snprintfz(filename, FILENAME_MAX, "%s/alarm-notify.sh", netdata_configured_plugins_dir);
183 host->health_default_exec = strdupz(config_get(CONFIG_SECTION_HEALTH, "script to execute on alarm", filename));
184 host->health_default_recipient = strdup("root");
187 // ------------------------------------------------------------------------
188 // load health configuration
190 if(host->health_enabled) {
191 health_alarm_log_load(host);
192 health_alarm_log_open(host);
194 rrdhost_wrlock(host);
195 health_readdir(host, health_config_dir());
196 rrdhost_unlock(host);
200 // ------------------------------------------------------------------------
201 // link it and add it to the index
206 host->next = localhost;
211 host->next = localhost->next;
212 localhost->next = host;
214 else localhost = host;
217 RRDHOST *t = rrdhost_index_add(host);
220 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);
225 info("Host '%s' with guid '%s' initialized"
229 ", history entries %d"
231 " (to '%s' with api key '%s')"
236 ", alarms default handler '%s'"
237 ", alarms default recipient '%s'"
241 , host->rrd_update_every
242 , rrd_memory_mode_name(host->rrd_memory_mode)
243 , host->rrd_history_entries
244 , host->rrdpush_enabled?"enabled":"disabled"
245 , host->rrdpush_destination?host->rrdpush_destination:""
246 , host->rrdpush_api_key?host->rrdpush_api_key:""
247 , host->health_enabled?"enabled":"disabled"
250 , host->health_log_filename
251 , host->health_default_exec
252 , host->health_default_recipient
256 rrd_hosts_available++;
262 RRDHOST *rrdhost_find_or_create(
268 , RRD_MEMORY_MODE mode
270 , int rrdpush_enabled
271 , char *rrdpush_destination
272 , char *rrdpush_api_key
274 debug(D_RRDHOST, "Searching for host '%s' with guid '%s'", hostname, guid);
276 RRDHOST *host = rrdhost_find_by_guid(guid, 0);
278 host = rrdhost_create(
287 , rrdpush_destination
293 host->health_enabled = health_enabled;
295 if(strcmp(host->hostname, hostname)) {
296 char *t = host->hostname;
297 char *n = strdupz(hostname);
302 if(host->rrd_update_every != update_every)
303 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);
305 if(host->rrd_history_entries != history)
306 error("Host '%s' has history of %d entries, but the wanted one is %d entries.", host->hostname, host->rrd_history_entries, history);
308 if(host->rrd_memory_mode != mode)
309 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));
312 rrdhost_cleanup_remote_stale(host);
317 void rrdhost_cleanup_remote_stale(RRDHOST *protected) {
321 rrdhost_foreach_write(h) {
324 && !h->connected_senders
325 && h->senders_disconnected_time + rrdhost_free_orphan_time > now_realtime_sec()) {
326 info("Host '%s' with machine guid '%s' is obsolete - cleaning up.", h->hostname, h->machine_guid);
336 // ----------------------------------------------------------------------------
337 // RRDHOST global / startup initialization
339 void rrd_init(char *hostname) {
344 debug(D_RRDHOST, "Initializing localhost with hostname '%s'", hostname);
345 localhost = rrdhost_create(
347 , registry_get_this_machine_guid()
349 , default_rrd_update_every
350 , default_rrd_history_entries
351 , default_rrd_memory_mode
352 , default_health_enabled
353 , default_rrdpush_enabled
354 , default_rrdpush_destination
355 , default_rrdpush_api_key
360 // ----------------------------------------------------------------------------
361 // RRDHOST - lock validations
362 // there are only used when NETDATA_INTERNAL_CHECKS is set
364 void rrdhost_check_rdlock_int(RRDHOST *host, const char *file, const char *function, const unsigned long line) {
365 debug(D_RRDHOST, "Checking read lock on host '%s'", host->hostname);
367 int ret = pthread_rwlock_trywrlock(&host->rrdhost_rwlock);
369 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);
372 void rrdhost_check_wrlock_int(RRDHOST *host, const char *file, const char *function, const unsigned long line) {
373 debug(D_RRDHOST, "Checking write lock on host '%s'", host->hostname);
375 int ret = pthread_rwlock_tryrdlock(&host->rrdhost_rwlock);
377 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);
380 void rrd_check_rdlock_int(const char *file, const char *function, const unsigned long line) {
381 debug(D_RRDHOST, "Checking read lock on all RRDs");
383 int ret = pthread_rwlock_trywrlock(&rrd_rwlock);
385 fatal("RRDs should be read-locked, but it are not, at function %s() at line %lu of file '%s'", function, line, file);
388 void rrd_check_wrlock_int(const char *file, const char *function, const unsigned long line) {
389 debug(D_RRDHOST, "Checking write lock on all RRDs");
391 int ret = pthread_rwlock_tryrdlock(&rrd_rwlock);
393 fatal("RRDs should be write-locked, but it are not, at function %s() at line %lu of file '%s'", function, line, file);
396 // ----------------------------------------------------------------------------
399 void rrdhost_free(RRDHOST *host) {
402 info("Freeing all memory for host '%s'...", host->hostname);
404 rrd_check_wrlock(); // make sure the RRDs are write locked
405 rrdhost_wrlock(host); // lock this RRDHOST
407 // ------------------------------------------------------------------------
408 // release its children resources
410 while(host->rrdset_root) rrdset_free(host->rrdset_root);
412 while(host->alarms) rrdcalc_free(host, host->alarms);
413 while(host->templates) rrdcalctemplate_free(host, host->templates);
414 health_alarm_log_free(host);
417 // ------------------------------------------------------------------------
418 // remove it from the indexes
420 if(rrdhost_index_del(host) != host)
421 error("RRDHOST '%s' removed from index, deleted the wrong entry.", host->hostname);
424 // ------------------------------------------------------------------------
425 // unlink it from the host
427 if(host == localhost) {
428 localhost = host->next;
431 // find the previous one
433 for(h = localhost; h && h->next != host ; h = h->next) ;
436 if(h) h->next = host->next;
437 else error("Request to free RRDHOST '%s': cannot find it", host->hostname);
440 // ------------------------------------------------------------------------
443 rrdpush_sender_thread_stop(host);
446 freez(host->cache_dir);
447 freez(host->varlib_dir);
448 freez(host->rrdpush_api_key);
449 freez(host->rrdpush_destination);
450 freez(host->health_default_exec);
451 freez(host->health_default_recipient);
452 freez(host->health_log_filename);
453 freez(host->hostname);
454 rrdhost_unlock(host);
457 rrd_hosts_available--;
460 void rrdhost_free_all(void) {
462 while(localhost) rrdhost_free(localhost);
466 // ----------------------------------------------------------------------------
469 void rrdhost_save(RRDHOST *host) {
472 info("Saving database of host '%s'...", host->hostname);
477 // we get a write lock
478 // to ensure only one thread is saving the database
479 rrdhost_wrlock(host);
481 rrdset_foreach_write(st, host) {
484 if(st->rrd_memory_mode == RRD_MEMORY_MODE_SAVE) {
485 debug(D_RRD_STATS, "Saving stats '%s' to '%s'.", st->name, st->cache_filename);
486 savememory(st->cache_filename, st, st->memsize);
489 rrddim_foreach_read(rd, st) {
490 if(likely(rd->rrd_memory_mode == RRD_MEMORY_MODE_SAVE)) {
491 debug(D_RRD_STATS, "Saving dimension '%s' to '%s'.", rd->name, rd->cache_filename);
492 savememory(rd->cache_filename, rd, rd->memsize);
499 rrdhost_unlock(host);
502 void rrdhost_save_all(void) {
503 info("Saving database [%zu hosts(s)]...", rrd_hosts_available);
508 rrdhost_foreach_read(host)