1 #define NETDATA_RRD_INTERNALS 1
4 RRDHOST *localhost = NULL;
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_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 #define rrdhost_index_add(rrdhost) (RRDHOST *)avl_insert_lock(&(rrdhost_root_index), (avl *)(rrdhost))
35 #define rrdhost_index_del(rrdhost) (RRDHOST *)avl_remove_lock(&(rrdhost_root_index), (avl *)(rrdhost))
38 // ----------------------------------------------------------------------------
39 // RRDHOST - internal helpers
41 static inline void rrdhost_init_hostname(RRDHOST *host, const char *hostname) {
42 freez(host->hostname);
43 host->hostname = strdupz(hostname);
44 host->hash_hostname = simple_hash(host->hostname);
47 static inline void rrdhost_init_os(RRDHOST *host, const char *os) {
49 host->os = strdupz(os?os:"unknown");
52 static inline void rrdhost_init_machine_guid(RRDHOST *host, const char *machine_guid) {
53 strncpy(host->machine_guid, machine_guid, GUID_LEN);
54 host->machine_guid[GUID_LEN] = '\0';
55 host->hash_machine_guid = simple_hash(host->machine_guid);
59 // ----------------------------------------------------------------------------
60 // RRDHOST - add a host
62 RRDHOST *rrdhost_create(const char *hostname,
67 RRD_MEMORY_MODE memory_mode,
70 char *rrdpush_destination,
71 char *rrdpush_api_key,
75 debug(D_RRDHOST, "Host '%s': adding with guid '%s'", hostname, guid);
77 RRDHOST *host = callocz(1, sizeof(RRDHOST));
79 host->rrd_update_every = update_every;
80 host->rrd_history_entries = entries;
81 host->rrd_memory_mode = memory_mode;
82 host->health_enabled = (memory_mode == RRD_MEMORY_MODE_NONE)? 0 : health_enabled;
83 host->rrdpush_enabled = (rrdpush_enabled && rrdpush_destination && *rrdpush_destination && rrdpush_api_key && *rrdpush_api_key);
84 host->rrdpush_destination = (host->rrdpush_enabled)?strdupz(rrdpush_destination):NULL;
85 host->rrdpush_api_key = (host->rrdpush_enabled)?strdupz(rrdpush_api_key):NULL;
87 host->rrdpush_pipe[0] = -1;
88 host->rrdpush_pipe[1] = -1;
89 host->rrdpush_socket = -1;
91 pthread_mutex_init(&host->rrdpush_mutex, NULL);
92 pthread_rwlock_init(&host->rrdhost_rwlock, NULL);
94 rrdhost_init_hostname(host, hostname);
95 rrdhost_init_machine_guid(host, guid);
96 rrdhost_init_os(host, os);
98 avl_init_lock(&(host->rrdset_root_index), rrdset_compare);
99 avl_init_lock(&(host->rrdset_root_index_name), rrdset_compare_name);
100 avl_init_lock(&(host->rrdfamily_root_index), rrdfamily_compare);
101 avl_init_lock(&(host->variables_root_index), rrdvar_compare);
103 // ------------------------------------------------------------------------
104 // initialize health variables
106 host->health_log.next_log_id = 1;
107 host->health_log.next_alarm_id = 1;
108 host->health_log.max = 1000;
109 host->health_log.next_log_id =
110 host->health_log.next_alarm_id = (uint32_t)now_realtime_sec();
112 long n = config_get_number(CONFIG_SECTION_HEALTH, "in memory max health log entries", host->health_log.max);
114 error("Host '%s': health configuration has invalid max log entries %ld. Using default %u", host->hostname, n, host->health_log.max);
115 config_set_number(CONFIG_SECTION_HEALTH, "in memory max health log entries", (long)host->health_log.max);
118 host->health_log.max = (unsigned int)n;
120 pthread_rwlock_init(&(host->health_log.alarm_log_rwlock), NULL);
122 char filename[FILENAME_MAX + 1];
126 host->cache_dir = strdupz(netdata_configured_cache_dir);
127 host->varlib_dir = strdupz(netdata_configured_varlib_dir);
131 // this is not localhost - append our GUID to localhost path
133 snprintfz(filename, FILENAME_MAX, "%s/%s", netdata_configured_cache_dir, host->machine_guid);
134 host->cache_dir = strdupz(filename);
136 if(host->rrd_memory_mode == RRD_MEMORY_MODE_MAP || host->rrd_memory_mode == RRD_MEMORY_MODE_SAVE) {
137 int r = mkdir(host->cache_dir, 0775);
138 if(r != 0 && errno != EEXIST)
139 error("Host '%s': cannot create directory '%s'", host->hostname, host->cache_dir);
142 snprintfz(filename, FILENAME_MAX, "%s/%s", netdata_configured_varlib_dir, host->machine_guid);
143 host->varlib_dir = strdupz(filename);
145 if(host->health_enabled) {
146 int r = mkdir(host->varlib_dir, 0775);
147 if(r != 0 && errno != EEXIST)
148 error("Host '%s': cannot create directory '%s'", host->hostname, host->varlib_dir);
150 snprintfz(filename, FILENAME_MAX, "%s/health", host->varlib_dir);
151 r = mkdir(filename, 0775);
152 if(r != 0 && errno != EEXIST)
153 error("Host '%s': cannot create directory '%s'", host->hostname, filename);
158 snprintfz(filename, FILENAME_MAX, "%s/health/health-log.db", host->varlib_dir);
159 host->health_log_filename = strdupz(filename);
161 snprintfz(filename, FILENAME_MAX, "%s/alarm-notify.sh", netdata_configured_plugins_dir);
162 host->health_default_exec = strdupz(config_get(CONFIG_SECTION_HEALTH, "script to execute on alarm", filename));
163 host->health_default_recipient = strdup("root");
166 // ------------------------------------------------------------------------
167 // load health configuration
169 if(host->health_enabled) {
170 health_alarm_log_load(host);
171 health_alarm_log_open(host);
173 rrdhost_wrlock(host);
174 health_readdir(host, health_config_dir());
175 rrdhost_unlock(host);
179 // ------------------------------------------------------------------------
180 // link it and add it to the index
185 host->next = localhost;
190 host->next = localhost->next;
191 localhost->next = host;
193 else localhost = host;
196 RRDHOST *t = rrdhost_index_add(host);
199 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);
204 info("Host '%s' with guid '%s' initialized"
208 ", history entries %d"
210 " (to '%s' with api key '%s')"
215 ", alarms default handler '%s'"
216 ", alarms default recipient '%s'"
220 , host->rrd_update_every
221 , rrd_memory_mode_name(host->rrd_memory_mode)
222 , host->rrd_history_entries
223 , host->rrdpush_enabled?"enabled":"disabled"
224 , host->rrdpush_destination?host->rrdpush_destination:""
225 , host->rrdpush_api_key?host->rrdpush_api_key:""
226 , host->health_enabled?"enabled":"disabled"
229 , host->health_log_filename
230 , host->health_default_exec
231 , host->health_default_recipient
240 RRDHOST *rrdhost_find_or_create(
246 , RRD_MEMORY_MODE mode
248 , int rrdpush_enabled
249 , char *rrdpush_destination
250 , char *rrdpush_api_key
252 debug(D_RRDHOST, "Searching for host '%s' with guid '%s'", hostname, guid);
254 RRDHOST *host = rrdhost_find_guid(guid, 0);
256 host = rrdhost_create(
265 , rrdpush_destination
271 host->health_enabled = health_enabled;
273 if(strcmp(host->hostname, hostname)) {
274 char *t = host->hostname;
275 char *n = strdupz(hostname);
280 if(host->rrd_update_every != update_every)
281 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);
283 if(host->rrd_history_entries != history)
284 error("Host '%s' has history of %d entries, but the wanted one is %d entries.", host->hostname, host->rrd_history_entries, history);
286 if(host->rrd_memory_mode != mode)
287 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));
290 rrdhost_cleanup_remote_stale(host);
295 void rrdhost_cleanup_remote_stale(RRDHOST *protected) {
299 rrdhost_foreach_write(h) {
302 && !h->connected_senders
303 && h->senders_disconnected_time + rrdhost_free_orphan_time > now_realtime_sec()) {
304 info("Host '%s' with machine guid '%s' is obsolete - cleaning up.", h->hostname, h->machine_guid);
314 // ----------------------------------------------------------------------------
315 // RRDHOST global / startup initialization
317 void rrd_init(char *hostname) {
322 debug(D_RRDHOST, "Initializing localhost with hostname '%s'", hostname);
323 localhost = rrdhost_create(
325 , registry_get_this_machine_guid()
327 , default_rrd_update_every
328 , default_rrd_history_entries
329 , default_rrd_memory_mode
330 , default_health_enabled
331 , default_rrdpush_enabled
332 , default_rrdpush_destination
333 , default_rrdpush_api_key
338 // ----------------------------------------------------------------------------
339 // RRDHOST - lock validations
340 // there are only used when NETDATA_INTERNAL_CHECKS is set
342 void rrdhost_check_rdlock_int(RRDHOST *host, const char *file, const char *function, const unsigned long line) {
343 debug(D_RRDHOST, "Checking read lock on host '%s'", host->hostname);
345 int ret = pthread_rwlock_trywrlock(&host->rrdhost_rwlock);
347 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);
350 void rrdhost_check_wrlock_int(RRDHOST *host, const char *file, const char *function, const unsigned long line) {
351 debug(D_RRDHOST, "Checking write lock on host '%s'", host->hostname);
353 int ret = pthread_rwlock_tryrdlock(&host->rrdhost_rwlock);
355 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);
358 void rrd_check_rdlock_int(const char *file, const char *function, const unsigned long line) {
359 debug(D_RRDHOST, "Checking read lock on all RRDs");
361 int ret = pthread_rwlock_trywrlock(&rrd_rwlock);
363 fatal("RRDs should be read-locked, but it are not, at function %s() at line %lu of file '%s'", function, line, file);
366 void rrd_check_wrlock_int(const char *file, const char *function, const unsigned long line) {
367 debug(D_RRDHOST, "Checking write lock on all RRDs");
369 int ret = pthread_rwlock_tryrdlock(&rrd_rwlock);
371 fatal("RRDs should be write-locked, but it are not, at function %s() at line %lu of file '%s'", function, line, file);
374 // ----------------------------------------------------------------------------
377 void rrdhost_free(RRDHOST *host) {
380 info("Freeing all memory for host '%s'...", host->hostname);
382 rrd_check_wrlock(); // make sure the RRDs are write locked
383 rrdhost_wrlock(host); // lock this RRDHOST
385 // ------------------------------------------------------------------------
386 // release its children resources
388 while(host->rrdset_root) rrdset_free(host->rrdset_root);
390 while(host->alarms) rrdcalc_free(host, host->alarms);
391 while(host->templates) rrdcalctemplate_free(host, host->templates);
392 health_alarm_log_free(host);
395 // ------------------------------------------------------------------------
396 // remove it from the indexes
398 if(rrdhost_index_del(host) != host)
399 error("RRDHOST '%s' removed from index, deleted the wrong entry.", host->hostname);
402 // ------------------------------------------------------------------------
403 // unlink it from the host
405 if(host == localhost) {
406 localhost = host->next;
409 // find the previous one
411 for(h = localhost; h && h->next != host ; h = h->next) ;
414 if(h) h->next = host->next;
415 else error("Request to free RRDHOST '%s': cannot find it", host->hostname);
418 // ------------------------------------------------------------------------
421 rrdpush_sender_thread_stop(host);
424 freez(host->cache_dir);
425 freez(host->varlib_dir);
426 freez(host->rrdpush_api_key);
427 freez(host->rrdpush_destination);
428 freez(host->health_default_exec);
429 freez(host->health_default_recipient);
430 freez(host->health_log_filename);
431 freez(host->hostname);
432 rrdhost_unlock(host);
435 info("Host memory cleanup completed...");
438 void rrdhost_free_all(void) {
440 while(localhost) rrdhost_free(localhost);
444 // ----------------------------------------------------------------------------
447 void rrdhost_save(RRDHOST *host) {
450 info("Saving host '%s' database...", host->hostname);
455 // we get a write lock
456 // to ensure only one thread is saving the database
457 rrdhost_wrlock(host);
459 rrdset_foreach_write(st, host) {
462 if(st->rrd_memory_mode == RRD_MEMORY_MODE_SAVE) {
463 debug(D_RRD_STATS, "Saving stats '%s' to '%s'.", st->name, st->cache_filename);
464 savememory(st->cache_filename, st, st->memsize);
467 rrddim_foreach_read(rd, st) {
468 if(likely(rd->rrd_memory_mode == RRD_MEMORY_MODE_SAVE)) {
469 debug(D_RRD_STATS, "Saving dimension '%s' to '%s'.", rd->name, rd->cache_filename);
470 savememory(rd->cache_filename, rd, rd->memsize);
477 rrdhost_unlock(host);
480 void rrdhost_save_all(void) {
481 info("Saving database...");
486 rrdhost_foreach_read(host)