@cryptotaxi247 / netdata-1 / commits / 9b9d0e5c6

do not offer orphaned hosts to viewers; fixes #2832

Costa Tsaousis (ktsaou) committed Oct 3, 2017 at 02:44 UTC 9b9d0e5c6f05eb71165f7abbb18d38dfadbedb5b
4 files changed +36 -26
src/rrd.h
+5 -3
@@ -368,9 +368,9 @@ typedef struct rrdset RRDSET;
368 // and may lead to missing information.
369
370 typedef enum rrdhost_flags {
371 - RRDHOST_ORPHAN = 1 << 0, // this host is orphan (not receiving data)
372 - RRDHOST_DELETE_OBSOLETE_CHARTS = 1 << 1, // delete files of obsolete charts
373 - RRDHOST_DELETE_ORPHAN_HOST = 1 << 2 // delete the entire host when orphan
371 + RRDHOST_FLAG_ORPHAN = 1 << 0, // this host is orphan (not receiving data)
372 + RRDHOST_FLAG_DELETE_OBSOLETE_CHARTS = 1 << 1, // delete files of obsolete charts
373 + RRDHOST_FLAG_DELETE_ORPHAN_HOST = 1 << 2 // delete the entire host when orphan
374 } RRDHOST_FLAGS;
375
376 #ifdef HAVE_C___ATOMIC
@@ -609,6 +609,8 @@ extern void rrdhost_free(RRDHOST *host);
609 extern void rrdhost_save_charts(RRDHOST *host);
610 extern void rrdhost_delete_charts(RRDHOST *host);
611
612 +extern int rrdhost_should_be_removed(RRDHOST *host, RRDHOST *protected, time_t now);
613 +
614 extern void rrdset_update_heterogeneous_flag(RRDSET *st);
615
616 extern RRDSET *rrdset_find(RRDHOST *host, const char *id);
src/rrd2json.c
+21 -13
@@ -126,7 +126,8 @@ void rrd_stats_api_v1_charts(RRDHOST *host, BUFFER *wb) {
126 }
127 rrdhost_unlock(host);
128
129 - buffer_sprintf(wb, "\n\t}"
129 + buffer_sprintf(wb
130 + , "\n\t}"
131 ",\n\t\"charts_count\": %zu"
132 ",\n\t\"dimensions_count\": %zu"
133 ",\n\t\"alarms_count\": %zu"
@@ -142,24 +143,31 @@ void rrd_stats_api_v1_charts(RRDHOST *host, BUFFER *wb) {
143
144 if(unlikely(rrd_hosts_available > 1)) {
145 rrd_rdlock();
146 +
147 + size_t found = 0;
148 RRDHOST *h;
149 rrdhost_foreach_read(h) {
147 - buffer_sprintf(wb,
148 - "%s\n\t\t{"
149 - "\n\t\t\t\"hostname\": \"%s\""
150 - "\n\t\t}"
151 - , (h != localhost) ? "," : ""
152 - , h->hostname
153 - );
150 + if(!rrdhost_should_be_removed(h, host, now)) {
151 + buffer_sprintf(wb
152 + , "%s\n\t\t{"
153 + "\n\t\t\t\"hostname\": \"%s\""
154 + "\n\t\t}"
155 + , (found > 0) ? "," : ""
156 + , h->hostname
157 + );
158 +
159 + found++;
160 + }
161 }
162 +
163 rrd_unlock();
164 }
165 else {
158 - buffer_sprintf(wb,
159 - "\n\t\t{"
160 - "\n\t\t\t\"hostname\": \"%s\""
161 - "\n\t\t}"
162 - , host->hostname
166 + buffer_sprintf(wb
167 + , "\n\t\t{"
168 + "\n\t\t\t\"hostname\": \"%s\""
169 + "\n\t\t}"
170 + , host->hostname
171 );
172 }
173
src/rrdhost.c
+8 -8
@@ -143,10 +143,10 @@ RRDHOST *rrdhost_create(const char *hostname,
143 avl_init_lock(&(host->variables_root_index), rrdvar_compare);
144
145 if(config_get_boolean(CONFIG_SECTION_GLOBAL, "delete obsolete charts files", 1))
146 - rrdhost_flag_set(host, RRDHOST_DELETE_OBSOLETE_CHARTS);
146 + rrdhost_flag_set(host, RRDHOST_FLAG_DELETE_OBSOLETE_CHARTS);
147
148 if(config_get_boolean(CONFIG_SECTION_GLOBAL, "delete orphan hosts files", 1) && !is_localhost)
149 - rrdhost_flag_set(host, RRDHOST_DELETE_ORPHAN_HOST);
149 + rrdhost_flag_set(host, RRDHOST_FLAG_DELETE_ORPHAN_HOST);
150
151
152 // ------------------------------------------------------------------------
@@ -354,10 +354,10 @@ RRDHOST *rrdhost_find_or_create(
354 return host;
355 }
356
357 -static inline int rrdhost_should_be_removed(RRDHOST *host, RRDHOST *protected, time_t now) {
357 +inline int rrdhost_should_be_removed(RRDHOST *host, RRDHOST *protected, time_t now) {
358 if(host != protected
359 && host != localhost
360 - && rrdhost_flag_check(host, RRDHOST_ORPHAN)
360 + && rrdhost_flag_check(host, RRDHOST_FLAG_ORPHAN)
361 && !host->connected_senders
362 && host->senders_disconnected_time
363 && host->senders_disconnected_time + rrdhost_free_orphan_time < now)
@@ -376,7 +376,7 @@ restart_after_removal:
376 if(rrdhost_should_be_removed(host, protected, now)) {
377 info("Host '%s' with machine guid '%s' is obsolete - cleaning up.", host->hostname, host->machine_guid);
378
379 - if(rrdhost_flag_check(host, RRDHOST_DELETE_ORPHAN_HOST))
379 + if(rrdhost_flag_check(host, RRDHOST_FLAG_DELETE_ORPHAN_HOST))
380 rrdhost_delete_charts(host);
381 else
382 rrdhost_save_charts(host);
@@ -590,7 +590,7 @@ void rrdhost_cleanup_charts(RRDHOST *host) {
590 info("Cleaning up database of host '%s'...", host->hostname);
591
592 RRDSET *st;
593 - uint32_t rrdhost_delete_obsolete_charts = rrdhost_flag_check(host, RRDHOST_DELETE_OBSOLETE_CHARTS);
593 + uint32_t rrdhost_delete_obsolete_charts = rrdhost_flag_check(host, RRDHOST_FLAG_DELETE_OBSOLETE_CHARTS);
594
595 // we get a write lock
596 // to ensure only one thread is saving the database
@@ -636,7 +636,7 @@ void rrdhost_cleanup_all(void) {
636
637 RRDHOST *host;
638 rrdhost_foreach_read(host) {
639 - if(host != localhost && rrdhost_flag_check(host, RRDHOST_DELETE_OBSOLETE_CHARTS) && !host->connected_senders)
639 + if(host != localhost && rrdhost_flag_check(host, RRDHOST_FLAG_DELETE_OBSOLETE_CHARTS) && !host->connected_senders)
640 rrdhost_delete_charts(host);
641 else
642 rrdhost_cleanup_charts(host);
@@ -654,7 +654,7 @@ void rrdhost_cleanup_obsolete_charts(RRDHOST *host) {
654
655 RRDSET *st;
656
657 - uint32_t rrdhost_delete_obsolete_charts = rrdhost_flag_check(host, RRDHOST_DELETE_OBSOLETE_CHARTS);
657 + uint32_t rrdhost_delete_obsolete_charts = rrdhost_flag_check(host, RRDHOST_FLAG_DELETE_OBSOLETE_CHARTS);
658
659 restart_after_removal:
660 rrdset_foreach_write(st, host) {
src/rrdpush.c
+2 -2
@@ -754,7 +754,7 @@ static int rrdpush_receive(int fd, const char *key, const char *hostname, const
754 if(host->connected_senders > 0)
755 info("STREAM %s [receive from [%s]:%s]: multiple streaming connections for the same host detected. If multiple netdata are pushing metrics for the same charts, at the same time, the result is unexpected.", host->hostname, client_ip, client_port);
756
757 - rrdhost_flag_clear(host, RRDHOST_ORPHAN);
757 + rrdhost_flag_clear(host, RRDHOST_FLAG_ORPHAN);
758 host->connected_senders++;
759 host->senders_disconnected_time = 0;
760 if(health_enabled != CONFIG_BOOLEAN_NO) {
@@ -781,7 +781,7 @@ static int rrdpush_receive(int fd, const char *key, const char *hostname, const
781 host->senders_disconnected_time = now_realtime_sec();
782 host->connected_senders--;
783 if(!host->connected_senders) {
784 - rrdhost_flag_set(host, RRDHOST_ORPHAN);
784 + rrdhost_flag_set(host, RRDHOST_FLAG_ORPHAN);
785 if(health_enabled == CONFIG_BOOLEAN_AUTO)
786 host->health_enabled = 0;
787 }