@cryptotaxi247 / netdata-1 / commits / eb304fda7

localhost is now dynamic

Costa Tsaousis (ktsaou) committed Feb 18, 2017 at 13:03 UTC eb304fda7a26091ffc2a4dd6392b65f06d387149
19 files changed +232 -170
src/Makefile.am
+1 -1
@@ -30,7 +30,7 @@ endif
30
31 netdata_SOURCES = \
32 appconfig.c appconfig.h \
33 - adaptive_resortable_list.c \adaptive_resortable_list.h \
33 + adaptive_resortable_list.c adaptive_resortable_list.h \
34 avl.c avl.h \
35 backends.c backends.h \
36 clocks.c clocks.h \
src/backends.c
+5 -5
@@ -157,7 +157,7 @@ void *backends_main(void *ptr) {
157 const char *type = config_get("backend", "type", "graphite");
158 const char *destination = config_get("backend", "destination", "localhost");
159 const char *prefix = config_get("backend", "prefix", "netdata");
160 - const char *hostname = config_get("backend", "hostname", localhost.hostname);
160 + const char *hostname = config_get("backend", "hostname", localhost->hostname);
161 int frequency = (int)config_get_number("backend", "update every", 10);
162 int buffer_on_failures = (int)config_get_number("backend", "buffer on failures", 10);
163 long timeoutms = config_get_number("backend", "timeout ms", frequency * 2 * 1000);
@@ -310,19 +310,19 @@ void *backends_main(void *ptr) {
310 if(unlikely(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &pthreadoldcancelstate) != 0))
311 error("Cannot set pthread cancel state to DISABLE.");
312
313 - rrdhost_rdlock(&localhost);
314 - for(st = localhost.rrdset_root; st ;st = st->next) {
313 + rrdhost_rdlock(localhost);
314 + for(st = localhost->rrdset_root; st ;st = st->next) {
315 pthread_rwlock_rdlock(&st->rwlock);
316
317 RRDDIM *rd;
318 for(rd = st->dimensions; rd ;rd = rd->next) {
319 if(rd->last_collected_time.tv_sec >= after)
320 - chart_buffered_metrics += backend_request_formatter(b, prefix, &localhost, hostname, st, rd, after, before, options);
320 + chart_buffered_metrics += backend_request_formatter(b, prefix, localhost, hostname, st, rd, after, before, options);
321 }
322
323 pthread_rwlock_unlock(&st->rwlock);
324 }
325 - rrdhost_unlock(&localhost);
325 + rrdhost_unlock(localhost);
326
327 if(unlikely(pthread_setcancelstate(pthreadoldcancelstate, NULL) != 0))
328 error("Cannot set pthread cancel state to RESTORE (%d).", pthreadoldcancelstate);
src/health.c
+40 -40
@@ -1555,7 +1555,7 @@ static inline int rrdcalc_add_alarm_from_config(RRDHOST *host, RRDCALC *rc) {
1555 if (rrdcalc_exists(host, rc->chart, rc->name, rc->hash_chart, rc->hash))
1556 return 0;
1557
1558 - rc->id = rrdcalc_get_unique_id(&localhost, rc->chart, rc->name, &rc->next_event_id);
1558 + rc->id = rrdcalc_get_unique_id(localhost, rc->chart, rc->name, &rc->next_event_id);
1559
1560 debug(D_HEALTH, "Health configuration adding alarm '%s.%s' (%u): exec '%s', recipient '%s', green %Lf, red %Lf, lookup: group %d, after %d, before %d, options %u, dimensions '%s', update every %d, calculation '%s', warning '%s', critical '%s', source '%s', delay up %d, delay down %d, delay max %d, delay_multiplier %f",
1561 rc->chart?rc->chart:"NOCHART",
@@ -2040,12 +2040,12 @@ int health_readfile(const char *path, const char *filename) {
2040 uint32_t hash = simple_uhash(key);
2041
2042 if(hash == hash_alarm && !strcasecmp(key, HEALTH_ALARM_KEY)) {
2043 - if(rc && !rrdcalc_add_alarm_from_config(&localhost, rc))
2044 - rrdcalc_free(&localhost, rc);
2043 + if(rc && !rrdcalc_add_alarm_from_config(localhost, rc))
2044 + rrdcalc_free(localhost, rc);
2045
2046 if(rt) {
2047 - if (!rrdcalctemplate_add_template_from_config(&localhost, rt))
2048 - rrdcalctemplate_free(&localhost, rt);
2047 + if (!rrdcalctemplate_add_template_from_config(localhost, rt))
2048 + rrdcalctemplate_free(localhost, rt);
2049 rt = NULL;
2050 }
2051
@@ -2065,13 +2065,13 @@ int health_readfile(const char *path, const char *filename) {
2065 }
2066 else if(hash == hash_template && !strcasecmp(key, HEALTH_TEMPLATE_KEY)) {
2067 if(rc) {
2068 - if(!rrdcalc_add_alarm_from_config(&localhost, rc))
2069 - rrdcalc_free(&localhost, rc);
2068 + if(!rrdcalc_add_alarm_from_config(localhost, rc))
2069 + rrdcalc_free(localhost, rc);
2070 rc = NULL;
2071 }
2072
2073 - if(rt && !rrdcalctemplate_add_template_from_config(&localhost, rt))
2074 - rrdcalctemplate_free(&localhost, rt);
2073 + if(rt && !rrdcalctemplate_add_template_from_config(localhost, rt))
2074 + rrdcalctemplate_free(localhost, rt);
2075
2076 rt = callocz(1, sizeof(RRDCALCTEMPLATE));
2077 rt->name = strdupz(value);
@@ -2332,11 +2332,11 @@ int health_readfile(const char *path, const char *filename) {
2332 }
2333 }
2334
2335 - if(rc && !rrdcalc_add_alarm_from_config(&localhost, rc))
2336 - rrdcalc_free(&localhost, rc);
2335 + if(rc && !rrdcalc_add_alarm_from_config(localhost, rc))
2336 + rrdcalc_free(localhost, rc);
2337
2338 - if(rt && !rrdcalctemplate_add_template_from_config(&localhost, rt))
2339 - rrdcalctemplate_free(&localhost, rt);
2338 + if(rt && !rrdcalctemplate_add_template_from_config(localhost, rt))
2339 + rrdcalctemplate_free(localhost, rt);
2340
2341 fclose(fp);
2342 return 1;
@@ -2410,7 +2410,7 @@ void health_init(void) {
2410 snprintfz(filename, FILENAME_MAX, "%s/health-log.db", pathname);
2411 health.log_filename = config_get("health", "health db file", filename);
2412
2413 - health_alarm_log_load(&localhost);
2413 + health_alarm_log_load(localhost);
2414 health_alarm_log_open();
2415
2416 char *path = health_config_dir();
@@ -2418,16 +2418,16 @@ void health_init(void) {
2418 snprintfz(filename, FILENAME_MAX, "%s/alarm-notify.sh", netdata_configured_plugins_dir);
2419 health.health_default_exec = config_get("health", "script to execute on alarm", filename);
2420
2421 - long n = config_get_number("health", "in memory max health log entries", (long)localhost.health_log.max);
2421 + long n = config_get_number("health", "in memory max health log entries", (long)localhost->health_log.max);
2422 if(n < 10) {
2423 - error("Health configuration has invalid max log entries %ld. Using default %u", n, localhost.health_log.max);
2424 - config_set_number("health", "in memory max health log entries", (long)localhost.health_log.max);
2423 + error("Health configuration has invalid max log entries %ld. Using default %u", n, localhost->health_log.max);
2424 + config_set_number("health", "in memory max health log entries", (long)localhost->health_log.max);
2425 }
2426 - else localhost.health_log.max = (unsigned int)n;
2426 + else localhost->health_log.max = (unsigned int)n;
2427
2428 - rrdhost_rwlock(&localhost);
2428 + rrdhost_rwlock(localhost);
2429 health_readdir(path);
2430 - rrdhost_unlock(&localhost);
2430 + rrdhost_unlock(localhost);
2431 }
2432
2433 // ----------------------------------------------------------------------------
@@ -2651,7 +2651,7 @@ static inline void health_rrdcalc2json_nolock(BUFFER *wb, RRDCALC *rc) {
2651 void health_alarms2json(RRDHOST *host, BUFFER *wb, int all) {
2652 int i;
2653
2654 - rrdhost_rdlock(&localhost);
2654 + rrdhost_rdlock(localhost);
2655 buffer_sprintf(wb, "{\n\t\"hostname\": \"%s\","
2656 "\n\t\"latest_alarm_log_unique_id\": %u,"
2657 "\n\t\"status\": %s,"
@@ -2681,7 +2681,7 @@ void health_alarms2json(RRDHOST *host, BUFFER *wb, int all) {
2681 // health_rrdcalctemplate2json_nolock(wb, rt);
2682
2683 buffer_strcat(wb, "\n\t}\n}\n");
2684 - rrdhost_unlock(&localhost);
2684 + rrdhost_unlock(localhost);
2685 }
2686
2687
@@ -2705,37 +2705,37 @@ void health_reload(void) {
2705 char *path = health_config_dir();
2706
2707 // free all running alarms
2708 - rrdhost_rwlock(&localhost);
2709 - health_free_all_nolock(&localhost);
2710 - rrdhost_unlock(&localhost);
2708 + rrdhost_rwlock(localhost);
2709 + health_free_all_nolock(localhost);
2710 + rrdhost_unlock(localhost);
2711
2712 // invalidate all previous entries in the alarm log
2713 ALARM_ENTRY *t;
2714 - for(t = localhost.health_log.alarms ; t ; t = t->next) {
2714 + for(t = localhost->health_log.alarms ; t ; t = t->next) {
2715 if(t->new_status != RRDCALC_STATUS_REMOVED)
2716 t->flags |= HEALTH_ENTRY_FLAG_UPDATED;
2717 }
2718
2719 // reset all thresholds to all charts
2720 RRDSET *st;
2721 - for(st = localhost.rrdset_root; st ; st = st->next) {
2721 + for(st = localhost->rrdset_root; st ; st = st->next) {
2722 st->green = NAN;
2723 st->red = NAN;
2724 }
2725
2726 // load the new alarms
2727 - rrdhost_rwlock(&localhost);
2727 + rrdhost_rwlock(localhost);
2728 health_readdir(path);
2729 - rrdhost_unlock(&localhost);
2729 + rrdhost_unlock(localhost);
2730
2731 // link the loaded alarms to their charts
2732 - for(st = localhost.rrdset_root; st ; st = st->next) {
2733 - rrdhost_rwlock(&localhost);
2732 + for(st = localhost->rrdset_root; st ; st = st->next) {
2733 + rrdhost_rwlock(localhost);
2734
2735 rrdsetcalc_link_matching(st);
2736 rrdcalctemplate_link_matching(st);
2737
2738 - rrdhost_unlock(&localhost);
2738 + rrdhost_unlock(localhost);
2739 }
2740 }
2741
@@ -3013,10 +3013,10 @@ void *health_main(void *ptr) {
3013 if(unlikely(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldstate) != 0))
3014 error("Cannot set pthread cancel state to DISABLE.");
3015
3016 - rrdhost_rdlock(&localhost);
3016 + rrdhost_rdlock(localhost);
3017
3018 // the first loop is to lookup values from the db
3019 - for(rc = localhost.alarms; rc; rc = rc->next) {
3019 + for(rc = localhost->alarms; rc; rc = rc->next) {
3020 if(unlikely(!rrdcalc_isrunnable(rc, now, &next_run))) {
3021 if(unlikely(rc->rrdcalc_flags & RRDCALC_FLAG_RUNNABLE))
3022 rc->rrdcalc_flags &= ~RRDCALC_FLAG_RUNNABLE;
@@ -3121,12 +3121,12 @@ void *health_main(void *ptr) {
3121 }
3122 }
3123 }
3124 - rrdhost_unlock(&localhost);
3124 + rrdhost_unlock(localhost);
3125
3126 if(unlikely(runnable && !netdata_exit)) {
3127 - rrdhost_rdlock(&localhost);
3127 + rrdhost_rdlock(localhost);
3128
3129 - for(rc = localhost.alarms; rc; rc = rc->next) {
3129 + for(rc = localhost->alarms; rc; rc = rc->next) {
3130 if(unlikely(!(rc->rrdcalc_flags & RRDCALC_FLAG_RUNNABLE)))
3131 continue;
3132
@@ -3251,7 +3251,7 @@ void *health_main(void *ptr) {
3251 rc->delay_last = delay;
3252 rc->delay_up_to_timestamp = now + delay;
3253 health_alarm_log(
3254 - &localhost,
3254 + localhost,
3255 rc->id,
3256 rc->next_event_id++,
3257 now,
@@ -3282,7 +3282,7 @@ void *health_main(void *ptr) {
3282 next_run = rc->next_update;
3283 }
3284
3285 - rrdhost_unlock(&localhost);
3285 + rrdhost_unlock(localhost);
3286 }
3287
3288 if (unlikely(pthread_setcancelstate(oldstate, NULL) != 0))
@@ -3293,7 +3293,7 @@ void *health_main(void *ptr) {
3293
3294 // execute notifications
3295 // and cleanup
3296 - health_alarm_log_process(&localhost);
3296 + health_alarm_log_process(localhost);
3297
3298 if(unlikely(netdata_exit))
3299 break;
src/ipc.c
+2 -2
@@ -184,8 +184,8 @@ int do_ipc(int update_every, usec_t dt) {
184 return 1;
185 }
186
187 - arrays_max = rrdvar_custom_host_variable_create(&localhost, "ipc.semaphores.arrays.max");
188 - semaphores_max = rrdvar_custom_host_variable_create(&localhost, "ipc.semaphores.max");
187 + arrays_max = rrdvar_custom_host_variable_create(localhost, "ipc.semaphores.arrays.max");
188 + semaphores_max = rrdvar_custom_host_variable_create(localhost, "ipc.semaphores.max");
189
190 if(arrays_max) rrdvar_custom_host_variable_set(arrays_max, limits.semmni);
191 if(semaphores_max) rrdvar_custom_host_variable_set(semaphores_max, limits.semmns);
src/log.c
+1 -1
@@ -1,7 +1,7 @@
1 #include "common.h"
2
3 const char *program_name = "";
4 -unsigned long long debug_flags = DEBUG;
4 +uint64_t debug_flags = DEBUG;
5
6 int access_log_syslog = 1;
7 int error_log_syslog = 1;
src/log.h
+28 -27
@@ -1,38 +1,39 @@
1 #ifndef NETDATA_LOG_H
2 #define NETDATA_LOG_H 1
3
4 -#define D_WEB_BUFFER 0x00000001
5 -#define D_WEB_CLIENT 0x00000002
6 -#define D_LISTENER 0x00000004
7 -#define D_WEB_DATA 0x00000008
8 -#define D_OPTIONS 0x00000010
9 -#define D_PROCNETDEV_LOOP 0x00000020
10 -#define D_RRD_STATS 0x00000040
11 -#define D_WEB_CLIENT_ACCESS 0x00000080
12 -#define D_TC_LOOP 0x00000100
13 -#define D_DEFLATE 0x00000200
14 -#define D_CONFIG 0x00000400
15 -#define D_PLUGINSD 0x00000800
16 -#define D_CHILDS 0x00001000
17 -#define D_EXIT 0x00002000
18 -#define D_CHECKS 0x00004000
19 -#define D_NFACCT_LOOP 0x00008000
20 -#define D_PROCFILE 0x00010000
21 -#define D_RRD_CALLS 0x00020000
22 -#define D_DICTIONARY 0x00040000
23 -#define D_MEMORY 0x00080000
24 -#define D_CGROUP 0x00100000
25 -#define D_REGISTRY 0x00200000
26 -#define D_VARIABLES 0x00400000
27 -#define D_HEALTH 0x00800000
28 -#define D_CONNECT_TO 0x01000000
29 -#define D_SYSTEM 0x80000000
4 +#define D_WEB_BUFFER 0x0000000000000001
5 +#define D_WEB_CLIENT 0x0000000000000002
6 +#define D_LISTENER 0x0000000000000004
7 +#define D_WEB_DATA 0x0000000000000008
8 +#define D_OPTIONS 0x0000000000000010
9 +#define D_PROCNETDEV_LOOP 0x0000000000000020
10 +#define D_RRD_STATS 0x0000000000000040
11 +#define D_WEB_CLIENT_ACCESS 0x0000000000000080
12 +#define D_TC_LOOP 0x0000000000000100
13 +#define D_DEFLATE 0x0000000000000200
14 +#define D_CONFIG 0x0000000000000400
15 +#define D_PLUGINSD 0x0000000000000800
16 +#define D_CHILDS 0x0000000000001000
17 +#define D_EXIT 0x0000000000002000
18 +#define D_CHECKS 0x0000000000004000
19 +#define D_NFACCT_LOOP 0x0000000000008000
20 +#define D_PROCFILE 0x0000000000010000
21 +#define D_RRD_CALLS 0x0000000000020000
22 +#define D_DICTIONARY 0x0000000000040000
23 +#define D_MEMORY 0x0000000000080000
24 +#define D_CGROUP 0x0000000000100000
25 +#define D_REGISTRY 0x0000000000200000
26 +#define D_VARIABLES 0x0000000000400000
27 +#define D_HEALTH 0x0000000000800000
28 +#define D_CONNECT_TO 0x0000000001000000
29 +#define D_RRDHOST 0x0000000002000000
30 +#define D_SYSTEM 0x8000000000000000
31
32 //#define DEBUG (D_WEB_CLIENT_ACCESS|D_LISTENER|D_RRD_STATS)
33 //#define DEBUG 0xffffffff
34 #define DEBUG (0)
35
35 -extern unsigned long long debug_flags;
36 +extern uint64_t debug_flags;
37
38 extern const char *program_name;
39
src/main.c
+7 -6
@@ -23,6 +23,7 @@ void netdata_cleanup_and_exit(int ret) {
23 //kill_childs();
24
25 // free database
26 + sleep(2);
27 rrdhost_free_all();
28 #endif
29
@@ -578,11 +579,11 @@ int main(int argc, char **argv) {
579
580 char *user = NULL;
581 {
581 - char *flags = config_get("global", "debug flags", "0x00000000");
582 + char *flags = config_get("global", "debug flags", "0x0000000000000000");
583 setenv("NETDATA_DEBUG_FLAGS", flags, 1);
584
585 debug_flags = strtoull(flags, NULL, 0);
585 - debug(D_OPTIONS, "Debug flags set to '0x%8llx'.", debug_flags);
586 + debug(D_OPTIONS, "Debug flags set to '0x%" PRIX64 "'.", debug_flags);
587
588 if(debug_flags != 0) {
589 struct rlimit rl = { RLIM_INFINITY, RLIM_INFINITY };
@@ -795,14 +796,14 @@ int main(int argc, char **argv) {
796 }
797
798 // ------------------------------------------------------------------------
798 - // initialize rrd host
799 + // initialize the registry
800
800 - rrdhost_init(hostname);
801 + registry_init();
802
803 // ------------------------------------------------------------------------
803 - // initialize the registry
804 + // initialize rrd host
805
805 - registry_init();
806 + rrd_init(hostname);
807
808 // ------------------------------------------------------------------------
809 // initialize health monitoring
src/plugins_d.c
+1 -1
@@ -109,7 +109,7 @@ void *pluginsd_worker_thread(void *arg) {
109 #endif
110
111 size_t count = 0;
112 - RRDHOST *host = &localhost;
112 + RRDHOST *host = localhost;
113
114 for(;;) {
115 if(unlikely(netdata_exit)) break;
src/proc_net_stat_conntrack.c
+1 -1
@@ -47,7 +47,7 @@ int do_proc_net_stat_conntrack(int update_every, usec_t dt) {
47 if(!do_sockets && !read_full)
48 return 1;
49
50 - rrdvar_max = rrdvar_custom_host_variable_create(&localhost, "netfilter.conntrack.max");
50 + rrdvar_max = rrdvar_custom_host_variable_create(localhost, "netfilter.conntrack.max");
51 }
52
53 if(likely(read_full)) {
src/registry.c
+1 -1
@@ -45,7 +45,7 @@ static inline void registry_json_header(struct web_client *w, const char *action
45 buffer_flush(w->response.data);
46 w->response.data->contenttype = CT_APPLICATION_JSON;
47 buffer_sprintf(w->response.data, "{\n\t\"action\": \"%s\",\n\t\"status\": \"%s\",\n\t\"hostname\": \"%s\",\n\t\"machine_guid\": \"%s\"",
48 - action, status, registry.hostname, localhost.machine_guid);
48 + action, status, registry.hostname, localhost->machine_guid);
49 }
50
51 static inline void registry_json_footer(struct web_client *w) {
src/registry.h
+2
@@ -69,4 +69,6 @@ extern int registry_request_hello_json(struct web_client *w);
69 // update the registry monitoring charts
70 extern void registry_statistics(void);
71
72 +extern char *registry_get_this_machine_guid(void);
73 +
74 #endif /* NETDATA_REGISTRY_H */
src/registry_init.c
+1 -2
@@ -15,7 +15,6 @@ int registry_init(void) {
15 // filenames
16 snprintfz(filename, FILENAME_MAX, "%s/netdata.public.unique.id", registry.pathname);
17 registry.machine_guid_filename = config_get("registry", "netdata unique id file", filename);
18 - registry_get_this_machine_guid();
18
19 snprintfz(filename, FILENAME_MAX, "%s/registry.db", registry.pathname);
20 registry.db_filename = config_get("registry", "registry db file", filename);
@@ -28,7 +27,7 @@ int registry_init(void) {
27 registry.persons_expiration = config_get_number("registry", "registry expire idle persons days", 365) * 86400;
28 registry.registry_domain = config_get("registry", "registry domain", "");
29 registry.registry_to_announce = config_get("registry", "registry to announce", "https://registry.my-netdata.io");
31 - registry.hostname = config_get("registry", "registry hostname", config_get("global", "hostname", localhost.hostname));
30 + registry.hostname = config_get("registry", "registry hostname", config_get("global", "hostname", "localhost"));
31 registry.verify_cookies_redirects = config_get_boolean("registry", "verify browser cookies support", 1);
32
33 setenv("NETDATA_REGISTRY_HOSTNAME", registry.hostname, 1);
src/registry_internals.c
+14 -12
@@ -275,8 +275,10 @@ static inline int is_machine_guid_blacklisted(const char *guid) {
275 }
276
277 char *registry_get_this_machine_guid(void) {
278 - if(likely(localhost.machine_guid[0]))
279 - return localhost.machine_guid;
278 + static char guid[GUID_LEN + 1] = "";
279 +
280 + if(likely(guid[0]))
281 + return guid;
282
283 // read it from disk
284 int fd = open(registry.machine_guid_filename, O_RDONLY);
@@ -286,38 +288,38 @@ char *registry_get_this_machine_guid(void) {
288 error("Failed to read machine GUID from '%s'", registry.machine_guid_filename);
289 else {
290 buf[GUID_LEN] = '\0';
289 - if(registry_regenerate_guid(buf, localhost.machine_guid) == -1) {
291 + if(registry_regenerate_guid(buf, guid) == -1) {
292 error("Failed to validate machine GUID '%s' from '%s'. Ignoring it - this might mean this netdata will appear as duplicate in the registry.",
293 buf, registry.machine_guid_filename);
294
293 - localhost.machine_guid[0] = '\0';
295 + guid[0] = '\0';
296 }
295 - else if(is_machine_guid_blacklisted(localhost.machine_guid))
296 - localhost.machine_guid[0] = '\0';
297 + else if(is_machine_guid_blacklisted(guid))
298 + guid[0] = '\0';
299 }
300 close(fd);
301 }
302
303 // generate a new one?
302 - if(!localhost.machine_guid[0]) {
304 + if(!guid[0]) {
305 uuid_t uuid;
306
307 uuid_generate_time(uuid);
306 - uuid_unparse_lower(uuid, localhost.machine_guid);
307 - localhost.machine_guid[GUID_LEN] = '\0';
308 + uuid_unparse_lower(uuid, guid);
309 + guid[GUID_LEN] = '\0';
310
311 // save it
312 fd = open(registry.machine_guid_filename, O_WRONLY|O_CREAT|O_TRUNC, 444);
313 if(fd == -1)
314 fatal("Cannot create unique machine id file '%s'. Please fix this.", registry.machine_guid_filename);
315
314 - if(write(fd, localhost.machine_guid, GUID_LEN) != GUID_LEN)
316 + if(write(fd, guid, GUID_LEN) != GUID_LEN)
317 fatal("Cannot write the unique machine id file '%s'. Please fix this.", registry.machine_guid_filename);
318
319 close(fd);
320 }
321
320 - setenv("NETDATA_REGISTRY_UNIQUE_ID", localhost.machine_guid, 1);
322 + setenv("NETDATA_REGISTRY_UNIQUE_ID", guid, 1);
323
322 - return localhost.machine_guid;
324 + return guid;
325 }
src/registry_internals.h
-2
@@ -68,8 +68,6 @@ extern int registry_regenerate_guid(const char *guid, char *result);
68
69 extern struct registry registry;
70
71 -extern char *registry_get_this_machine_guid(void);
72 -
71 // REGISTRY LOW-LEVEL REQUESTS (in registry-internals.c)
72 extern REGISTRY_PERSON *registry_request_access(char *person_guid, char *machine_guid, char *url, char *name, time_t when);
73 extern REGISTRY_PERSON *registry_request_delete(char *person_guid, char *machine_guid, char *url, char *delete_url, time_t when);
src/rrd.h
+16 -7
@@ -120,7 +120,7 @@ struct rrddim {
120 // (the user overwrites the name of the charts)
121 // DO NOT FREE THIS - IT IS ALLOCATED IN CONFIG
122
123 - RRD_ALGORITHM algorithm; // the algorithm that is applied to add new collected values
123 + RRD_ALGORITHM algorithm; // the algorithm that is applied to add new collected values
124 RRD_MEMORY_MODE memory_mode; // the memory mode for this dimension
125
126 collected_number multiplier; // the multiplier of the collected values
@@ -297,7 +297,10 @@ struct rrdhost {
297 avl avl;
298
299 char *hostname;
300 + uint32_t hash_hostname;
301 +
302 char machine_guid[GUID_LEN + 1];
303 + uint32_t hash_machine_guid;
304
305 RRDSET *rrdset_root;
306 pthread_rwlock_t rrdset_root_rwlock;
@@ -326,8 +329,12 @@ struct rrdhost {
329 struct rrdhost *next;
330 };
331 typedef struct rrdhost RRDHOST;
329 -extern RRDHOST localhost;
330 -extern void rrdhost_init(char *hostname);
332 +
333 +extern RRDHOST *localhost;
334 +
335 +extern void rrd_init(char *hostname);
336 +
337 +extern RRDHOST *rrdhost_find(const char *guid, uint32_t hash);
338
339 #ifdef NETDATA_INTERNAL_CHECKS
340 #define rrdhost_check_wrlock(host) rrdhost_check_wrlock_int(host, __FILE__, __FUNCTION__, __LINE__)
@@ -345,7 +352,7 @@ extern void rrdhost_rdlock(RRDHOST *host);
352 extern void rrdhost_unlock(RRDHOST *host);
353
354 // ----------------------------------------------------------------------------
348 -// RRD SET functions
355 +// RRDSET functions
356
357 extern void rrdset_set_name(RRDSET *st, const char *name);
358
@@ -367,13 +374,13 @@ extern void rrdhost_free(RRDHOST *host);
374 extern void rrdhost_save(RRDHOST *host);
375
376 extern RRDSET *rrdset_find(RRDHOST *host, const char *id);
370 -#define rrdset_find_localhost(id) rrdset_find(&localhost, id)
377 +#define rrdset_find_localhost(id) rrdset_find(localhost, id)
378
379 extern RRDSET *rrdset_find_bytype(RRDHOST *host, const char *type, const char *id);
373 -#define rrdset_find_bytype_localhost(type, id) rrdset_find_bytype(&localhost, type, id)
380 +#define rrdset_find_bytype_localhost(type, id) rrdset_find_bytype(localhost, type, id)
381
382 extern RRDSET *rrdset_find_byname(RRDHOST *host, const char *name);
376 -#define rrdset_find_byname_localhost(name) rrdset_find_byname(&localhost, name)
383 +#define rrdset_find_byname_localhost(name) rrdset_find_byname(localhost, name)
384
385 extern void rrdset_next_usec_unfiltered(RRDSET *st, usec_t microseconds);
386 extern void rrdset_next_usec(RRDSET *st, usec_t microseconds);
@@ -434,6 +441,8 @@ extern collected_number rrddim_set(RRDSET *st, const char *id, collected_number
441
442 #ifdef NETDATA_RRD_INTERNALS
443
444 +extern avl_tree_lock rrdhost_root_index;
445 +
446 extern char *rrdset_strncpyz_name(char *to, const char *from, size_t length);
447 extern char *rrdset_cache_dir(const char *id);
448
src/rrd2json.c
+16 -16
@@ -90,15 +90,15 @@ void rrd_stats_api_v1_charts(BUFFER *wb)
90 ",\n\t\"update_every\": %d"
91 ",\n\t\"history\": %d"
92 ",\n\t\"charts\": {"
93 - , localhost.hostname
93 + , localhost->hostname
94 , program_version
95 , os_type
96 , rrd_update_every
97 , rrd_default_history_entries
98 );
99
100 - pthread_rwlock_rdlock(&localhost.rrdset_root_rwlock);
101 - for(st = localhost.rrdset_root, c = 0; st ; st = st->next) {
100 + pthread_rwlock_rdlock(&localhost->rrdset_root_rwlock);
101 + for(st = localhost->rrdset_root, c = 0; st ; st = st->next) {
102 if(st->enabled && st->dimensions) {
103 if(c) buffer_strcat(wb, ",");
104 buffer_strcat(wb, "\n\t\t\"");
@@ -110,11 +110,11 @@ void rrd_stats_api_v1_charts(BUFFER *wb)
110 }
111
112 RRDCALC *rc;
113 - for(rc = localhost.alarms; rc ; rc = rc->next) {
113 + for(rc = localhost->alarms; rc ; rc = rc->next) {
114 if(rc->rrdset)
115 alarms++;
116 }
117 - pthread_rwlock_unlock(&localhost.rrdset_root_rwlock);
117 + pthread_rwlock_unlock(&localhost->rrdset_root_rwlock);
118
119 buffer_sprintf(wb, "\n\t}"
120 ",\n\t\"charts_count\": %zu"
@@ -151,14 +151,14 @@ static inline size_t prometheus_name_copy(char *d, const char *s, size_t usable)
151
152 void rrd_stats_api_v1_charts_allmetrics_prometheus(BUFFER *wb)
153 {
154 - pthread_rwlock_rdlock(&localhost.rrdset_root_rwlock);
154 + pthread_rwlock_rdlock(&localhost->rrdset_root_rwlock);
155
156 char host[PROMETHEUS_ELEMENT_MAX + 1];
157 prometheus_name_copy(host, config_get("global", "hostname", "localhost"), PROMETHEUS_ELEMENT_MAX);
158
159 // for each chart
160 RRDSET *st;
161 - for(st = localhost.rrdset_root; st ; st = st->next) {
161 + for(st = localhost->rrdset_root; st ; st = st->next) {
162 char chart[PROMETHEUS_ELEMENT_MAX + 1];
163 prometheus_name_copy(chart, st->id, PROMETHEUS_ELEMENT_MAX);
164
@@ -201,7 +201,7 @@ void rrd_stats_api_v1_charts_allmetrics_prometheus(BUFFER *wb)
201 }
202 }
203
204 - pthread_rwlock_unlock(&localhost.rrdset_root_rwlock);
204 + pthread_rwlock_unlock(&localhost->rrdset_root_rwlock);
205 }
206
207 // ----------------------------------------------------------------------------
@@ -226,14 +226,14 @@ static inline size_t shell_name_copy(char *d, const char *s, size_t usable) {
226
227 void rrd_stats_api_v1_charts_allmetrics_shell(BUFFER *wb)
228 {
229 - pthread_rwlock_rdlock(&localhost.rrdset_root_rwlock);
229 + pthread_rwlock_rdlock(&localhost->rrdset_root_rwlock);
230
231 char host[SHELL_ELEMENT_MAX + 1];
232 shell_name_copy(host, config_get("global", "hostname", "localhost"), SHELL_ELEMENT_MAX);
233
234 // for each chart
235 RRDSET *st;
236 - for(st = localhost.rrdset_root; st ; st = st->next) {
236 + for(st = localhost->rrdset_root; st ; st = st->next) {
237 calculated_number total = 0.0;
238 char chart[SHELL_ELEMENT_MAX + 1];
239 shell_name_copy(chart, st->id, SHELL_ELEMENT_MAX);
@@ -271,7 +271,7 @@ void rrd_stats_api_v1_charts_allmetrics_shell(BUFFER *wb)
271 buffer_strcat(wb, "\n# NETDATA ALARMS RUNNING\n");
272
273 RRDCALC *rc;
274 - for(rc = localhost.alarms; rc ;rc = rc->next) {
274 + for(rc = localhost->alarms; rc ;rc = rc->next) {
275 if(!rc->rrdset) continue;
276
277 char chart[SHELL_ELEMENT_MAX + 1];
@@ -292,7 +292,7 @@ void rrd_stats_api_v1_charts_allmetrics_shell(BUFFER *wb)
292 buffer_sprintf(wb, "NETDATA_ALARM_%s_%s_STATUS=\"%s\"\n", chart, alarm, rrdcalc_status2string(rc->status));
293 }
294
295 - pthread_rwlock_unlock(&localhost.rrdset_root_rwlock);
295 + pthread_rwlock_unlock(&localhost->rrdset_root_rwlock);
296 }
297
298 // ----------------------------------------------------------------------------
@@ -421,15 +421,15 @@ void rrd_stats_all_json(BUFFER *wb)
421
422 buffer_strcat(wb, RRD_GRAPH_JSON_HEADER);
423
424 - pthread_rwlock_rdlock(&localhost.rrdset_root_rwlock);
425 - for(st = localhost.rrdset_root, c = 0; st ; st = st->next) {
424 + pthread_rwlock_rdlock(&localhost->rrdset_root_rwlock);
425 + for(st = localhost->rrdset_root, c = 0; st ; st = st->next) {
426 if(st->enabled && st->dimensions) {
427 if(c) buffer_strcat(wb, ",\n");
428 memory += rrd_stats_one_json(st, NULL, wb);
429 c++;
430 }
431 }
432 - pthread_rwlock_unlock(&localhost.rrdset_root_rwlock);
432 + pthread_rwlock_unlock(&localhost->rrdset_root_rwlock);
433
434 buffer_sprintf(wb, "\n\t],\n"
435 "\t\"hostname\": \"%s\",\n"
@@ -437,7 +437,7 @@ void rrd_stats_all_json(BUFFER *wb)
437 "\t\"history\": %d,\n"
438 "\t\"memory\": %lu\n"
439 "}\n"
440 - , localhost.hostname
440 + , localhost->hostname
441 , rrd_update_every
442 , rrd_default_history_entries
443 , memory
src/rrdhost.c
+92 -42
@@ -2,46 +2,88 @@
2 #include "common.h"
3
4 // ----------------------------------------------------------------------------
5 -// RRDHOST
6 -
7 -RRDHOST localhost = {
8 - .hostname = "localhost",
9 - .machine_guid = "",
10 - .rrdset_root = NULL,
11 - .rrdset_root_rwlock = PTHREAD_RWLOCK_INITIALIZER,
12 - .rrdset_root_index = {
13 - { NULL, rrdset_compare },
14 - AVL_LOCK_INITIALIZER
15 - },
16 - .rrdset_root_index_name = {
17 - { NULL, rrdset_compare_name },
18 - AVL_LOCK_INITIALIZER
19 - },
20 - .rrdfamily_root_index = {
21 - { NULL, rrdfamily_compare },
22 - AVL_LOCK_INITIALIZER
23 - },
24 - .variables_root_index = {
25 - { NULL, rrdvar_compare },
26 - AVL_LOCK_INITIALIZER
27 - },
28 - .health_log = {
29 - .next_log_id = 1,
30 - .next_alarm_id = 1,
31 - .count = 0,
32 - .max = 1000,
33 - .alarms = NULL,
34 - .alarm_log_rwlock = PTHREAD_RWLOCK_INITIALIZER
35 - },
36 - .next = NULL
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
39 -void rrdhost_init(char *hostname) {
40 - localhost.hostname = hostname;
41 - localhost.health_log.next_log_id =
42 - localhost.health_log.next_alarm_id = (uint32_t)now_realtime_sec();
18 +RRDHOST *rrdhost_find(const char *guid, uint32_t hash) {
19 + RRDHOST tmp;
20 + strncpyz(tmp.machine_guid, guid, GUID_LEN);
21 + tmp.hash_machine_guid = (hash)?hash:simple_hash(tmp.machine_guid);
22 +
23 + return (RRDHOST *)avl_search_lock(&(rrdhost_root_index), (avl *) &tmp);
24 }
25
26 +#define rrdhost_index_add(rrdhost) (RRDHOST *)avl_insert_lock(&(rrdhost_root_index), (avl *)(rrdhost))
27 +#define rrdhost_index_del(rrdhost) (RRDHOST *)avl_remove_lock(&(rrdhost_root_index), (avl *)(rrdhost))
28 +
29 +
30 +// ----------------------------------------------------------------------------
31 +// RRDHOST - internal helpers
32 +
33 +static inline void rrdhost_init_hostname(RRDHOST *host, const char *hostname) {
34 + freez(host->hostname);
35 + host->hostname = strdupz(hostname);
36 + host->hash_hostname = simple_hash(host->hostname);
37 +}
38 +
39 +static inline void rrdhost_init_machine_guid(RRDHOST *host, const char *machine_guid) {
40 + strncpy(host->machine_guid, machine_guid, GUID_LEN);
41 + host->machine_guid[GUID_LEN] = '\0';
42 + host->hash_machine_guid = simple_hash(host->machine_guid);
43 +}
44 +
45 +// ----------------------------------------------------------------------------
46 +// RRDHOST - add a host
47 +
48 +RRDHOST *rrdhost_create(const char *hostname, const char *guid) {
49 + RRDHOST *host = callocz(1, sizeof(RRDHOST));
50 +
51 + pthread_rwlock_init(&(host->rrdset_root_rwlock), NULL);
52 +
53 + rrdhost_init_hostname(host, hostname);
54 + rrdhost_init_machine_guid(host, guid);
55 +
56 + avl_init_lock(&(host->rrdset_root_index), rrdset_compare);
57 + avl_init_lock(&(host->rrdset_root_index_name), rrdset_compare_name);
58 + avl_init_lock(&(host->rrdfamily_root_index), rrdfamily_compare);
59 + avl_init_lock(&(host->variables_root_index), rrdvar_compare);
60 +
61 + host->health_log.next_log_id = 1;
62 + host->health_log.next_alarm_id = 1;
63 + host->health_log.max = 1000;
64 + host->health_log.next_log_id =
65 + host->health_log.next_alarm_id = (uint32_t)now_realtime_sec();
66 + pthread_rwlock_init(&(host->health_log.alarm_log_rwlock), NULL);
67 +
68 + if(rrdhost_index_add(host) != host)
69 + fatal("Cannot add host '%s' to index. It already exists.", hostname);
70 +
71 + debug(D_RRDHOST, "Added host '%s'", host->hostname);
72 + return host;
73 +}
74 +
75 +// ----------------------------------------------------------------------------
76 +// RRDHOST global / startup initialization
77 +
78 +RRDHOST *localhost = NULL;
79 +
80 +void rrd_init(char *hostname) {
81 + localhost = rrdhost_create(hostname, registry_get_this_machine_guid());
82 +}
83 +
84 +// ----------------------------------------------------------------------------
85 +// RRDHOST - locks
86 +
87 void rrdhost_rwlock(RRDHOST *host) {
88 pthread_rwlock_wrlock(&host->rrdset_root_rwlock);
89 }
@@ -69,7 +111,9 @@ void rrdhost_check_wrlock_int(RRDHOST *host, const char *file, const char *funct
111 }
112
113 void rrdhost_free(RRDHOST *host) {
72 - info("Freeing all memory...");
114 + if(!host) return;
115 +
116 + info("Freeing all memory for host '%s'...", host->hostname);
117
118 rrdhost_rwlock(host);
119
@@ -110,13 +154,17 @@ void rrdhost_free(RRDHOST *host) {
154 }
155 host->rrdset_root = NULL;
156
157 + freez(host->hostname);
158 rrdhost_unlock(host);
159 + freez(host);
160
115 - info("Memory cleanup completed...");
161 + info("Host memory cleanup completed...");
162 }
163
164 void rrdhost_save(RRDHOST *host) {
119 - info("Saving database...");
165 + if(!host) return;
166 +
167 + info("Saving host '%s' database...", host->hostname);
168
169 RRDSET *st;
170 RRDDIM *rd;
@@ -147,21 +195,23 @@ void rrdhost_save(RRDHOST *host) {
195 }
196
197 void rrdhost_free_all(void) {
150 - RRDHOST *host;
198 + RRDHOST *host = localhost;
199
200 // FIXME: lock all hosts
201
154 - for(host = &localhost; host ;) {
202 + while(host) {
203 RRDHOST *next = host = host->next;
204 rrdhost_free(host);
205 host = next;
206 }
207
208 + localhost = NULL;
209 +
210 // FIXME: unlock all hosts
211 }
212
213 void rrdhost_save_all(void) {
214 RRDHOST *host;
165 - for(host = &localhost; host ; host = host->next)
215 + for(host = localhost; host ; host = host->next)
216 rrdhost_save(host);
217 }
src/rrdset.c
+1 -1
@@ -210,7 +210,7 @@ static inline void timeval_align(struct timeval *tv, int update_every) {
210 // RRDSET - create a chart
211
212 RRDSET *rrdset_create(const char *type, const char *id, const char *name, const char *family, const char *context, const char *title, const char *units, long priority, int update_every, int chart_type) {
213 - RRDHOST *host = &localhost;
213 + RRDHOST *host = localhost;
214
215 if(!type || !type[0]) {
216 fatal("Cannot create rrd stats without a type.");
src/web_client.c
+3 -3
@@ -683,7 +683,7 @@ int web_client_api_request_v1_alarms(struct web_client *w, char *url)
683
684 buffer_flush(w->response.data);
685 w->response.data->contenttype = CT_APPLICATION_JSON;
686 - health_alarms2json(&localhost, w->response.data, all);
686 + health_alarms2json(localhost, w->response.data, all);
687 return 200;
688 }
689
@@ -704,7 +704,7 @@ int web_client_api_request_v1_alarm_log(struct web_client *w, char *url)
704
705 buffer_flush(w->response.data);
706 w->response.data->contenttype = CT_APPLICATION_JSON;
707 - health_alarm_log2json(&localhost, w->response.data, after);
707 + health_alarm_log2json(localhost, w->response.data, after);
708 return 200;
709 }
710
@@ -2202,7 +2202,7 @@ void web_client_process(struct web_client *w) {
2202 debug(D_WEB_CLIENT_ACCESS, "%llu: Sending list of RRD_STATS...", w->id);
2203
2204 buffer_flush(w->response.data);
2205 - RRDSET *st = localhost.rrdset_root;
2205 + RRDSET *st = localhost->rrdset_root;
2206
2207 for ( ; st ; st = st->next )
2208 buffer_sprintf(w->response.data, "%s\n", st->name);