1
// SPDX-License-Identifier: GPL-3.0-or-later
2
3
-#include "../libnetdata.h"
3
+#include "appconfig_internals.h"
4
5
-/*
6
- * @Input:
7
- * Connector / instance to add to an internal structure
8
- * @Return
9
- * The current head of the linked list of connector_instance
10
- *
11
- */
12
-
13
-_CONNECTOR_INSTANCE *add_connector_instance(struct section *connector, struct section *instance)
14
-{
15
- static struct _connector_instance *global_connector_instance = NULL;
16
- struct _connector_instance *local_ci, *local_ci_tmp;
17
-
18
- if (unlikely(!connector)) {
19
- if (unlikely(!instance))
20
- return global_connector_instance;
21
-
22
- local_ci = global_connector_instance;
23
- while (local_ci) {
24
- local_ci_tmp = local_ci->next;
25
- freez(local_ci);
26
- local_ci = local_ci_tmp;
27
- }
28
- global_connector_instance = NULL;
29
- return NULL;
30
- }
31
-
32
- local_ci = callocz(1, sizeof(struct _connector_instance));
33
- local_ci->instance = instance;
34
- local_ci->connector = connector;
35
- strncpyz(local_ci->instance_name, instance->name, CONFIG_MAX_NAME);
36
- strncpyz(local_ci->connector_name, connector->name, CONFIG_MAX_NAME);
37
- local_ci->next = global_connector_instance;
38
- global_connector_instance = local_ci;
39
-
40
- return global_connector_instance;
41
-}
42
-
43
-int is_valid_connector(char *type, int check_reserved)
44
-{
45
- int rc = 1;
46
-
47
- if (unlikely(!type))
48
- return 0;
49
-
50
- if (!check_reserved) {
51
- if (unlikely(is_valid_connector(type,1))) {
52
- return 0;
53
- }
54
- //if (unlikely(*type == ':')
55
- // return 0;
56
- char *separator = strrchr(type, ':');
57
- if (likely(separator)) {
58
- *separator = '\0';
59
- rc = separator - type;
60
- } else
61
- return 0;
62
- }
63
-// else {
64
-// if (unlikely(is_valid_connector(type,1))) {
65
-// netdata_log_error("Section %s invalid -- reserved name", type);
66
-// return 0;
67
-// }
68
-// }
69
-
70
- if (!strcmp(type, "graphite") || !strcmp(type, "graphite:plaintext")) {
71
- return rc;
72
- } else if (!strcmp(type, "graphite:http") || !strcmp(type, "graphite:https")) {
73
- return rc;
74
- } else if (!strcmp(type, "json") || !strcmp(type, "json:plaintext")) {
75
- return rc;
76
- } else if (!strcmp(type, "json:http") || !strcmp(type, "json:https")) {
77
- return rc;
78
- } else if (!strcmp(type, "opentsdb") || !strcmp(type, "opentsdb:telnet")) {
79
- return rc;
80
- } else if (!strcmp(type, "opentsdb:http") || !strcmp(type, "opentsdb:https")) {
81
- return rc;
82
- } else if (!strcmp(type, "prometheus_remote_write")) {
83
- return rc;
84
- } else if (!strcmp(type, "prometheus_remote_write:http") || !strcmp(type, "prometheus_remote_write:https")) {
85
- return rc;
86
- } else if (!strcmp(type, "kinesis") || !strcmp(type, "kinesis:plaintext")) {
87
- return rc;
88
- } else if (!strcmp(type, "pubsub") || !strcmp(type, "pubsub:plaintext")) {
89
- return rc;
90
- } else if (!strcmp(type, "mongodb") || !strcmp(type, "mongodb:plaintext")) {
91
- return rc;
92
- }
93
-
94
- return 0;
95
-}
96
-
97
-// ----------------------------------------------------------------------------
98
-// locking
99
-
100
-inline void appconfig_wrlock(struct config *root) {
101
- netdata_mutex_lock(&root->mutex);
102
-}
103
-
104
-inline void appconfig_unlock(struct config *root) {
105
- netdata_mutex_unlock(&root->mutex);
106
-}
107
-
108
-inline void config_section_wrlock(struct section *co) {
109
- netdata_mutex_lock(&co->mutex);
110
-}
111
-
112
-inline void config_section_unlock(struct section *co) {
113
- netdata_mutex_unlock(&co->mutex);
114
-}
115
-
116
-
117
-// ----------------------------------------------------------------------------
118
-// config name-value index
119
-
120
-static int appconfig_option_compare(void *a, void *b) {
121
- if(((struct config_option *)a)->hash < ((struct config_option *)b)->hash) return -1;
122
- else if(((struct config_option *)a)->hash > ((struct config_option *)b)->hash) return 1;
123
- else return strcmp(((struct config_option *)a)->name, ((struct config_option *)b)->name);
124
-}
125
-
126
-#define appconfig_option_index_add(co, cv) (struct config_option *)avl_insert_lock(&((co)->values_index), (avl_t *)(cv))
127
-#define appconfig_option_index_del(co, cv) (struct config_option *)avl_remove_lock(&((co)->values_index), (avl_t *)(cv))
128
-
129
-static struct config_option *appconfig_option_index_find(struct section *co, const char *name, uint32_t hash) {
130
- struct config_option tmp;
131
- tmp.hash = (hash)?hash:simple_hash(name);
132
- tmp.name = (char *)name;
133
-
134
- return (struct config_option *)avl_search_lock(&(co->values_index), (avl_t *) &tmp);
135
-}
136
-
137
-
138
-// ----------------------------------------------------------------------------
139
-// config sections index
140
-
141
-int appconfig_section_compare(void *a, void *b) {
142
- if(((struct section *)a)->hash < ((struct section *)b)->hash) return -1;
143
- else if(((struct section *)a)->hash > ((struct section *)b)->hash) return 1;
144
- else return strcmp(((struct section *)a)->name, ((struct section *)b)->name);
145
-}
146
-
147
-#define appconfig_index_add(root, cfg) (struct section *)avl_insert_lock(&(root)->index, (avl_t *)(cfg))
148
-#define appconfig_index_del(root, cfg) (struct section *)avl_remove_lock(&(root)->index, (avl_t *)(cfg))
149
-
150
-static struct section *appconfig_index_find(struct config *root, const char *name, uint32_t hash) {
151
- struct section tmp;
152
- tmp.hash = (hash)?hash:simple_hash(name);
153
- tmp.name = (char *)name;
154
-
155
- return (struct section *)avl_search_lock(&root->index, (avl_t *) &tmp);
156
-}
157
-
158
-
159
-// ----------------------------------------------------------------------------
160
-// config section methods
161
-
162
-static inline struct section *appconfig_section_find(struct config *root, const char *section) {
163
- return appconfig_index_find(root, section, 0);
164
-}
165
-
166
-static inline struct section *appconfig_section_create(struct config *root, const char *section) {
167
- netdata_log_debug(D_CONFIG, "Creating section '%s'.", section);
168
-
169
- struct section *co = callocz(1, sizeof(struct section));
170
- co->name = strdupz(section);
171
- co->hash = simple_hash(co->name);
172
- netdata_mutex_init(&co->mutex);
173
-
174
- avl_init_lock(&co->values_index, appconfig_option_compare);
175
-
176
- if(unlikely(appconfig_index_add(root, co) != co))
177
- netdata_log_error("INTERNAL ERROR: indexing of section '%s', already exists.", co->name);
5
+int appconfig_exists(struct config *root, const char *section, const char *name) {
6
+ struct config_section *sect = appconfig_section_find(root, section);
7
+ if(!sect) return 0;
8
179
- appconfig_wrlock(root);
180
- struct section *co2 = root->last_section;
181
- if(co2) {
182
- co2->next = co;
183
- } else {
184
- root->first_section = co;
185
- }
186
- root->last_section = co;
187
- appconfig_unlock(root);
9
+ struct config_option *opt = appconfig_option_find(sect, name);
10
+ if(!opt) return 0;
11
189
- return co;
12
+ return 1;
13
}
14
192
-void appconfig_section_destroy_non_loaded(struct config *root, const char *section)
193
-{
194
- struct section *co;
195
- struct config_option *cv, *cv_next;
196
-
197
- netdata_log_debug(D_CONFIG, "Destroying section '%s'.", section);
198
-
199
- co = appconfig_section_find(root, section);
200
- if(!co) {
201
- netdata_log_error("Could not destroy section '%s'. Not found.", section);
202
- return;
203
- }
204
-
205
- config_section_wrlock(co);
206
- for(cv = co->values; cv ; cv = cv->next) {
207
- if (cv->flags & CONFIG_VALUE_LOADED) {
208
- /* Do not destroy values that were loaded from the configuration files. */
209
- config_section_unlock(co);
210
- return;
211
- }
212
- }
213
- for(cv = co->values ; cv ; cv = cv_next) {
214
- cv_next = cv->next;
215
- if(unlikely(!appconfig_option_index_del(co, cv)))
216
- netdata_log_error("Cannot remove config option '%s' from section '%s'.", cv->name, co->name);
217
- freez(cv->value);
218
- freez(cv->name);
219
- freez(cv);
220
- }
221
- co->values = NULL;
222
- config_section_unlock(co);
223
-
224
- if (unlikely(!appconfig_index_del(root, co))) {
225
- netdata_log_error("Cannot remove section '%s' from config.", section);
15
+void appconfig_set_default_raw_value(struct config *root, const char *section, const char *name, const char *value) {
16
+ struct config_section *sect = appconfig_section_find(root, section);
17
+ if(!sect) {
18
+ appconfig_set_raw_value(root, section, name, value, CONFIG_VALUE_TYPE_UNKNOWN);
19
return;
20
}
228
-
229
- appconfig_wrlock(root);
230
-
231
- if (root->first_section == co) {
232
- root->first_section = co->next;
233
-
234
- if (root->last_section == co)
235
- root->last_section = root->first_section;
236
- } else {
237
- struct section *co_cur = root->first_section, *co_prev = NULL;
238
-
239
- while(co_cur && co_cur != co) {
240
- co_prev = co_cur;
241
- co_cur = co_cur->next;
242
- }
243
-
244
- if (co_cur) {
245
- co_prev->next = co_cur->next;
246
-
247
- if (root->last_section == co_cur)
248
- root->last_section = co_prev;
249
- }
250
- }
251
-
252
- appconfig_unlock(root);
253
-
254
- avl_destroy_lock(&co->values_index);
255
- freez(co->name);
256
- pthread_mutex_destroy(&co->mutex);
257
- freez(co);
258
-}
259
-
260
-void appconfig_section_option_destroy_non_loaded(struct config *root, const char *section, const char *name)
261
-{
262
- netdata_log_debug(D_CONFIG, "Destroying section option '%s -> %s'.", section, name);
21
264
- struct section *co;
265
- co = appconfig_section_find(root, section);
266
- if (!co) {
267
- netdata_log_error("Could not destroy section option '%s -> %s'. The section not found.", section, name);
22
+ struct config_option *opt = appconfig_option_find(sect, name);
23
+ if(!opt) {
24
+ appconfig_set_raw_value(root, section, name, value, CONFIG_VALUE_TYPE_UNKNOWN);
25
return;
26
}
27
271
- config_section_wrlock(co);
28
+ opt->flags |= CONFIG_VALUE_USED;
29
273
- struct config_option *cv;
274
-
275
- cv = appconfig_option_index_find(co, name, simple_hash(name));
276
-
277
- if (cv && cv->flags & CONFIG_VALUE_LOADED) {
278
- config_section_unlock(co);
30
+ if(opt->flags & CONFIG_VALUE_LOADED)
31
return;
280
- }
32
282
- if (unlikely(!(cv && appconfig_option_index_del(co, cv)))) {
283
- config_section_unlock(co);
284
- netdata_log_error("Could not destroy section option '%s -> %s'. The option not found.", section, name);
285
- return;
286
- }
33
+ if(string_strcmp(opt->value, value) != 0) {
34
+ opt->flags |= CONFIG_VALUE_CHANGED;
35
288
- if (co->values == cv) {
289
- co->values = co->values->next;
290
- } else {
291
- struct config_option *cv_cur = co->values, *cv_prev = NULL;
292
- while (cv_cur && cv_cur != cv) {
293
- cv_prev = cv_cur;
294
- cv_cur = cv_cur->next;
295
- }
296
- if (cv_cur) {
297
- cv_prev->next = cv_cur->next;
298
- }
36
+ string_freez(opt->value);
37
+ opt->value = string_strdupz(value);
38
}
300
-
301
- freez(cv->value);
302
- freez(cv->name);
303
- freez(cv);
304
-
305
- config_section_unlock(co);
306
- return;
307
-}
308
-
309
-// ----------------------------------------------------------------------------
310
-// config name-value methods
311
-
312
-static inline struct config_option *appconfig_value_create(struct section *co, const char *name, const char *value) {
313
- netdata_log_debug(D_CONFIG, "Creating config entry for name '%s', value '%s', in section '%s'.", name, value, co->name);
314
-
315
- struct config_option *cv = callocz(1, sizeof(struct config_option));
316
- cv->name = strdupz(name);
317
- cv->hash = simple_hash(cv->name);
318
- cv->value = strdupz(value);
319
-
320
- struct config_option *found = appconfig_option_index_add(co, cv);
321
- if(found != cv) {
322
- netdata_log_error("indexing of config '%s' in section '%s': already exists - using the existing one.", cv->name, co->name);
323
- freez(cv->value);
324
- freez(cv->name);
325
- freez(cv);
326
- return found;
327
- }
328
-
329
- config_section_wrlock(co);
330
- struct config_option *cv2 = co->values;
331
- if(cv2) {
332
- while (cv2->next) cv2 = cv2->next;
333
- cv2->next = cv;
334
- }
335
- else co->values = cv;
336
- config_section_unlock(co);
337
-
338
- return cv;
39
}
40
341
-int appconfig_exists(struct config *root, const char *section, const char *name) {
342
- struct config_option *cv;
343
-
344
- netdata_log_debug(D_CONFIG, "request to get config in section '%s', name '%s'", section, name);
345
-
346
- struct section *co = appconfig_section_find(root, section);
347
- if(!co) return 0;
348
-
349
- cv = appconfig_option_index_find(co, name, 0);
350
- if(!cv) return 0;
351
-
352
- return 1;
353
-}
354
-
355
-int appconfig_move(struct config *root, const char *section_old, const char *name_old, const char *section_new, const char *name_new) {
356
- struct config_option *cv_old, *cv_new;
357
- int ret = -1;
358
-
359
- netdata_log_debug(D_CONFIG, "request to rename config in section '%s', old name '%s', to section '%s', new name '%s'", section_old, name_old, section_new, name_new);
360
-
361
- struct section *co_old = appconfig_section_find(root, section_old);
362
- if(!co_old) return ret;
363
-
364
- struct section *co_new = appconfig_section_find(root, section_new);
365
- if(!co_new) co_new = appconfig_section_create(root, section_new);
366
-
367
- config_section_wrlock(co_old);
368
- if(co_old != co_new)
369
- config_section_wrlock(co_new);
370
-
371
- cv_old = appconfig_option_index_find(co_old, name_old, 0);
372
- if(!cv_old) goto cleanup;
373
-
374
- cv_new = appconfig_option_index_find(co_new, name_new, 0);
375
- if(cv_new) goto cleanup;
376
-
377
- if(unlikely(appconfig_option_index_del(co_old, cv_old) != cv_old))
378
- netdata_log_error("INTERNAL ERROR: deletion of config '%s' from section '%s', deleted the wrong config entry.", cv_old->name, co_old->name);
379
-
380
- if(co_old->values == cv_old) {
381
- co_old->values = cv_old->next;
382
- }
383
- else {
384
- struct config_option *t;
385
- for(t = co_old->values; t && t->next != cv_old ;t = t->next) ;
386
- if(!t || t->next != cv_old)
387
- netdata_log_error("INTERNAL ERROR: cannot find variable '%s' in section '%s' of the config - but it should be there.", cv_old->name, co_old->name);
388
- else
389
- t->next = cv_old->next;
390
- }
391
-
392
- freez(cv_old->name);
393
- cv_old->name = strdupz(name_new);
394
- cv_old->hash = simple_hash(cv_old->name);
395
-
396
- cv_new = cv_old;
397
- cv_new->next = co_new->values;
398
- co_new->values = cv_new;
399
-
400
- if(unlikely(appconfig_option_index_add(co_new, cv_old) != cv_old))
401
- netdata_log_error("INTERNAL ERROR: re-indexing of config '%s' in section '%s', already exists.", cv_old->name, co_new->name);
402
-
403
- ret = 0;
404
-
405
-cleanup:
406
- if(co_old != co_new)
407
- config_section_unlock(co_new);
408
- config_section_unlock(co_old);
409
- return ret;
410
-}
411
-
412
-char *appconfig_get_by_section(struct section *co, const char *name, const char *default_value)
413
-{
414
- struct config_option *cv;
415
-
416
- // Only calls internal to this file check for a NULL result and they do not supply a NULL arg.
417
- // External caller should treat NULL as an error case.
418
- cv = appconfig_option_index_find(co, name, 0);
419
- if (!cv) {
420
- if (!default_value) return NULL;
421
- cv = appconfig_value_create(co, name, default_value);
422
- if (!cv) return NULL;
423
- }
424
- cv->flags |= CONFIG_VALUE_USED;
425
-
426
- if((cv->flags & CONFIG_VALUE_LOADED) || (cv->flags & CONFIG_VALUE_CHANGED)) {
427
- // this is a loaded value from the config file
428
- // if it is different than the default, mark it
429
- if(!(cv->flags & CONFIG_VALUE_CHECKED)) {
430
- if(default_value && strcmp(cv->value, default_value) != 0) cv->flags |= CONFIG_VALUE_CHANGED;
431
- cv->flags |= CONFIG_VALUE_CHECKED;
432
- }
433
- }
434
-
435
- return(cv->value);
436
-}
437
-
438
-
439
-char *appconfig_get(struct config *root, const char *section, const char *name, const char *default_value)
440
-{
441
- if (default_value == NULL)
442
- netdata_log_debug(D_CONFIG, "request to get config in section '%s', name '%s' or fail", section, name);
443
- else
444
- netdata_log_debug(D_CONFIG, "request to get config in section '%s', name '%s', default_value '%s'", section, name, default_value);
445
-
446
- struct section *co = appconfig_section_find(root, section);
447
- if (!co && !default_value)
448
- return NULL;
449
- if(!co) co = appconfig_section_create(root, section);
450
-
451
- return appconfig_get_by_section(co, name, default_value);
452
-}
41
+bool stream_conf_needs_dbengine(struct config *root) {
42
+ struct config_section *sect;
43
+ bool ret = false;
44
454
-long long appconfig_get_number(struct config *root, const char *section, const char *name, long long value)
455
-{
456
- char buffer[100], *s;
457
- sprintf(buffer, "%lld", value);
45
+ APPCONFIG_LOCK(root);
46
+ for(sect = root->sections; sect; sect = sect->next) {
47
+ if(string_strcmp(sect->name, "stream") == 0)
48
+ continue; // the first section is not relevant
49
459
- s = appconfig_get(root, section, name, buffer);
460
- if(!s) return value;
461
-
462
- return strtoll(s, NULL, 0);
463
-}
464
-
465
-NETDATA_DOUBLE appconfig_get_float(struct config *root, const char *section, const char *name, NETDATA_DOUBLE value)
466
-{
467
- char buffer[100], *s;
468
- sprintf(buffer, "%0.5" NETDATA_DOUBLE_MODIFIER, value);
469
-
470
- s = appconfig_get(root, section, name, buffer);
471
- if(!s) return value;
472
-
473
- return str2ndd(s, NULL);
474
-}
475
-
476
-inline int appconfig_test_boolean_value(char *s) {
477
- if(!strcasecmp(s, "yes") || !strcasecmp(s, "true") || !strcasecmp(s, "on")
478
- || !strcasecmp(s, "auto") || !strcasecmp(s, "on demand"))
479
- return 1;
480
-
481
- return 0;
482
-}
483
-
484
-int appconfig_get_boolean_by_section(struct section *co, const char *name, int value) {
485
- char *s;
486
-
487
- s = appconfig_get_by_section(co, name, (!value)?"no":"yes");
488
- if(!s) return value;
489
-
490
- return appconfig_test_boolean_value(s);
491
-}
492
-
493
-int appconfig_get_boolean(struct config *root, const char *section, const char *name, int value)
494
-{
495
- char *s;
496
- if(value) s = "yes";
497
- else s = "no";
498
-
499
- s = appconfig_get(root, section, name, s);
500
- if(!s) return value;
501
-
502
- return appconfig_test_boolean_value(s);
503
-}
504
-
505
-int appconfig_get_boolean_ondemand(struct config *root, const char *section, const char *name, int value)
506
-{
507
- char *s;
508
-
509
- if(value == CONFIG_BOOLEAN_AUTO)
510
- s = "auto";
511
-
512
- else if(value == CONFIG_BOOLEAN_NO)
513
- s = "no";
514
-
515
- else
516
- s = "yes";
517
-
518
- s = appconfig_get(root, section, name, s);
519
- if(!s) return value;
520
-
521
- if(!strcmp(s, "yes") || !strcmp(s, "true") || !strcmp(s, "on"))
522
- return CONFIG_BOOLEAN_YES;
523
- else if(!strcmp(s, "no") || !strcmp(s, "false") || !strcmp(s, "off"))
524
- return CONFIG_BOOLEAN_NO;
525
- else if(!strcmp(s, "auto") || !strcmp(s, "on demand"))
526
- return CONFIG_BOOLEAN_AUTO;
527
-
528
- return value;
529
-}
530
-
531
-const char *appconfig_set_default(struct config *root, const char *section, const char *name, const char *value)
532
-{
533
- struct config_option *cv;
534
-
535
- netdata_log_debug(D_CONFIG, "request to set default config in section '%s', name '%s', value '%s'", section, name, value);
536
-
537
- struct section *co = appconfig_section_find(root, section);
538
- if(!co) return appconfig_set(root, section, name, value);
539
-
540
- cv = appconfig_option_index_find(co, name, 0);
541
- if(!cv) return appconfig_set(root, section, name, value);
542
-
543
- cv->flags |= CONFIG_VALUE_USED;
544
-
545
- if(cv->flags & CONFIG_VALUE_LOADED)
546
- return cv->value;
547
-
548
- if(strcmp(cv->value, value) != 0) {
549
- cv->flags |= CONFIG_VALUE_CHANGED;
550
-
551
- freez(cv->value);
552
- cv->value = strdupz(value);
553
- }
554
-
555
- return cv->value;
556
-}
557
-
558
-const char *appconfig_set(struct config *root, const char *section, const char *name, const char *value)
559
-{
560
- struct config_option *cv;
561
-
562
- netdata_log_debug(D_CONFIG, "request to set config in section '%s', name '%s', value '%s'", section, name, value);
563
-
564
- struct section *co = appconfig_section_find(root, section);
565
- if(!co) co = appconfig_section_create(root, section);
566
-
567
- cv = appconfig_option_index_find(co, name, 0);
568
- if(!cv) cv = appconfig_value_create(co, name, value);
569
- cv->flags |= CONFIG_VALUE_USED;
570
-
571
- if(strcmp(cv->value, value) != 0) {
572
- cv->flags |= CONFIG_VALUE_CHANGED;
573
-
574
- freez(cv->value);
575
- cv->value = strdupz(value);
576
- }
577
-
578
- return value;
579
-}
580
-
581
-long long appconfig_set_number(struct config *root, const char *section, const char *name, long long value)
582
-{
583
- char buffer[100];
584
- sprintf(buffer, "%lld", value);
585
-
586
- appconfig_set(root, section, name, buffer);
587
-
588
- return value;
589
-}
590
-
591
-NETDATA_DOUBLE appconfig_set_float(struct config *root, const char *section, const char *name, NETDATA_DOUBLE value)
592
-{
593
- char buffer[100];
594
- sprintf(buffer, "%0.5" NETDATA_DOUBLE_MODIFIER, value);
595
-
596
- appconfig_set(root, section, name, buffer);
597
-
598
- return value;
599
-}
600
-
601
-int appconfig_set_boolean(struct config *root, const char *section, const char *name, int value)
602
-{
603
- char *s;
604
- if(value) s = "yes";
605
- else s = "no";
606
-
607
- appconfig_set(root, section, name, s);
608
-
609
- return value;
610
-}
611
-
612
-int appconfig_get_duration(struct config *root, const char *section, const char *name, const char *value)
613
-{
614
- int result = 0;
615
- const char *s;
616
-
617
- s = appconfig_get(root, section, name, value);
618
- if(!s) goto fallback;
619
-
620
- if(!config_parse_duration(s, &result)) {
621
- netdata_log_error("config option '[%s].%s = %s' is configured with an valid duration", section, name, s);
622
- goto fallback;
623
- }
624
-
625
- return result;
626
-
627
- fallback:
628
- if(!config_parse_duration(value, &result))
629
- netdata_log_error("INTERNAL ERROR: default duration supplied for option '[%s].%s = %s' is not a valid duration", section, name, value);
630
-
631
- return result;
632
-}
633
-
634
-// ----------------------------------------------------------------------------
635
-// config load/save
636
-
637
-int appconfig_load(struct config *root, char *filename, int overwrite_used, const char *section_name)
638
-{
639
- int line = 0;
640
- struct section *co = NULL;
641
- int is_exporter_config = 0;
642
- int _connectors = 0; // number of exporting connector sections we have
643
- char working_instance[CONFIG_MAX_NAME + 1];
644
- char working_connector[CONFIG_MAX_NAME + 1];
645
- struct section *working_connector_section = NULL;
646
- int global_exporting_section = 0;
647
-
648
- char buffer[CONFIG_FILE_LINE_MAX + 1], *s;
649
-
650
- if(!filename) filename = CONFIG_DIR "/" CONFIG_FILENAME;
651
-
652
- netdata_log_debug(D_CONFIG, "CONFIG: opening config file '%s'", filename);
653
-
654
- FILE *fp = fopen(filename, "r");
655
- if(!fp) {
656
- if(errno != ENOENT)
657
- netdata_log_info("CONFIG: cannot open file '%s'. Using internal defaults.", filename);
658
-
659
- return 0;
660
- }
661
-
662
- uint32_t section_hash = 0;
663
- if(section_name) {
664
- section_hash = simple_hash(section_name);
665
- }
666
- is_exporter_config = (strstr(filename, EXPORTING_CONF) != NULL);
667
-
668
- while(fgets(buffer, CONFIG_FILE_LINE_MAX, fp) != NULL) {
669
- buffer[CONFIG_FILE_LINE_MAX] = '\0';
670
- line++;
671
-
672
- s = trim(buffer);
673
- if(!s || *s == '#') {
674
- netdata_log_debug(D_CONFIG, "CONFIG: ignoring line %d of file '%s', it is empty.", line, filename);
675
- continue;
676
- }
677
-
678
- int len = (int) strlen(s);
679
- if(*s == '[' && s[len - 1] == ']') {
680
- // new section
681
- s[len - 1] = '\0';
682
- s++;
683
-
684
- if (is_exporter_config) {
685
- global_exporting_section =
686
- !(strcmp(s, CONFIG_SECTION_EXPORTING)) || !(strcmp(s, CONFIG_SECTION_PROMETHEUS));
687
- if (unlikely(!global_exporting_section)) {
688
- int rc;
689
- rc = is_valid_connector(s, 0);
690
- if (likely(rc)) {
691
- strncpyz(working_connector, s, CONFIG_MAX_NAME);
692
- s = s + rc + 1;
693
- if (unlikely(!(*s))) {
694
- _connectors++;
695
- sprintf(buffer, "instance_%d", _connectors);
696
- s = buffer;
697
- }
698
- strncpyz(working_instance, s, CONFIG_MAX_NAME);
699
- working_connector_section = NULL;
700
- if (unlikely(appconfig_section_find(root, working_instance))) {
701
- netdata_log_error("Instance (%s) already exists", working_instance);
702
- co = NULL;
703
- continue;
704
- }
705
- } else {
706
- co = NULL;
707
- netdata_log_error("Section (%s) does not specify a valid connector", s);
708
- continue;
709
- }
710
- }
711
- }
712
-
713
- co = appconfig_section_find(root, s);
714
- if(!co) co = appconfig_section_create(root, s);
715
-
716
- if(co && section_name && overwrite_used && section_hash == co->hash && !strcmp(section_name, co->name)) {
717
- config_section_wrlock(co);
718
- struct config_option *cv2 = co->values;
719
- while (cv2) {
720
- struct config_option *save = cv2->next;
721
- struct config_option *found = appconfig_option_index_del(co, cv2);
722
- if(found != cv2)
723
- netdata_log_error("INTERNAL ERROR: Cannot remove '%s' from section '%s', it was not inserted before.",
724
- cv2->name, co->name);
725
-
726
- freez(cv2->name);
727
- freez(cv2->value);
728
- freez(cv2);
729
- cv2 = save;
730
- }
731
- co->values = NULL;
732
- config_section_unlock(co);
733
- }
734
-
735
- continue;
736
- }
737
-
738
- if(!co) {
739
- // line outside a section
740
- netdata_log_error("CONFIG: ignoring line %d ('%s') of file '%s', it is outside all sections.", line, s, filename);
741
- continue;
742
- }
743
-
744
- if(section_name && overwrite_used && section_hash != co->hash && strcmp(section_name, co->name)) {
745
- continue;
746
- }
747
-
748
- char *name = s;
749
- char *value = strchr(s, '=');
750
- if(!value) {
751
- netdata_log_error("CONFIG: ignoring line %d ('%s') of file '%s', there is no = in it.", line, s, filename);
752
- continue;
753
- }
754
- *value = '\0';
755
- value++;
756
-
757
- name = trim(name);
758
- value = trim(value);
759
-
760
- if(!name || *name == '#') {
761
- netdata_log_error("CONFIG: ignoring line %d of file '%s', name is empty.", line, filename);
50
+ struct config_option *opt = appconfig_get_raw_value_of_option_in_section(sect, "enabled", NULL, CONFIG_VALUE_TYPE_UNKNOWN, NULL);
51
+ if(!opt || !appconfig_test_boolean_value(string2str(opt->value)))
52
continue;
763
- }
764
-
765
- if(!value) value = "";
766
-
767
- struct config_option *cv = appconfig_option_index_find(co, name, 0);
768
-
769
- if (!cv) {
770
- cv = appconfig_value_create(co, name, value);
771
- if (likely(is_exporter_config) && unlikely(!global_exporting_section)) {
772
- if (unlikely(!working_connector_section)) {
773
- working_connector_section = appconfig_section_find(root, working_connector);
774
- if (!working_connector_section)
775
- working_connector_section = appconfig_section_create(root, working_connector);
776
- if (likely(working_connector_section)) {
777
- add_connector_instance(working_connector_section, co);
778
- }
779
- }
780
- }
781
- } else {
782
- if (((cv->flags & CONFIG_VALUE_USED) && overwrite_used) || !(cv->flags & CONFIG_VALUE_USED)) {
783
- netdata_log_debug(
784
- D_CONFIG, "CONFIG: line %d of file '%s', overwriting '%s/%s'.", line, filename, co->name, cv->name);
785
- freez(cv->value);
786
- cv->value = strdupz(value);
787
- } else
788
- netdata_log_debug(
789
- D_CONFIG,
790
- "CONFIG: ignoring line %d of file '%s', '%s/%s' is already present and used.",
791
- line,
792
- filename,
793
- co->name,
794
- cv->name);
795
- }
796
- cv->flags |= CONFIG_VALUE_LOADED;
797
- }
798
-
799
- fclose(fp);
53
801
- return 1;
802
-}
803
-
804
-void appconfig_generate(struct config *root, BUFFER *wb, int only_changed, bool netdata_conf)
805
-{
806
- int i, pri;
807
- struct section *co;
808
- struct config_option *cv;
809
-
810
- {
811
- int found_host_labels = 0;
812
- for (co = root->first_section; co; co = co->next)
813
- if(!strcmp(co->name, CONFIG_SECTION_HOST_LABEL))
814
- found_host_labels = 1;
815
-
816
- if(netdata_conf && !found_host_labels) {
817
- appconfig_section_create(root, CONFIG_SECTION_HOST_LABEL);
818
- appconfig_get(root, CONFIG_SECTION_HOST_LABEL, "name", "value");
54
+ opt = appconfig_get_raw_value_of_option_in_section(sect, "db", NULL, CONFIG_VALUE_TYPE_UNKNOWN, NULL);
55
+ if(opt && string_strcmp(opt->value, "dbengine") == 0) {
56
+ ret = true;
57
+ break;
58
}
59
}
60
+ APPCONFIG_UNLOCK(root);
61
822
- if(netdata_conf) {
823
- buffer_strcat(wb,
824
- "# netdata configuration\n"
825
- "#\n"
826
- "# You can download the latest version of this file, using:\n"
827
- "#\n"
828
- "# wget -O /etc/netdata/netdata.conf http://localhost:19999/netdata.conf\n"
829
- "# or\n"
830
- "# curl -o /etc/netdata/netdata.conf http://localhost:19999/netdata.conf\n"
831
- "#\n"
832
- "# You can uncomment and change any of the options below.\n"
833
- "# The value shown in the commented settings, is the default value.\n"
834
- "#\n"
835
- "\n# global netdata configuration\n");
836
- }
837
-
838
- for(i = 0; i <= 17 ;i++) {
839
- appconfig_wrlock(root);
840
- for(co = root->first_section; co ; co = co->next) {
841
- if(!strcmp(co->name, CONFIG_SECTION_GLOBAL)) pri = 0;
842
- else if(!strcmp(co->name, CONFIG_SECTION_DB)) pri = 1;
843
- else if(!strcmp(co->name, CONFIG_SECTION_DIRECTORIES)) pri = 2;
844
- else if(!strcmp(co->name, CONFIG_SECTION_LOGS)) pri = 3;
845
- else if(!strcmp(co->name, CONFIG_SECTION_ENV_VARS)) pri = 4;
846
- else if(!strcmp(co->name, CONFIG_SECTION_HOST_LABEL)) pri = 5;
847
- else if(!strcmp(co->name, CONFIG_SECTION_SQLITE)) pri = 6;
848
- else if(!strcmp(co->name, CONFIG_SECTION_CLOUD)) pri = 7;
849
- else if(!strcmp(co->name, CONFIG_SECTION_ML)) pri = 8;
850
- else if(!strcmp(co->name, CONFIG_SECTION_HEALTH)) pri = 9;
851
- else if(!strcmp(co->name, CONFIG_SECTION_WEB)) pri = 10;
852
- else if(!strcmp(co->name, CONFIG_SECTION_WEBRTC)) pri = 11;
853
- // by default, new sections will get pri = 12 (set at the end, below)
854
- else if(!strcmp(co->name, CONFIG_SECTION_REGISTRY)) pri = 13;
855
- else if(!strcmp(co->name, CONFIG_SECTION_GLOBAL_STATISTICS)) pri = 14;
856
- else if(!strcmp(co->name, CONFIG_SECTION_PLUGINS)) pri = 15;
857
- else if(!strcmp(co->name, CONFIG_SECTION_STATSD)) pri = 16;
858
- else if(!strncmp(co->name, "plugin:", 7)) pri = 17; // << change the loop too if you change this
859
- else pri = 12; // this is used for any new (currently unknown) sections
860
-
861
- if(i == pri) {
862
- int loaded = 0;
863
- int used = 0;
864
- int changed = 0;
865
- int count = 0;
866
-
867
- config_section_wrlock(co);
868
- for(cv = co->values; cv ; cv = cv->next) {
869
- used += (cv->flags & CONFIG_VALUE_USED)?1:0;
870
- loaded += (cv->flags & CONFIG_VALUE_LOADED)?1:0;
871
- changed += (cv->flags & CONFIG_VALUE_CHANGED)?1:0;
872
- count++;
873
- }
874
- config_section_unlock(co);
875
-
876
- if(!count) continue;
877
- if(only_changed && !changed && !loaded) continue;
878
-
879
- if(!used) {
880
- buffer_sprintf(wb, "\n# section '%s' is not used.", co->name);
881
- }
882
-
883
- buffer_sprintf(wb, "\n[%s]\n", co->name);
884
-
885
- config_section_wrlock(co);
886
- for(cv = co->values; cv ; cv = cv->next) {
887
-
888
- if(used && !(cv->flags & CONFIG_VALUE_USED)) {
889
- buffer_sprintf(wb, "\n\t# option '%s' is not used.\n", cv->name);
890
- }
891
- buffer_sprintf(wb, "\t%s%s = %s\n",
892
- (
893
- !(cv->flags & CONFIG_VALUE_LOADED) &&
894
- !(cv->flags & CONFIG_VALUE_CHANGED) &&
895
- (cv->flags & CONFIG_VALUE_USED)
896
- )?"# ":"", cv->name, cv->value);
897
- }
898
- config_section_unlock(co);
899
- }
900
- }
901
- appconfig_unlock(root);
902
- }
62
+ return ret;
63
}
64
905
-/**
906
- * Parse Duration
907
- *
908
- * Parse the string setting the result
909
- *
910
- * @param string the timestamp string
911
- * @param result the output variable
912
- *
913
- * @return It returns 1 on success and 0 otherwise
914
- */
915
-int config_parse_duration(const char* string, int* result) {
916
- while(*string && isspace((uint8_t)*string)) string++;
917
-
918
- if(unlikely(!*string)) goto fallback;
919
-
920
- if(*string == 'n' && !strcmp(string, "never")) {
921
- // this is a valid option
922
- *result = 0;
923
- return 1;
924
- }
65
+bool stream_conf_has_uuid_section(struct config *root) {
66
+ struct config_section *sect = NULL;
67
+ bool is_parent = false;
68
926
- // make sure it is a number
927
- if(!(isdigit((uint8_t)*string) || *string == '+' || *string == '-')) goto fallback;
69
+ APPCONFIG_LOCK(root);
70
+ for (sect = root->sections; sect; sect = sect->next) {
71
+ nd_uuid_t uuid;
72
929
- char *e = NULL;
930
- NETDATA_DOUBLE n = str2ndd(string, &e);
931
- if(e && *e) {
932
- switch (*e) {
933
- case 'Y':
934
- *result = (int) (n * 31536000);
935
- break;
936
- case 'M':
937
- *result = (int) (n * 2592000);
938
- break;
939
- case 'w':
940
- *result = (int) (n * 604800);
941
- break;
942
- case 'd':
943
- *result = (int) (n * 86400);
944
- break;
945
- case 'h':
946
- *result = (int) (n * 3600);
947
- break;
948
- case 'm':
949
- *result = (int) (n * 60);
950
- break;
951
- case 's':
952
- default:
953
- *result = (int) (n);
954
- break;
73
+ if (uuid_parse(string2str(sect->name), uuid) != -1 &&
74
+ appconfig_get_boolean_by_section(sect, "enabled", 0)) {
75
+ is_parent = true;
76
+ break;
77
}
78
}
957
- else
958
- *result = (int)(n);
959
-
960
- return 1;
961
-
962
- fallback:
963
- *result = 0;
964
- return 0;
965
-}
79
+ APPCONFIG_UNLOCK(root);
80
967
-struct section *appconfig_get_section(struct config *root, const char *name)
968
-{
969
- return appconfig_section_find(root, name);
81
+ return is_parent;
82
}