]> arthur.barton.de Git - netdata.git/commitdiff
added support for custom variables that can be exposed to alarm expressions without...
authorCosta Tsaousis (ktsaou) <costa@tsaousis.gr>
Sat, 12 Nov 2016 23:19:43 +0000 (01:19 +0200)
committerCosta Tsaousis (ktsaou) <costa@tsaousis.gr>
Sat, 12 Nov 2016 23:19:43 +0000 (01:19 +0200)
src/health.c
src/health.h

index 5dc6586d3e7b19ef913dc4232cf57d858b710da1..876acac56bd1a0dd498ee2f527c6a1b44b901887 100644 (file)
@@ -488,14 +488,15 @@ static inline void rrdvar_free(RRDHOST *host, avl_tree_lock *tree, RRDVAR *rv) {
 
     if(tree) {
         debug(D_VARIABLES, "Deleting variable '%s'", rv->name);
-        rrdvar_index_del(tree, rv);
+        if(unlikely(!rrdvar_index_del(tree, rv)))
+            error("Attempted to delete variable '%s' from host '%s', but it is not found.", rv->name, host->hostname);
     }
 
     freez(rv->name);
     freez(rv);
 }
 
-static inline RRDVAR *rrdvar_create_and_index(const char *scope, avl_tree_lock *tree, const char *name, int type, calculated_number *value) {
+static inline RRDVAR *rrdvar_create_and_index(const char *scope, avl_tree_lock *tree, const char *name, int type, void *value) {
     char *variable = strdupz(name);
     rrdvar_fix_name(variable);
     uint32_t hash = simple_hash(variable);
@@ -533,11 +534,70 @@ static inline RRDVAR *rrdvar_create_and_index(const char *scope, avl_tree_lock *
     return rv;
 }
 
+// ----------------------------------------------------------------------------
+// CUSTOM VARIABLES
+
+RRDVAR *rrdvar_custom_host_variable_create(RRDHOST *host, const char *name) {
+    calculated_number *v = callocz(1, sizeof(calculated_number));
+    *v = NAN;
+    RRDVAR *rv = rrdvar_create_and_index("host", &host->variables_root_index, name, RRDVAR_TYPE_CALCULATED_ALLOCATED, v);
+    if(unlikely(!rv)) {
+        free(v);
+        error("Requested variable '%s' already exists - possibly 2 plugins will be updating it at the same time", name);
+
+        char *variable = strdupz(name);
+        rrdvar_fix_name(variable);
+        uint32_t hash = simple_hash(variable);
+
+        rv = rrdvar_index_find(&host->variables_root_index, variable, hash);
+    }
+
+    return rv;
+}
+
+void rrdvar_custom_host_variable_destroy(RRDHOST *host, const char *name) {
+    char *variable = strdupz(name);
+    rrdvar_fix_name(variable);
+    uint32_t hash = simple_hash(variable);
+
+    RRDVAR *rv = rrdvar_index_find(&host->variables_root_index, variable, hash);
+    freez(variable);
+
+    if(!rv) {
+        error("Attempted to remove variable '%s' from host '%s', but it does not exist.", name, host->hostname);
+        return;
+    }
+
+    if(rv->type != RRDVAR_TYPE_CALCULATED_ALLOCATED) {
+        error("Attempted to remove variable '%s' from host '%s', but it does not a custom allocated variable.", name, host->hostname);
+        return;
+    }
+
+    if(!rrdvar_index_del(&host->variables_root_index, rv)) {
+        error("Attempted to remove variable '%s' from host '%s', but it cannot be found.", name, host->hostname);
+        return;
+    }
+
+    freez(rv->name);
+    freez(rv->value);
+    freez(rv);
+}
+
+void rrdvar_custom_host_variable_set(RRDVAR *rv, calculated_number value) {
+    if(rv->type != RRDVAR_TYPE_CALCULATED_ALLOCATED)
+        error("requested to set variable '%s' to value " CALCULATED_NUMBER_FORMAT " but the variable is not a custom one.", rv->name, value);
+    else {
+        calculated_number *v = rv->value;
+        *v = value;
+    }
+}
+
 // ----------------------------------------------------------------------------
 // RRDVAR lookup
 
 static calculated_number rrdvar2number(RRDVAR *rv) {
     switch(rv->type) {
+        case RRDVAR_TYPE_CALCULATED_ALLOCATED:
         case RRDVAR_TYPE_CALCULATED: {
             calculated_number *n = (calculated_number *)rv->value;
             return *n;
index 57096c6b3a8e8c25a19892f7e5051683f00c7c2e..10885c623769b6665aec2a1fc25a397b89bcf924 100644 (file)
@@ -5,11 +5,13 @@ extern int health_enabled;
 
 extern int rrdvar_compare(void *a, void *b);
 
-#define RRDVAR_TYPE_CALCULATED 1
-#define RRDVAR_TYPE_TIME_T     2
-#define RRDVAR_TYPE_COLLECTED  3
-#define RRDVAR_TYPE_TOTAL      4
-#define RRDVAR_TYPE_INT        5
+#define RRDVAR_TYPE_CALCULATED              1
+#define RRDVAR_TYPE_TIME_T                  2
+#define RRDVAR_TYPE_COLLECTED               3
+#define RRDVAR_TYPE_TOTAL                   4
+#define RRDVAR_TYPE_INT                     5
+#define RRDVAR_TYPE_CALCULATED_ALLOCATED    6
+
 
 // the variables as stored in the variables indexes
 // there are 3 indexes:
@@ -352,4 +354,8 @@ extern void health_alarm_log2json(RRDHOST *host, BUFFER *wb, uint32_t after);
 
 void health_api_v1_chart_variables2json(RRDSET *st, BUFFER *buf);
 
+extern RRDVAR *rrdvar_custom_host_variable_create(RRDHOST *host, const char *name);
+extern void rrdvar_custom_host_variable_destroy(RRDHOST *host, const char *name);
+extern void rrdvar_custom_host_variable_set(RRDVAR *rv, calculated_number value);
+
 #endif //NETDATA_HEALTH_H