]> arthur.barton.de Git - netdata.git/blob - src/rrdhost.c
7c6d6418d8cec76a12b673a7f2238070ac9760ef
[netdata.git] / src / rrdhost.c
1 #define NETDATA_RRD_INTERNALS 1
2 #include "common.h"
3
4 RRDHOST *localhost = NULL;
5
6 // ----------------------------------------------------------------------------
7 // RRDHOST index
8
9 int rrdhost_compare(void* a, void* b) {
10     if(((RRDHOST *)a)->hash_machine_guid < ((RRDHOST *)b)->hash_machine_guid) return -1;
11     else if(((RRDHOST *)a)->hash_machine_guid > ((RRDHOST *)b)->hash_machine_guid) return 1;
12     else return strcmp(((RRDHOST *)a)->machine_guid, ((RRDHOST *)b)->machine_guid);
13 }
14
15 avl_tree_lock rrdhost_root_index = {
16         .avl_tree = { NULL, rrdhost_compare },
17         .rwlock = AVL_LOCK_INITIALIZER
18 };
19
20 RRDHOST *rrdhost_find(const char *guid, uint32_t hash) {
21     debug(D_RRDHOST, "Searching in index for host with guid '%s'", guid);
22
23     RRDHOST tmp;
24     strncpyz(tmp.machine_guid, guid, GUID_LEN);
25     tmp.hash_machine_guid = (hash)?hash:simple_hash(tmp.machine_guid);
26
27     return (RRDHOST *)avl_search_lock(&(rrdhost_root_index), (avl *) &tmp);
28 }
29
30 #define rrdhost_index_add(rrdhost) (RRDHOST *)avl_insert_lock(&(rrdhost_root_index), (avl *)(rrdhost))
31 #define rrdhost_index_del(rrdhost) (RRDHOST *)avl_remove_lock(&(rrdhost_root_index), (avl *)(rrdhost))
32
33
34 // ----------------------------------------------------------------------------
35 // RRDHOST - internal helpers
36
37 static inline void rrdhost_init_hostname(RRDHOST *host, const char *hostname) {
38     freez(host->hostname);
39     host->hostname = strdupz(hostname);
40     host->hash_hostname = simple_hash(host->hostname);
41 }
42
43 static inline void rrdhost_init_machine_guid(RRDHOST *host, const char *machine_guid) {
44     strncpy(host->machine_guid, machine_guid, GUID_LEN);
45     host->machine_guid[GUID_LEN] = '\0';
46     host->hash_machine_guid = simple_hash(host->machine_guid);
47 }
48
49 // ----------------------------------------------------------------------------
50 // RRDHOST - add a host
51
52 RRDHOST *rrdhost_create(const char *hostname,
53         const char *guid,
54         int update_every,
55         int entries,
56         RRD_MEMORY_MODE memory_mode,
57         int health_enabled) {
58
59     debug(D_RRDHOST, "Adding host '%s' with guid '%s'", hostname, guid);
60
61     RRDHOST *host = callocz(1, sizeof(RRDHOST));
62
63     host->rrd_update_every    = update_every;
64     host->rrd_history_entries = entries;
65     host->rrd_memory_mode     = memory_mode;
66     host->health_enabled      = health_enabled;
67
68     pthread_rwlock_init(&(host->rrdhost_rwlock), NULL);
69
70     rrdhost_init_hostname(host, hostname);
71     rrdhost_init_machine_guid(host, guid);
72
73     avl_init_lock(&(host->rrdset_root_index), rrdset_compare);
74     avl_init_lock(&(host->rrdset_root_index_name), rrdset_compare_name);
75     avl_init_lock(&(host->rrdfamily_root_index), rrdfamily_compare);
76     avl_init_lock(&(host->variables_root_index), rrdvar_compare);
77
78
79     // ------------------------------------------------------------------------
80     // initialize health variables
81
82     host->health_log.next_log_id = 1;
83     host->health_log.next_alarm_id = 1;
84     host->health_log.max = 1000;
85     host->health_log.next_log_id =
86     host->health_log.next_alarm_id = (uint32_t)now_realtime_sec();
87
88     long n = config_get_number("health", "in memory max health log entries", host->health_log.max);
89     if(n < 10) {
90         error("Health configuration has invalid max log entries %ld. Using default %u", n, host->health_log.max);
91         config_set_number("health", "in memory max health log entries", (long)host->health_log.max);
92     }
93     else
94         host->health_log.max = (unsigned int)n;
95
96     pthread_rwlock_init(&(host->health_log.alarm_log_rwlock), NULL);
97
98     char filename[FILENAME_MAX + 1];
99
100     if(!localhost) {
101         // this is localhost
102
103         host->cache_dir = strdupz(netdata_configured_cache_dir);
104         host->varlib_dir = strdupz(netdata_configured_varlib_dir);
105
106         snprintfz(filename, FILENAME_MAX, "%s/health/health-log.db", host->varlib_dir);
107         host->health_log_filename = strdupz(config_get("health", "health db file", filename));
108
109     }
110     else {
111         // this is not localhost - append our GUID to localhost path
112
113         snprintfz(filename, FILENAME_MAX, "%s/%s", netdata_configured_cache_dir, host->machine_guid);
114         host->cache_dir = strdupz(filename);
115
116         if(host->rrd_memory_mode == RRD_MEMORY_MODE_MAP || host->rrd_memory_mode == RRD_MEMORY_MODE_SAVE) {
117             int r = mkdir(host->cache_dir, 0775);
118             if(r != 0 && errno != EEXIST)
119                 error("Cannot create directory '%s'", host->cache_dir);
120         }
121
122         snprintfz(filename, FILENAME_MAX, "%s/%s", netdata_configured_varlib_dir, host->machine_guid);
123         host->varlib_dir = strdupz(filename);
124
125         if(host->health_enabled) {
126             int r = mkdir(host->varlib_dir, 0775);
127             if(r != 0 && errno != EEXIST)
128                 error("Cannot create directory '%s'", host->varlib_dir);
129         }
130
131         snprintfz(filename, FILENAME_MAX, "%s/health/health-log.db", host->varlib_dir);
132         host->health_log_filename = strdupz(filename);
133
134     }
135
136     snprintfz(filename, FILENAME_MAX, "%s/alarm-notify.sh", netdata_configured_plugins_dir);
137     host->health_default_exec = strdupz(config_get("health", "script to execute on alarm", filename));
138     host->health_default_recipient = strdup("root");
139
140
141     // ------------------------------------------------------------------------
142     // load health configuration
143
144     health_alarm_log_load(host);
145     health_alarm_log_open(host);
146
147     rrdhost_wrlock(host);
148     health_readdir(host, health_config_dir());
149     rrdhost_unlock(host);
150
151
152     // ------------------------------------------------------------------------
153     // add it to the index
154
155     if(rrdhost_index_add(host) != host)
156         fatal("Cannot add host '%s' to index. It already exists.", hostname);
157
158     debug(D_RRDHOST, "Added host '%s' with guid '%s'", host->hostname, host->machine_guid);
159     return host;
160 }
161
162 RRDHOST *rrdhost_find_or_create(const char *hostname, const char *guid) {
163     debug(D_RRDHOST, "Searching for host '%s' with guid '%s'", hostname, guid);
164
165     RRDHOST *host = rrdhost_find(guid, 0);
166     if(!host)
167         host = rrdhost_create(hostname,
168                 guid,
169                 default_localhost_rrd_update_every,
170                 default_localhost_rrd_history_entries,
171                 default_localhost_rrd_memory_mode,
172                 default_localhost_health_enabled
173         );
174
175     return host;
176 }
177
178 // ----------------------------------------------------------------------------
179 // RRDHOST global / startup initialization
180
181 void rrd_init(char *hostname) {
182     debug(D_RRDHOST, "Initializing localhost with hostname '%s'", hostname);
183
184     localhost = rrdhost_create(hostname,
185             registry_get_this_machine_guid(),
186             default_localhost_rrd_update_every,
187             default_localhost_rrd_history_entries,
188             default_localhost_rrd_memory_mode,
189             default_localhost_health_enabled
190     );
191 }
192
193 // ----------------------------------------------------------------------------
194 // RRDHOST - lock validations
195 // there are only used when NETDATA_INTERNAL_CHECKS is set
196
197 void rrdhost_check_rdlock_int(RRDHOST *host, const char *file, const char *function, const unsigned long line) {
198     debug(D_RRDHOST, "Checking read lock on host '%s'", host->hostname);
199
200     int ret = pthread_rwlock_trywrlock(&host->rrdhost_rwlock);
201     if(ret == 0)
202         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);
203 }
204
205 void rrdhost_check_wrlock_int(RRDHOST *host, const char *file, const char *function, const unsigned long line) {
206     debug(D_RRDHOST, "Checking write lock on host '%s'", host->hostname);
207
208     int ret = pthread_rwlock_tryrdlock(&host->rrdhost_rwlock);
209     if(ret == 0)
210         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);
211 }
212
213 // ----------------------------------------------------------------------------
214 // RRDHOST - free
215
216 void rrdhost_free(RRDHOST *host) {
217     if(!host) return;
218
219     info("Freeing all memory for host '%s'...", host->hostname);
220
221     rrdhost_wrlock(host);
222
223     RRDSET *st;
224     for(st = host->rrdset_root; st ;) {
225         RRDSET *next = st->next;
226
227         rrdset_free(st);
228
229         st = next;
230     }
231     host->rrdset_root = NULL;
232
233     if(rrdhost_index_del(host) != host)
234         error("RRDHOST '%s' removed from index, deleted the wrong entry.", host->hostname);
235
236     freez(host->cache_dir);
237     freez(host->varlib_dir);
238     freez(host->health_default_exec);
239     freez(host->health_default_recipient);
240     freez(host->health_log_filename);
241     freez(host->hostname);
242     rrdhost_unlock(host);
243     freez(host);
244
245     info("Host memory cleanup completed...");
246 }
247
248 void rrdhost_free_all(void) {
249     RRDHOST *host = localhost;
250
251     // FIXME: lock all hosts
252
253     while(host) {
254         RRDHOST *next = host = host->next;
255         rrdhost_free(host);
256         host = next;
257     }
258
259     localhost = NULL;
260
261     // FIXME: unlock all hosts
262 }
263
264 // ----------------------------------------------------------------------------
265 // RRDHOST - save
266
267 void rrdhost_save(RRDHOST *host) {
268     if(!host) return;
269
270     info("Saving host '%s' database...", host->hostname);
271
272     RRDSET *st;
273     RRDDIM *rd;
274
275     // we get a write lock
276     // to ensure only one thread is saving the database
277     rrdhost_wrlock(host);
278
279     for(st = host->rrdset_root; st ; st = st->next) {
280         rrdset_rdlock(st);
281
282         if(st->rrd_memory_mode == RRD_MEMORY_MODE_SAVE) {
283             debug(D_RRD_STATS, "Saving stats '%s' to '%s'.", st->name, st->cache_filename);
284             savememory(st->cache_filename, st, st->memsize);
285         }
286
287         for(rd = st->dimensions; rd ; rd = rd->next) {
288             if(likely(rd->rrd_memory_mode == RRD_MEMORY_MODE_SAVE)) {
289                 debug(D_RRD_STATS, "Saving dimension '%s' to '%s'.", rd->name, rd->cache_filename);
290                 savememory(rd->cache_filename, rd, rd->memsize);
291             }
292         }
293
294         rrdset_unlock(st);
295     }
296
297     rrdhost_unlock(host);
298 }
299
300 void rrdhost_save_all(void) {
301     info("Saving database...");
302
303     RRDHOST *host;
304     for(host = localhost; host ; host = host->next)
305         rrdhost_save(host);
306 }