]> arthur.barton.de Git - netdata.git/blob - src/rrdhost.c
avoid code duplication
[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         int rrdpush_enabled,
69         char *rrdpush_destination,
70         char *rrdpush_api_key,
71         int is_localhost
72 ) {
73
74     debug(D_RRDHOST, "Host '%s': adding with guid '%s'", hostname, guid);
75
76     RRDHOST *host = callocz(1, sizeof(RRDHOST));
77
78     host->rrd_update_every    = update_every;
79     host->rrd_history_entries = entries;
80     host->rrd_memory_mode     = memory_mode;
81     host->health_enabled      = (memory_mode == RRD_MEMORY_MODE_NONE)? 0 : health_enabled;
82     host->rrdpush_enabled     = (rrdpush_enabled && rrdpush_destination && *rrdpush_destination && rrdpush_api_key && *rrdpush_api_key);
83     host->rrdpush_destination = (host->rrdpush_enabled)?strdupz(rrdpush_destination):NULL;
84     host->rrdpush_api_key     = (host->rrdpush_enabled)?strdupz(rrdpush_api_key):NULL;
85
86     host->rrdpush_pipe[0] = -1;
87     host->rrdpush_pipe[1] = -1;
88     host->rrdpush_socket  = -1;
89
90     pthread_mutex_init(&host->rrdpush_mutex, NULL);
91     pthread_rwlock_init(&host->rrdhost_rwlock, NULL);
92
93     rrdhost_init_hostname(host, hostname);
94     rrdhost_init_machine_guid(host, guid);
95     rrdhost_init_os(host, os);
96
97     avl_init_lock(&(host->rrdset_root_index),      rrdset_compare);
98     avl_init_lock(&(host->rrdset_root_index_name), rrdset_compare_name);
99     avl_init_lock(&(host->rrdfamily_root_index),   rrdfamily_compare);
100     avl_init_lock(&(host->variables_root_index),   rrdvar_compare);
101
102     // ------------------------------------------------------------------------
103     // initialize health variables
104
105     host->health_log.next_log_id = 1;
106     host->health_log.next_alarm_id = 1;
107     host->health_log.max = 1000;
108     host->health_log.next_log_id =
109     host->health_log.next_alarm_id = (uint32_t)now_realtime_sec();
110
111     long n = config_get_number(CONFIG_SECTION_HEALTH, "in memory max health log entries", host->health_log.max);
112     if(n < 10) {
113         error("Host '%s': health configuration has invalid max log entries %ld. Using default %u", host->hostname, n, host->health_log.max);
114         config_set_number(CONFIG_SECTION_HEALTH, "in memory max health log entries", (long)host->health_log.max);
115     }
116     else
117         host->health_log.max = (unsigned int)n;
118
119     pthread_rwlock_init(&(host->health_log.alarm_log_rwlock), NULL);
120
121     char filename[FILENAME_MAX + 1];
122
123     if(is_localhost) {
124
125         host->cache_dir  = strdupz(netdata_configured_cache_dir);
126         host->varlib_dir = strdupz(netdata_configured_varlib_dir);
127
128     }
129     else {
130         // this is not localhost - append our GUID to localhost path
131
132         snprintfz(filename, FILENAME_MAX, "%s/%s", netdata_configured_cache_dir, host->machine_guid);
133         host->cache_dir = strdupz(filename);
134
135         if(host->rrd_memory_mode == RRD_MEMORY_MODE_MAP || host->rrd_memory_mode == RRD_MEMORY_MODE_SAVE) {
136             int r = mkdir(host->cache_dir, 0775);
137             if(r != 0 && errno != EEXIST)
138                 error("Host '%s': cannot create directory '%s'", host->hostname, host->cache_dir);
139         }
140
141         snprintfz(filename, FILENAME_MAX, "%s/%s", netdata_configured_varlib_dir, host->machine_guid);
142         host->varlib_dir = strdupz(filename);
143
144         if(host->health_enabled) {
145             int r = mkdir(host->varlib_dir, 0775);
146             if(r != 0 && errno != EEXIST)
147                 error("Host '%s': cannot create directory '%s'", host->hostname, host->varlib_dir);
148
149             snprintfz(filename, FILENAME_MAX, "%s/health", host->varlib_dir);
150             r = mkdir(filename, 0775);
151             if(r != 0 && errno != EEXIST)
152                 error("Host '%s': cannot create directory '%s'", host->hostname, filename);
153         }
154
155     }
156
157     snprintfz(filename, FILENAME_MAX, "%s/health/health-log.db", host->varlib_dir);
158     host->health_log_filename = strdupz(filename);
159
160     snprintfz(filename, FILENAME_MAX, "%s/alarm-notify.sh", netdata_configured_plugins_dir);
161     host->health_default_exec = strdupz(config_get(CONFIG_SECTION_HEALTH, "script to execute on alarm", filename));
162     host->health_default_recipient = strdup("root");
163
164
165     // ------------------------------------------------------------------------
166     // load health configuration
167
168     if(host->health_enabled) {
169         health_alarm_log_load(host);
170         health_alarm_log_open(host);
171
172         rrdhost_wrlock(host);
173         health_readdir(host, health_config_dir());
174         rrdhost_unlock(host);
175     }
176
177
178     // ------------------------------------------------------------------------
179     // link it and add it to the index
180
181     rrd_wrlock();
182
183     if(is_localhost) {
184         host->next = localhost;
185         localhost = host;
186     }
187     else {
188         if(localhost) {
189             host->next = localhost->next;
190             localhost->next = host;
191         }
192         else localhost = host;
193     }
194
195     RRDHOST *t = rrdhost_index_add(host);
196
197     if(t != host) {
198         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);
199         rrdhost_free(host);
200         host = NULL;
201     }
202     else {
203         info("Host '%s' with guid '%s' initialized"
204                      ", os: %s"
205                      ", update every: %d"
206                      ", memory mode: %s"
207                      ", history entries: %d"
208                      ", streaming: %s"
209                      " to: '%s' (api key: '%s')"
210                      ", health: %s"
211                      ", cache_dir: '%s'"
212                      ", varlib_dir: '%s'"
213                      ", health_log: '%s'"
214                      ", alarms default handler: '%s'"
215                      ", alarms default recipient: '%s'"
216              , host->hostname
217              , host->machine_guid
218              , host->os
219              , host->rrd_update_every
220              , rrd_memory_mode_name(host->rrd_memory_mode)
221              , host->rrd_history_entries
222              , host->rrdpush_enabled?"enabled":"disabled"
223              , host->rrdpush_destination
224              , host->rrdpush_api_key
225              , host->health_enabled?"enabled":"disabled"
226              , host->cache_dir
227              , host->varlib_dir
228              , host->health_log_filename
229              , host->health_default_exec
230              , host->health_default_recipient
231         );
232     }
233
234     rrd_unlock();
235
236     return host;
237 }
238
239 RRDHOST *rrdhost_find_or_create(
240           const char *hostname
241         , const char *guid
242         , const char *os
243         , int update_every
244         , int history
245         , RRD_MEMORY_MODE mode
246         , int health_enabled
247         , int rrdpush_enabled
248         , char *rrdpush_destination
249         , char *rrdpush_api_key
250 ) {
251     debug(D_RRDHOST, "Searching for host '%s' with guid '%s'", hostname, guid);
252
253     RRDHOST *host = rrdhost_find(guid, 0);
254     if(!host) {
255         host = rrdhost_create(
256                 hostname
257                 , guid
258                 , os
259                 , update_every
260                 , history
261                 , mode
262                 , health_enabled
263                 , rrdpush_enabled
264                 , rrdpush_destination
265                 , rrdpush_api_key
266                 , 0
267         );
268     }
269     else {
270         host->health_enabled = health_enabled;
271
272         if(strcmp(host->hostname, hostname)) {
273             char *t = host->hostname;
274             char *n = strdupz(hostname);
275             host->hostname = n;
276             freez(t);
277         }
278
279         if(host->rrd_update_every != update_every)
280             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);
281
282         if(host->rrd_history_entries != history)
283             error("Host '%s' has history of %d entries, but the wanted one is %d entries.", host->hostname, host->rrd_history_entries, history);
284
285         if(host->rrd_memory_mode != mode)
286             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));
287     }
288
289     return host;
290 }
291
292 // ----------------------------------------------------------------------------
293 // RRDHOST global / startup initialization
294
295 void rrd_init(char *hostname) {
296     health_init();
297     registry_init();
298     rrdpush_init();
299
300     debug(D_RRDHOST, "Initializing localhost with hostname '%s'", hostname);
301     localhost = rrdhost_create(
302             hostname
303             , registry_get_this_machine_guid()
304             , os_type
305             , default_rrd_update_every
306             , default_rrd_history_entries
307             , default_rrd_memory_mode
308             , default_health_enabled
309             , default_rrdpush_enabled
310             , default_rrdpush_destination
311             , default_rrdpush_api_key
312             , 1
313     );
314 }
315
316 // ----------------------------------------------------------------------------
317 // RRDHOST - lock validations
318 // there are only used when NETDATA_INTERNAL_CHECKS is set
319
320 void rrdhost_check_rdlock_int(RRDHOST *host, const char *file, const char *function, const unsigned long line) {
321     debug(D_RRDHOST, "Checking read lock on host '%s'", host->hostname);
322
323     int ret = pthread_rwlock_trywrlock(&host->rrdhost_rwlock);
324     if(ret == 0)
325         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);
326 }
327
328 void rrdhost_check_wrlock_int(RRDHOST *host, const char *file, const char *function, const unsigned long line) {
329     debug(D_RRDHOST, "Checking write lock on host '%s'", host->hostname);
330
331     int ret = pthread_rwlock_tryrdlock(&host->rrdhost_rwlock);
332     if(ret == 0)
333         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);
334 }
335
336 void rrd_check_rdlock_int(const char *file, const char *function, const unsigned long line) {
337     debug(D_RRDHOST, "Checking read lock on all RRDs");
338
339     int ret = pthread_rwlock_trywrlock(&rrd_rwlock);
340     if(ret == 0)
341         fatal("RRDs should be read-locked, but it are not, at function %s() at line %lu of file '%s'", function, line, file);
342 }
343
344 void rrd_check_wrlock_int(const char *file, const char *function, const unsigned long line) {
345     debug(D_RRDHOST, "Checking write lock on all RRDs");
346
347     int ret = pthread_rwlock_tryrdlock(&rrd_rwlock);
348     if(ret == 0)
349         fatal("RRDs should be write-locked, but it are not, at function %s() at line %lu of file '%s'", function, line, file);
350 }
351
352 // ----------------------------------------------------------------------------
353 // RRDHOST - free
354
355 void rrdhost_free(RRDHOST *host) {
356     if(!host) return;
357
358     info("Freeing all memory for host '%s'...", host->hostname);
359
360     rrd_check_wrlock();     // make sure the RRDs are write locked
361     rrdhost_wrlock(host);   // lock this RRDHOST
362
363     // ------------------------------------------------------------------------
364     // release its children resources
365
366     while(host->rrdset_root) rrdset_free(host->rrdset_root);
367
368     while(host->alarms) rrdcalc_free(host, host->alarms);
369     while(host->templates) rrdcalctemplate_free(host, host->templates);
370     health_alarm_log_free(host);
371
372
373     // ------------------------------------------------------------------------
374     // remove it from the indexes
375
376     if(rrdhost_index_del(host) != host)
377         error("RRDHOST '%s' removed from index, deleted the wrong entry.", host->hostname);
378
379
380     // ------------------------------------------------------------------------
381     // unlink it from the host
382
383     if(host == localhost) {
384         localhost = host->next;
385     }
386     else {
387         // find the previous one
388         RRDHOST *h;
389         for(h = localhost; h && h->next != host ; h = h->next) ;
390
391         // bypass it
392         if(h) h->next = host->next;
393         else error("Request to free RRDHOST '%s': cannot find it", host->hostname);
394     }
395
396     // ------------------------------------------------------------------------
397     // free it
398
399     rrdpush_sender_thread_stop(host);
400
401     freez(host->os);
402     freez(host->cache_dir);
403     freez(host->varlib_dir);
404     freez(host->rrdpush_api_key);
405     freez(host->rrdpush_destination);
406     freez(host->health_default_exec);
407     freez(host->health_default_recipient);
408     freez(host->health_log_filename);
409     freez(host->hostname);
410     rrdhost_unlock(host);
411     freez(host);
412
413     info("Host memory cleanup completed...");
414 }
415
416 void rrdhost_free_all(void) {
417     rrd_wrlock();
418     while(localhost) rrdhost_free(localhost);
419     rrd_unlock();
420 }
421
422 // ----------------------------------------------------------------------------
423 // RRDHOST - save
424
425 void rrdhost_save(RRDHOST *host) {
426     if(!host) return;
427
428     info("Saving host '%s' database...", host->hostname);
429
430     RRDSET *st;
431     RRDDIM *rd;
432
433     // we get a write lock
434     // to ensure only one thread is saving the database
435     rrdhost_wrlock(host);
436
437     rrdset_foreach_write(st, host) {
438         rrdset_rdlock(st);
439
440         if(st->rrd_memory_mode == RRD_MEMORY_MODE_SAVE) {
441             debug(D_RRD_STATS, "Saving stats '%s' to '%s'.", st->name, st->cache_filename);
442             savememory(st->cache_filename, st, st->memsize);
443         }
444
445         rrddim_foreach_read(rd, st) {
446             if(likely(rd->rrd_memory_mode == RRD_MEMORY_MODE_SAVE)) {
447                 debug(D_RRD_STATS, "Saving dimension '%s' to '%s'.", rd->name, rd->cache_filename);
448                 savememory(rd->cache_filename, rd, rd->memsize);
449             }
450         }
451
452         rrdset_unlock(st);
453     }
454
455     rrdhost_unlock(host);
456 }
457
458 void rrdhost_save_all(void) {
459     info("Saving database...");
460
461     rrd_rdlock();
462
463     RRDHOST *host;
464     rrdhost_foreach_read(host)
465         rrdhost_save(host);
466
467     rrd_unlock();
468 }