appconfig.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. /*
  3. * This section manages ini config files, like netdata.conf and stream.conf
  4. *
  5. * It is organized like this:
  6. *
  7. * struct config (i.e. netdata.conf or stream.conf)
  8. * .sections = a linked list of struct section
  9. * .mutex = a mutex to protect the above linked list due to multi-threading
  10. * .index = an AVL tree of struct section
  11. *
  12. * struct section (i.e. [global] or [health] of netdata.conf)
  13. * .value = a linked list of struct config_option
  14. * .mutex = a mutex to protect the above linked list due to multi-threading
  15. * .value_index = an AVL tree of struct config_option
  16. *
  17. * struct config_option (ie. a name-value pair for each ini file option)
  18. *
  19. * The following operations on name-value options are supported:
  20. * SET to set the value of an option
  21. * SET DEFAULT to set the value and the default value of an option
  22. * GET to get the value of an option
  23. * EXISTS to check if an option exists
  24. * MOVE to move an option from a section to another section, and/or rename it
  25. *
  26. * GET and SET operations are provided for the following data types:
  27. * STRING
  28. * NUMBER (long long)
  29. * FLOAT (long double)
  30. * BOOLEAN (false, true)
  31. * BOOLEAN ONDEMAND (false, true, auto)
  32. *
  33. * GET and SET operations create struct config_option, if it is not already present.
  34. * This allows netdata to run even without netdata.conf and stream.conf. The internal
  35. * defaults are used to create the structure that should exist in the ini file and the config
  36. * file can be downloaded from the server.
  37. *
  38. * Also 2 operations are supported for the whole config file:
  39. *
  40. * LOAD To load the ini file from disk
  41. * GENERATE To generate the ini file (this is used to download the ini file from the server)
  42. *
  43. * For each option (name-value pair), the system maintains 4 flags:
  44. * LOADED to indicate that the value has been loaded from the file
  45. * USED to indicate that netdata used the value
  46. * CHANGED to indicate that the value has been changed from the loaded value or the internal default value
  47. * CHECKED is used internally for optimization (to avoid an strcmp() every time GET is called).
  48. *
  49. * TODO:
  50. * 1. The linked lists and the mutexes can be removed and the AVL trees can become DICTIONARY.
  51. * This part of the code was written before we add traversal to AVL.
  52. *
  53. * 2. High level data types could be supported, to simplify the rest of the code:
  54. * MULTIPLE CHOICE to let the user select one of the supported keywords
  55. * this would allow users see in comments the available options
  56. *
  57. * SIMPLE PATTERN to let the user define netdata SIMPLE PATTERNS
  58. *
  59. * 3. Sorting of options should be supported.
  60. * Today, when the ini file is downloaded from the server, the options are shown in the order
  61. * they appear in the linked list (the order they were added, listing changed options first).
  62. * If we remove the linked list, the order they appear in the AVL tree will be used (which is
  63. * random due to simple_hash()).
  64. * Ideally, we support sorting of options when generating the ini file.
  65. *
  66. * 4. There is no free() operation. So, memory is freed on netdata exit.
  67. *
  68. * 5. Avoid memory fragmentation
  69. * Since entries are created from multiple threads and a lot of allocations are required
  70. * for each config_option, fragmentation can be a problem for IoT.
  71. *
  72. * 6. Although this way of managing options is quite flexible and dynamic, it wastes memory
  73. * for the names of the options. Since most of the option names are static, we could provide
  74. * a method to allocate only the dynamic option names.
  75. */
  76. #ifndef NETDATA_CONFIG_H
  77. #define NETDATA_CONFIG_H 1
  78. #include "../libnetdata.h"
  79. #define CONFIG_FILENAME "netdata.conf"
  80. #define CONFIG_SECTION_GLOBAL "global"
  81. #define CONFIG_SECTION_WEB "web"
  82. #define CONFIG_SECTION_STATSD "statsd"
  83. #define CONFIG_SECTION_PLUGINS "plugins"
  84. #define CONFIG_SECTION_REGISTRY "registry"
  85. #define CONFIG_SECTION_HEALTH "health"
  86. #define CONFIG_SECTION_BACKEND "backend"
  87. #define CONFIG_SECTION_STREAM "stream"
  88. // these are used to limit the configuration names and values lengths
  89. // they are not enforced by config.c functions (they will strdup() all strings, no matter of their length)
  90. #define CONFIG_MAX_NAME 1024
  91. #define CONFIG_MAX_VALUE 2048
  92. struct config {
  93. struct section *sections;
  94. netdata_mutex_t mutex;
  95. avl_tree_lock index;
  96. };
  97. #define CONFIG_BOOLEAN_INVALID 100 // an invalid value to check for validity (used as default initialization when needed)
  98. #define CONFIG_BOOLEAN_NO 0 // disabled
  99. #define CONFIG_BOOLEAN_YES 1 // enabled
  100. #ifndef CONFIG_BOOLEAN_AUTO
  101. #define CONFIG_BOOLEAN_AUTO 2 // enabled if it has useful info when enabled
  102. #endif
  103. extern int appconfig_load(struct config *root, char *filename, int overwrite_used);
  104. extern char *appconfig_get(struct config *root, const char *section, const char *name, const char *default_value);
  105. extern long long appconfig_get_number(struct config *root, const char *section, const char *name, long long value);
  106. extern LONG_DOUBLE appconfig_get_float(struct config *root, const char *section, const char *name, LONG_DOUBLE value);
  107. extern int appconfig_get_boolean(struct config *root, const char *section, const char *name, int value);
  108. extern int appconfig_get_boolean_ondemand(struct config *root, const char *section, const char *name, int value);
  109. extern const char *appconfig_set(struct config *root, const char *section, const char *name, const char *value);
  110. extern const char *appconfig_set_default(struct config *root, const char *section, const char *name, const char *value);
  111. extern long long appconfig_set_number(struct config *root, const char *section, const char *name, long long value);
  112. extern LONG_DOUBLE appconfig_set_float(struct config *root, const char *section, const char *name, LONG_DOUBLE value);
  113. extern int appconfig_set_boolean(struct config *root, const char *section, const char *name, int value);
  114. extern int appconfig_exists(struct config *root, const char *section, const char *name);
  115. extern int appconfig_move(struct config *root, const char *section_old, const char *name_old, const char *section_new, const char *name_new);
  116. extern void appconfig_generate(struct config *root, BUFFER *wb, int only_changed);
  117. extern int appconfig_section_compare(void *a, void *b);
  118. #endif /* NETDATA_CONFIG_H */