]> arthur.barton.de Git - netdata.git/blobdiff - src/rrdhost.c
create health directory for each host
[netdata.git] / src / rrdhost.c
index 171686e08c0d6b7c8f9dd32e247d045e16e3c23f..02ccf6bfe4ae6138ef866964cb56388c53d136b5 100644 (file)
@@ -3,6 +3,9 @@
 
 RRDHOST *localhost = NULL;
 
+pthread_rwlock_t rrd_rwlock = PTHREAD_RWLOCK_INITIALIZER;
+
+
 // ----------------------------------------------------------------------------
 // RRDHOST index
 
@@ -46,15 +49,27 @@ static inline void rrdhost_init_machine_guid(RRDHOST *host, const char *machine_
     host->hash_machine_guid = simple_hash(host->machine_guid);
 }
 
+
 // ----------------------------------------------------------------------------
 // RRDHOST - add a host
 
-RRDHOST *rrdhost_create(const char *hostname, const char *guid) {
-    debug(D_RRDHOST, "Adding host '%s' with guid '%s'", hostname, guid);
+RRDHOST *rrdhost_create(const char *hostname,
+        const char *guid,
+        int update_every,
+        int entries,
+        RRD_MEMORY_MODE memory_mode,
+        int health_enabled) {
+
+    debug(D_RRDHOST, "Host '%s': adding with guid '%s'", hostname, guid);
 
     RRDHOST *host = callocz(1, sizeof(RRDHOST));
 
-    pthread_rwlock_init(&(host->rrdset_root_rwlock), NULL);
+    host->rrd_update_every    = update_every;
+    host->rrd_history_entries = entries;
+    host->rrd_memory_mode     = memory_mode;
+    host->health_enabled      = health_enabled;
+
+    pthread_rwlock_init(&(host->rrdhost_rwlock), NULL);
 
     rrdhost_init_hostname(host, hostname);
     rrdhost_init_machine_guid(host, guid);
@@ -64,7 +79,6 @@ RRDHOST *rrdhost_create(const char *hostname, const char *guid) {
     avl_init_lock(&(host->rrdfamily_root_index), rrdfamily_compare);
     avl_init_lock(&(host->variables_root_index), rrdvar_compare);
 
-
     // ------------------------------------------------------------------------
     // initialize health variables
 
@@ -76,7 +90,7 @@ RRDHOST *rrdhost_create(const char *hostname, const char *guid) {
 
     long n = config_get_number("health", "in memory max health log entries", host->health_log.max);
     if(n < 10) {
-        error("Health configuration has invalid max log entries %ld. Using default %u", n, host->health_log.max);
+        error("Host '%s': health configuration has invalid max log entries %ld. Using default %u", host->hostname, n, host->health_log.max);
         config_set_number("health", "in memory max health log entries", (long)host->health_log.max);
     }
     else
@@ -88,13 +102,43 @@ RRDHOST *rrdhost_create(const char *hostname, const char *guid) {
 
     if(!localhost) {
         // this is localhost
-        snprintfz(filename, FILENAME_MAX, "%s/health/health-log.db", netdata_configured_varlib_dir);
+
+        host->cache_dir = strdupz(netdata_configured_cache_dir);
+        host->varlib_dir = strdupz(netdata_configured_varlib_dir);
+
+        snprintfz(filename, FILENAME_MAX, "%s/health/health-log.db", host->varlib_dir);
         host->health_log_filename = strdupz(config_get("health", "health db file", filename));
+
     }
     else {
         // this is not localhost - append our GUID to localhost path
-        snprintfz(filename, FILENAME_MAX, "%s.%s", localhost->health_log_filename, host->machine_guid);
+
+        snprintfz(filename, FILENAME_MAX, "%s/%s", netdata_configured_cache_dir, host->machine_guid);
+        host->cache_dir = strdupz(filename);
+
+        if(host->rrd_memory_mode == RRD_MEMORY_MODE_MAP || host->rrd_memory_mode == RRD_MEMORY_MODE_SAVE) {
+            int r = mkdir(host->cache_dir, 0775);
+            if(r != 0 && errno != EEXIST)
+                error("Host '%s': cannot create directory '%s'", host->hostname, host->cache_dir);
+        }
+
+        snprintfz(filename, FILENAME_MAX, "%s/%s", netdata_configured_varlib_dir, host->machine_guid);
+        host->varlib_dir = strdupz(filename);
+
+        if(host->health_enabled) {
+            int r = mkdir(host->varlib_dir, 0775);
+            if(r != 0 && errno != EEXIST)
+                error("Host '%s': cannot create directory '%s'", host->hostname, host->varlib_dir);
+        }
+
+        snprintfz(filename, FILENAME_MAX, "%s/health", host->varlib_dir);
+        int r = mkdir(filename, 0775);
+        if(r != 0 && errno != EEXIST)
+            error("Host '%s': cannot create directory '%s'", host->hostname, filename);
+
+        snprintfz(filename, FILENAME_MAX, "%s/health/health-log.db", host->varlib_dir);
         host->health_log_filename = strdupz(filename);
+
     }
 
     snprintfz(filename, FILENAME_MAX, "%s/alarm-notify.sh", netdata_configured_plugins_dir);
@@ -108,18 +152,27 @@ RRDHOST *rrdhost_create(const char *hostname, const char *guid) {
     health_alarm_log_load(host);
     health_alarm_log_open(host);
 
-    rrdhost_rwlock(host);
+    rrdhost_wrlock(host);
     health_readdir(host, health_config_dir());
     rrdhost_unlock(host);
 
 
     // ------------------------------------------------------------------------
-    // add it to the index
+    // link it and add it to the index
+
+    rrd_wrlock();
+
+    if(localhost) {
+        host->next = localhost->next;
+        localhost->next = host;
+    }
 
     if(rrdhost_index_add(host) != host)
-        fatal("Cannot add host '%s' to index. It already exists.", hostname);
+        fatal("Host '%s': cannot add host to index. It already exists.", hostname);
+
+    rrd_unlock();
 
-    debug(D_RRDHOST, "Added host '%s' with guid '%s'", host->hostname, host->machine_guid);
+    debug(D_RRDHOST, "Host '%s', added with guid '%s'", host->hostname, host->machine_guid);
     return host;
 }
 
@@ -128,7 +181,13 @@ RRDHOST *rrdhost_find_or_create(const char *hostname, const char *guid) {
 
     RRDHOST *host = rrdhost_find(guid, 0);
     if(!host)
-        host = rrdhost_create(hostname, guid);
+        host = rrdhost_create(hostname,
+                guid,
+                default_rrd_update_every,
+                default_rrd_history_entries,
+                default_rrd_memory_mode,
+                default_health_enabled
+        );
 
     return host;
 }
@@ -138,82 +197,101 @@ RRDHOST *rrdhost_find_or_create(const char *hostname, const char *guid) {
 
 void rrd_init(char *hostname) {
     debug(D_RRDHOST, "Initializing localhost with hostname '%s'", hostname);
-    localhost = rrdhost_create(hostname, registry_get_this_machine_guid());
+
+    localhost = rrdhost_create(hostname,
+            registry_get_this_machine_guid(),
+            default_rrd_update_every,
+            default_rrd_history_entries,
+            default_rrd_memory_mode,
+            default_health_enabled
+    );
 }
 
 // ----------------------------------------------------------------------------
-// RRDHOST - locks
+// RRDHOST - lock validations
+// there are only used when NETDATA_INTERNAL_CHECKS is set
 
-void rrdhost_rwlock(RRDHOST *host) {
-    debug(D_RRDHOST, "Write lock host '%s'", host->hostname);
-    pthread_rwlock_wrlock(&host->rrdset_root_rwlock);
-}
+void rrdhost_check_rdlock_int(RRDHOST *host, const char *file, const char *function, const unsigned long line) {
+    debug(D_RRDHOST, "Checking read lock on host '%s'", host->hostname);
 
-void rrdhost_rdlock(RRDHOST *host) {
-    debug(D_RRDHOST, "Read lock host '%s'", host->hostname);
-    pthread_rwlock_rdlock(&host->rrdset_root_rwlock);
+    int ret = pthread_rwlock_trywrlock(&host->rrdhost_rwlock);
+    if(ret == 0)
+        fatal("RRDHOST '%s' should be read-locked, but it is not, at function %s() at line %lu of file '%s'", host->hostname, function, line, file);
 }
 
-void rrdhost_unlock(RRDHOST *host) {
-    debug(D_RRDHOST, "Unlock host '%s'", host->hostname);
-    pthread_rwlock_unlock(&host->rrdset_root_rwlock);
+void rrdhost_check_wrlock_int(RRDHOST *host, const char *file, const char *function, const unsigned long line) {
+    debug(D_RRDHOST, "Checking write lock on host '%s'", host->hostname);
+
+    int ret = pthread_rwlock_tryrdlock(&host->rrdhost_rwlock);
+    if(ret == 0)
+        fatal("RRDHOST '%s' should be write-locked, but it is not, at function %s() at line %lu of file '%s'", host->hostname, function, line, file);
 }
 
-void rrdhost_check_rdlock_int(RRDHOST *host, const char *file, const char *function, const unsigned long line) {
-    debug(D_RRDHOST, "Read lock host '%s'", host->hostname);
+void rrd_check_rdlock_int(const char *file, const char *function, const unsigned long line) {
+    debug(D_RRDHOST, "Checking read lock on all RRDs");
 
-    int ret = pthread_rwlock_trywrlock(&host->rrdset_root_rwlock);
+    int ret = pthread_rwlock_trywrlock(&rrd_rwlock);
     if(ret == 0)
-        fatal("RRDHOST '%s' should be read-locked, but it is not, at function %s() at line %lu of file '%s'", host->hostname, function, line, file);
+        fatal("RRDs should be read-locked, but it are not, at function %s() at line %lu of file '%s'", function, line, file);
 }
 
-void rrdhost_check_wrlock_int(RRDHOST *host, const char *file, const char *function, const unsigned long line) {
-    debug(D_RRDHOST, "Write lock host '%s'", host->hostname);
+void rrd_check_wrlock_int(const char *file, const char *function, const unsigned long line) {
+    debug(D_RRDHOST, "Checking write lock on all RRDs");
 
-    int ret = pthread_rwlock_tryrdlock(&host->rrdset_root_rwlock);
+    int ret = pthread_rwlock_tryrdlock(&rrd_rwlock);
     if(ret == 0)
-        fatal("RRDHOST '%s' should be write-locked, but it is not, at function %s() at line %lu of file '%s'", host->hostname, function, line, file);
+        fatal("RRDs should be write-locked, but it are not, at function %s() at line %lu of file '%s'", function, line, file);
 }
 
+// ----------------------------------------------------------------------------
+// RRDHOST - free
+
 void rrdhost_free(RRDHOST *host) {
     if(!host) return;
 
     info("Freeing all memory for host '%s'...", host->hostname);
 
-    rrdhost_rwlock(host);
+    rrd_check_wrlock();     // make sure the RRDs are write locked
+    rrdhost_wrlock(host);   // lock this RRDHOST
 
-    RRDSET *st;
-    for(st = host->rrdset_root; st ;) {
-        RRDSET *next = st->next;
+    // ------------------------------------------------------------------------
+    // release its children resources
 
-        pthread_rwlock_wrlock(&st->rwlock);
+    while(host->rrdset_root) rrdset_free(host->rrdset_root);
 
-        while(st->variables)  rrdsetvar_free(st->variables);
-        while(st->alarms)     rrdsetcalc_unlink(st->alarms);
-        while(st->dimensions) rrddim_free(st, st->dimensions);
+    while(host->alarms) rrdcalc_free(host, host->alarms);
+    while(host->templates) rrdcalctemplate_free(host, host->templates);
+    health_alarm_log_free(host);
 
-        if(unlikely(rrdset_index_del(host, st) != st))
-            error("RRDSET: INTERNAL ERROR: attempt to remove from index chart '%s', removed a different chart.", st->id);
 
-        rrdset_index_del_name(host, st);
+    // ------------------------------------------------------------------------
+    // remove it from the indexes
 
-        st->rrdfamily->use_count--;
-        if(!st->rrdfamily->use_count)
-            rrdfamily_free(host, st->rrdfamily);
+    if(rrdhost_index_del(host) != host)
+        error("RRDHOST '%s' removed from index, deleted the wrong entry.", host->hostname);
 
-        pthread_rwlock_unlock(&st->rwlock);
 
-        if(st->mapped == RRD_MEMORY_MODE_SAVE || st->mapped == RRD_MEMORY_MODE_MAP) {
-            debug(D_RRD_CALLS, "Unmapping stats '%s'.", st->name);
-            munmap(st, st->memsize);
-        }
-        else
-            freez(st);
+    // ------------------------------------------------------------------------
+    // unlink it from the host
 
-        st = next;
+    if(host == localhost) {
+        localhost = host->next;
     }
-    host->rrdset_root = NULL;
+    else {
+        // find the previous one
+        RRDHOST *h;
+        for(h = localhost; h && h->next != host ; h = h->next) ;
+
+        // bypass it
+        if(h) h->next = host->next;
+        else error("Request to free RRDHOST '%s': cannot find it", host->hostname);
+    }
+
+    // ------------------------------------------------------------------------
+    // free it
 
+    freez(host->cache_dir);
+    freez(host->varlib_dir);
     freez(host->health_default_exec);
     freez(host->health_default_recipient);
     freez(host->health_log_filename);
@@ -224,6 +302,15 @@ void rrdhost_free(RRDHOST *host) {
     info("Host memory cleanup completed...");
 }
 
+void rrdhost_free_all(void) {
+    rrd_wrlock();
+    while(localhost) rrdhost_free(localhost);
+    rrd_unlock();
+}
+
+// ----------------------------------------------------------------------------
+// RRDHOST - save
+
 void rrdhost_save(RRDHOST *host) {
     if(!host) return;
 
@@ -232,49 +319,39 @@ void rrdhost_save(RRDHOST *host) {
     RRDSET *st;
     RRDDIM *rd;
 
-    // we get an write lock
+    // we get a write lock
     // to ensure only one thread is saving the database
-    rrdhost_rwlock(host);
+    rrdhost_wrlock(host);
 
-    for(st = host->rrdset_root; st ; st = st->next) {
-        pthread_rwlock_rdlock(&st->rwlock);
+    rrdset_foreach_write(st, host) {
+        rrdset_rdlock(st);
 
-        if(st->mapped == RRD_MEMORY_MODE_SAVE) {
-            debug(D_RRD_CALLS, "Saving stats '%s' to '%s'.", st->name, st->cache_filename);
+        if(st->rrd_memory_mode == RRD_MEMORY_MODE_SAVE) {
+            debug(D_RRD_STATS, "Saving stats '%s' to '%s'.", st->name, st->cache_filename);
             savememory(st->cache_filename, st, st->memsize);
         }
 
-        for(rd = st->dimensions; rd ; rd = rd->next) {
-            if(likely(rd->memory_mode == RRD_MEMORY_MODE_SAVE)) {
-                debug(D_RRD_CALLS, "Saving dimension '%s' to '%s'.", rd->name, rd->cache_filename);
+        rrddim_foreach_read(rd, st) {
+            if(likely(rd->rrd_memory_mode == RRD_MEMORY_MODE_SAVE)) {
+                debug(D_RRD_STATS, "Saving dimension '%s' to '%s'.", rd->name, rd->cache_filename);
                 savememory(rd->cache_filename, rd, rd->memsize);
             }
         }
 
-        pthread_rwlock_unlock(&st->rwlock);
+        rrdset_unlock(st);
     }
 
     rrdhost_unlock(host);
 }
 
-void rrdhost_free_all(void) {
-    RRDHOST *host = localhost;
-
-    // FIXME: lock all hosts
-
-    while(host) {
-        RRDHOST *next = host = host->next;
-        rrdhost_free(host);
-        host = next;
-    }
+void rrdhost_save_all(void) {
+    info("Saving database...");
 
-    localhost = NULL;
+    rrd_rdlock();
 
-    // FIXME: unlock all hosts
-}
-
-void rrdhost_save_all(void) {
     RRDHOST *host;
-    for(host = localhost; host ; host = host->next)
+    rrdhost_foreach_read(host)
         rrdhost_save(host);
+
+    rrd_unlock();
 }