@cryptotaxi247 / netdata-1 / commits / e77241c3c

Support configurable syslog facilities (#5792)

* Changes in libnetdata/log/log.c to append facility * Fixing compilatioN * Fixing compilation problem! * Remove comments and adjust indentation inside log * Update log.c * Elimination of unnecessary calls to function

thiagoftsm committed Apr 5, 2019 at 14:05 UTC e77241c3cfc542e75042d24b94bc71d0773ae7b9
3 files changed +424 -3
daemon/main.c
+4
@@ -355,6 +355,10 @@ void log_init(void) {
355 snprintfz(filename, FILENAME_MAX, "%s/access.log", netdata_configured_log_dir);
356 stdaccess_filename = config_get(CONFIG_SECTION_GLOBAL, "access log", filename);
357
358 + char deffacility[8];
359 + snprintfz(deffacility,7,"%s","daemon");
360 + facility_log = config_get(CONFIG_SECTION_GLOBAL, "facility log", deffacility);
361 +
362 error_log_throttle_period = config_get_number(CONFIG_SECTION_GLOBAL, "errors flood protection period", error_log_throttle_period);
363 error_log_errors_per_period = (unsigned long)config_get_number(CONFIG_SECTION_GLOBAL, "errors to trigger flood protection", (long long int)error_log_errors_per_period);
364 error_log_errors_per_period_backup = error_log_errors_per_period;
libnetdata/log/log.c
+419 -3
@@ -18,12 +18,427 @@ FILE *stdaccess = NULL;
18 const char *stdaccess_filename = NULL;
19 const char *stderr_filename = NULL;
20 const char *stdout_filename = NULL;
21 +const char *facility_log = NULL;
22
22 -void syslog_init(void) {
23 +// ----------------------------------------------------------------------------
24 +// Log facility(https://tools.ietf.org/html/rfc5424)
25 +//
26 +// The facilities accepted in the Netdata are in according with the following
27 +// header files for their respective operate system:
28 +// sys/syslog.h (Linux )
29 +// sys/sys/syslog.h (FreeBSD)
30 +// bsd/sys/syslog.h (darwin-xnu)
31 +
32 +#define LOG_AUTH_KEY "auth"
33 +#define LOG_AUTHPRIV_KEY "authpriv"
34 +#ifdef __FreeBSD__
35 +# define LOG_CONSOLE_KEY "console"
36 +#endif
37 +#define LOG_CRON_KEY "cron"
38 +#define LOG_DAEMON_KEY "daemon"
39 +#define LOG_FTP_KEY "ftp"
40 +#ifdef __APPLE__
41 +# define LOG_INSTALL_KEY "install"
42 +#endif
43 +#define LOG_KERN_KEY "kern"
44 +#define LOG_LPR_KEY "lpr"
45 +#define LOG_MAIL_KEY "mail"
46 +//#define LOG_INTERNAL_MARK_KEY "mark"
47 +#ifdef __APPLE__
48 +# define LOG_NETINFO_KEY "netinfo"
49 +# define LOG_RAS_KEY "ras"
50 +# define LOG_REMOTEAUTH_KEY "remoteauth"
51 +#endif
52 +#define LOG_NEWS_KEY "news"
53 +#ifdef __FreeBSD__
54 +# define LOG_NTP_KEY "ntp"
55 +#endif
56 +#define LOG_SECURITY_KEY "security"
57 +#define LOG_SYSLOG_KEY "syslog"
58 +#define LOG_USER_KEY "user"
59 +#define LOG_UUCP_KEY "uucp"
60 +#ifdef __APPLE__
61 +# define LOG_LAUNCHD_KEY "launchd"
62 +#endif
63 +#define LOG_LOCAL0_KEY "local0"
64 +#define LOG_LOCAL1_KEY "local1"
65 +#define LOG_LOCAL2_KEY "local2"
66 +#define LOG_LOCAL3_KEY "local3"
67 +#define LOG_LOCAL4_KEY "local4"
68 +#define LOG_LOCAL5_KEY "local5"
69 +#define LOG_LOCAL6_KEY "local6"
70 +#define LOG_LOCAL7_KEY "local7"
71 +
72 +static int log_facility_id(const char *facility_name)
73 +{
74 + static int
75 + hash_auth = 0,
76 + hash_authpriv = 0,
77 +#ifdef __FreeBSD__
78 + hash_console = 0,
79 +#endif
80 + hash_cron = 0,
81 + hash_daemon = 0,
82 + hash_ftp = 0,
83 +#ifdef __APPLE__
84 + hash_install = 0,
85 +#endif
86 + hash_kern = 0,
87 + hash_lpr = 0,
88 + hash_mail = 0,
89 +// hash_mark = 0,
90 +#ifdef __APPLE__
91 + hash_netinfo = 0,
92 + hash_ras = 0,
93 + hash_remoteauth = 0,
94 +#endif
95 + hash_news = 0,
96 +#ifdef __FreeBSD__
97 + hash_ntp = 0,
98 +#endif
99 + hash_security = 0,
100 + hash_syslog = 0,
101 + hash_user = 0,
102 + hash_uucp = 0,
103 +#ifdef __APPLE__
104 + hash_launchd = 0,
105 +#endif
106 + hash_local0 = 0,
107 + hash_local1 = 0,
108 + hash_local2 = 0,
109 + hash_local3 = 0,
110 + hash_local4 = 0,
111 + hash_local5 = 0,
112 + hash_local6 = 0,
113 + hash_local7 = 0;
114 +
115 + if(unlikely(!hash_auth))
116 + {
117 + hash_auth = simple_hash(LOG_AUTH_KEY);
118 + hash_authpriv = simple_hash(LOG_AUTHPRIV_KEY);
119 +#ifdef __FreeBSD__
120 + hash_console = simple_hash(LOG_CONSOLE_KEY);
121 +#endif
122 + hash_cron = simple_hash(LOG_CRON_KEY);
123 + hash_daemon = simple_hash(LOG_DAEMON_KEY);
124 + hash_ftp = simple_hash(LOG_FTP_KEY);
125 +#ifdef __APPLE__
126 + hash_install = simple_hash(LOG_INSTALL_KEY);
127 +#endif
128 + hash_kern = simple_hash(LOG_KERN_KEY);
129 + hash_lpr = simple_hash(LOG_LPR_KEY);
130 + hash_mail = simple_hash(LOG_MAIL_KEY);
131 +// hash_mark = simple_uhash();
132 +#ifdef __APPLE__
133 + hash_netinfo = simple_hash(LOG_NETINFO_KEY);
134 + hash_ras = simple_hash(LOG_RAS_KEY);
135 + hash_remoteauth = simple_hash(LOG_REMOTEAUTH_KEY);
136 +#endif
137 + hash_news = simple_hash(LOG_NEWS_KEY);
138 +#ifdef __FreeBSD__
139 + hash_ntp = simple_hash(LOG_NTP_KEY);
140 +#endif
141 + hash_security = simple_hash(LOG_SECURITY_KEY);
142 + hash_syslog = simple_hash(LOG_SYSLOG_KEY);
143 + hash_user = simple_hash(LOG_USER_KEY);
144 + hash_uucp = simple_hash(LOG_UUCP_KEY);
145 +#ifdef __APPLE__
146 + hash_launchd = simple_hash(LOG_LAUNCHD_KEY);
147 +#endif
148 + hash_local0 = simple_hash(LOG_LOCAL0_KEY);
149 + hash_local1 = simple_hash(LOG_LOCAL1_KEY);
150 + hash_local2 = simple_hash(LOG_LOCAL2_KEY);
151 + hash_local3 = simple_hash(LOG_LOCAL3_KEY);
152 + hash_local4 = simple_hash(LOG_LOCAL4_KEY);
153 + hash_local5 = simple_hash(LOG_LOCAL5_KEY);
154 + hash_local6 = simple_hash(LOG_LOCAL6_KEY);
155 + hash_local7 = simple_hash(LOG_LOCAL7_KEY);
156 + }
157 +
158 + int hash = simple_hash(facility_name);
159 + if ( hash == hash_auth )
160 + {
161 + return LOG_AUTH;
162 + }
163 + else if ( hash == hash_authpriv )
164 + {
165 + return LOG_AUTHPRIV;
166 + }
167 +#ifdef __FreeBSD__
168 + else if ( hash == hash_console )
169 + {
170 + return LOG_CONSOLE;
171 + }
172 +#endif
173 + else if ( hash == hash_cron )
174 + {
175 + return LOG_CRON;
176 + }
177 + else if ( hash == hash_daemon )
178 + {
179 + return LOG_DAEMON;
180 + }
181 + else if ( hash == hash_ftp )
182 + {
183 + return LOG_FTP;
184 + }
185 +#ifdef __APPLE__
186 + else if ( hash == hash_install )
187 + {
188 + return LOG_INSTALL;
189 + }
190 +#endif
191 + else if ( hash == hash_kern )
192 + {
193 + return LOG_KERN;
194 + }
195 + else if ( hash == hash_lpr )
196 + {
197 + return LOG_LPR;
198 + }
199 + else if ( hash == hash_mail )
200 + {
201 + return LOG_MAIL;
202 + }
203 + /*
204 + else if ( hash == hash_mark )
205 + {
206 + //this is internal for all OS
207 + return INTERNAL_MARK;
208 + }
209 + */
210 +#ifdef __APPLE__
211 + else if ( hash == hash_netinfo )
212 + {
213 + return LOG_NETINFO;
214 + }
215 + else if ( hash == hash_ras )
216 + {
217 + return LOG_RAS;
218 + }
219 + else if ( hash == hash_remoteauth )
220 + {
221 + return LOG_REMOTEAUTH;
222 + }
223 +#endif
224 + else if ( hash == hash_news )
225 + {
226 + return LOG_NEWS;
227 + }
228 +#ifdef __FreeBSD__
229 + else if ( hash == hash_ntp )
230 + {
231 + return LOG_NTP;
232 + }
233 +#endif
234 + else if ( hash == hash_security )
235 + {
236 + //FreeBSD is the unique that does not consider
237 + //this facility deprecated. We are keeping
238 + //it for other OS while they are kept in their headers.
239 +#ifdef __FreeBSD__
240 + return LOG_SECURITY;
241 +#else
242 + return LOG_AUTH;
243 +#endif
244 + }
245 + else if ( hash == hash_syslog )
246 + {
247 + return LOG_SYSLOG;
248 + }
249 + else if ( hash == hash_user )
250 + {
251 + return LOG_USER;
252 + }
253 + else if ( hash == hash_uucp )
254 + {
255 + return LOG_UUCP;
256 + }
257 + else if ( hash == hash_local0 )
258 + {
259 + return LOG_LOCAL0;
260 + }
261 + else if ( hash == hash_local1 )
262 + {
263 + return LOG_LOCAL1;
264 + }
265 + else if ( hash == hash_local2 )
266 + {
267 + return LOG_LOCAL2;
268 + }
269 + else if ( hash == hash_local3 )
270 + {
271 + return LOG_LOCAL3;
272 + }
273 + else if ( hash == hash_local4 )
274 + {
275 + return LOG_LOCAL4;
276 + }
277 + else if ( hash == hash_local5 )
278 + {
279 + return LOG_LOCAL5;
280 + }
281 + else if ( hash == hash_local6 )
282 + {
283 + return LOG_LOCAL6;
284 + }
285 + else if ( hash == hash_local7 )
286 + {
287 + return LOG_LOCAL7;
288 + }
289 +#ifdef __APPLE__
290 + else if ( hash == hash_launchd )
291 + {
292 + return LOG_LAUNCHD;
293 + }
294 +#endif
295 +
296 + return LOG_DAEMON;
297 +}
298 +
299 +//we do not need to use this now, but I already created this function to be
300 +//used case necessary.
301 +/*
302 +char *log_facility_name(int code)
303 +{
304 + char *defvalue = { "daemon" };
305 + switch(code)
306 + {
307 + case LOG_AUTH:
308 + {
309 + return "auth";
310 + }
311 + case LOG_AUTHPRIV:
312 + {
313 + return "authpriv";
314 + }
315 +#ifdef __FreeBSD__
316 + case LOG_CONSOLE:
317 + {
318 + return "console";
319 + }
320 +#endif
321 + case LOG_CRON:
322 + {
323 + return "cron";
324 + }
325 + case LOG_DAEMON:
326 + {
327 + return defvalue;
328 + }
329 + case LOG_FTP:
330 + {
331 + return "ftp";
332 + }
333 +#ifdef __APPLE__
334 + case LOG_INSTALL:
335 + {
336 + return "install";
337 + }
338 +#endif
339 + case LOG_KERN:
340 + {
341 + return "kern";
342 + }
343 + case LOG_LPR:
344 + {
345 + return "lpr";
346 + }
347 + case LOG_MAIL:
348 + {
349 + return "mail";
350 + }
351 +#ifdef __APPLE__
352 + case LOG_NETINFO:
353 + {
354 + return "netinfo" ;
355 + }
356 + case LOG_RAS:
357 + {
358 + return "ras";
359 + }
360 + case LOG_REMOTEAUTH:
361 + {
362 + return "remoteauth";
363 + }
364 +#endif
365 + case LOG_NEWS:
366 + {
367 + return "news";
368 + }
369 +#ifdef __FreeBSD__
370 + case LOG_NTP:
371 + {
372 + return "ntp" ;
373 + }
374 + case LOG_SECURITY:
375 + {
376 + return "security";
377 + }
378 +#endif
379 + case LOG_SYSLOG:
380 + {
381 + return "syslog";
382 + }
383 + case LOG_USER:
384 + {
385 + return "user";
386 + }
387 + case LOG_UUCP:
388 + {
389 + return "uucp";
390 + }
391 + case LOG_LOCAL0:
392 + {
393 + return "local0";
394 + }
395 + case LOG_LOCAL1:
396 + {
397 + return "local1";
398 + }
399 + case LOG_LOCAL2:
400 + {
401 + return "local2";
402 + }
403 + case LOG_LOCAL3:
404 + {
405 + return "local3";
406 + }
407 + case LOG_LOCAL4:
408 + {
409 + return "local4" ;
410 + }
411 + case LOG_LOCAL5:
412 + {
413 + return "local5";
414 + }
415 + case LOG_LOCAL6:
416 + {
417 + return "local6";
418 + }
419 + case LOG_LOCAL7:
420 + {
421 + return "local7" ;
422 + }
423 +#ifdef __APPLE__
424 + case LOG_LAUNCHD:
425 + {
426 + return "launchd";
427 + }
428 +#endif
429 + }
430 +
431 + return defvalue;
432 +}
433 +*/
434 +
435 +// ----------------------------------------------------------------------------
436 +
437 +void syslog_init() {
438 static int i = 0;
439
440 if(!i) {
26 - openlog(program_name, LOG_PID, LOG_DAEMON);
441 + openlog(program_name, LOG_PID,log_facility_id(facility_log));
442 i = 1;
443 }
444 }
@@ -70,7 +485,8 @@ static FILE *open_log_file(int fd, FILE *fp, const char *filename, int *enabled_
485 if(!strcmp(filename, "syslog")) {
486 filename = "/dev/null";
487 devnull = 1;
73 - syslog_init();
488 +
489 + syslog_init();
490 if(enabled_syslog) *enabled_syslog = 1;
491 }
492 else if(enabled_syslog) *enabled_syslog = 0;
libnetdata/log/log.h
+1
@@ -54,6 +54,7 @@ extern FILE *stdaccess;
54 extern const char *stdaccess_filename;
55 extern const char *stderr_filename;
56 extern const char *stdout_filename;
57 +extern const char *facility_log;
58
59 extern int access_log_syslog;
60 extern int error_log_syslog;