Old White Learning Programming-Netdata Learning

Web data - daemon

Learn related technologies in Netdata.

Version

netdata v1.14.0-71-ga5e952c

Option

Define opiton.

struct option_def option_definitions[] = {
 265 // opt description arg name default value 266 { 'c', "Configuration file to load.", "filename", CONFIG_DIR "/" CONFIG_FILENAME}, 267 { 'D', "Do not fork. Run in the foreground.", NULL, "run in the background"}, 268 { 'd', "Fork. Run in the background.", NULL, "run in the background"}, 269 { 'h', "Display this help message.", NULL, NULL}, 270 { 'P', "File to save a pid while running.", "filename", "do not save pid to a file"}, 271 { 'i', "The IP address to listen to.", "IP", "all IP addresses IPv4 and IPv6"}, 272 { 'p', "API/Web port to use.", "port", "19999"}, 273 { 's', "Prefix for /proc and /sys (for containers).", "path", "no prefix"}, 274 { 't', "The internal clock of netdata.", "seconds", "1"}, 275 { 'u', "Run as user.", "username", "netdata"}, 276 { 'v', "Print netdata version and exit.", NULL, NULL}, 277 { 'V', "Print netdata version and exit.", NULL, NULL}, 278 { 'W', "See Advanced options below.", "options", NULL}, 

The default startup script:

ExecStart=/usr/sbin/net data -P /var/run/netdata/netdata.pid -D -W set global 'process scheduling policy' 'keep' -W set global 'OOM score' 'keep'

There are a few more parameters, use -W to set. To be added

Configuration file analysis

section

The configuration contains the following sections

 85#define CONFIG_SECTION_GLOBAL     "global"
86#define CONFIG_SECTION_WEB "web" 87#define CONFIG_SECTION_STATSD "statsd" 88#define CONFIG_SECTION_PLUGINS "plugins" 89#define CONFIG_SECTION_CLOUD "cloud" 90#define CONFIG_SECTION_REGISTRY "registry" 91#define CONFIG_SECTION_HEALTH "health" 92#define CONFIG_SECTION_BACKEND "backend" 93#define CONFIG_SECTION_STREAM "stream" 94#define CONFIG_SECTION_EXPORTING "exporting:global" 95#define CONFIG_SECTION_PROMETHEUS "prometheus:exporter" 96#define CONFIG_SECTION_HOST_LABEL "host labels" 97#define EXPORTING_CONF "exporting.conf" 

There are several key-values ​​under each section.

structure

The configuration file is maintained by an avl tree.

143struct config {
144    struct section *first_section; 145 struct section *last_section; // optimize inserting at the end 146 netdata_mutex_t mutex; 147 avl_tree_lock index; 148}; 

mutex is a pthread_mutex_t mutex, mainly used as a write lock. The other lock is avl lock, which is mainly used for reading lock.

126struct section {
127    avl avl;                // the index entry of this section - this has to be first!
128
129 uint32_t hash; // a simple hash to speed up searching 130 // we first compare hashes, and only if the hashes are equal we do string comparisons 131 132 char *name; 133 134 struct section *next; // gloabl config_mutex protects just this 135 136 struct config_option *values; 137 avl_tree_lock values_index; 138 139 netdata_mutex_t mutex; // this locks only the writers, to ensure atomic updates 140 // readers are protected using the rwlock in avl_tree_lock 141}; 

Hash uses the FNV hash algorithm.

Compare

112static int appconfig_option_compare(void *a, void *b) {
113    if(((struct config_option *)a)->hash < ((struct config_option *)b)->hash) return -1; 114 else if(((struct config_option *)a)->hash > ((struct config_option *)b)->hash) return 1; 115 else return strcmp(((struct config_option *)a)->name, ((struct config_option *)b)->name); 116} 

Here first compare hash, then compare name.

[To be continued]

Guess you like

Origin www.cnblogs.com/1994july/p/12709320.html