]> arthur.barton.de Git - netdata.git/blobdiff - src/rrdhost.c
Merge pull request #1854 from ktsaou/master
[netdata.git] / src / rrdhost.c
index 658fba001290c3ab565740b4d024e06aa0ad3347..81c695452999758feb44bfa5ac4974f50005d756 100644 (file)
@@ -5,6 +5,7 @@ RRDHOST *localhost = NULL;
 
 pthread_rwlock_t rrd_rwlock = PTHREAD_RWLOCK_INITIALIZER;
 
+time_t rrdhost_free_orphan_time = 3600;
 
 // ----------------------------------------------------------------------------
 // RRDHOST index
@@ -20,7 +21,7 @@ avl_tree_lock rrdhost_root_index = {
         .rwlock = AVL_LOCK_INITIALIZER
 };
 
-RRDHOST *rrdhost_find(const char *guid, uint32_t hash) {
+RRDHOST *rrdhost_find_guid(const char *guid, uint32_t hash) {
     debug(D_RRDHOST, "Searching in index for host with guid '%s'", guid);
 
     RRDHOST tmp;
@@ -201,18 +202,18 @@ RRDHOST *rrdhost_create(const char *hostname,
     }
     else {
         info("Host '%s' with guid '%s' initialized"
-                     ", os: %s"
-                     ", update every: %d"
-                     ", memory mode: %s"
-                     ", history entries: %d"
-                     ", streaming: %s"
-                     " to: '%s' (api key: '%s')"
-                     ", health: %s"
-                     ", cache_dir: '%s'"
-                     ", varlib_dir: '%s'"
-                     ", health_log: '%s'"
-                     ", alarms default handler: '%s'"
-                     ", alarms default recipient: '%s'"
+                     ", os %s"
+                     ", update every %d"
+                     ", memory mode %s"
+                     ", history entries %d"
+                     ", streaming %s"
+                     " (to '%s' with api key '%s')"
+                     ", health %s"
+                     ", cache_dir '%s'"
+                     ", varlib_dir '%s'"
+                     ", health_log '%s'"
+                     ", alarms default handler '%s'"
+                     ", alarms default recipient '%s'"
              , host->hostname
              , host->machine_guid
              , host->os
@@ -220,8 +221,8 @@ RRDHOST *rrdhost_create(const char *hostname,
              , rrd_memory_mode_name(host->rrd_memory_mode)
              , host->rrd_history_entries
              , host->rrdpush_enabled?"enabled":"disabled"
-             , host->rrdpush_destination
-             , host->rrdpush_api_key
+             , host->rrdpush_destination?host->rrdpush_destination:""
+             , host->rrdpush_api_key?host->rrdpush_api_key:""
              , host->health_enabled?"enabled":"disabled"
              , host->cache_dir
              , host->varlib_dir
@@ -250,7 +251,7 @@ RRDHOST *rrdhost_find_or_create(
 ) {
     debug(D_RRDHOST, "Searching for host '%s' with guid '%s'", hostname, guid);
 
-    RRDHOST *host = rrdhost_find(guid, 0);
+    RRDHOST *host = rrdhost_find_guid(guid, 0);
     if(!host) {
         host = rrdhost_create(
                 hostname
@@ -286,9 +287,30 @@ RRDHOST *rrdhost_find_or_create(
             error("Host '%s' has memory mode '%s', but the wanted one is '%s'.", host->hostname, rrd_memory_mode_name(host->rrd_memory_mode), rrd_memory_mode_name(mode));
     }
 
+    rrdhost_cleanup_remote_stale(host);
+
     return host;
 }
 
+void rrdhost_cleanup_remote_stale(RRDHOST *protected) {
+    rrd_wrlock();
+
+    RRDHOST *h;
+    rrdhost_foreach_write(h) {
+        if(h != protected
+           && h != localhost
+           && !h->connected_senders
+           && h->senders_disconnected_time + rrdhost_free_orphan_time > now_realtime_sec()) {
+            info("Host '%s' with machine guid '%s' is obsolete - cleaning up.", h->hostname, h->machine_guid);
+            rrdhost_save(h);
+            rrdhost_free(h);
+            break;
+        }
+    }
+
+    rrd_unlock();
+}
+
 // ----------------------------------------------------------------------------
 // RRDHOST global / startup initialization
 
@@ -396,10 +418,7 @@ void rrdhost_free(RRDHOST *host) {
     // ------------------------------------------------------------------------
     // free it
 
-    if(host->rrdpush_spawn) {
-        pthread_cancel(host->rrdpush_thread);
-        rrdpush_sender_thread_cleanup(host);
-    }
+    rrdpush_sender_thread_stop(host);
 
     freez(host->os);
     freez(host->cache_dir);