1 #define NETDATA_RRD_INTERNALS 1
4 RRDHOST *localhost = NULL;
6 pthread_rwlock_t rrd_rwlock = PTHREAD_RWLOCK_INITIALIZER;
9 // ----------------------------------------------------------------------------
12 int rrdhost_compare(void* a, void* b) {
13 if(((RRDHOST *)a)->hash_machine_guid < ((RRDHOST *)b)->hash_machine_guid) return -1;
14 else if(((RRDHOST *)a)->hash_machine_guid > ((RRDHOST *)b)->hash_machine_guid) return 1;
15 else return strcmp(((RRDHOST *)a)->machine_guid, ((RRDHOST *)b)->machine_guid);
18 avl_tree_lock rrdhost_root_index = {
19 .avl_tree = { NULL, rrdhost_compare },
20 .rwlock = AVL_LOCK_INITIALIZER
23 RRDHOST *rrdhost_find(const char *guid, uint32_t hash) {
24 debug(D_RRDHOST, "Searching in index for host with guid '%s'", guid);
27 strncpyz(tmp.machine_guid, guid, GUID_LEN);
28 tmp.hash_machine_guid = (hash)?hash:simple_hash(tmp.machine_guid);
30 return (RRDHOST *)avl_search_lock(&(rrdhost_root_index), (avl *) &tmp);
33 #define rrdhost_index_add(rrdhost) (RRDHOST *)avl_insert_lock(&(rrdhost_root_index), (avl *)(rrdhost))
34 #define rrdhost_index_del(rrdhost) (RRDHOST *)avl_remove_lock(&(rrdhost_root_index), (avl *)(rrdhost))
37 // ----------------------------------------------------------------------------
38 // RRDHOST - internal helpers
40 static inline void rrdhost_init_hostname(RRDHOST *host, const char *hostname) {
41 freez(host->hostname);
42 host->hostname = strdupz(hostname);
43 host->hash_hostname = simple_hash(host->hostname);
46 static inline void rrdhost_init_machine_guid(RRDHOST *host, const char *machine_guid) {
47 strncpy(host->machine_guid, machine_guid, GUID_LEN);
48 host->machine_guid[GUID_LEN] = '\0';
49 host->hash_machine_guid = simple_hash(host->machine_guid);
53 // ----------------------------------------------------------------------------
54 // RRDHOST - add a host
56 RRDHOST *rrdhost_create(const char *hostname,
60 RRD_MEMORY_MODE memory_mode,
63 debug(D_RRDHOST, "Adding host '%s' with guid '%s'", hostname, guid);
65 RRDHOST *host = callocz(1, sizeof(RRDHOST));
67 host->rrd_update_every = update_every;
68 host->rrd_history_entries = entries;
69 host->rrd_memory_mode = memory_mode;
70 host->health_enabled = health_enabled;
72 pthread_rwlock_init(&(host->rrdhost_rwlock), NULL);
74 rrdhost_init_hostname(host, hostname);
75 rrdhost_init_machine_guid(host, guid);
77 avl_init_lock(&(host->rrdset_root_index), rrdset_compare);
78 avl_init_lock(&(host->rrdset_root_index_name), rrdset_compare_name);
79 avl_init_lock(&(host->rrdfamily_root_index), rrdfamily_compare);
80 avl_init_lock(&(host->variables_root_index), rrdvar_compare);
82 // ------------------------------------------------------------------------
83 // initialize health variables
85 host->health_log.next_log_id = 1;
86 host->health_log.next_alarm_id = 1;
87 host->health_log.max = 1000;
88 host->health_log.next_log_id =
89 host->health_log.next_alarm_id = (uint32_t)now_realtime_sec();
91 long n = config_get_number("health", "in memory max health log entries", host->health_log.max);
93 error("Health configuration has invalid max log entries %ld. Using default %u", n, host->health_log.max);
94 config_set_number("health", "in memory max health log entries", (long)host->health_log.max);
97 host->health_log.max = (unsigned int)n;
99 pthread_rwlock_init(&(host->health_log.alarm_log_rwlock), NULL);
101 char filename[FILENAME_MAX + 1];
106 host->cache_dir = strdupz(netdata_configured_cache_dir);
107 host->varlib_dir = strdupz(netdata_configured_varlib_dir);
109 snprintfz(filename, FILENAME_MAX, "%s/health/health-log.db", host->varlib_dir);
110 host->health_log_filename = strdupz(config_get("health", "health db file", filename));
114 // this is not localhost - append our GUID to localhost path
116 snprintfz(filename, FILENAME_MAX, "%s/%s", netdata_configured_cache_dir, host->machine_guid);
117 host->cache_dir = strdupz(filename);
119 if(host->rrd_memory_mode == RRD_MEMORY_MODE_MAP || host->rrd_memory_mode == RRD_MEMORY_MODE_SAVE) {
120 int r = mkdir(host->cache_dir, 0775);
121 if(r != 0 && errno != EEXIST)
122 error("Cannot create directory '%s'", host->cache_dir);
125 snprintfz(filename, FILENAME_MAX, "%s/%s", netdata_configured_varlib_dir, host->machine_guid);
126 host->varlib_dir = strdupz(filename);
128 if(host->health_enabled) {
129 int r = mkdir(host->varlib_dir, 0775);
130 if(r != 0 && errno != EEXIST)
131 error("Cannot create directory '%s'", host->varlib_dir);
134 snprintfz(filename, FILENAME_MAX, "%s/health/health-log.db", host->varlib_dir);
135 host->health_log_filename = strdupz(filename);
139 snprintfz(filename, FILENAME_MAX, "%s/alarm-notify.sh", netdata_configured_plugins_dir);
140 host->health_default_exec = strdupz(config_get("health", "script to execute on alarm", filename));
141 host->health_default_recipient = strdup("root");
144 // ------------------------------------------------------------------------
145 // load health configuration
147 health_alarm_log_load(host);
148 health_alarm_log_open(host);
150 rrdhost_wrlock(host);
151 health_readdir(host, health_config_dir());
152 rrdhost_unlock(host);
155 // ------------------------------------------------------------------------
156 // link it and add it to the index
161 host->next = localhost->next;
162 localhost->next = host;
165 if(rrdhost_index_add(host) != host)
166 fatal("Cannot add host '%s' to index. It already exists.", hostname);
170 debug(D_RRDHOST, "Added host '%s' with guid '%s'", host->hostname, host->machine_guid);
174 RRDHOST *rrdhost_find_or_create(const char *hostname, const char *guid) {
175 debug(D_RRDHOST, "Searching for host '%s' with guid '%s'", hostname, guid);
177 RRDHOST *host = rrdhost_find(guid, 0);
179 host = rrdhost_create(hostname,
181 default_rrd_update_every,
182 default_rrd_history_entries,
183 default_rrd_memory_mode,
184 default_health_enabled
190 // ----------------------------------------------------------------------------
191 // RRDHOST global / startup initialization
193 void rrd_init(char *hostname) {
194 debug(D_RRDHOST, "Initializing localhost with hostname '%s'", hostname);
196 localhost = rrdhost_create(hostname,
197 registry_get_this_machine_guid(),
198 default_rrd_update_every,
199 default_rrd_history_entries,
200 default_rrd_memory_mode,
201 default_health_enabled
205 // ----------------------------------------------------------------------------
206 // RRDHOST - lock validations
207 // there are only used when NETDATA_INTERNAL_CHECKS is set
209 void rrdhost_check_rdlock_int(RRDHOST *host, const char *file, const char *function, const unsigned long line) {
210 debug(D_RRDHOST, "Checking read lock on host '%s'", host->hostname);
212 int ret = pthread_rwlock_trywrlock(&host->rrdhost_rwlock);
214 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);
217 void rrdhost_check_wrlock_int(RRDHOST *host, const char *file, const char *function, const unsigned long line) {
218 debug(D_RRDHOST, "Checking write lock on host '%s'", host->hostname);
220 int ret = pthread_rwlock_tryrdlock(&host->rrdhost_rwlock);
222 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);
225 void rrd_check_rdlock_int(const char *file, const char *function, const unsigned long line) {
226 debug(D_RRDHOST, "Checking read lock on all RRDs");
228 int ret = pthread_rwlock_trywrlock(&rrd_rwlock);
230 fatal("RRDs should be read-locked, but it are not, at function %s() at line %lu of file '%s'", function, line, file);
233 void rrd_check_wrlock_int(const char *file, const char *function, const unsigned long line) {
234 debug(D_RRDHOST, "Checking write lock on all RRDs");
236 int ret = pthread_rwlock_tryrdlock(&rrd_rwlock);
238 fatal("RRDs should be write-locked, but it are not, at function %s() at line %lu of file '%s'", function, line, file);
241 // ----------------------------------------------------------------------------
244 void rrdhost_free(RRDHOST *host) {
247 info("Freeing all memory for host '%s'...", host->hostname);
249 rrd_check_wrlock(); // make sure the RRDs are write locked
250 rrdhost_wrlock(host); // lock this RRDHOST
252 // ------------------------------------------------------------------------
253 // release its children resources
255 while(host->rrdset_root) rrdset_free(host->rrdset_root);
257 while(host->alarms) rrdcalc_free(host, host->alarms);
258 while(host->templates) rrdcalctemplate_free(host, host->templates);
259 health_alarm_log_free(host);
262 // ------------------------------------------------------------------------
263 // remove it from the indexes
265 if(rrdhost_index_del(host) != host)
266 error("RRDHOST '%s' removed from index, deleted the wrong entry.", host->hostname);
269 // ------------------------------------------------------------------------
270 // unlink it from the host
272 if(host == localhost) {
273 localhost = host->next;
276 // find the previous one
278 for(h = localhost; h && h->next != host ; h = h->next) ;
281 if(h) h->next = host->next;
282 else error("Request to free RRDHOST '%s': cannot find it", host->hostname);
285 // ------------------------------------------------------------------------
288 freez(host->cache_dir);
289 freez(host->varlib_dir);
290 freez(host->health_default_exec);
291 freez(host->health_default_recipient);
292 freez(host->health_log_filename);
293 freez(host->hostname);
294 rrdhost_unlock(host);
297 info("Host memory cleanup completed...");
300 void rrdhost_free_all(void) {
302 while(localhost) rrdhost_free(localhost);
306 // ----------------------------------------------------------------------------
309 void rrdhost_save(RRDHOST *host) {
312 info("Saving host '%s' database...", host->hostname);
317 // we get a write lock
318 // to ensure only one thread is saving the database
319 rrdhost_wrlock(host);
321 rrdset_foreach_write(st, host) {
324 if(st->rrd_memory_mode == RRD_MEMORY_MODE_SAVE) {
325 debug(D_RRD_STATS, "Saving stats '%s' to '%s'.", st->name, st->cache_filename);
326 savememory(st->cache_filename, st, st->memsize);
329 rrddim_foreach_read(rd, st) {
330 if(likely(rd->rrd_memory_mode == RRD_MEMORY_MODE_SAVE)) {
331 debug(D_RRD_STATS, "Saving dimension '%s' to '%s'.", rd->name, rd->cache_filename);
332 savememory(rd->cache_filename, rd, rd->memsize);
339 rrdhost_unlock(host);
342 void rrdhost_save_all(void) {
343 info("Saving database...");
348 rrdhost_foreach_read(host)