]> arthur.barton.de Git - netdata.git/blob - src/rrdsetvar.c
dns_query_time plugin: replace "." with "_" in dimensions
[netdata.git] / src / rrdsetvar.c
1 #define NETDATA_HEALTH_INTERNALS
2 #include "common.h"
3
4 // ----------------------------------------------------------------------------
5 // RRDSETVAR management
6 // CHART VARIABLES
7
8 static inline void rrdsetvar_free_variables(RRDSETVAR *rs) {
9     RRDSET *st = rs->rrdset;
10
11     // CHART
12
13     rrdvar_free(st->rrdhost, &st->variables_root_index, rs->var_local);
14     rs->var_local = NULL;
15
16     // FAMILY
17
18     rrdvar_free(st->rrdhost, &st->rrdfamily->variables_root_index, rs->var_family);
19     rs->var_family = NULL;
20
21     rrdvar_free(st->rrdhost, &st->rrdhost->variables_root_index, rs->var_host);
22     rs->var_host = NULL;
23
24     // HOST
25
26     rrdvar_free(st->rrdhost, &st->rrdfamily->variables_root_index, rs->var_family_name);
27     rs->var_family_name = NULL;
28
29     rrdvar_free(st->rrdhost, &st->rrdhost->variables_root_index, rs->var_host_name);
30     rs->var_host_name = NULL;
31
32     // KEYS
33
34     freez(rs->key_fullid);
35     rs->key_fullid = NULL;
36
37     freez(rs->key_fullname);
38     rs->key_fullname = NULL;
39 }
40
41 static inline void rrdsetvar_create_variables(RRDSETVAR *rs) {
42     rrdsetvar_free_variables(rs);
43
44     RRDSET *st = rs->rrdset;
45
46     // KEYS
47
48     char buffer[RRDVAR_MAX_LENGTH + 1];
49     snprintfz(buffer, RRDVAR_MAX_LENGTH, "%s.%s", st->id, rs->variable);
50     rs->key_fullid = strdupz(buffer);
51
52     snprintfz(buffer, RRDVAR_MAX_LENGTH, "%s.%s", st->name, rs->variable);
53     rs->key_fullname = strdupz(buffer);
54
55     // CHART
56
57     rs->var_local       = rrdvar_create_and_index("local",  &st->variables_root_index,               rs->variable, rs->type, rs->value);
58
59     // FAMILY
60
61     rs->var_family      = rrdvar_create_and_index("family", &st->rrdfamily->variables_root_index,    rs->key_fullid,   rs->type, rs->value);
62     rs->var_family_name = rrdvar_create_and_index("family", &st->rrdfamily->variables_root_index,    rs->key_fullname, rs->type, rs->value);
63
64     // HOST
65
66     rs->var_host        = rrdvar_create_and_index("host",   &st->rrdhost->variables_root_index,      rs->key_fullid,   rs->type, rs->value);
67     rs->var_host_name   = rrdvar_create_and_index("host",   &st->rrdhost->variables_root_index,      rs->key_fullname, rs->type, rs->value);
68
69 }
70
71 RRDSETVAR *rrdsetvar_create(RRDSET *st, const char *variable, int type, void *value, uint32_t options) {
72     debug(D_VARIABLES, "RRDVARSET create for chart id '%s' name '%s' with variable name '%s'", st->id, st->name, variable);
73     RRDSETVAR *rs = (RRDSETVAR *)callocz(1, sizeof(RRDSETVAR));
74
75     rs->variable = strdupz(variable);
76     rs->type = type;
77     rs->value = value;
78     rs->options = options;
79     rs->rrdset = st;
80
81     rs->next = st->variables;
82     st->variables = rs;
83
84     rrdsetvar_create_variables(rs);
85
86     return rs;
87 }
88
89 void rrdsetvar_rename_all(RRDSET *st) {
90     debug(D_VARIABLES, "RRDSETVAR rename for chart id '%s' name '%s'", st->id, st->name);
91
92     RRDSETVAR *rs, *next = st->variables;
93     while((rs = next)) {
94         next = rs->next;
95         rrdsetvar_create_variables(rs);
96     }
97
98     rrdsetcalc_link_matching(st);
99 }
100
101 void rrdsetvar_free(RRDSETVAR *rs) {
102     RRDSET *st = rs->rrdset;
103     debug(D_VARIABLES, "RRDSETVAR free for chart id '%s' name '%s', variable '%s'", st->id, st->name, rs->variable);
104
105     if(st->variables == rs) {
106         st->variables = rs->next;
107     }
108     else {
109         RRDSETVAR *t;
110         for (t = st->variables; t && t->next != rs; t = t->next);
111         if(!t) error("RRDSETVAR '%s' not found in chart '%s' variables linked list", rs->key_fullname, st->id);
112         else t->next = rs->next;
113     }
114
115     rrdsetvar_free_variables(rs);
116
117     freez(rs->variable);
118     freez(rs);
119 }
120