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