]> arthur.barton.de Git - netdata.git/commitdiff
add mirrored hosts to the my-netdata menu
authorCosta Tsaousis (ktsaou) <costa@tsaousis.gr>
Mon, 27 Feb 2017 00:43:41 +0000 (02:43 +0200)
committerCosta Tsaousis (ktsaou) <costa@tsaousis.gr>
Mon, 27 Feb 2017 00:43:41 +0000 (02:43 +0200)
src/rrd.h
src/rrd2json.c
src/rrdhost.c
src/web_client.c
web/index.html

index ec79447712e85442fa24af0115079761db031446..26623a24045382c63d066d98eed6e957cc76d72c 100644 (file)
--- a/src/rrd.h
+++ b/src/rrd.h
@@ -454,11 +454,14 @@ extern pthread_rwlock_t rrd_rwlock;
 
 // ----------------------------------------------------------------------------
 
+extern size_t rrd_hosts_available;
 extern time_t rrdhost_free_orphan_time;
 
 extern void rrd_init(char *hostname);
 
-extern RRDHOST *rrdhost_find_guid(const char *guid, uint32_t hash);
+extern RRDHOST *rrdhost_find_by_hostname(const char *hostname, uint32_t hash);
+extern RRDHOST *rrdhost_find_by_guid(const char *guid, uint32_t hash);
+
 extern RRDHOST *rrdhost_find_or_create(
         const char *hostname
         , const char *guid
index 84db40433245964aa1be15cfbf422d81a688780d..2a46fb134568721be224599c6168fac301d20d0d 100644 (file)
@@ -122,12 +122,38 @@ void rrd_stats_api_v1_charts(RRDHOST *host, BUFFER *wb)
                     ",\n\t\"dimensions_count\": %zu"
                     ",\n\t\"alarms_count\": %zu"
                     ",\n\t\"rrd_memory_bytes\": %zu"
-                    "\n}\n"
+                    ",\n\t\"hosts_count\": %zu"
+                    ",\n\t\"hosts\": ["
                    , c
                    , dimensions
                    , alarms
                    , memory
+                   , rrd_hosts_available
     );
+
+    if(unlikely(rrd_hosts_available > 1)) {
+        rrd_rdlock();
+        RRDHOST *h;
+        rrdhost_foreach_read(h)
+            buffer_sprintf(wb,
+                    "%s\n\t\t{"
+                    "\n\t\t\t\"hostname\": \"%s\""
+                    "\n\t\t}"
+                    , (h != localhost)?",":""
+                    , h->hostname
+            );
+        rrd_unlock();
+    }
+    else {
+        buffer_sprintf(wb,
+                "\n\t\t{"
+                "\n\t\t\t\"hostname\": \"%s\""
+                "\n\t\t}"
+                , host->hostname
+        );
+    }
+
+    buffer_sprintf(wb, "\n\t]\n}\n");
 }
 
 // ----------------------------------------------------------------------------
index 81c695452999758feb44bfa5ac4974f50005d756..cfca4b2cfcb362c13c0088f6150a4a88848293b4 100644 (file)
@@ -2,7 +2,7 @@
 #include "common.h"
 
 RRDHOST *localhost = NULL;
-
+size_t rrd_hosts_available = 0;
 pthread_rwlock_t rrd_rwlock = PTHREAD_RWLOCK_INITIALIZER;
 
 time_t rrdhost_free_orphan_time = 3600;
@@ -21,7 +21,7 @@ avl_tree_lock rrdhost_root_index = {
         .rwlock = AVL_LOCK_INITIALIZER
 };
 
