]> arthur.barton.de Git - netdata.git/blob - src/rrdhost.c
external plugins can now work for a specific host
[netdata.git] / src / rrdhost.c
1 #define NETDATA_RRD_INTERNALS 1
2 #include "common.h"
3
4 // ----------------------------------------------------------------------------
5 // RRDHOST index
6
7 int rrdhost_compare(void* a, void* b) {
8     if(((RRDHOST *)a)->hash_machine_guid < ((RRDHOST *)b)->hash_machine_guid) return -1;
9     else if(((RRDHOST *)a)->hash_machine_guid > ((RRDHOST *)b)->hash_machine_guid) return 1;
10     else return strcmp(((RRDHOST *)a)->machine_guid, ((RRDHOST *)b)->machine_guid);
11 }
12
13 avl_tree_lock rrdhost_root_index = {
14         .avl_tree = { NULL, rrdhost_compare },
15         .rwlock = AVL_LOCK_INITIALIZER
16 };
17
18 RRDHOST *rrdhost_find(const char *guid, uint32_t hash) {
19     debug(D_RRDHOST, "Searching in index for host with guid '%s'", guid);
20
21     RRDHOST tmp;
22     strncpyz(tmp.machine_guid, guid, GUID_LEN);
23     tmp.hash_machine_guid = (hash)?hash:simple_hash(tmp.machine_guid);
24
25     return (RRDHOST *)avl_search_lock(&(rrdhost_root_index), (avl *) &tmp);
26 }
27
28 #define rrdhost_index_add(rrdhost) (RRDHOST *)avl_insert_lock(&(rrdhost_root_index), (avl *)(rrdhost))
29 #define rrdhost_index_del(rrdhost) (RRDHOST *)avl_remove_lock(&(rrdhost_root_index), (avl *)(rrdhost))
30
31
32 // ----------------------------------------------------------------------------
33 // RRDHOST - internal helpers
34
35 static inline void rrdhost_init_hostname(RRDHOST *host, const char *hostname) {
36     freez(host->hostname);
37     host->hostname = strdupz(hostname);
38     host->hash_hostname = simple_hash(host->hostname);
39 }
40
41 static inline void rrdhost_init_machine_guid(RRDHOST *host, const char *machine_guid) {
42     strncpy(host->machine_guid, machine_guid, GUID_LEN);
43     host->machine_guid[GUID_LEN] = '\0';
44     host->hash_machine_guid = simple_hash(host->machine_guid);
45 }
46
47 // ----------------------------------------------------------------------------
48 // RRDHOST - add a host
49
50 RRDHOST *rrdhost_create(const char *hostname, const char *guid) {
51     debug(D_RRDHOST, "Adding host '%s' with guid '%s'", hostname, guid);
52
53     RRDHOST *host = callocz(1, sizeof(RRDHOST));
54
55     pthread_rwlock_init(&(host->rrdset_root_rwlock), NULL);
56
57     rrdhost_init_hostname(host, hostname);
58     rrdhost_init_machine_guid(host, guid);
59
60     avl_init_lock(&(host->rrdset_root_index), rrdset_compare);
61     avl_init_lock(&(host->rrdset_root_index_name), rrdset_compare_name);
62     avl_init_lock(&(host->rrdfamily_root_index), rrdfamily_compare);
63     avl_init_lock(&(host->variables_root_index), rrdvar_compare);
64
65     host->health_log.next_log_id = 1;
66     host->health_log.next_alarm_id = 1;
67     host->health_log.max = 1000;
68     host->health_log.next_log_id =
69     host->health_log.next_alarm_id = (uint32_t)now_realtime_sec();
70     pthread_rwlock_init(&(host->health_log.alarm_log_rwlock), NULL);
71
72     if(rrdhost_index_add(host) != host)
73         fatal("Cannot add host '%s' to index. It already exists.", hostname);
74
75     debug(D_RRDHOST, "Added host '%s' with guid '%s'", host->hostname, host->machine_guid);
76     return host;
77 }
78
79 RRDHOST *rrdhost_find_or_create(const char *hostname, const char *guid) {
80     debug(D_RRDHOST, "Searching for host '%s' with guid '%s'", hostname, guid);
81
82     RRDHOST *host = rrdhost_find(guid, 0);
83     if(!host)
84         host = rrdhost_create(hostname, guid);
85
86     return host;
87 }
88
89 // ----------------------------------------------------------------------------
90 // RRDHOST global / startup initialization
91
92 RRDHOST *localhost = NULL;
93
94 void rrd_init(char *hostname) {
95     debug(D_RRDHOST, "Initializing localhost with hostname '%s'", hostname);
96     localhost = rrdhost_create(hostname, registry_get_this_machine_guid());
97 }
98
99 // ----------------------------------------------------------------------------
100 // RRDHOST - locks
101
102 void rrdhost_rwlock(RRDHOST *host) {
103     debug(D_RRDHOST, "Write lock host '%s'", host->hostname);
104     pthread_rwlock_wrlock(&host->rrdset_root_rwlock);
105 }
106
107 void rrdhost_rdlock(RRDHOST *host) {
108     debug(D_RRDHOST, "Read lock host '%s'", host->hostname);
109     pthread_rwlock_rdlock(&host->rrdset_root_rwlock);
110 }
111
112 void rrdhost_unlock(RRDHOST *host) {
113     debug(D_RRDHOST, "Unlock host '%s'", host->hostname);
114     pthread_rwlock_unlock(&host->rrdset_root_rwlock);
115 }
116
117 void rrdhost_check_rdlock_int(RRDHOST *host, const char *file, const char *function, const unsigned long line) {
118     debug(D_RRDHOST, "Read lock host '%s'", host->hostname);
119
120     int ret = pthread_rwlock_trywrlock(&host->rrdset_root_rwlock);
121     if(ret == 0)
122         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);
123 }
124
125 void rrdhost_check_wrlock_int(RRDHOST *host, const char *file, const char *function, const unsigned long line) {
126     debug(D_RRDHOST, "Write lock host '%s'", host->hostname);
127
128     int ret = pthread_rwlock_tryrdlock(&host->rrdset_root_rwlock);
129     if(ret == 0)
130         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);
131 }
132
133 void rrdhost_free(RRDHOST *host) {
134     if(!host) return;
135
136     info("Freeing all memory for host '%s'...", host->hostname);
137
138     rrdhost_rwlock(host);
139
140     RRDSET *st;
141     for(st = host->rrdset_root; st ;) {
142         RRDSET *next = st->next;
143
144         pthread_rwlock_wrlock(&st->rwlock);
145
146         while(st->variables)
147             rrdsetvar_free(st->variables);
148
149         while(st->alarms)
150             rrdsetcalc_unlink(st->alarms);
151
152         while(st->dimensions)
153             rrddim_free(st, st->dimensions);
154
155         if(unlikely(rrdset_index_del(host, st) != st))
156             error("RRDSET: INTERNAL ERROR: attempt to remove from index chart '%s', removed a different chart.", st->id);
157
158         rrdset_index_del_name(host, st);
159
160         st->rrdfamily->use_count--;
161         if(!st->rrdfamily->use_count)
162             rrdfamily_free(host, st->rrdfamily);
163
164         pthread_rwlock_unlock(&st->rwlock);
165
166         if(st->mapped == RRD_MEMORY_MODE_SAVE || st->mapped == RRD_MEMORY_MODE_MAP) {
167             debug(D_RRD_CALLS, "Unmapping stats '%s'.", st->name);
168             munmap(st, st->memsize);
169         }
170         else
171             freez(st);
172
173         st = next;
174     }
175     host->rrdset_root = NULL;
176
177     freez(host->hostname);
178     rrdhost_unlock(host);
179     freez(host);
180
181     info("Host memory cleanup completed...");
182 }
183
184 void rrdhost_save(RRDHOST *host) {
185     if(!host) return;
186
187     info("Saving host '%s' database...", host->hostname);
188
189     RRDSET *st;
190     RRDDIM *rd;
191
192     // we get an write lock
193     // to ensure only one thread is saving the database
194     rrdhost_rwlock(host);
195
196     for(st = host->rrdset_root; st ; st = st->next) {
197         pthread_rwlock_rdlock(&st->rwlock);
198
199         if(st->mapped == RRD_MEMORY_MODE_SAVE) {
200             debug(D_RRD_CALLS, "Saving stats '%s' to '%s'.", st->name, st->cache_filename);
201             savememory(st->cache_filename, st, st->memsize);
202         }
203
204         for(rd = st->dimensions; rd ; rd = rd->next) {
205             if(likely(rd->memory_mode == RRD_MEMORY_MODE_SAVE)) {
206                 debug(D_RRD_CALLS, "Saving dimension '%s' to '%s'.", rd->name, rd->cache_filename);
207                 savememory(rd->cache_filename, rd, rd->memsize);
208             }
209         }
210
211         pthread_rwlock_unlock(&st->rwlock);
212     }
213
214     rrdhost_unlock(host);
215 }
216
217 void rrdhost_free_all(void) {
218     RRDHOST *host = localhost;
219
220     // FIXME: lock all hosts
221
222     while(host) {
223         RRDHOST *next = host = host->next;
224         rrdhost_free(host);
225         host = next;
226     }
227
228     localhost = NULL;
229
230     // FIXME: unlock all hosts
231 }
232
233 void rrdhost_save_all(void) {
234     RRDHOST *host;
235     for(host = localhost; host ; host = host->next)
236         rrdhost_save(host);
237 }