-
Notifications
You must be signed in to change notification settings - Fork 26
/
journal.c
389 lines (334 loc) · 11.9 KB
/
journal.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
/*
* This program is part of the Clear Linux Project
*
* Copyright 2015-2017 Intel Corporation
*
* This program is free software; you can redistribute it and/or modify it under
* the terms and conditions of the GNU Lesser General Public License, as
* published by the Free Software Foundation; either version 2.1 of the License,
* or (at your option) any later version.
*
* This program is distributed in the hope it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
* A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*/
#define _GNU_SOURCE
#include <getopt.h>
#include <poll.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
/* Certain static analysis tools do not understand GCC's __INCLUDE_LEVEL__
* macro; the conditional definition below is used to fix the build with
* systemd's _sd-common.h, included by the public systemd headers, which rely on
* the macro being defined and having an accurate value.
*/
#ifndef __INCLUDE_LEVEL__
# define __INCLUDE_LEVEL__ 2
#endif
#include <systemd/sd-journal.h>
#include <systemd/sd-id128.h>
#include "config.h"
#include "log.h"
#include "telemetry.h"
#include "probe.h"
#include "nica/nc-string.h"
#define BOOT_ID_LEN 33
static nc_string *payload = NULL;
static uint32_t severity = 2;
static uint32_t payload_version = 1;
static char error_class[30] = "org.clearlinux/journal/error";
static sd_journal *journal = NULL;
static inline void tm_journal_err(const char *msg, int ret)
{
telem_log(LOG_ERR, "%s: %s\n", msg, strerror(-ret));
}
static inline void tm_journal_match_err(int ret)
{
telem_log(LOG_ERR, "sd_journal_add_match() failed: %s\n", strerror(-ret));
}
static void add_to_payload(const void *data, size_t length)
{
if (payload != NULL) {
nc_string_append_printf(payload, "%.*s\n", (int)length,
(char *)data);
} else {
payload = nc_string_dup_printf("%.*s\n", (int)length,
(char *)data);
}
}
static bool send_data(char *class)
{
struct telem_ref *handle = NULL;
int ret;
if ((ret = tm_create_record(&handle, severity, class,
payload_version)) < 0) {
telem_log(LOG_ERR, "Failed to create record: %s\n",
strerror(-ret));
goto fail;
}
char *payload_str = strdup(payload->str);
nc_string_free(payload);
payload = NULL;
if ((ret = tm_set_payload(handle, (char *)payload_str)) < 0) {
telem_log(LOG_ERR, "Failed to set payload: %s\n", strerror(-ret));
free(payload_str);
tm_free_record(handle);
goto fail;
}
free(payload_str);
if ((ret = tm_send_record(handle)) < 0) {
telem_log(LOG_ERR, "Failed to send record: %s\n", strerror(-ret));
tm_free_record(handle);
goto fail;
}
tm_free_record(handle);
return true;
fail:
return false;
}
static int read_new_entries(sd_journal *journal)
{
int ret;
const void *data;
size_t length;
int num_entries = 0;
while ((ret = sd_journal_next(journal)) > 0) {
ret = sd_journal_get_data(journal, "MESSAGE", &data, &length);
if (ret < 0) {
tm_journal_err("Failed to read journal entry", ret);
return -1;
}
add_to_payload(data, length);
// For now, we send one record per log message, in case the
// there is a large backlog of messages and we exceed the
// payload size limit (8KB). And ignore errors, hoping that it's
// a transient problem.
if (!send_data(error_class)) {
telem_log(LOG_ERR, "Failed to send data. Ignoring.\n");
return num_entries;
}
num_entries++;
}
/* Since the newest entry in the journal might not match our filters,
* the 0 return code either indicates that we've processed the last
* journal entry, or it was skipped, and we're now positioned at the
* very end of the journal.
*/
if (ret == 0) {
return num_entries;
} else {
tm_journal_err("Failed to advance journal pointer", ret);
return -1;
}
}
static bool process_existing_entries(sd_journal *journal)
{
int ret;
ret = sd_journal_seek_head(journal);
if (ret < 0) {
tm_journal_err("Failed to seek to start of journal", ret);
return false;
}
ret = read_new_entries(journal);
if (ret < 0) {
return false;
} else if (ret == 0) {
return true;
}
return true;
}
static bool get_boot_id(char **data)
{
sd_id128_t boot_id_128;
char boot_id[BOOT_ID_LEN];
int r;
// We are only interested in messages from the current boot
r = sd_id128_get_boot(&boot_id_128);
if (r < 0) {
tm_journal_err("sd_id128_get_boot failed", r);
return false;
}
sd_id128_to_string(boot_id_128, boot_id);
if (asprintf(data, "%s%s", "_BOOT_ID=", boot_id) < 0) {
abort();
}
return true;
}
#define JOURNAL_MATCH(data) \
do { \
r = sd_journal_add_match(journal, data, 0); \
if (r < 0) { \
tm_journal_match_err(r); \
return false; \
} \
} while (0);
#define JOURNAL_AND \
do { \
r = sd_journal_add_conjunction(journal); \
if (r < 0) { \
tm_journal_match_err(r); \
return false; \
} \
} while (0);
#define JOURNAL_OR \
do { \
r = sd_journal_add_disjunction(journal); \
if (r < 0) { \
tm_journal_match_err(r); \
return false; \
} \
} while (0);
static bool add_filters(sd_journal *journal)
{
char *data = NULL;
int r;
if (!get_boot_id(&data)) {
return false;
}
/* The semantics of how journal entry matching works is described in
* detail in sd_journal_add_match(3).
*
* When the privacy filter override is enabled, the matches declared
* here correspond to this logical expression:
*
* BOOTID && ((P0 || P1 || P2 || P3) || EXITED)
*
* Otherwise, the expression is:
*
* BOOTID && EXITED
*
* BOOTID is short for _BOOT_ID=VAL, where VAL is the boot ID for the
* current boot. P0, P1, etc stand for PRIORITY=0, etc. And EXITED is
* short for EXIT_CODE=exited.
*/
JOURNAL_MATCH(data);
free(data);
JOURNAL_AND;
// Filter messages with the four highest log levels, all indicating
// errors, but only when the privacy filters override is in effect;
// because the log messages contain arbitrary strings, and this probe
// does not yet keep a whitelist of allowed patterns or a blacklist of
// banned patterns.
if (access(TM_PRIVACY_FILTERS_OVERRIDE, F_OK) == 0) {
JOURNAL_MATCH("PRIORITY=0");
JOURNAL_MATCH("PRIORITY=1");
JOURNAL_MATCH("PRIORITY=2");
JOURNAL_MATCH("PRIORITY=3");
JOURNAL_OR;
}
// Only set for service-level error conditions
JOURNAL_MATCH("EXIT_CODE=exited");
return true;
}
static bool process_journal(bool process_existing)
{
int r;
r = sd_journal_open(&journal, SD_JOURNAL_LOCAL_ONLY);
if (r < 0) {
tm_journal_err("Failed to open journal", r);
return false;
}
if (!add_filters(journal)) {
return false;
}
if (process_existing) {
if (!process_existing_entries(journal)) {
return false;
}
} else {
if ((sd_journal_seek_tail(journal) < 0) || (sd_journal_previous(journal) < 0)) {
return false;
}
}
struct pollfd pfd;
pfd.fd = sd_journal_get_fd(journal);
pfd.events = (short int)sd_journal_get_events(journal);
// Now wait for new journal entries
while (true) {
r = poll(&pfd, 1, -1);
if (r < 0) {
telem_perror("Polling failed on the journal socket");
return false;
} else if (r == 0) {
// unreachable, since we don't set a timeout
telem_log(LOG_ERR, "poll timed out\n");
return false;
} else {
r = sd_journal_process(journal);
// we don't care about the NOP or INVALIDATE cases
if (r == SD_JOURNAL_APPEND) {
r = read_new_entries(journal);
if (r == 0) {
continue;
} else if (r < 0) {
return false;
}
}
}
}
}
static const struct option prog_opts[] = {
{ "help", no_argument, 0, 'h' },
{ "config-file", required_argument, 0, 'f' },
{ "version", no_argument, 0, 'V' },
{ "tail", no_argument, 0, 't' },
{ 0, 0, 0, 0 }
};
static void print_help(void)
{
printf("Usage:\n");
printf(" journalprobe [OPTIONS] - collect data from systemd journal\n");
printf("\n");
printf("Help Options:\n");
printf(" -h, --help Show help options\n");
printf("\n");
printf("Application Options:\n");
printf(" -f, --config_file Specify a configuration file other than default\n");
printf(" -t, --tail Don't process entries already in the journal\n");
printf(" -V, --version Print the program version\n");
printf("\n");
}
int main(int argc, char **argv)
{
int ret = EXIT_FAILURE;
int opt;
bool process_existing = true;
while ((opt = getopt_long(argc, argv, "hf:Vt", prog_opts, NULL)) != -1) {
switch (opt) {
case 'h':
print_help();
goto success;
case 'V':
printf(PACKAGE_VERSION "\n");
goto success;
case 'f':
if (tm_set_config_file(optarg) != 0) {
telem_log(LOG_ERR, "Configuration file"
" path not valid\n");
exit(EXIT_FAILURE);
}
break;
case 't':
process_existing = false;
break;
}
}
if (!process_journal(process_existing)) {
goto fail;
}
success:
ret = EXIT_SUCCESS;
fail:
if (journal) {
sd_journal_close(journal);
}
if (payload) {
nc_string_free(payload);
}
return ret;
}
/* vi: set ts=8 sw=8 sts=4 et tw=80 cino=(0: */