-RRDHOST *rrdhost_find_guid(const char *guid, uint32_t hash) {
+RRDHOST *rrdhost_find_by_guid(const char *guid, uint32_t hash) {
     debug(D_RRDHOST, "Searching in index for host with guid '%s'", guid);
 
     RRDHOST tmp;
@@ -31,6 +31,25 @@ RRDHOST *rrdhost_find_guid(const char *guid, uint32_t hash) {
     return (RRDHOST *)avl_search_lock(&(rrdhost_root_index), (avl *) &tmp);
 }
 
+RRDHOST *rrdhost_find_by_hostname(const char *hostname, uint32_t hash) {
+    if(unlikely(!strcmp(hostname, "localhost")))
+        return localhost;
+
+    if(unlikely(!hash)) hash = simple_hash(hostname);
+
+    rrd_rdlock();
+    RRDHOST *host;
+    rrdhost_foreach_read(host) {
+        if(unlikely((hash == host->hash_hostname && !strcmp(hostname, host->hostname)))) {
+            rrd_unlock();
+            return host;
+        }
+    }
+    rrd_unlock();
+
+    return NULL;
+}
+
 #define rrdhost_index_add(rrdhost) (RRDHOST *)avl_insert_lock(&(rrdhost_root_index), (avl *)(rrdhost))
 #define rrdhost_index_del(rrdhost) (RRDHOST *)avl_remove_lock(&(rrdhost_root_index), (avl *)(rrdhost))
 
@@ -232,6 +251,7 @@ RRDHOST *rrdhost_create(const char *hostname,
         );
     }
 
+    rrd_hosts_available++;
     rrd_unlock();
 
     return host;
@@ -251,7 +271,7 @@ RRDHOST *rrdhost_find_or_create(
 ) {
     debug(D_RRDHOST, "Searching for host '%s' with guid '%s'", hostname, guid);
 
-    RRDHOST *host = rrdhost_find_guid(guid, 0);
+    RRDHOST *host = rrdhost_find_by_guid(guid, 0);
     if(!host) {
         host = rrdhost_create(
                 hostname
@@ -432,7 +452,7 @@ void rrdhost_free(RRDHOST *host) {
     rrdhost_unlock(host);
     freez(host);
 
-    info("Host memory cleanup completed...");
+    rrd_hosts_available--;
 }
 
 void rrdhost_free_all(void) {
@@ -447,7 +467,7 @@ void rrdhost_free_all(void) {
 void rrdhost_save(RRDHOST *host) {
     if(!host) return;
 
-    info("Saving host '%s' database...", host->hostname);
+    info("Saving database of host '%s'...", host->hostname);
 
     RRDSET *st;
     RRDDIM *rd;
@@ -478,7 +498,7 @@ void rrdhost_save(RRDHOST *host) {
 }
 
 void rrdhost_save_all(void) {
-    info("Saving database...");
+    info("Saving database [%zu hosts(s)]...", rrd_hosts_available);
 
     rrd_rdlock();
 
index 908e72a39a536997b6be22c432785d722d932e4c..ef39d3b5834e92ff2444848816fca0e6db94f11b 100644 (file)
@@ -1034,19 +1034,10 @@ static inline int web_client_switch_host(RRDHOST *host, struct web_client *w, ch
 
         uint32_t hash = simple_hash(tok);
 
-        if(unlikely(hash == hash_localhost && !strcmp(tok, "localhost")))
-            return web_client_process_url(localhost, w, url);
-
-        rrd_rdlock();
-        RRDHOST *h;
-        rrdhost_foreach_read(h) {
-            if(unlikely((hash == h->hash_hostname && !strcmp(tok, h->hostname)) ||
-                        (hash == h->hash_machine_guid && !strcmp(tok, h->machine_guid)))) {
-                rrd_unlock();
-                return web_client_process_url(h, w, url);
-            }
-        }
-        rrd_unlock();
+        host = rrdhost_find_by_hostname(tok, hash);
+        if(!host) host = rrdhost_find_by_guid(tok, hash);
+
+        if(host) return web_client_process_url(host, w, url);
     }
 
     buffer_flush(w->response.data);
index 2f116602376a5c64723a210e3ffed7248a1d799d..f12508a0cb73e9856d712a4919c0cf4c54e4bca4 100644 (file)
         var netdataRegistryCallback = function(machines_array) {
             var el = '';
             var a1 = '';
-            var found = 0;
+            var found = 0, hosted = 0;
+            var len, i, url, hostname, icon;
+
+            if(options.hosts.length > 1) {
+                el += '<li><a href="#" onClick="return false;" style="color: #666;" target="_blank">remote databases mirrored to this host</a></li>';
+                a1 += '<li><a href="#" onClick="return false;"><i class="fa fa-info-circle" aria-hidden="true" style="color: #666;"></i></a></li>';
+
+                var base = document.location.origin.toString() + document.location.pathname.toString();
+                if(base.endsWith("/host/" + options.hostname + "/"))
+                    base = base.substring(0, base.length - ("/host/" + options.hostname + "/").toString().length);
+
+                if(base.endsWith("/"))
+                    base = base.substring(0, base.length - 1);
+
+                i = 0;
+                len = options.hosts.length;
+                while(len--) {
+                    hostname = options.hosts[i].hostname;
+                    if(i == 0) {
+                        url = base + "/";
+                        icon = "home";
+                    }
+                    else {
+                        url = base + "/host/" + hostname + "/";
+                        icon = "window-restore";
+                    }
+
+                    el += '<li id="registry_server_hosted_' + len.toString() + '"><a class="registry_link" href="' + url + '" onClick="return gotoHostedModalHandler(\'' + url + '\');">' + hostname + '</a></li>';
+                    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>';
+                    hosted++;
+                    i++;
+                }
+
+                el += '<li role="separator" class="divider"></li>';
+                a1 += '<li role="separator" class="divider"></li>';
+            }
 
             if(machines_array === null) {
                 var ret = loadLocalStorage("registryCallback");
                     return 0;
                 });
 
-                var len = machines.length;
+                len = machines.length;
                 while(len--) {
                     var u = machines[len];
                     found++;
             location.reload();
         }
 
+        function gotoHostedModalHandler(url) {
+            document.location = url + urlOptions.genHash();
+            return false;
+        }
+
         var gotoServerValidateRemaining = 0;
         var gotoServerMiddleClick = false;
         var gotoServerStop = false;
             categories_idx: {},
             families: [],
             families_idx: {},
+            hosts: [],
 
             chartsPerRow: 0,
             // chartsMinWidth: 1450,
                         options.version = data.version;
                         netdataDashboard.os = data.os;
 
+                        if(typeof data.hosts != 'undefined')
+                            options.hosts = data.hosts;
+
                         // update the dashboard hostname
                         document.getElementById('hostname').innerHTML = options.hostname;
                         document.getElementById('hostname').href = NETDATA.serverDefault;