add mirrored hosts to the my-netdata menu
Costa Tsaousis (ktsaou) committed
Feb 27, 2017 at 02:43 UTC
246a8d3f8dc0ef1f88771ba87d67110e428cb420
5 files changed
+107
-23
src/rrd.h
+4
-1
@@ -454,11 +454,14 @@ extern pthread_rwlock_t rrd_rwlock;
454
455
// ----------------------------------------------------------------------------
456
457
+extern size_t rrd_hosts_available;
458
extern time_t rrdhost_free_orphan_time;
459
460
extern void rrd_init(char *hostname);
461
461
-extern RRDHOST *rrdhost_find_guid(const char *guid, uint32_t hash);
462
+extern RRDHOST *rrdhost_find_by_hostname(const char *hostname, uint32_t hash);
463
+extern RRDHOST *rrdhost_find_by_guid(const char *guid, uint32_t hash);
464
+
465
extern RRDHOST *rrdhost_find_or_create(
466
const char *hostname
467
, const char *guid
src/rrd2json.c
+27
-1
@@ -122,12 +122,38 @@ void rrd_stats_api_v1_charts(RRDHOST *host, BUFFER *wb)
122
",\n\t\"dimensions_count\": %zu"
123
",\n\t\"alarms_count\": %zu"
124
",\n\t\"rrd_memory_bytes\": %zu"
125
- "\n}\n"
125
+ ",\n\t\"hosts_count\": %zu"
126
+ ",\n\t\"hosts\": ["
127
, c
128
, dimensions
129
, alarms
130
, memory
131
+ , rrd_hosts_available
132
);
133
+
134
+ if(unlikely(rrd_hosts_available > 1)) {
135
+ rrd_rdlock();
136
+ RRDHOST *h;
137
+ rrdhost_foreach_read(h)
138
+ buffer_sprintf(wb,
139
+ "%s\n\t\t{"
140
+ "\n\t\t\t\"hostname\": \"%s\""
141
+ "\n\t\t}"
142
+ , (h != localhost)?",":""
143
+ , h->hostname
144
+ );
145
+ rrd_unlock();
146
+ }
147
+ else {
148
+ buffer_sprintf(wb,
149
+ "\n\t\t{"
150
+ "\n\t\t\t\"hostname\": \"%s\""
151
+ "\n\t\t}"
152
+ , host->hostname
153
+ );
154
+ }
155
+
156
+ buffer_sprintf(wb, "\n\t]\n}\n");
157
}
158
159
// ----------------------------------------------------------------------------
src/rrdhost.c
+26
-6
@@ -2,7 +2,7 @@
2
#include "common.h"
3
4
RRDHOST *localhost = NULL;
5
-
5
+size_t rrd_hosts_available = 0;
6
pthread_rwlock_t rrd_rwlock = PTHREAD_RWLOCK_INITIALIZER;
7
8
time_t rrdhost_free_orphan_time = 3600;
@@ -21,7 +21,7 @@ avl_tree_lock rrdhost_root_index = {
21
.rwlock = AVL_LOCK_INITIALIZER
22
};
23
24
-RRDHOST *rrdhost_find_guid(const char *guid, uint32_t hash) {
24
+RRDHOST *rrdhost_find_by_guid(const char *guid, uint32_t hash) {
25
debug(D_RRDHOST, "Searching in index for host with guid '%s'", guid);
26
27
RRDHOST tmp;
@@ -31,6 +31,25 @@ RRDHOST *rrdhost_find_guid(const char *guid, uint32_t hash) {
31
return (RRDHOST *)avl_search_lock(&(rrdhost_root_index), (avl *) &tmp);
32
}
33
34
+RRDHOST *rrdhost_find_by_hostname(const char *hostname, uint32_t hash) {
35
+ if(unlikely(!strcmp(hostname, "localhost")))
36
+ return localhost;
37
+
38
+ if(unlikely(!hash)) hash = simple_hash(hostname);
39
+
40
+ rrd_rdlock();
41
+ RRDHOST *host;
42
+ rrdhost_foreach_read(host) {
43
+ if(unlikely((hash == host->hash_hostname && !strcmp(hostname, host->hostname)))) {
44
+ rrd_unlock();
45
+ return host;
46
+ }
47
+ }
48
+ rrd_unlock();
49
+
50
+ return NULL;
51
+}
52
+
53
#define rrdhost_index_add(rrdhost) (RRDHOST *)avl_insert_lock(&(rrdhost_root_index), (avl *)(rrdhost))
54
#define rrdhost_index_del(rrdhost) (RRDHOST *)avl_remove_lock(&(rrdhost_root_index), (avl *)(rrdhost))
55
@@ -232,6 +251,7 @@ RRDHOST *rrdhost_create(const char *hostname,
251
);
252
}
253
254
+ rrd_hosts_available++;
255
rrd_unlock();
256
257
return host;
@@ -251,7 +271,7 @@ RRDHOST *rrdhost_find_or_create(
271
) {
272
debug(D_RRDHOST, "Searching for host '%s' with guid '%s'", hostname, guid);
273
254
- RRDHOST *host = rrdhost_find_guid(guid, 0);
274
+ RRDHOST *host = rrdhost_find_by_guid(guid, 0);
275
if(!host) {
276
host = rrdhost_create(
277
hostname
@@ -432,7 +452,7 @@ void rrdhost_free(RRDHOST *host) {
452
rrdhost_unlock(host);
453
freez(host);
454
435
- info("Host memory cleanup completed...");
455
+ rrd_hosts_available--;
456
}
457
458
void rrdhost_free_all(void) {
@@ -447,7 +467,7 @@ void rrdhost_free_all(void) {
467
void rrdhost_save(RRDHOST *host) {
468
if(!host) return;
469
450
- info("Saving host '%s' database...", host->hostname);
470
+ info("Saving database of host '%s'...", host->hostname);
471
472
RRDSET *st;
473
RRDDIM *rd;
@@ -478,7 +498,7 @@ void rrdhost_save(RRDHOST *host) {
498
}
499
500
void rrdhost_save_all(void) {
481
- info("Saving database...");
501
+ info("Saving database [%zu hosts(s)]...", rrd_hosts_available);
502
503
rrd_rdlock();
504
src/web_client.c
+4
-13
@@ -1034,19 +1034,10 @@ static inline int web_client_switch_host(RRDHOST *host, struct web_client *w, ch
1034
1035
uint32_t hash = simple_hash(tok);
1036
1037
- if(unlikely(hash == hash_localhost && !strcmp(tok, "localhost")))
1038
- return web_client_process_url(localhost, w, url);
1039
-
1040
- rrd_rdlock();
1041
- RRDHOST *h;
1042
- rrdhost_foreach_read(h) {
1043
- if(unlikely((hash == h->hash_hostname && !strcmp(tok, h->hostname)) ||
1044
- (hash == h->hash_machine_guid && !strcmp(tok, h->machine_guid)))) {
1045
- rrd_unlock();
1046
- return web_client_process_url(h, w, url);
1047
- }
1048
- }
1049
- rrd_unlock();
1037
+ host = rrdhost_find_by_hostname(tok, hash);
1038
+ if(!host) host = rrdhost_find_by_guid(tok, hash);
1039
+
1040
+ if(host) return web_client_process_url(host, w, url);
1041
}
1042
1043
buffer_flush(w->response.data);
web/index.html
+46
-2
@@ -597,7 +597,42 @@
597
var netdataRegistryCallback = function(machines_array) {
598
var el = '';
599
var a1 = '';
600
- var found = 0;
600
+ var found = 0, hosted = 0;
601
+ var len, i, url, hostname, icon;
602
+
603
+ if(options.hosts.length > 1) {
604
+ el += '<li><a href="#" onClick="return false;" style="color: #666;" target="_blank">remote databases mirrored to this host</a></li>';
605
+ a1 += '<li><a href="#" onClick="return false;"><i class="fa fa-info-circle" aria-hidden="true" style="color: #666;"></i></a></li>';
606
+
607
+ var base = document.location.origin.toString() + document.location.pathname.toString();
608
+ if(base.endsWith("/host/" + options.hostname + "/"))
609
+ base = base.substring(0, base.length - ("/host/" + options.hostname + "/").toString().length);
610
+
611
+ if(base.endsWith("/"))
612
+ base = base.substring(0, base.length - 1);
613
+
614
+ i = 0;
615
+ len = options.hosts.length;
616
+ while(len--) {
617
+ hostname = options.hosts[i].hostname;
618
+ if(i == 0) {
619
+ url = base + "/";
620
+ icon = "home";
621
+ }
622
+ else {
623
+ url = base + "/host/" + hostname + "/";
624
+ icon = "window-restore";
625
+ }
626
+
627
+ el += '<li id="registry_server_hosted_' + len.toString() + '"><a class="registry_link" href="' + url + '" onClick="return gotoHostedModalHandler(\'' + url + '\');">' + hostname + '</a></li>';
628
+ a1 += '<li id="registry_action_hosted_' + len.toString() + '"><a class="registry_link" href="' + url + '" onClick="return gotoHostedModalHandler(\'' + url + '\');"><i class="fa fa-' + icon + '" aria-hidden="true" style="color: #999;"></i></a></li>';
629
+ hosted++;
630
+ i++;
631
+ }
632
+
633
+ el += '<li role="separator" class="divider"></li>';
634
+ a1 += '<li role="separator" class="divider"></li>';
635
+ }
636
637
if(machines_array === null) {
638
var ret = loadLocalStorage("registryCallback");
@@ -616,7 +651,7 @@
651
return 0;
652
});
653
619
- var len = machines.length;
654
+ len = machines.length;
655
while(len--) {
656
var u = machines[len];
657
found++;
@@ -707,6 +742,11 @@
742
location.reload();
743
}
744
745
+ function gotoHostedModalHandler(url) {
746
+ document.location = url + urlOptions.genHash();
747
+ return false;
748
+ }
749
+
750
var gotoServerValidateRemaining = 0;
751
var gotoServerMiddleClick = false;
752
var gotoServerStop = false;
@@ -878,6 +918,7 @@
918
categories_idx: {},
919
families: [],
920
families_idx: {},
921
+ hosts: [],
922
923
chartsPerRow: 0,
924
// chartsMinWidth: 1450,
@@ -2257,6 +2298,9 @@
2298
options.version = data.version;
2299
netdataDashboard.os = data.os;
2300
2301
+ if(typeof data.hosts != 'undefined')
2302
+ options.hosts = data.hosts;
2303
+
2304
// update the dashboard hostname
2305
document.getElementById('hostname').innerHTML = options.hostname;
2306
document.getElementById('hostname').href = NETDATA.serverDefault;