forked from netdata/netdata
/
log.c
2441 lines (2046 loc) · 76.9 KB
/
log.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: GPL-3.0-or-later
// do not REMOVE this, it is used by systemd-journal includes to prevent saving the file, function, line of the
// source code that makes the calls, allowing our loggers to log the lines of source code that actually log
#define SD_JOURNAL_SUPPRESS_LOCATION
#include "../libnetdata.h"
#ifdef __FreeBSD__
#include <sys/endian.h>
#endif
#ifdef __APPLE__
#include <machine/endian.h>
#endif
#ifdef HAVE_BACKTRACE
#include <execinfo.h>
#endif
#ifdef HAVE_SYSTEMD
#include <systemd/sd-journal.h>
#endif
#include <syslog.h>
const char *program_name = "";
uint64_t debug_flags = 0;
#ifdef ENABLE_ACLK
int aclklog_enabled = 0;
#endif
// ----------------------------------------------------------------------------
struct nd_log_source;
static bool nd_log_limit_reached(struct nd_log_source *source);
// ----------------------------------------------------------------------------
// logging method
typedef enum __attribute__((__packed__)) {
NDLM_DISABLED = 0,
NDLM_DEVNULL,
NDLM_DEFAULT,
NDLM_JOURNAL,
NDLM_SYSLOG,
NDLM_STDOUT,
NDLM_STDERR,
NDLM_FILE,
} ND_LOG_METHOD;
static struct {
ND_LOG_METHOD method;
const char *name;
} nd_log_methods[] = {
{ .method = NDLM_DISABLED, .name = "none" },
{ .method = NDLM_DEVNULL, .name = "/dev/null" },
{ .method = NDLM_DEFAULT, .name = "default" },
{ .method = NDLM_JOURNAL, .name = "journal" },
{ .method = NDLM_SYSLOG, .name = "syslog" },
{ .method = NDLM_STDOUT, .name = "stdout" },
{ .method = NDLM_STDERR, .name = "stderr" },
{ .method = NDLM_FILE, .name = "file" },
};
static ND_LOG_METHOD nd_log_method2id(const char *method) {
if(!method || !*method)
return NDLM_DEFAULT;
size_t entries = sizeof(nd_log_methods) / sizeof(nd_log_methods[0]);
for(size_t i = 0; i < entries ;i++) {
if(strcmp(nd_log_methods[i].name, method) == 0)
return nd_log_methods[i].method;
}
return NDLM_FILE;
}
static const char *nd_log_id2method(ND_LOG_METHOD method) {
size_t entries = sizeof(nd_log_methods) / sizeof(nd_log_methods[0]);
for(size_t i = 0; i < entries ;i++) {
if(method == nd_log_methods[i].method)
return nd_log_methods[i].name;
}
return "unknown";
}
#define IS_VALID_LOG_METHOD_FOR_EXTERNAL_PLUGINS(ndlo) ((ndlo) == NDLM_JOURNAL || (ndlo) == NDLM_SYSLOG || (ndlo) == NDLM_STDERR)
const char *nd_log_method_for_external_plugins(const char *s) {
if(s && *s) {
ND_LOG_METHOD method = nd_log_method2id(s);
if(IS_VALID_LOG_METHOD_FOR_EXTERNAL_PLUGINS(method))
return nd_log_id2method(method);
}
return nd_log_id2method(NDLM_STDERR);
}
// ----------------------------------------------------------------------------
// workaround strerror_r()
#if defined(STRERROR_R_CHAR_P)
// GLIBC version of strerror_r
static const char *strerror_result(const char *a, const char *b) { (void)b; return a; }
#elif defined(HAVE_STRERROR_R)
// POSIX version of strerror_r
static const char *strerror_result(int a, const char *b) { (void)a; return b; }
#elif defined(HAVE_C__GENERIC)
// what a trick!
// http://stackoverflow.com/questions/479207/function-overloading-in-c
static const char *strerror_result_int(int a, const char *b) { (void)a; return b; }
static const char *strerror_result_string(const char *a, const char *b) { (void)b; return a; }
#define strerror_result(a, b) _Generic((a), \
int: strerror_result_int, \
char *: strerror_result_string \
)(a, b)
#else
#error "cannot detect the format of function strerror_r()"
#endif
static const char *errno2str(int errnum, char *buf, size_t size) {
return strerror_result(strerror_r(errnum, buf, size), buf);
}
// ----------------------------------------------------------------------------
// facilities
//
// sys/syslog.h (Linux)
// sys/sys/syslog.h (FreeBSD)
// bsd/sys/syslog.h (darwin-xnu)
static struct {
int facility;
const char *name;
} nd_log_facilities[] = {
{ LOG_AUTH, "auth" },
{ LOG_AUTHPRIV, "authpriv" },
{ LOG_CRON, "cron" },
{ LOG_DAEMON, "daemon" },
{ LOG_FTP, "ftp" },
{ LOG_KERN, "kern" },
{ LOG_LPR, "lpr" },
{ LOG_MAIL, "mail" },
{ LOG_NEWS, "news" },
{ LOG_SYSLOG, "syslog" },
{ LOG_USER, "user" },
{ LOG_UUCP, "uucp" },
{ LOG_LOCAL0, "local0" },
{ LOG_LOCAL1, "local1" },
{ LOG_LOCAL2, "local2" },
{ LOG_LOCAL3, "local3" },
{ LOG_LOCAL4, "local4" },
{ LOG_LOCAL5, "local5" },
{ LOG_LOCAL6, "local6" },
{ LOG_LOCAL7, "local7" },
#ifdef __FreeBSD__
{ LOG_CONSOLE, "console" },
{ LOG_NTP, "ntp" },
// FreeBSD does not consider 'security' as deprecated.
{ LOG_SECURITY, "security" },
#else
// For all other O/S 'security' is mapped to 'auth'.
{ LOG_AUTH, "security" },
#endif
#ifdef __APPLE__
{ LOG_INSTALL, "install" },
{ LOG_NETINFO, "netinfo" },
{ LOG_RAS, "ras" },
{ LOG_REMOTEAUTH, "remoteauth" },
{ LOG_LAUNCHD, "launchd" },
#endif
};
static int nd_log_facility2id(const char *facility) {
size_t entries = sizeof(nd_log_facilities) / sizeof(nd_log_facilities[0]);
for(size_t i = 0; i < entries ;i++) {
if(strcmp(nd_log_facilities[i].name, facility) == 0)
return nd_log_facilities[i].facility;
}
return LOG_DAEMON;
}
static const char *nd_log_id2facility(int facility) {
size_t entries = sizeof(nd_log_facilities) / sizeof(nd_log_facilities[0]);
for(size_t i = 0; i < entries ;i++) {
if(nd_log_facilities[i].facility == facility)
return nd_log_facilities[i].name;
}
return "daemon";
}
// ----------------------------------------------------------------------------
// priorities
static struct {
ND_LOG_FIELD_PRIORITY priority;
const char *name;
} nd_log_priorities[] = {
{ .priority = NDLP_EMERG, .name = "emergency" },
{ .priority = NDLP_EMERG, .name = "emerg" },
{ .priority = NDLP_ALERT, .name = "alert" },
{ .priority = NDLP_CRIT, .name = "critical" },
{ .priority = NDLP_CRIT, .name = "crit" },
{ .priority = NDLP_ERR, .name = "error" },
{ .priority = NDLP_ERR, .name = "err" },
{ .priority = NDLP_WARNING, .name = "warning" },
{ .priority = NDLP_WARNING, .name = "warn" },
{ .priority = NDLP_NOTICE, .name = "notice" },
{ .priority = NDLP_INFO, .name = NDLP_INFO_STR },
{ .priority = NDLP_DEBUG, .name = "debug" },
};
int nd_log_priority2id(const char *priority) {
size_t entries = sizeof(nd_log_priorities) / sizeof(nd_log_priorities[0]);
for(size_t i = 0; i < entries ;i++) {
if(strcmp(nd_log_priorities[i].name, priority) == 0)
return nd_log_priorities[i].priority;
}
return NDLP_INFO;
}
const char *nd_log_id2priority(ND_LOG_FIELD_PRIORITY priority) {
size_t entries = sizeof(nd_log_priorities) / sizeof(nd_log_priorities[0]);
for(size_t i = 0; i < entries ;i++) {
if(priority == nd_log_priorities[i].priority)
return nd_log_priorities[i].name;
}
return NDLP_INFO_STR;
}
// ----------------------------------------------------------------------------
// log sources
const char *nd_log_sources[] = {
[NDLS_UNSET] = "UNSET",
[NDLS_ACCESS] = "access",
[NDLS_ACLK] = "aclk",
[NDLS_COLLECTORS] = "collector",
[NDLS_DAEMON] = "daemon",
[NDLS_HEALTH] = "health",
[NDLS_DEBUG] = "debug",
};
size_t nd_log_source2id(const char *source, ND_LOG_SOURCES def) {
size_t entries = sizeof(nd_log_sources) / sizeof(nd_log_sources[0]);
for(size_t i = 0; i < entries ;i++) {
if(strcmp(nd_log_sources[i], source) == 0)
return i;
}
return def;
}
static const char *nd_log_id2source(ND_LOG_SOURCES source) {
size_t entries = sizeof(nd_log_sources) / sizeof(nd_log_sources[0]);
if(source < entries)
return nd_log_sources[source];
return nd_log_sources[NDLS_COLLECTORS];
}
// ----------------------------------------------------------------------------
// log output formats
typedef enum __attribute__((__packed__)) {
NDLF_JOURNAL,
NDLF_LOGFMT,
NDLF_JSON,
} ND_LOG_FORMAT;
static struct {
ND_LOG_FORMAT format;
const char *name;
} nd_log_formats[] = {
{ .format = NDLF_JOURNAL, .name = "journal" },
{ .format = NDLF_LOGFMT, .name = "logfmt" },
{ .format = NDLF_JSON, .name = "json" },
};
static ND_LOG_FORMAT nd_log_format2id(const char *format) {
if(!format || !*format)
return NDLF_LOGFMT;
size_t entries = sizeof(nd_log_formats) / sizeof(nd_log_formats[0]);
for(size_t i = 0; i < entries ;i++) {
if(strcmp(nd_log_formats[i].name, format) == 0)
return nd_log_formats[i].format;
}
return NDLF_LOGFMT;
}
static const char *nd_log_id2format(ND_LOG_FORMAT format) {
size_t entries = sizeof(nd_log_formats) / sizeof(nd_log_formats[0]);
for(size_t i = 0; i < entries ;i++) {
if(format == nd_log_formats[i].format)
return nd_log_formats[i].name;
}
return "logfmt";
}
// ----------------------------------------------------------------------------
// format dates
void log_date(char *buffer, size_t len, time_t now) {
if(unlikely(!buffer || !len))
return;
time_t t = now;
struct tm *tmp, tmbuf;
tmp = localtime_r(&t, &tmbuf);
if (unlikely(!tmp)) {
buffer[0] = '\0';
return;
}
if (unlikely(strftime(buffer, len, "%Y-%m-%d %H:%M:%S", tmp) == 0))
buffer[0] = '\0';
buffer[len - 1] = '\0';
}
// ----------------------------------------------------------------------------
struct nd_log_limit {
usec_t started_monotonic_ut;
uint32_t counter;
uint32_t prevented;
uint32_t throttle_period;
uint32_t logs_per_period;
uint32_t logs_per_period_backup;
};
#define ND_LOG_LIMITS_DEFAULT (struct nd_log_limit){ .logs_per_period = ND_LOG_DEFAULT_THROTTLE_LOGS, .logs_per_period_backup = ND_LOG_DEFAULT_THROTTLE_LOGS, .throttle_period = ND_LOG_DEFAULT_THROTTLE_PERIOD, }
#define ND_LOG_LIMITS_UNLIMITED (struct nd_log_limit){ .logs_per_period = 0, .logs_per_period_backup = 0, .throttle_period = 0, }
struct nd_log_source {
SPINLOCK spinlock;
ND_LOG_METHOD method;
ND_LOG_FORMAT format;
const char *filename;
int fd;
FILE *fp;
ND_LOG_FIELD_PRIORITY min_priority;
const char *pending_msg;
struct nd_log_limit limits;
};
static __thread ND_LOG_SOURCES overwrite_thread_source = 0;
void nd_log_set_thread_source(ND_LOG_SOURCES source) {
overwrite_thread_source = source;
}
static struct {
uuid_t invocation_id;
ND_LOG_SOURCES overwrite_process_source;
struct nd_log_source sources[_NDLS_MAX];
struct {
bool initialized;
} journal;
struct {
bool initialized;
int fd;
char filename[FILENAME_MAX + 1];
} journal_direct;
struct {
bool initialized;
int facility;
} syslog;
struct {
SPINLOCK spinlock;
bool initialized;
} std_output;
struct {
SPINLOCK spinlock;
bool initialized;
} std_error;
} nd_log = {
.overwrite_process_source = 0,
.journal = {
.initialized = false,
},
.journal_direct = {
.initialized = false,
.fd = -1,
},
.syslog = {
.initialized = false,
.facility = LOG_DAEMON,
},
.std_output = {
.spinlock = NETDATA_SPINLOCK_INITIALIZER,
.initialized = false,
},
.std_error = {
.spinlock = NETDATA_SPINLOCK_INITIALIZER,
.initialized = false,
},
.sources = {
[NDLS_UNSET] = {
.spinlock = NETDATA_SPINLOCK_INITIALIZER,
.method = NDLM_DISABLED,
.format = NDLF_JOURNAL,
.filename = NULL,
.fd = -1,
.fp = NULL,
.min_priority = NDLP_EMERG,
.limits = ND_LOG_LIMITS_UNLIMITED,
},
[NDLS_ACCESS] = {
.spinlock = NETDATA_SPINLOCK_INITIALIZER,
.method = NDLM_DEFAULT,
.format = NDLF_LOGFMT,
.filename = LOG_DIR "/access.log",
.fd = -1,
.fp = NULL,
.min_priority = NDLP_DEBUG,
.limits = ND_LOG_LIMITS_UNLIMITED,
},
[NDLS_ACLK] = {
.spinlock = NETDATA_SPINLOCK_INITIALIZER,
.method = NDLM_FILE,
.format = NDLF_LOGFMT,
.filename = LOG_DIR "/aclk.log",
.fd = -1,
.fp = NULL,
.min_priority = NDLP_DEBUG,
.limits = ND_LOG_LIMITS_UNLIMITED,
},
[NDLS_COLLECTORS] = {
.spinlock = NETDATA_SPINLOCK_INITIALIZER,
.method = NDLM_DEFAULT,
.format = NDLF_LOGFMT,
.filename = LOG_DIR "/collectors.log",
.fd = STDERR_FILENO,
.fp = NULL,
.min_priority = NDLP_INFO,
.limits = ND_LOG_LIMITS_DEFAULT,
},
[NDLS_DEBUG] = {
.spinlock = NETDATA_SPINLOCK_INITIALIZER,
.method = NDLM_DISABLED,
.format = NDLF_LOGFMT,
.filename = LOG_DIR "/debug.log",
.fd = STDOUT_FILENO,
.fp = NULL,
.min_priority = NDLP_DEBUG,
.limits = ND_LOG_LIMITS_UNLIMITED,
},
[NDLS_DAEMON] = {
.spinlock = NETDATA_SPINLOCK_INITIALIZER,
.method = NDLM_DEFAULT,
.filename = LOG_DIR "/daemon.log",
.format = NDLF_LOGFMT,
.fd = -1,
.fp = NULL,
.min_priority = NDLP_INFO,
.limits = ND_LOG_LIMITS_DEFAULT,
},
[NDLS_HEALTH] = {
.spinlock = NETDATA_SPINLOCK_INITIALIZER,
.method = NDLM_DEFAULT,
.format = NDLF_LOGFMT,
.filename = LOG_DIR "/health.log",
.fd = -1,
.fp = NULL,
.min_priority = NDLP_DEBUG,
.limits = ND_LOG_LIMITS_UNLIMITED,
},
},
};
__attribute__((constructor)) void initialize_invocation_id(void) {
// check for a NETDATA_INVOCATION_ID
if(uuid_parse_flexi(getenv("NETDATA_INVOCATION_ID"), nd_log.invocation_id) != 0) {
// not found, check for systemd set INVOCATION_ID
if(uuid_parse_flexi(getenv("INVOCATION_ID"), nd_log.invocation_id) != 0) {
// not found, generate a new one
uuid_generate_random(nd_log.invocation_id);
}
}
char uuid[UUID_COMPACT_STR_LEN];
uuid_unparse_lower_compact(nd_log.invocation_id, uuid);
setenv("NETDATA_INVOCATION_ID", uuid, 1);
}
int nd_log_health_fd(void) {
if(nd_log.sources[NDLS_HEALTH].method == NDLM_FILE && nd_log.sources[NDLS_HEALTH].fd != -1)
return nd_log.sources[NDLS_HEALTH].fd;
return STDERR_FILENO;
}
void nd_log_set_user_settings(ND_LOG_SOURCES source, const char *setting) {
char buf[FILENAME_MAX + 100];
if(setting && *setting)
strncpyz(buf, setting, sizeof(buf) - 1);
else
buf[0] = '\0';
struct nd_log_source *ls = &nd_log.sources[source];
char *output = strrchr(buf, '@');
if(!output)
// all of it is the output
output = buf;
else {
// we found an '@', the next char is the output
*output = '\0';
output++;
// parse the other params
char *remaining = buf;
while(remaining) {
char *value = strsep_skip_consecutive_separators(&remaining, ",");
if (!value || !*value) continue;
char *name = strsep_skip_consecutive_separators(&value, "=");
if (!name || !*name) continue;
if(strcmp(name, "logfmt") == 0)
ls->format = NDLF_LOGFMT;
else if(strcmp(name, "json") == 0)
ls->format = NDLF_JSON;
else if(strcmp(name, "journal") == 0)
ls->format = NDLF_JOURNAL;
else if(strcmp(name, "level") == 0 && value && *value)
ls->min_priority = nd_log_priority2id(value);
else if(strcmp(name, "protection") == 0 && value && *value) {
if(strcmp(value, "off") == 0 || strcmp(value, "none") == 0) {
ls->limits = ND_LOG_LIMITS_UNLIMITED;
ls->limits.counter = 0;
ls->limits.prevented = 0;
}
else {
ls->limits = ND_LOG_LIMITS_DEFAULT;
char *slash = strchr(value, '/');
if(slash) {
*slash = '\0';
slash++;
ls->limits.logs_per_period = ls->limits.logs_per_period_backup = str2u(value);
ls->limits.throttle_period = str2u(slash);
}
else {
ls->limits.logs_per_period = ls->limits.logs_per_period_backup = str2u(value);
ls->limits.throttle_period = ND_LOG_DEFAULT_THROTTLE_PERIOD;
}
}
}
else
nd_log(NDLS_DAEMON, NDLP_ERR, "Error while parsing configuration of log source '%s'. "
"In config '%s', '%s' is not understood.",
nd_log_id2source(source), setting, name);
}
}
if(!output || !*output || strcmp(output, "none") == 0 || strcmp(output, "off") == 0) {
ls->method = NDLM_DISABLED;
ls->filename = "/dev/null";
}
else if(strcmp(output, "journal") == 0) {
ls->method = NDLM_JOURNAL;
ls->filename = NULL;
}
else if(strcmp(output, "syslog") == 0) {
ls->method = NDLM_SYSLOG;
ls->filename = NULL;
}
else if(strcmp(output, "/dev/null") == 0) {
ls->method = NDLM_DEVNULL;
ls->filename = "/dev/null";
}
else if(strcmp(output, "system") == 0) {
if(ls->fd == STDERR_FILENO) {
ls->method = NDLM_STDERR;
ls->filename = NULL;
ls->fd = STDERR_FILENO;
}
else {
ls->method = NDLM_STDOUT;
ls->filename = NULL;
ls->fd = STDOUT_FILENO;
}
}
else if(strcmp(output, "stderr") == 0) {
ls->method = NDLM_STDERR;
ls->filename = NULL;
ls->fd = STDERR_FILENO;
}
else if(strcmp(output, "stdout") == 0) {
ls->method = NDLM_STDOUT;
ls->filename = NULL;
ls->fd = STDOUT_FILENO;
}
else {
ls->method = NDLM_FILE;
ls->filename = strdupz(output);
}
#if defined(NETDATA_INTERNAL_CHECKS) || defined(NETDATA_DEV_MODE)
ls->min_priority = NDLP_DEBUG;
#endif
if(source == NDLS_COLLECTORS) {
// set the method for the collector processes we will spawn
ND_LOG_METHOD method;
ND_LOG_FORMAT format = ls->format;
ND_LOG_FIELD_PRIORITY priority = ls->min_priority;
if(ls->method == NDLM_SYSLOG || ls->method == NDLM_JOURNAL)
method = ls->method;
else
method = NDLM_STDERR;
setenv("NETDATA_LOG_METHOD", nd_log_id2method(method), 1);
setenv("NETDATA_LOG_FORMAT", nd_log_id2format(format), 1);
setenv("NETDATA_LOG_LEVEL", nd_log_id2priority(priority), 1);
}
}
void nd_log_set_priority_level(const char *setting) {
if(!setting || !*setting)
setting = "info";
ND_LOG_FIELD_PRIORITY priority = nd_log_priority2id(setting);
#if defined(NETDATA_INTERNAL_CHECKS) || defined(NETDATA_DEV_MODE)
priority = NDLP_DEBUG;
#endif
nd_log.sources[NDLS_DAEMON].min_priority = priority;
nd_log.sources[NDLS_COLLECTORS].min_priority = priority;
// the right one
setenv("NETDATA_LOG_LEVEL", nd_log_id2priority(priority), 1);
}
void nd_log_set_facility(const char *facility) {
if(!facility || !*facility)
facility = "daemon";
nd_log.syslog.facility = nd_log_facility2id(facility);
setenv("NETDATA_SYSLOG_FACILITY", nd_log_id2facility(nd_log.syslog.facility), 1);
}
void nd_log_set_flood_protection(size_t logs, time_t period) {
nd_log.sources[NDLS_DAEMON].limits.logs_per_period =
nd_log.sources[NDLS_DAEMON].limits.logs_per_period_backup;
nd_log.sources[NDLS_COLLECTORS].limits.logs_per_period =
nd_log.sources[NDLS_COLLECTORS].limits.logs_per_period_backup = logs;
nd_log.sources[NDLS_DAEMON].limits.throttle_period =
nd_log.sources[NDLS_COLLECTORS].limits.throttle_period = period;
char buf[100];
snprintfz(buf, sizeof(buf), "%" PRIu64, (uint64_t )period);
setenv("NETDATA_ERRORS_THROTTLE_PERIOD", buf, 1);
snprintfz(buf, sizeof(buf), "%" PRIu64, (uint64_t )logs);
setenv("NETDATA_ERRORS_PER_PERIOD", buf, 1);
}
static bool nd_log_journal_systemd_init(void) {
#ifdef HAVE_SYSTEMD
nd_log.journal.initialized = true;
#else
nd_log.journal.initialized = false;
#endif
return nd_log.journal.initialized;
}
static void nd_log_journal_direct_set_env(void) {
if(nd_log.sources[NDLS_COLLECTORS].method == NDLM_JOURNAL)
setenv("NETDATA_SYSTEMD_JOURNAL_PATH", nd_log.journal_direct.filename, 1);
}
static bool nd_log_journal_direct_init(const char *path) {
if(nd_log.journal_direct.initialized) {
nd_log_journal_direct_set_env();
return true;
}
int fd;
char filename[FILENAME_MAX + 1];
if(!is_path_unix_socket(path)) {
journal_construct_path(filename, sizeof(filename), netdata_configured_host_prefix, "netdata");
if (!is_path_unix_socket(filename) || (fd = journal_direct_fd(filename)) == -1) {
journal_construct_path(filename, sizeof(filename), netdata_configured_host_prefix, NULL);
if (!is_path_unix_socket(filename) || (fd = journal_direct_fd(filename)) == -1) {
journal_construct_path(filename, sizeof(filename), NULL, "netdata");
if (!is_path_unix_socket(filename) || (fd = journal_direct_fd(filename)) == -1) {
journal_construct_path(filename, sizeof(filename), NULL, NULL);
if (!is_path_unix_socket(filename) || (fd = journal_direct_fd(filename)) == -1)
return false;
}
}
}
}
else {
snprintfz(filename, sizeof(filename), "%s", path);
fd = journal_direct_fd(filename);
}
if(fd < 0)
return false;
nd_log.journal_direct.fd = fd;
nd_log.journal_direct.initialized = true;
strncpyz(nd_log.journal_direct.filename, filename, sizeof(nd_log.journal_direct.filename) - 1);
nd_log_journal_direct_set_env();
return true;
}
static void nd_log_syslog_init() {
if(nd_log.syslog.initialized)
return;
openlog(program_name, LOG_PID, nd_log.syslog.facility);
nd_log.syslog.initialized = true;
}
void nd_log_initialize_for_external_plugins(const char *name) {
// if we don't run under Netdata, log to stderr,
// otherwise, use the logging method Netdata wants us to use.
setenv("NETDATA_LOG_METHOD", "stderr", 0);
setenv("NETDATA_LOG_FORMAT", "logfmt", 0);
nd_log.overwrite_process_source = NDLS_COLLECTORS;
program_name = name;
for(size_t i = 0; i < _NDLS_MAX ;i++) {
nd_log.sources[i].method = STDERR_FILENO;
nd_log.sources[i].fd = -1;
nd_log.sources[i].fp = NULL;
}
nd_log_set_priority_level(getenv("NETDATA_LOG_LEVEL"));
nd_log_set_facility(getenv("NETDATA_SYSLOG_FACILITY"));
time_t period = 1200;
size_t logs = 200;
const char *s = getenv("NETDATA_ERRORS_THROTTLE_PERIOD");
if(s && *s >= '0' && *s <= '9') {
period = str2l(s);
if(period < 0) period = 0;
}
s = getenv("NETDATA_ERRORS_PER_PERIOD");
if(s && *s >= '0' && *s <= '9')
logs = str2u(s);
nd_log_set_flood_protection(logs, period);
if(!netdata_configured_host_prefix) {
s = getenv("NETDATA_HOST_PREFIX");
if(s && *s)
netdata_configured_host_prefix = (char *)s;
}
ND_LOG_METHOD method = nd_log_method2id(getenv("NETDATA_LOG_METHOD"));
ND_LOG_FORMAT format = nd_log_format2id(getenv("NETDATA_LOG_FORMAT"));
if(!IS_VALID_LOG_METHOD_FOR_EXTERNAL_PLUGINS(method)) {
if(is_stderr_connected_to_journal()) {
nd_log(NDLS_COLLECTORS, NDLP_WARNING, "NETDATA_LOG_METHOD is not set. Using journal.");
method = NDLM_JOURNAL;
}
else {
nd_log(NDLS_COLLECTORS, NDLP_WARNING, "NETDATA_LOG_METHOD is not set. Using stderr.");
method = NDLM_STDERR;
}
}
switch(method) {
case NDLM_JOURNAL:
if(!nd_log_journal_direct_init(getenv("NETDATA_SYSTEMD_JOURNAL_PATH")) ||
!nd_log_journal_direct_init(NULL) || !nd_log_journal_systemd_init()) {
nd_log(NDLS_COLLECTORS, NDLP_WARNING, "Failed to initialize journal. Using stderr.");
method = NDLM_STDERR;
}
break;
case NDLM_SYSLOG:
nd_log_syslog_init();
break;
default:
method = NDLM_STDERR;
break;
}
for(size_t i = 0; i < _NDLS_MAX ;i++) {
nd_log.sources[i].method = method;
nd_log.sources[i].format = format;
nd_log.sources[i].fd = -1;
nd_log.sources[i].fp = NULL;
}
// nd_log(NDLS_COLLECTORS, NDLP_NOTICE, "FINAL_LOG_METHOD: %s", nd_log_id2method(method));
}
static bool nd_log_replace_existing_fd(struct nd_log_source *e, int new_fd) {
if(new_fd == -1 || e->fd == -1 ||
(e->fd == STDOUT_FILENO && nd_log.std_output.initialized) ||
(e->fd == STDERR_FILENO && nd_log.std_error.initialized))
return false;
if(new_fd != e->fd) {
int t = dup2(new_fd, e->fd);
bool ret = true;
if (t == -1) {
netdata_log_error("Cannot dup2() new fd %d to old fd %d for '%s'", new_fd, e->fd, e->filename);
ret = false;
}
else
close(new_fd);
if(e->fd == STDOUT_FILENO)
nd_log.std_output.initialized = true;
else if(e->fd == STDERR_FILENO)
nd_log.std_error.initialized = true;
return ret;
}
return false;
}
static void nd_log_open(struct nd_log_source *e, ND_LOG_SOURCES source) {
if(e->method == NDLM_DEFAULT)
nd_log_set_user_settings(source, e->filename);
if((e->method == NDLM_FILE && !e->filename) ||
(e->method == NDLM_DEVNULL && e->fd == -1))
e->method = NDLM_DISABLED;
if(e->fp)
fflush(e->fp);
switch(e->method) {
case NDLM_SYSLOG:
nd_log_syslog_init();
break;
case NDLM_JOURNAL:
nd_log_journal_direct_init(NULL);
nd_log_journal_systemd_init();
break;
case NDLM_STDOUT:
e->fp = stdout;
e->fd = STDOUT_FILENO;
break;
case NDLM_DISABLED:
break;
case NDLM_DEFAULT:
case NDLM_STDERR:
e->method = NDLM_STDERR;
e->fp = stderr;
e->fd = STDERR_FILENO;
break;
case NDLM_DEVNULL:
case NDLM_FILE: {
int fd = open(e->filename, O_WRONLY | O_APPEND | O_CREAT, 0664);
if(fd == -1) {
if(e->fd != STDOUT_FILENO && e->fd != STDERR_FILENO) {
e->fd = STDERR_FILENO;
e->method = NDLM_STDERR;
netdata_log_error("Cannot open log file '%s'. Falling back to stderr.", e->filename);
}
else
netdata_log_error("Cannot open log file '%s'. Leaving fd %d as-is.", e->filename, e->fd);
}
else {
if (!nd_log_replace_existing_fd(e, fd)) {
if(e->fd == STDOUT_FILENO || e->fd == STDERR_FILENO) {
if(e->fd == STDOUT_FILENO)
e->method = NDLM_STDOUT;
else if(e->fd == STDERR_FILENO)
e->method = NDLM_STDERR;
// we have dup2() fd, so we can close the one we opened
if(fd != STDOUT_FILENO && fd != STDERR_FILENO)
close(fd);
}
else
e->fd = fd;
}
}
// at this point we have e->fd set properly
if(e->fd == STDOUT_FILENO)
e->fp = stdout;
else if(e->fd == STDERR_FILENO)
e->fp = stderr;
if(!e->fp) {
e->fp = fdopen(e->fd, "a");
if (!e->fp) {
netdata_log_error("Cannot fdopen() fd %d ('%s')", e->fd, e->filename);
if(e->fd != STDOUT_FILENO && e->fd != STDERR_FILENO)
close(e->fd);
e->fp = stderr;
e->fd = STDERR_FILENO;
}
}
else {
if (setvbuf(e->fp, NULL, _IOLBF, 0) != 0)
netdata_log_error("Cannot set line buffering on fd %d ('%s')", e->fd, e->filename);
}
}
break;
}
}
static void nd_log_stdin_init(int fd, const char *filename) {
int f = open(filename, O_WRONLY | O_APPEND | O_CREAT, 0664);
if(f == -1)
return;
if(f != fd) {
dup2(f, fd);
close(f);
}
}
void nd_log_initialize(void) {
nd_log_stdin_init(STDIN_FILENO, "/dev/null");
for(size_t i = 0 ; i < _NDLS_MAX ; i++)
nd_log_open(&nd_log.sources[i], i);
}
void nd_log_reopen_log_files(void) {
netdata_log_info("Reopening all log files.");
nd_log.std_output.initialized = false;
nd_log.std_error.initialized = false;
nd_log_initialize();
netdata_log_info("Log files re-opened.");
}
void chown_open_file(int fd, uid_t uid, gid_t gid) {
if(fd == -1) return;
struct stat buf;
if(fstat(fd, &buf) == -1) {
netdata_log_error("Cannot fstat() fd %d", fd);
return;
}
if((buf.st_uid != uid || buf.st_gid != gid) && S_ISREG(buf.st_mode)) {