1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
|
// SPDX-License-Identifier: GPL-3.0-or-later
#include "appconfig_internals.h"
ENUM_STR_MAP_DEFINE(CONFIG_VALUE_TYPES) = {
{ .id = CONFIG_VALUE_TYPE_UNKNOWN, .name ="unknown", },
{ .id = CONFIG_VALUE_TYPE_TEXT, .name ="text", },
{ .id = CONFIG_VALUE_TYPE_HOSTNAME, .name ="hostname", },
{ .id = CONFIG_VALUE_TYPE_USERNAME, .name ="username", },
{ .id = CONFIG_VALUE_TYPE_FILENAME, .name ="filename", },
{ .id = CONFIG_VALUE_TYPE_PATH, .name ="path", },
{ .id = CONFIG_VALUE_TYPE_SIMPLE_PATTERN, .name ="simple pattern", },
{ .id = CONFIG_VALUE_TYPE_URL, .name ="URL", },
{ .id = CONFIG_VALUE_TYPE_ENUM, .name ="one of keywords", },
{ .id = CONFIG_VALUE_TYPE_BITMAP, .name ="any of keywords", },
{ .id = CONFIG_VALUE_TYPE_INTEGER, .name ="number (integer)", },
{ .id = CONFIG_VALUE_TYPE_DOUBLE, .name ="number (double)", },
{ .id = CONFIG_VALUE_TYPE_BOOLEAN, .name ="yes or no", },
{ .id = CONFIG_VALUE_TYPE_BOOLEAN_ONDEMAND, .name ="yes, no, or auto", },
{ .id = CONFIG_VALUE_TYPE_DURATION_IN_SECS, .name ="duration (seconds)", },
{ .id = CONFIG_VALUE_TYPE_DURATION_IN_MS, .name ="duration (ms)", },
{ .id = CONFIG_VALUE_TYPE_DURATION_IN_DAYS, .name ="duration (days)", },
{ .id = CONFIG_VALUE_TYPE_SIZE_IN_BYTES, .name ="size (bytes)", },
{ .id = CONFIG_VALUE_TYPE_SIZE_IN_MB, .name ="size (MiB)", },
};
ENUM_STR_DEFINE_FUNCTIONS(CONFIG_VALUE_TYPES, CONFIG_VALUE_TYPE_UNKNOWN, "unknown");
// ----------------------------------------------------------------------------
// config load/save
int appconfig_load(struct config *root, char *filename, int overwrite_used, const char *section_name) {
int line = 0;
struct config_section *sect = NULL;
int is_exporter_config = 0;
int _connectors = 0; // number of exporting connector sections we have
char working_instance[CONFIG_MAX_NAME + 1];
char working_connector[CONFIG_MAX_NAME + 1];
struct config_section *working_connector_section = NULL;
int global_exporting_section = 0;
char buffer[CONFIG_FILE_LINE_MAX + 1], *s;
if(!filename) filename = CONFIG_DIR "/" CONFIG_FILENAME;
netdata_log_debug(D_CONFIG, "CONFIG: opening config file '%s'", filename);
FILE *fp = fopen(filename, "r");
if(!fp) {
if(errno != ENOENT)
netdata_log_info("CONFIG: cannot open file '%s'. Using internal defaults.", filename);
return 0;
}
CLEAN_STRING *section_string = string_strdupz(section_name);
is_exporter_config = (strstr(filename, EXPORTING_CONF) != NULL);
while(fgets(buffer, CONFIG_FILE_LINE_MAX, fp) != NULL) {
buffer[CONFIG_FILE_LINE_MAX] = '\0';
line++;
s = trim(buffer);
if(!s || *s == '#') {
netdata_log_debug(D_CONFIG, "CONFIG: ignoring line %d of file '%s', it is empty.", line, filename);
continue;
}
int len = (int) strlen(s);
if(*s == '[' && s[len - 1] == ']') {
// new section
s[len - 1] = '\0';
s++;
if (is_exporter_config) {
global_exporting_section = !(strcmp(s, CONFIG_SECTION_EXPORTING)) || !(strcmp(s, CONFIG_SECTION_PROMETHEUS));
if (unlikely(!global_exporting_section)) {
int rc;
rc = is_valid_connector(s, 0);
if (likely(rc)) {
strncpyz(working_connector, s, CONFIG_MAX_NAME);
s = s + rc + 1;
if (unlikely(!(*s))) {
_connectors++;
sprintf(buffer, "instance_%d", _connectors);
s = buffer;
}
strncpyz(working_instance, s, CONFIG_MAX_NAME);
working_connector_section = NULL;
if (unlikely(appconfig_section_find(root, working_instance))) {
netdata_log_error("Instance (%s) already exists", working_instance);
sect = NULL;
continue;
}
}
else {
sect = NULL;
netdata_log_error("Section (%s) does not specify a valid connector", s);
continue;
}
}
}
sect = appconfig_section_find(root, s);
if(!sect)
sect = appconfig_section_create(root, s);
if(sect && section_string && overwrite_used && section_string == sect->name) {
SECTION_LOCK(sect);
while(sect->values)
appconfig_option_remove_and_delete(sect, sect->values, true);
SECTION_UNLOCK(sect);
}
continue;
}
if(!sect) {
// line outside a section
netdata_log_error("CONFIG: ignoring line %d ('%s') of file '%s', it is outside all sections.", line, s, filename);
continue;
}
if(section_string && overwrite_used && section_string != sect->name)
continue;
char *name = s;
char *value = strchr(s, '=');
if(!value) {
netdata_log_error("CONFIG: ignoring line %d ('%s') of file '%s', there is no = in it.", line, s, filename);
continue;
}
*value = '\0';
value++;
name = trim(name);
value = trim(value);
if(!name || *name == '#') {
netdata_log_error("CONFIG: ignoring line %d of file '%s', name is empty.", line, filename);
continue;
}
if(!value) value = "";
struct config_option *opt = appconfig_option_find(sect, name);
if (!opt) {
opt = appconfig_option_create(sect, name, value);
if (likely(is_exporter_config) && unlikely(!global_exporting_section)) {
if (unlikely(!working_connector_section)) {
working_connector_section = appconfig_section_find(root, working_connector);
if (!working_connector_section)
working_connector_section = appconfig_section_create(root, working_connector);
if (likely(working_connector_section)) {
add_connector_instance(working_connector_section, sect);
}
}
}
}
else {
if (((opt->flags & CONFIG_VALUE_USED) && overwrite_used) || !(opt->flags & CONFIG_VALUE_USED)) {
string_freez(opt->value);
opt->value = string_strdupz(value);
}
}
opt->flags |= CONFIG_VALUE_LOADED;
}
fclose(fp);
return 1;
}
void appconfig_generate(struct config *root, BUFFER *wb, int only_changed, bool netdata_conf)
{
int i, pri;
struct config_section *sect;
struct config_option *opt;
{
int found_host_labels = 0;
for (sect = root->sections; sect; sect = sect->next)
if(!string_strcmp(sect->name, CONFIG_SECTION_HOST_LABEL))
found_host_labels = 1;
if(netdata_conf && !found_host_labels) {
appconfig_section_create(root, CONFIG_SECTION_HOST_LABEL);
appconfig_get_raw_value(root, CONFIG_SECTION_HOST_LABEL, "name", "value", CONFIG_VALUE_TYPE_TEXT, NULL);
}
}
if(netdata_conf) {
buffer_strcat(wb,
"# netdata configuration\n"
"#\n"
"# You can download the latest version of this file, using:\n"
"#\n"
"# wget -O /etc/netdata/netdata.conf http://localhost:19999/netdata.conf\n"
"# or\n"
"# curl -o /etc/netdata/netdata.conf http://localhost:19999/netdata.conf\n"
"#\n"
"# You can uncomment and change any of the options below.\n"
"# The value shown in the commented settings, is the default value.\n"
"#\n"
"\n# global netdata configuration\n");
}
for(i = 0; i <= 17 ;i++) {
APPCONFIG_LOCK(root);
for(sect = root->sections; sect; sect = sect->next) {
if(!string_strcmp(sect->name, CONFIG_SECTION_GLOBAL)) pri = 0;
else if(!string_strcmp(sect->name, CONFIG_SECTION_DB)) pri = 1;
else if(!string_strcmp(sect->name, CONFIG_SECTION_DIRECTORIES)) pri = 2;
else if(!string_strcmp(sect->name, CONFIG_SECTION_LOGS)) pri = 3;
else if(!string_strcmp(sect->name, CONFIG_SECTION_ENV_VARS)) pri = 4;
else if(!string_strcmp(sect->name, CONFIG_SECTION_HOST_LABEL)) pri = 5;
else if(!string_strcmp(sect->name, CONFIG_SECTION_SQLITE)) pri = 6;
else if(!string_strcmp(sect->name, CONFIG_SECTION_CLOUD)) pri = 7;
else if(!string_strcmp(sect->name, CONFIG_SECTION_ML)) pri = 8;
else if(!string_strcmp(sect->name, CONFIG_SECTION_HEALTH)) pri = 9;
else if(!string_strcmp(sect->name, CONFIG_SECTION_WEB)) pri = 10;
else if(!string_strcmp(sect->name, CONFIG_SECTION_WEBRTC)) pri = 11;
// by default, new sections will get pri = 12 (set at the end, below)
else if(!string_strcmp(sect->name, CONFIG_SECTION_REGISTRY)) pri = 13;
else if(!string_strcmp(sect->name, CONFIG_SECTION_GLOBAL_STATISTICS)) pri = 14;
else if(!string_strcmp(sect->name, CONFIG_SECTION_PLUGINS)) pri = 15;
else if(!string_strcmp(sect->name, CONFIG_SECTION_STATSD)) pri = 16;
else if(!string_strncmp(sect->name, "plugin:", 7)) pri = 17; // << change the loop too if you change this
else pri = 12; // this is used for any new (currently unknown) sections
if(i == pri) {
int loaded = 0;
int used = 0;
int changed = 0;
int count = 0;
SECTION_LOCK(sect);
for(opt = sect->values; opt; opt = opt->next) {
used += (opt->flags & CONFIG_VALUE_USED)?1:0;
loaded += (opt->flags & CONFIG_VALUE_LOADED)?1:0;
changed += (opt->flags & CONFIG_VALUE_CHANGED)?1:0;
count++;
}
SECTION_UNLOCK(sect);
if(!count) continue;
if(only_changed && !changed && !loaded) continue;
if(!used)
buffer_sprintf(wb, "\n# section '%s' is not used.", string2str(sect->name));
buffer_sprintf(wb, "\n[%s]\n", string2str(sect->name));
size_t options_added = 0;
bool last_had_comments = false;
SECTION_LOCK(sect);
for(opt = sect->values; opt; opt = opt->next) {
bool unused = used && !(opt->flags & CONFIG_VALUE_USED);
bool migrated = used && (opt->flags & CONFIG_VALUE_MIGRATED);
bool reformatted = used && (opt->flags & CONFIG_VALUE_REFORMATTED);
bool show_default = used && (opt->flags & (CONFIG_VALUE_LOADED|CONFIG_VALUE_CHANGED) && opt->value_default);
if((unused || migrated || reformatted || show_default)) {
if(options_added)
buffer_strcat(wb, "\n");
buffer_sprintf(wb, "\t#| >>> [%s].%s <<<\n",
string2str(sect->name), string2str(opt->name));
last_had_comments = true;
}
else if(last_had_comments) {
buffer_strcat(wb, "\n");
last_had_comments = false;
}
if(unused)
buffer_sprintf(wb, "\t#| found in the config file, but is not used\n");
if(migrated && reformatted)
buffer_sprintf(wb, "\t#| migrated from: [%s].%s = %s\n",
string2str(opt->migrated.section), string2str(opt->migrated.name),
string2str(opt->value_original));
else {
if (migrated)
buffer_sprintf(wb, "\t#| migrated from: [%s].%s\n",
string2str(opt->migrated.section), string2str(opt->migrated.name));
if (reformatted)
buffer_sprintf(wb, "\t#| reformatted from: %s\n",
string2str(opt->value_original));
}
if(show_default)
buffer_sprintf(wb, "\t#| datatype: %s, default value: %s\n",
CONFIG_VALUE_TYPES_2str(opt->type),
string2str(opt->value_default));
buffer_sprintf(wb, "\t%s%s = %s\n",
(
!(opt->flags & CONFIG_VALUE_LOADED) &&
!(opt->flags & CONFIG_VALUE_CHANGED) &&
(opt->flags & CONFIG_VALUE_USED)
) ? "# " : "",
string2str(opt->name),
string2str(opt->value));
options_added++;
}
SECTION_UNLOCK(sect);
}
}
APPCONFIG_UNLOCK(root);
}
}
|