1 #define NETDATA_RRD_INTERNALS 1
4 RRDHOST *localhost = NULL;
6 // ----------------------------------------------------------------------------
9 int rrdhost_compare(void* a, void* b) {
10 if(((RRDHOST *)a)->hash_machine_guid < ((RRDHOST *)b)->hash_machine_guid) return -1;
11 else if(((RRDHOST *)a)->hash_machine_guid > ((RRDHOST *)b)->hash_machine_guid) return 1;
12 else return strcmp(((RRDHOST *)a)->machine_guid, ((RRDHOST *)b)->machine_guid);
15 avl_tree_lock rrdhost_root_index = {
16 .avl_tree = { NULL, rrdhost_compare },
17 .rwlock = AVL_LOCK_INITIALIZER
20 RRDHOST *rrdhost_find(const char *guid, uint32_t hash) {
21 debug(D_RRDHOST, "Searching in index for host with guid '%s'", guid);
24 strncpyz(tmp.machine_guid, guid, GUID_LEN);
25 tmp.hash_machine_guid = (hash)?hash:simple_hash(tmp.machine_guid);
27 return (RRDHOST *)avl_search_lock(&(rrdhost_root_index), (avl *) &tmp);
30 #define rrdhost_index_add(rrdhost) (RRDHOST *)avl_insert_lock(&(rrdhost_root_index), (avl *)(rrdhost))
31 #define rrdhost_index_del(rrdhost) (RRDHOST *)avl_remove_lock(&(rrdhost_root_index), (avl *)(rrdhost))
34 // ----------------------------------------------------------------------------
35 // RRDHOST - internal helpers
37 static inline void rrdhost_init_hostname(RRDHOST *host, const char *hostname) {
38 freez(host->hostname);
39 host->hostname = strdupz(hostname);
40 host->hash_hostname = simple_hash(host->hostname);
43 static inline void rrdhost_init_machine_guid(RRDHOST *host, const char *machine_guid) {
44 strncpy(host->machine_guid, machine_guid, GUID_LEN);
45 host->machine_guid[GUID_LEN] = '\0';
46 host->hash_machine_guid = simple_hash(host->machine_guid);
49 // ----------------------------------------------------------------------------
50 // RRDHOST - add a host
52 RRDHOST *rrdhost_create(const char *hostname,
56 RRD_MEMORY_MODE memory_mode,
59 debug(D_RRDHOST, "Adding host '%s' with guid '%s'", hostname, guid);
61 RRDHOST *host = callocz(1, sizeof(RRDHOST));
63 host->rrd_update_every = update_every;
64 host->rrd_history_entries = entries;
65 host->rrd_memory_mode = memory_mode;
66 host->health_enabled = health_enabled;
68 pthread_rwlock_init(&(host->rrdhost_rwlock), NULL);
70 rrdhost_init_hostname(host, hostname);
71 rrdhost_init_machine_guid(host, guid);
73 avl_init_lock(&(host->rrdset_root_index), rrdset_compare);
74 avl_init_lock(&(host->rrdset_root_index_name), rrdset_compare_name);
75 avl_init_lock(&(host->rrdfamily_root_index), rrdfamily_compare);
76 avl_init_lock(&(host->variables_root_index), rrdvar_compare);
79 // ------------------------------------------------------------------------
80 // initialize health variables
82 host->health_log.next_log_id = 1;
83 host->health_log.next_alarm_id = 1;
84 host->health_log.max = 1000;
85 host->health_log.next_log_id =
86 host->health_log.next_alarm_id = (uint32_t)now_realtime_sec();
88 long n = config_get_number("health", "in memory max health log entries", host->health_log.max);
90 error("Health configuration has invalid max log entries %ld. Using default %u", n, host->health_log.max);
91 config_set_number("health", "in memory max health log entries", (long)host->health_log.max);
94 host->health_log.max = (unsigned int)n;
96 pthread_rwlock_init(&(host->health_log.alarm_log_rwlock), NULL);
98 char filename[FILENAME_MAX + 1];
103 host->cache_dir = strdupz(netdata_configured_cache_dir);
104 host->varlib_dir = strdupz(netdata_configured_varlib_dir);
106 snprintfz(filename, FILENAME_MAX, "%s/health/health-log.db", host->varlib_dir);
107 host->health_log_filename = strdupz(config_get("health", "health db file", filename));
111 // this is not localhost - append our GUID to localhost path
113 snprintfz(filename, FILENAME_MAX, "%s/%s", netdata_configured_cache_dir, host->machine_guid);
114 host->cache_dir = strdupz(filename);
116 if(host->rrd_memory_mode == RRD_MEMORY_MODE_MAP || host->rrd_memory_mode == RRD_MEMORY_MODE_SAVE) {
117 int r = mkdir(host->cache_dir, 0775);
118 if(r != 0 && errno != EEXIST)
119 error("Cannot create directory '%s'", host->cache_dir);
122 snprintfz(filename, FILENAME_MAX, "%s/%s", netdata_configured_varlib_dir, host->machine_guid);
123 host->varlib_dir = strdupz(filename);
125 if(host->health_enabled) {
126 int r = mkdir(host->varlib_dir, 0775);
127 if(r != 0 && errno != EEXIST)
128 error("Cannot create directory '%s'", host->varlib_dir);
131 snprintfz(filename, FILENAME_MAX, "%s/health/health-log.db", host->varlib_dir);
132 host->health_log_filename = strdupz(filename);
136 snprintfz(filename, FILENAME_MAX, "%s/alarm-notify.sh", netdata_configured_plugins_dir);
137 host->health_default_exec = strdupz(config_get("health", "script to execute on alarm", filename));
138 host->health_default_recipient = strdup("root");
141 // ------------------------------------------------------------------------
142 // load health configuration
144 health_alarm_log_load(host);
145 health_alarm_log_open(host);
147 rrdhost_wrlock(host);
148 health_readdir(host, health_config_dir());
149 rrdhost_unlock(host);
152 // ------------------------------------------------------------------------
153 // add it to the index
155 if(rrdhost_index_add(host) != host)
156 fatal("Cannot add host '%s' to index. It already exists.", hostname);
158 debug(D_RRDHOST, "Added host '%s' with guid '%s'", host->hostname, host->machine_guid);
162 RRDHOST *rrdhost_find_or_create(const char *hostname, const char *guid) {
163 debug(D_RRDHOST, "Searching for host '%s' with guid '%s'", hostname, guid);
165 RRDHOST *host = rrdhost_find(guid, 0);
167 host = rrdhost_create(hostname,
169 default_localhost_rrd_update_every,
170 default_localhost_rrd_history_entries,
171 default_localhost_rrd_memory_mode,
172 default_localhost_health_enabled
178 // ----------------------------------------------------------------------------
179 // RRDHOST global / startup initialization
181 void rrd_init(char *hostname) {
182 debug(D_RRDHOST, "Initializing localhost with hostname '%s'", hostname);
184 localhost = rrdhost_create(hostname,
185 registry_get_this_machine_guid(),
186 default_localhost_rrd_update_every,
187 default_localhost_rrd_history_entries,
188 default_localhost_rrd_memory_mode,
189 default_localhost_health_enabled
193 // ----------------------------------------------------------------------------
194 // RRDHOST - lock validations
195 // there are only used when NETDATA_INTERNAL_CHECKS is set
197 void rrdhost_check_rdlock_int(RRDHOST *host, const char *file, const char *function, const unsigned long line) {
198 debug(D_RRDHOST, "Checking read lock on host '%s'", host->hostname);
200 int ret = pthread_rwlock_trywrlock(&host->rrdhost_rwlock);
202 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);
205 void rrdhost_check_wrlock_int(RRDHOST *host, const char *file, const char *function, const unsigned long line) {
206 debug(D_RRDHOST, "Checking write lock on host '%s'", host->hostname);
208 int ret = pthread_rwlock_tryrdlock(&host->rrdhost_rwlock);
210 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);
213 // ----------------------------------------------------------------------------
216 void rrdhost_free(RRDHOST *host) {
219 info("Freeing all memory for host '%s'...", host->hostname);
221 rrdhost_wrlock(host);
224 for(st = host->rrdset_root; st ;) {
225 RRDSET *next = st->next;
231 host->rrdset_root = NULL;
233 if(rrdhost_index_del(host) != host)
234 error("RRDHOST '%s' removed from index, deleted the wrong entry.", host->hostname);
236 freez(host->cache_dir);
237 freez(host->varlib_dir);
238 freez(host->health_default_exec);
239 freez(host->health_default_recipient);
240 freez(host->health_log_filename);
241 freez(host->hostname);
242 rrdhost_unlock(host);
245 info("Host memory cleanup completed...");
248 void rrdhost_free_all(void) {
249 RRDHOST *host = localhost;
251 // FIXME: lock all hosts
254 RRDHOST *next = host = host->next;
261 // FIXME: unlock all hosts
264 // ----------------------------------------------------------------------------
267 void rrdhost_save(RRDHOST *host) {
270 info("Saving host '%s' database...", host->hostname);
275 // we get a write lock
276 // to ensure only one thread is saving the database
277 rrdhost_wrlock(host);
279 for(st = host->rrdset_root; st ; st = st->next) {
282 if(st->rrd_memory_mode == RRD_MEMORY_MODE_SAVE) {
283 debug(D_RRD_STATS, "Saving stats '%s' to '%s'.", st->name, st->cache_filename);
284 savememory(st->cache_filename, st, st->memsize);
287 for(rd = st->dimensions; rd ; rd = rd->next) {
288 if(likely(rd->rrd_memory_mode == RRD_MEMORY_MODE_SAVE)) {
289 debug(D_RRD_STATS, "Saving dimension '%s' to '%s'.", rd->name, rd->cache_filename);
290 savememory(rd->cache_filename, rd, rd->memsize);
297 rrdhost_unlock(host);
300 void rrdhost_save_all(void) {
301 info("Saving database...");
304 for(host = localhost; host ; host = host->next)