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
|
// SPDX-License-Identifier: GPL-3.0-or-later
#include "nd_log-internals.h"
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 int nd_log_journal_direct_fd_find_and_open(char *filename, size_t size) {
int fd;
if(netdata_configured_host_prefix && *netdata_configured_host_prefix) {
journal_construct_path(filename, size, netdata_configured_host_prefix, "netdata");
if (is_path_unix_socket(filename) && (fd = journal_direct_fd(filename)) != -1)
return fd;
journal_construct_path(filename, size, netdata_configured_host_prefix, NULL);
if (is_path_unix_socket(filename) && (fd = journal_direct_fd(filename)) != -1)
return fd;
}
journal_construct_path(filename, size, NULL, "netdata");
if (is_path_unix_socket(filename) && (fd = journal_direct_fd(filename)) != -1)
return fd;
journal_construct_path(filename, size, NULL, NULL);
if (is_path_unix_socket(filename) && (fd = journal_direct_fd(filename)) != -1)
return fd;
return -1;
}
bool nd_log_journal_socket_available(void) {
char filename[FILENAME_MAX];
int fd = nd_log_journal_direct_fd_find_and_open(filename, sizeof(filename));
if(fd == -1) return false;
close(fd);
return true;
}
static void nd_log_journal_direct_set_env(void) {
if(nd_log.sources[NDLS_COLLECTORS].method == NDLM_JOURNAL)
nd_setenv("NETDATA_SYSTEMD_JOURNAL_PATH", nd_log.journal_direct.filename, 1);
}
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];
if(!is_path_unix_socket(path))
fd = nd_log_journal_direct_fd_find_and_open(filename, sizeof(filename));
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;
}
bool nd_logger_journal_libsystemd(struct log_field *fields __maybe_unused, size_t fields_max __maybe_unused) {
#ifdef HAVE_SYSTEMD
// --- FIELD_PARSER_VERSIONS ---
//
// IMPORTANT:
// THERE ARE 6 VERSIONS OF THIS CODE
//
// 1. journal (direct socket API),
// 2. journal (libsystemd API),
// 3. logfmt,
// 4. json,
// 5. convert to uint64
// 6. convert to int64
//
// UPDATE ALL OF THEM FOR NEW FEATURES OR FIXES
struct iovec iov[fields_max];
int iov_count = 0;
memset(iov, 0, sizeof(iov));
CLEAN_BUFFER *tmp = NULL;
for (size_t i = 0; i < fields_max; i++) {
if (!fields[i].entry.set || !fields[i].journal)
continue;
const char *key = fields[i].journal;
char *value = NULL;
int rc = 0;
switch (fields[i].entry.type) {
case NDFT_TXT:
if(*fields[i].entry.txt)
rc = asprintf(&value, "%s=%s", key, fields[i].entry.txt);
break;
case NDFT_STR:
rc = asprintf(&value, "%s=%s", key, string2str(fields[i].entry.str));
break;
case NDFT_BFR:
if(buffer_strlen(fields[i].entry.bfr))
rc = asprintf(&value, "%s=%s", key, buffer_tostring(fields[i].entry.bfr));
break;
case NDFT_U64:
rc = asprintf(&value, "%s=%" PRIu64, key, fields[i].entry.u64);
break;
case NDFT_I64:
rc = asprintf(&value, "%s=%" PRId64, key, fields[i].entry.i64);
break;
case NDFT_DBL:
rc = asprintf(&value, "%s=%f", key, fields[i].entry.dbl);
break;
case NDFT_UUID:
if(!uuid_is_null(*fields[i].entry.uuid)) {
char u[UUID_COMPACT_STR_LEN];
uuid_unparse_lower_compact(*fields[i].entry.uuid, u);
rc = asprintf(&value, "%s=%s", key, u);
}
break;
case NDFT_CALLBACK: {
if(!tmp)
tmp = buffer_create(1024, NULL);
else
buffer_flush(tmp);
if(fields[i].entry.cb.formatter(tmp, fields[i].entry.cb.formatter_data))
rc = asprintf(&value, "%s=%s", key, buffer_tostring(tmp));
}
break;
default:
rc = asprintf(&value, "%s=%s", key, "UNHANDLED");
break;
}
if (rc != -1 && value) {
iov[iov_count].iov_base = value;
iov[iov_count].iov_len = strlen(value);
iov_count++;
}
}
static bool sockets_before[1024];
bool detect_systemd_socket = __atomic_load_n(&nd_log.journal.first_msg, __ATOMIC_RELAXED) == false;
if(detect_systemd_socket) {
for(int i = 3 ; (size_t)i < _countof(sockets_before); i++)
sockets_before[i] = fd_is_socket(i);
}
int r = sd_journal_sendv(iov, iov_count);
if(r == 0 && detect_systemd_socket) {
__atomic_store_n(&nd_log.journal.first_msg, true, __ATOMIC_RELAXED);
// this is the first successful libsystemd log
// let's detect its fd number (we need it for the spawn server)
for(int i = 3 ; (size_t)i < _countof(sockets_before); i++) {
if (!sockets_before[i] && fd_is_socket(i)) {
nd_log.journal.fd = i;
break;
}
}
}
// Clean up allocated memory
for (int i = 0; i < iov_count; i++) {
if (iov[i].iov_base != NULL) {
free(iov[i].iov_base);
}
}
return r == 0;
#else
return false;
#endif
}
bool nd_logger_journal_direct(struct log_field *fields, size_t fields_max) {
if(!nd_log.journal_direct.initialized)
return false;
// --- FIELD_PARSER_VERSIONS ---
//
// IMPORTANT:
// THERE ARE 6 VERSIONS OF THIS CODE
//
// 1. journal (direct socket API),
// 2. journal (libsystemd API),
// 3. logfmt,
// 4. json,
// 5. convert to uint64
// 6. convert to int64
//
// UPDATE ALL OF THEM FOR NEW FEATURES OR FIXES
CLEAN_BUFFER *wb = buffer_create(4096, NULL);
CLEAN_BUFFER *tmp = NULL;
for (size_t i = 0; i < fields_max; i++) {
if (!fields[i].entry.set || !fields[i].journal)
continue;
const char *key = fields[i].journal;
const char *s = NULL;
switch(fields[i].entry.type) {
case NDFT_TXT:
s = fields[i].entry.txt;
break;
case NDFT_STR:
s = string2str(fields[i].entry.str);
break;
case NDFT_BFR:
s = buffer_tostring(fields[i].entry.bfr);
break;
case NDFT_U64:
buffer_strcat(wb, key);
buffer_putc(wb, '=');
buffer_print_uint64(wb, fields[i].entry.u64);
buffer_putc(wb, '\n');
break;
case NDFT_I64:
buffer_strcat(wb, key);
buffer_putc(wb, '=');
buffer_print_int64(wb, fields[i].entry.i64);
buffer_putc(wb, '\n');
break;
case NDFT_DBL:
buffer_strcat(wb, key);
buffer_putc(wb, '=');
buffer_print_netdata_double(wb, fields[i].entry.dbl);
buffer_putc(wb, '\n');
break;
case NDFT_UUID:
if(!uuid_is_null(*fields[i].entry.uuid)) {
char u[UUID_COMPACT_STR_LEN];
uuid_unparse_lower_compact(*fields[i].entry.uuid, u);
buffer_strcat(wb, key);
buffer_putc(wb, '=');
buffer_fast_strcat(wb, u, sizeof(u) - 1);
buffer_putc(wb, '\n');
}
break;
case NDFT_CALLBACK: {
if(!tmp)
tmp = buffer_create(1024, NULL);
else
buffer_flush(tmp);
if(fields[i].entry.cb.formatter(tmp, fields[i].entry.cb.formatter_data))
s = buffer_tostring(tmp);
else
s = NULL;
}
break;
default:
s = "UNHANDLED";
break;
}
if(s && *s) {
buffer_strcat(wb, key);
if(!strchr(s, '\n')) {
buffer_putc(wb, '=');
buffer_strcat(wb, s);
buffer_putc(wb, '\n');
}
else {
buffer_putc(wb, '\n');
size_t size = strlen(s);
uint64_t le_size = htole64(size);
buffer_memcat(wb, &le_size, sizeof(le_size));
buffer_memcat(wb, s, size);
buffer_putc(wb, '\n');
}
}
}
return journal_direct_send(nd_log.journal_direct.fd, buffer_tostring(wb), buffer_strlen(wb));
}
|