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