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