@cryptotaxi247 / netdata-1 / commits / 32cd848cb

Add serial numbers to chart names (#12067)

Vladimir Kobal committed Feb 21, 2022 at 16:56 UTC 32cd848cb9b1c14a68fcb82549f38c13a788ae73
1 file changed +22 -9
database/rrdset.c
+22 -9
@@ -144,25 +144,38 @@ int rrdset_set_name(RRDSET *st, const char *name) {
144
145 debug(D_RRD_CALLS, "rrdset_set_name() old: '%s', new: '%s'", st->name?st->name:"", name);
146
147 - char b[CONFIG_MAX_VALUE + 1];
148 - char n[RRD_ID_LENGTH_MAX + 1];
147 + char full_name[RRD_ID_LENGTH_MAX + 1];
148 + char sanitized_name[CONFIG_MAX_VALUE + 1];
149 + char new_name[CONFIG_MAX_VALUE + 1];
150
150 - snprintfz(n, RRD_ID_LENGTH_MAX, "%s.%s", st->type, name);
151 - rrdset_strncpyz_name(b, n, CONFIG_MAX_VALUE);
151 + snprintfz(full_name, RRD_ID_LENGTH_MAX, "%s.%s", st->type, name);
152 + rrdset_strncpyz_name(sanitized_name, full_name, CONFIG_MAX_VALUE);
153 + strncpyz(new_name, sanitized_name, CONFIG_MAX_VALUE);
154
153 - if(rrdset_index_find_name(host, b, 0)) {
154 - info("RRDSET: chart name '%s' on host '%s' already exists.", b, host->hostname);
155 - return 0;
155 + if(rrdset_index_find_name(host, new_name, 0)) {
156 + info("RRDSET: chart name '%s' on host '%s' already exists.", new_name, host->hostname);
157 + if(!strcmp(st->id, full_name) && !st->name) {
158 + unsigned i = 1;
159 +
160 + do {
161 + snprintfz(new_name, CONFIG_MAX_VALUE, "%s_%u", sanitized_name, i);
162 + i++;
163 + } while (rrdset_index_find_name(host, new_name, 0));
164 +
165 + info("RRDSET: using name '%s' for chart '%s' on host '%s'.", new_name, full_name, host->hostname);
166 + } else {
167 + return 0;
168 + }
169 }
170
171 if(st->name) {
172 rrdset_index_del_name(host, st);
160 - st->name = config_set_default(st->config_section, "name", b);
173 + st->name = config_set_default(st->config_section, "name", new_name);
174 st->hash_name = simple_hash(st->name);
175 rrdsetvar_rename_all(st);
176 }
177 else {
165 - st->name = config_get(st->config_section, "name", b);
178 + st->name = config_get(st->config_section, "name", new_name);
179 st->hash_name = simple_hash(st->name);
180 }
181