master
c 213 lines 7.11 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 #include "nd_log-internals.h"
4
5 void nd_log_set_user_settings(ND_LOG_SOURCES source, const char *setting) {
6 char buf[FILENAME_MAX + 100];
7 if(setting && *setting)
8 strncpyz(buf, setting, sizeof(buf) - 1);
9 else
10 buf[0] = '\0';
11
12 struct nd_log_source *ls = &nd_log.sources[source];
13 char *output = strrchr(buf, '@');
14
15 if(!output)
16 // all of it is the output
17 output = buf;
18 else {
19 // we found an '@', the next char is the output
20 *output = '\0';
21 output++;
22
23 // parse the other params
24 char *remaining = buf;
25 while(remaining) {
26 char *value = strsep_skip_consecutive_separators(&remaining, ",");
27 if (!value || !*value) continue;
28
29 char *name = strsep_skip_consecutive_separators(&value, "=");
30 if (!name || !*name) continue;
31
32 if(strcmp(name, "logfmt") == 0)
33 ls->format = NDLF_LOGFMT;
34 else if(strcmp(name, "json") == 0)
35 ls->format = NDLF_JSON;
36 else if(strcmp(name, "journal") == 0)
37 ls->format = NDLF_JOURNAL;
38 #if defined(OS_WINDOWS)
39 #if defined(HAVE_ETW)
40 else if(strcmp(name, ETW_NAME) == 0)
41 ls->format = NDLF_ETW;
42 #endif
43 #if defined(HAVE_WEL)
44 else if(strcmp(name, WEL_NAME) == 0)
45 ls->format = NDLF_WEL;
46 #endif
47 #endif
48 else if(strcmp(name, "level") == 0 && value && *value)
49 ls->min_priority = nd_log_priority2id(value);
50 else if(strcmp(name, "protection") == 0 && value && *value) {
51 if(strcmp(value, "off") == 0 || strcmp(value, "none") == 0) {
52 ls->limits = ND_LOG_LIMITS_UNLIMITED;
53 ls->limits.counter = 0;
54 ls->limits.prevented = 0;
55 }
56 else {
57 ls->limits = ND_LOG_LIMITS_DEFAULT;
58
59 char *slash = strchr(value, '/');
60 if(slash) {
61 *slash = '\0';
62 slash++;
63 ls->limits.logs_per_period = ls->limits.logs_per_period_backup = str2u(value);
64
65 int period;
66 if(!duration_parse_seconds(slash, &period)) {
67 nd_log(NDLS_DAEMON, NDLP_ERR, "Error while parsing period '%s'", slash);
68 period = ND_LOG_DEFAULT_THROTTLE_PERIOD;
69 }
70
71 ls->limits.throttle_period = period;
72 }
73 else {
74 ls->limits.logs_per_period = ls->limits.logs_per_period_backup = str2u(value);
75 ls->limits.throttle_period = ND_LOG_DEFAULT_THROTTLE_PERIOD;
76 }
77 }
78 }
79 else
80 nd_log(NDLS_DAEMON, NDLP_ERR,
81 "Error while parsing configuration of log source '%s'. "
82 "In config '%s', '%s' is not understood.",
83 nd_log_id2source(source), setting, name);
84 }
85 }
86
87 if(!output || !*output || strcmp(output, "none") == 0 || strcmp(output, "off") == 0) {
88 ls->method = NDLM_DISABLED;
89 ls->filename = "/dev/null";
90 }
91 else if(strcmp(output, "journal") == 0) {
92 ls->method = NDLM_JOURNAL;
93 ls->filename = NULL;
94 }
95 #if defined(OS_WINDOWS)
96 #if defined(HAVE_ETW)
97 else if(strcmp(output, ETW_NAME) == 0) {
98 ls->method = NDLM_ETW;
99 ls->filename = NULL;
100 }
101 #endif
102 #if defined(HAVE_WEL)
103 else if(strcmp(output, WEL_NAME) == 0) {
104 ls->method = NDLM_WEL;
105 ls->filename = NULL;
106 }
107 #endif
108 #endif
109 else if(strcmp(output, "syslog") == 0) {
110 ls->method = NDLM_SYSLOG;
111 ls->filename = NULL;
112 }
113 else if(strcmp(output, "/dev/null") == 0) {
114 ls->method = NDLM_DEVNULL;
115 ls->filename = "/dev/null";
116 }
117 else if(strcmp(output, "system") == 0) {
118 if(ls->fd == STDERR_FILENO) {
119 ls->method = NDLM_STDERR;
120 ls->filename = NULL;
121 ls->fd = STDERR_FILENO;
122 }
123 else {
124 ls->method = NDLM_STDOUT;
125 ls->filename = NULL;
126 ls->fd = STDOUT_FILENO;
127 }
128 }
129 else if(strcmp(output, "stderr") == 0) {
130 ls->method = NDLM_STDERR;
131 ls->filename = NULL;
132 ls->fd = STDERR_FILENO;
133 }
134 else if(strcmp(output, "stdout") == 0) {
135 ls->method = NDLM_STDOUT;
136 ls->filename = NULL;
137 ls->fd = STDOUT_FILENO;
138 }
139 else {
140 ls->method = NDLM_FILE;
141 ls->filename = strdupz(output);
142 }
143
144 #if defined(NETDATA_INTERNAL_CHECKS) || defined(NETDATA_DEV_MODE)
145 ls->min_priority = NDLP_DEBUG;
146 #endif
147
148 if(source == NDLS_COLLECTORS) {
149 // set the method for the collector processes we will spawn
150
151 ND_LOG_METHOD method = NDLM_STDERR;
152 ND_LOG_FORMAT format = NDLF_LOGFMT;
153 ND_LOG_FIELD_PRIORITY priority = ls->min_priority;
154
155 if(IS_VALID_LOG_METHOD_FOR_EXTERNAL_PLUGINS(ls->method)) {
156 method = ls->method;
157 format = ls->format;
158 }
159
160 nd_setenv("NETDATA_LOG_METHOD", nd_log_id2method(method), 1);
161 nd_setenv("NETDATA_LOG_FORMAT", nd_log_id2format(format), 1);
162 nd_setenv("NETDATA_LOG_LEVEL", nd_log_id2priority(priority), 1);
163 }
164 }
165
166 void nd_log_set_priority_level(const char *setting) {
167 if(!setting || !*setting)
168 setting = "info";
169
170 ND_LOG_FIELD_PRIORITY priority = nd_log_priority2id(setting);
171
172 #if defined(NETDATA_INTERNAL_CHECKS) || defined(NETDATA_DEV_MODE)
173 priority = NDLP_DEBUG;
174 #endif
175
176 for (size_t i = 0; i < _NDLS_MAX; i++) {
177 if (i != NDLS_DEBUG)
178 nd_log.sources[i].min_priority = priority;
179 }
180
181 // the right one
182 nd_setenv("NETDATA_LOG_LEVEL", nd_log_id2priority(priority), 1);
183 }
184
185 void nd_log_set_facility(const char *facility) {
186 if(!facility || !*facility)
187 facility = "daemon";
188
189 nd_log.syslog.facility = nd_log_facility2id(facility);
190 nd_setenv("NETDATA_SYSLOG_FACILITY", nd_log_id2facility(nd_log.syslog.facility), 1);
191 }
192
193 void nd_log_set_flood_protection(size_t logs, time_t period) {
194 // daemon logs
195 spinlock_lock(&nd_log.sources[NDLS_DAEMON].limits.spinlock);
196 nd_log.sources[NDLS_DAEMON].limits.logs_per_period = logs;
197 nd_log.sources[NDLS_DAEMON].limits.logs_per_period_backup = logs;
198 nd_log.sources[NDLS_DAEMON].limits.throttle_period = period;
199 spinlock_unlock(&nd_log.sources[NDLS_DAEMON].limits.spinlock);
200
201 // collectors logs
202 spinlock_lock(&nd_log.sources[NDLS_COLLECTORS].limits.spinlock);
203 nd_log.sources[NDLS_COLLECTORS].limits.logs_per_period = logs;
204 nd_log.sources[NDLS_COLLECTORS].limits.logs_per_period_backup = logs;
205 nd_log.sources[NDLS_COLLECTORS].limits.throttle_period = period;
206 spinlock_unlock(&nd_log.sources[NDLS_COLLECTORS].limits.spinlock);
207
208 char buf[100];
209 snprintfz(buf, sizeof(buf), "%" PRIu64, (uint64_t)period);
210 nd_setenv("NETDATA_ERRORS_THROTTLE_PERIOD", buf, 1);
211 snprintfz(buf, sizeof(buf), "%" PRIu64, (uint64_t)logs);
212 nd_setenv("NETDATA_ERRORS_PER_PERIOD", buf, 1);
213 }