]> arthur.barton.de Git - netdata.git/commitdiff
external plugins can now work for a specific host
authorCosta Tsaousis (ktsaou) <costa@tsaousis.gr>
Sat, 18 Feb 2017 11:38:37 +0000 (13:38 +0200)
committerCosta Tsaousis (ktsaou) <costa@tsaousis.gr>
Tue, 21 Feb 2017 23:00:21 +0000 (01:00 +0200)
src/plugins_d.c
src/plugins_d.h
src/rrd.h
src/rrdhost.c

index b79608effd59c1423f493bc6181240496631b4da..ec82c1e7e579bb35c1155ee29ab079cf6f213648 100644 (file)
@@ -206,6 +206,25 @@ void *pluginsd_worker_thread(void *arg) {
 
                 count++;
             }
+            else if(likely(hash == HOST_HASH && !strcmp(s, "HOST"))) {
+                char *guid = words[1];
+                char *hostname = words[2];
+
+                if(unlikely(!guid || !*guid)) {
+                    error("PLUGINSD: '%s' is requesting a HOST, without a guid. Disabling it.", cd->fullfilename);
+                    cd->enabled = 0;
+                    killpid(cd->pid, SIGTERM);
+                    break;
+                }
+                if(unlikely(!hostname || !*hostname)) {
+                    error("PLUGINSD: '%s' is requesting a HOST, without a hostname. Disabling it.", cd->fullfilename);
+                    cd->enabled = 0;
+                    killpid(cd->pid, SIGTERM);
+                    break;
+                }
+
+                host = rrdhost_find_or_create(hostname, guid);
+            }
             else if(likely(hash == FLUSH_HASH && !strcmp(s, "FLUSH"))) {
                 debug(D_PLUGINSD, "PLUGINSD: '%s' is requesting a FLUSH", cd->fullfilename);
                 st = NULL;
@@ -246,7 +265,7 @@ void *pluginsd_worker_thread(void *arg) {
                 if(likely(update_every_s)) update_every = str2i(update_every_s);
                 if(unlikely(!update_every)) update_every = cd->update_every;
 
-                int chart_type = RRDSET_TYPE_LINE;
+                RRDSET_TYPE chart_type = RRDSET_TYPE_LINE;
                 if(unlikely(chart)) chart_type = rrdset_type_id(chart);
 
                 if(unlikely(noname || !name || !*name || strcasecmp(name, "NULL") == 0 || strcasecmp(name, "(NULL)") == 0)) name = NULL;
index 3c74355a3d6b30f6a7a93e2ba02e5898cdf4ad4e..91c5c14b5d3fd2e8f6311cbbc819d68c5ce2d0f4 100644 (file)
@@ -11,7 +11,7 @@ struct plugind {
 
     char filename[FILENAME_MAX+1];      // just the filename
     char fullfilename[FILENAME_MAX+1];  // with path
-    char cmd[PLUGINSD_CMD_MAX+1];       // the command that is executes
+    char cmd[PLUGINSD_CMD_MAX+1];       // the command that it executes
 
     pid_t pid;
     pthread_t thread;
index ceae9963b98885d3ab2771e711b164ad6ff62fab..2beb1433f31f70489eb3353b8c47c9e2a15d1044 100644 (file)
--- a/src/rrd.h
+++ b/src/rrd.h
@@ -335,6 +335,7 @@ extern RRDHOST *localhost;
 extern void rrd_init(char *hostname);
 
 extern RRDHOST *rrdhost_find(const char *guid, uint32_t hash);
+extern RRDHOST *rrdhost_find_or_create(const char *hostname, const char *guid);
 
 #ifdef NETDATA_INTERNAL_CHECKS
 #define rrdhost_check_wrlock(host) rrdhost_check_wrlock_int(host, __FILE__, __FUNCTION__, __LINE__)
index 64a96c59a811412db6f26e2c279818d0fdf8b415..e1b74a68ffabca344956702d8422bc2c5308992a 100644 (file)
@@ -16,6 +16,8 @@ avl_tree_lock rrdhost_root_index = {
 };
 
 RRDHOST *rrdhost_find(const char *guid, uint32_t hash) {
+    debug(D_RRDHOST, "Searching in index for host with guid '%s'", guid);
+
     RRDHOST tmp;
     strncpyz(tmp.machine_guid, guid, GUID_LEN);
     tmp.hash_machine_guid = (hash)?hash:simple_hash(tmp.machine_guid);
@@ -46,6 +48,8 @@ static inline void rrdhost_init_machine_guid(RRDHOST *host, const char *machine_
 // 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 *host = callocz(1, sizeof(RRDHOST));
 
     pthread_rwlock_init(&(host->rrdset_root_rwlock), NULL);
@@ -68,7 +72,17 @@ RRDHOST *rrdhost_create(const char *hostname, const char *guid) {
     if(rrdhost_index_add(host) != host)
         fatal("Cannot add host '%s' to index. It already exists.", hostname);
 
-    debug(D_RRDHOST, "Added host '%s'", host->hostname);
+    debug(D_RRDHOST, "Added host '%s' with guid '%s'", host->hostname, host->machine_guid);
+    return host;
+}
+
+RRDHOST *rrdhost_find_or_create(const char *hostname, const char *guid) {
+    debug(D_RRDHOST, "Searching for host '%s' with guid '%s'", hostname, guid);
+
+    RRDHOST *host = rrdhost_find(guid, 0);
+    if(!host)
+        host = rrdhost_create(hostname, guid);
+
     return host;
 }
 
@@ -78,6 +92,7 @@ RRDHOST *rrdhost_create(const char *hostname, const char *guid) {
 RRDHOST *localhost = NULL;
 
 void rrd_init(char *hostname) {
+    debug(D_RRDHOST, "Initializing localhost with hostname '%s'", hostname);
     localhost = rrdhost_create(hostname, registry_get_this_machine_guid());
 }
 
@@ -85,27 +100,32 @@ void rrd_init(char *hostname) {
 // RRDHOST - locks
 
 void rrdhost_rwlock(RRDHOST *host) {
+    debug(D_RRDHOST, "Write lock host '%s'", host->hostname);
     pthread_rwlock_wrlock(&host->rrdset_root_rwlock);
 }
 
 void rrdhost_rdlock(RRDHOST *host) {
+    debug(D_RRDHOST, "Read lock host '%s'", host->hostname);
     pthread_rwlock_rdlock(&host->rrdset_root_rwlock);
 }
 
 void rrdhost_unlock(RRDHOST *host) {
+    debug(D_RRDHOST, "Unlock host '%s'", host->hostname);
     pthread_rwlock_unlock(&host->rrdset_root_rwlock);
 }
 
 void rrdhost_check_rdlock_int(RRDHOST *host, const char *file, const char *function, const unsigned long line) {
-    int ret = pthread_rwlock_trywrlock(&host->rrdset_root_rwlock);
+    debug(D_RRDHOST, "Read lock host '%s'", host->hostname);
 
+    int ret = pthread_rwlock_trywrlock(&host->rrdset_root_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_check_wrlock_int(RRDHOST *host, const char *file, const char *function, const unsigned long line) {
-    int ret = pthread_rwlock_tryrdlock(&host->rrdset_root_rwlock);
+    debug(D_RRDHOST, "Write lock host '%s'", host->hostname);
 
+    int ret = pthread_rwlock_tryrdlock(&host->rrdset_root_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);
 }