]> arthur.barton.de Git - netdata.git/blob - src/rrdhost.c
properly initialize stream receiving thread
[netdata.git] / src / rrdhost.c
1 #define NETDATA_RRD_INTERNALS 1
2 #include "common.h"
3
4 RRDHOST *localhost = NULL;
5
6 pthread_rwlock_t rrd_rwlock = PTHREAD_RWLOCK_INITIALIZER;
7
8
9 // ----------------------------------------------------------------------------
10 // RRDHOST index
11
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);
16 }
17
18 avl_tree_lock rrdhost_root_index = {
19         .avl_tree = { NULL, rrdhost_compare },
20         .rwlock = AVL_LOCK_INITIALIZER
21 };
22
23 RRDHOST *rrdhost_find(const char *guid, uint32_t hash) {
24     debug(D_RRDHOST, "Searching in index for host with guid '%s'", guid);
25
26     RRDHOST tmp;
27     strncpyz(tmp.machine_guid, guid, GUID_LEN);
28     tmp.hash_machine_guid = (hash)?hash:simple_hash(tmp.machine_guid);
29
30     return (RRDHOST *)avl_search_lock(&(rrdhost_root_index), (avl *) &tmp);
31 }
32
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))
35
36
37 // ----------------------------------------------------------------------------
38 // RRDHOST - internal helpers
39
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);
44 }
45
46 static inline void rrdhost_init_os(RRDHOST *host, const char *os) {
47     freez(host->os);
48     host->os = strdupz(os?os:"unknown");
49 }
50
51 static inline void rrdhost_init_machine_guid(RRDHOST *host, const char *machine_guid) {
52     strncpy(host->machine_guid, machine_guid, GUID_LEN);
53     host->machine_guid[GUID_LEN] = '\0';
54     host->hash_machine_guid = simple_hash(host->machine_guid);
55 }
56
57
58 // ----------------------------------------------------------------------------
59 // RRDHOST - add a host
60
61 RRDHOST *rrdhost_create(const char *hostname,
62         const char *guid,
63         const char *os,
64         int update_every,
65         int entries,
66         RRD_MEMORY_MODE memory_mode,
67         int health_enabled) {
68
69     debug(D_RRDHOST, "Host '%s': adding with guid '%s'", hostname, guid);
70
71     RRDHOST *host = callocz(1, sizeof(RRDHOST));
72
73     host->rrd_update_every    = update_every;
74     host->rrd_history_entries = entries;
75     host->rrd_memory_mode     = memory_mode;
76     host->health_enabled      = health_enabled;
77
78     pthread_rwlock_init(&(host->rrdhost_rwlock), NULL);
79
80     rrdhost_init_hostname(host, hostname);
81     rrdhost_init_machine_guid(host, guid);
82     rrdhost_init_os(host, os);
83
84     avl_init_lock(&(host->rrdset_root_index), rrdset_compare);
85     avl_init_lock(&(host->rrdset_root_index_name), rrdset_compare_name);
86     avl_init_lock(&(host->rrdfamily_root_index), rrdfamily_compare);
87     avl_init_lock(&(host->variables_root_index), rrdvar_compare);
88
89     // ------------------------------------------------------------------------
90     // initialize health variables
91
92     host->health_log.next_log_id = 1;
93     host->health_log.next_alarm_id = 1;
94     host->health_log.max = 1000;
95     host->health_log.next_log_id =
96     host->health_log.next_alarm_id = (uint32_t)now_realtime_sec();
97
98     long n = config_get_number("health", "in memory max health log entries", host->health_log.max);
99     if(n < 10) {
100         error("Host '%s': health configuration has invalid max log entries %ld. Using default %u", host->hostname, n, host->health_log.max);
101         config_set_number("health", "in memory max health log entries", (long)host->health_log.max);
102     }
103     else
104         host->health_log.max = (unsigned int)n;
105
106     pthread_rwlock_init(&(host->health_log.alarm_log_rwlock), NULL);
107
108     char filename[FILENAME_MAX + 1];
109
110     if(!localhost) {
111         // this is localhost
112
113         host->cache_dir = strdupz(netdata_configured_cache_dir);
114         host->varlib_dir = strdupz(netdata_configured_varlib_dir);
115
116         snprintfz(filename, FILENAME_MAX, "%s/health/health-log.db", host->varlib_dir);
117         host->health_log_filename = strdupz(config_get("health", "health db file", filename));
118
119     }
120     else {
121         // this is not localhost - append our GUID to localhost path
122
123         snprintfz(filename, FILENAME_MAX, "%s/%s", netdata_configured_cache_dir, host->machine_guid);
124         host->cache_dir = strdupz(filename);
125
126         if(host->rrd_memory_mode == RRD_MEMORY_MODE_MAP || host->rrd_memory_mode == RRD_MEMORY_MODE_SAVE) {
127             int r = mkdir(host->cache_dir, 0775);
128             if(r != 0 && errno != EEXIST)
129                 error("Host '%s': cannot create directory '%s'", host->hostname, host->cache_dir);
130         }
131
132         snprintfz(filename, FILENAME_MAX, "%s/%s", netdata_configured_varlib_dir, host->machine_guid);
133         host->varlib_dir = strdupz(filename);
134
135         if(host->health_enabled) {
136             int r = mkdir(host->varlib_dir, 0775);
137             if(r != 0 && errno != EEXIST)
138                 error("Host '%s': cannot create directory '%s'", host->hostname, host->varlib_dir);
139         }
140
141         snprintfz(filename, FILENAME_MAX, "%s/health", host->varlib_dir);
142         int r = mkdir(filename, 0775);
143         if(r != 0 && errno != EEXIST)
144             error("Host '%s': cannot create directory '%s'", host->hostname, filename);
145
146         snprintfz(filename, FILENAME_MAX, "%s/health/health-log.db", host->varlib_dir);
147         host->health_log_filename = strdupz(filename);
148
149     }
150
151     snprintfz(filename, FILENAME_MAX, "%s/alarm-notify.sh", netdata_configured_plugins_dir);
152     host->health_default_exec = strdupz(config_get("health", "script to execute on alarm", filename));
153     host->health_default_recipient = strdup("root");
154
155
156     // ------------------------------------------------------------------------
157     // load health configuration
158
159     health_alarm_log_load(host);
160     health_alarm_log_open(host);
161
162     rrdhost_wrlock(host);
163     health_readdir(host, health_config_dir());
164     rrdhost_unlock(host);
165
166
167     // ------------------------------------------------------------------------
168     // link it and add it to the index
169
170     rrd_wrlock();
171
172     if(localhost) {
173         host->next = localhost->next;
174         localhost->next = host;
175     }
176
177     if(rrdhost_index_add(host) != host)
178         fatal("Host '%s': cannot add host to index. It already exists.", hostname);
179
180     rrd_unlock();
181
182     debug(D_RRDHOST, "Host '%s', added with guid '%s'", host->hostname, host->machine_guid);
183     return host;
184 }
185
186 RRDHOST *rrdhost_find_or_create(const char *hostname, const char *guid, const char *os, int update_every, int history, RRD_MEMORY_MODE mode, int health_enabled) {
187     debug(D_RRDHOST, "Searching for host '%s' with guid '%s'", hostname, guid);
188
189     RRDHOST *host = rrdhost_find(guid, 0);
190     if(!host) {
191         host = rrdhost_create(hostname, guid, os, update_every, history, mode, health_enabled);
192     }
193     else {
194         host->health_enabled = health_enabled;
195
196         if(strcmp(host->hostname, hostname)) {
197             char *t = host->hostname;
198             char *n = strdupz(hostname);
199             host->hostname = n;
200             freez(t);
201         }
202
203         if(host->rrd_update_every != update_every)
204             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);
205
206         if(host->rrd_history_entries != history)
207             error("Host '%s' has history of %d entries, but the wanted one is %d entries.", host->hostname, host->rrd_history_entries, history);
208
209         if(host->rrd_memory_mode != mode)
210             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));
211     }
212
213     return host;
214 }
215
216 // ----------------------------------------------------------------------------
217 // RRDHOST global / startup initialization
218
219 void rrd_init(char *hostname) {
220     debug(D_RRDHOST, "Initializing localhost with hostname '%s'", hostname);
221
222     localhost = rrdhost_create(hostname,
223             registry_get_this_machine_guid(),
224             os_type,
225             default_rrd_update_every,
226             default_rrd_history_entries,
227             default_rrd_memory_mode,
228             default_health_enabled
229     );
230 }
231
232 // ----------------------------------------------------------------------------
233 // RRDHOST - lock validations
234 // there are only used when NETDATA_INTERNAL_CHECKS is set
235
236 void rrdhost_check_rdlock_int(RRDHOST *host, const char *file, const char *function, const unsigned long line) {
237     debug(D_RRDHOST, "Checking read lock on host '%s'", host->hostname);
238
239     int ret = pthread_rwlock_trywrlock(&host->rrdhost_rwlock);
240     if(ret == 0)
241         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);
242 }
243
244 void rrdhost_check_wrlock_int(RRDHOST *host, const char *file, const char *function, const unsigned long line) {
245     debug(D_RRDHOST, "Checking write lock on host '%s'", host->hostname);
246
247     int ret = pthread_rwlock_tryrdlock(&host->rrdhost_rwlock);
248     if(ret == 0)
249         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);
250 }
251
252 void rrd_check_rdlock_int(const char *file, const char *function, const unsigned long line) {
253     debug(D_RRDHOST, "Checking read lock on all RRDs");
254
255     int ret = pthread_rwlock_trywrlock(&rrd_rwlock);
256     if(ret == 0)
257         fatal("RRDs should be read-locked, but it are not, at function %s() at line %lu of file '%s'", function, line, file);
258 }
259
260 void rrd_check_wrlock_int(const char *file, const char *function, const unsigned long line) {
261     debug(D_RRDHOST, "Checking write lock on all RRDs");
262
263     int ret = pthread_rwlock_tryrdlock(&rrd_rwlock);
264     if(ret == 0)
265         fatal("RRDs should be write-locked, but it are not, at function %s() at line %lu of file '%s'", function, line, file);
266 }
267
268 // ----------------------------------------------------------------------------
269 // RRDHOST - free
270
271 void rrdhost_free(RRDHOST *host) {
272     if(!host) return;
273
274     info("Freeing all memory for host '%s'...", host->hostname);
275
276     rrd_check_wrlock();     // make sure the RRDs are write locked
277     rrdhost_wrlock(host);   // lock this RRDHOST
278
279     // ------------------------------------------------------------------------
280     // release its children resources
281
282     while(host->rrdset_root) rrdset_free(host->rrdset_root);
283
284     while(host->alarms) rrdcalc_free(host, host->alarms);
285     while(host->templates) rrdcalctemplate_free(host, host->templates);
286     health_alarm_log_free(host);
287
288
289     // ------------------------------------------------------------------------
290     // remove it from the indexes
291
292     if(rrdhost_index_del(host) != host)
293         error("RRDHOST '%s' removed from index, deleted the wrong entry.", host->hostname);
294
295
296     // ------------------------------------------------------------------------
297     // unlink it from the host
298
299     if(host == localhost) {
300         localhost = host->next;
301     }
302     else {
303         // find the previous one
304         RRDHOST *h;
305         for(h = localhost; h && h->next != host ; h = h->next) ;
306
307         // bypass it
308         if(h) h->next = host->next;
309         else error("Request to free RRDHOST '%s': cannot find it", host->hostname);
310     }
311
312     // ------------------------------------------------------------------------
313     // free it
314
315     freez(host->os);
316     freez(host->cache_dir);
317     freez(host->varlib_dir);
318     freez(host->health_default_exec);
319     freez(host->health_default_recipient);
320     freez(host->health_log_filename);
321     freez(host->hostname);
322     rrdhost_unlock(host);
323     freez(host);
324
325     info("Host memory cleanup completed...");
326 }
327
328 void rrdhost_free_all(void) {
329     rrd_wrlock();
330     while(localhost) rrdhost_free(localhost);
331     rrd_unlock();
332 }
333
334 // ----------------------------------------------------------------------------
335 // RRDHOST - save
336
337 void rrdhost_save(RRDHOST *host) {
338     if(!host) return;
339
340     info("Saving host '%s' database...", host->hostname);
341
342     RRDSET *st;
343     RRDDIM *rd;
344
345     // we get a write lock
346     // to ensure only one thread is saving the database
347     rrdhost_wrlock(host);
348
349     rrdset_foreach_write(st, host) {
350         rrdset_rdlock(st);
351
352         if(st->rrd_memory_mode == RRD_MEMORY_MODE_SAVE) {
353             debug(D_RRD_STATS, "Saving stats '%s' to '%s'.", st->name, st->cache_filename);
354             savememory(st->cache_filename, st, st->memsize);
355         }
356
357         rrddim_foreach_read(rd, st) {
358             if(likely(rd->rrd_memory_mode == RRD_MEMORY_MODE_SAVE)) {
359                 debug(D_RRD_STATS, "Saving dimension '%s' to '%s'.", rd->name, rd->cache_filename);
360                 savememory(rd->cache_filename, rd, rd->memsize);
361             }
362         }
363
364         rrdset_unlock(st);
365     }
366
367     rrdhost_unlock(host);
368 }
369
370 void rrdhost_save_all(void) {
371     info("Saving database...");
372
373     rrd_rdlock();
374
375     RRDHOST *host;
376     rrdhost_foreach_read(host)
377         rrdhost_save(host);
378
379     rrd_unlock();
380 }