]>
Commit | Line | Data |
---|---|---|
4ce2e883 | 1 | /* |
4ce2e883 DC |
2 | Copyright (C) 2019 Proxmox Server Solutions GmbH |
3 | ||
814a5472 | 4 | Copyright: mini-journal is under GNU GPL, the GNU General Public License. |
4ce2e883 DC |
5 | |
6 | This program is free software; you can redistribute it and/or modify | |
7 | it under the terms of the GNU General Public License as published by | |
8 | the Free Software Foundation; version 2 dated June, 1991. | |
9 | ||
10 | This program is distributed in the hope that it will be useful, | |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | GNU General Public License for more details. | |
14 | ||
15 | You should have received a copy of the GNU General Public License | |
16 | along with this program; if not, write to the Free Software | |
17 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA | |
18 | 02111-1307, USA. | |
19 | ||
20 | Author: Dominik Csapak <d.csapak@proxmox.com> | |
4ce2e883 DC |
21 | */ |
22 | ||
23 | #include <errno.h> | |
24 | #include <stdbool.h> | |
25 | #include <stdio.h> | |
26 | #include <stdlib.h> | |
27 | #include <string.h> | |
28 | #include <systemd/sd-journal.h> | |
29 | #include <time.h> | |
30 | #include <unistd.h> | |
31 | ||
499710df | 32 | #define BUFSIZE 4095 |
4ce2e883 | 33 | |
499710df | 34 | static char buf[BUFSIZE + 1]; |
4ce2e883 DC |
35 | static size_t offset = 0; |
36 | ||
4ce2e883 DC |
37 | uint64_t get_timestamp(sd_journal *j) { |
38 | uint64_t timestamp; | |
39 | int r = sd_journal_get_realtime_usec(j, ×tamp); | |
40 | if (r < 0) { | |
4a5869e3 | 41 | fprintf(stderr, "Failed %s\n", strerror(-r)); |
b03d6da1 | 42 | return -1; |
4ce2e883 DC |
43 | } |
44 | return timestamp; | |
45 | } | |
46 | ||
47 | void print_to_buf(const char * string, uint32_t length) { | |
48 | if (!length) { | |
4a5869e3 | 49 | return; |
4ce2e883 DC |
50 | } |
51 | size_t string_offset = 0; | |
52 | size_t remaining = length; | |
53 | while (offset + remaining > BUFSIZE) { | |
f41595d3 TL |
54 | strncpy(buf + offset, string + string_offset, BUFSIZE - offset); |
55 | string_offset += BUFSIZE - offset; | |
4a5869e3 TL |
56 | remaining = length - string_offset; |
57 | write (1, buf, BUFSIZE); | |
58 | offset = 0; | |
4ce2e883 | 59 | } |
f41595d3 | 60 | strncpy(buf + offset, string + string_offset, remaining); |
4ce2e883 DC |
61 | offset += remaining; |
62 | } | |
63 | ||
4ce2e883 DC |
64 | void print_cursor(sd_journal *j) { |
65 | int r; | |
66 | char *cursor = NULL; | |
67 | r = sd_journal_get_cursor(j, &cursor); | |
68 | if (r < 0) { | |
4a5869e3 TL |
69 | fprintf(stderr, "Failed to get cursor: %s\n", strerror(-r)); |
70 | exit(1); | |
4ce2e883 DC |
71 | } |
72 | print_to_buf(cursor, strlen(cursor)); | |
73 | print_to_buf("\n", 1); | |
74 | free(cursor); | |
75 | } | |
76 | ||
77 | void print_first_cursor(sd_journal *j) { | |
fc1f5270 | 78 | static bool printed_first_cursor = false; |
4ce2e883 | 79 | if (!printed_first_cursor) { |
4a5869e3 TL |
80 | print_cursor(j); |
81 | printed_first_cursor = true; | |
4ce2e883 DC |
82 | } |
83 | } | |
84 | ||
4ce2e883 DC |
85 | void print_reboot(sd_journal *j) { |
86 | const char *d; | |
87 | size_t l; | |
88 | int r = sd_journal_get_data(j, "_BOOT_ID", (const void **)&d, &l); | |
89 | if (r < 0) { | |
4a5869e3 TL |
90 | fprintf(stderr, "Failed %s\n", strerror(-r)); |
91 | return; | |
4ce2e883 DC |
92 | } |
93 | ||
94 | // remove '_BOOT_ID=' | |
95 | d += 9; | |
96 | l -= 9; | |
97 | ||
fc1f5270 | 98 | static char bootid[32]; |
4ce2e883 | 99 | if (bootid[0] != '\0') { // we have some bootid |
4a5869e3 TL |
100 | if (strncmp(bootid, d, l)) { // a new bootid found |
101 | strncpy(bootid, d, l); | |
102 | print_to_buf("-- Reboot --\n", 13); | |
103 | } | |
4ce2e883 | 104 | } else { |
4a5869e3 | 105 | strncpy(bootid, d, l); |
4ce2e883 DC |
106 | } |
107 | } | |
108 | ||
109 | void print_timestamp(sd_journal *j) { | |
110 | uint64_t timestamp; | |
111 | int r = sd_journal_get_realtime_usec(j, ×tamp); | |
112 | if (r < 0) { | |
4a5869e3 TL |
113 | fprintf(stderr, "Failed %s\n", strerror(-r)); |
114 | return; | |
4ce2e883 DC |
115 | } |
116 | ||
fc1f5270 TL |
117 | static uint64_t last_timestamp; |
118 | static char timestring[16]; | |
4ce2e883 | 119 | if (timestamp >= (last_timestamp+(1000*1000))) { |
4a5869e3 TL |
120 | timestamp = timestamp / (1000*1000); // usec to sec |
121 | struct tm time; | |
122 | localtime_r((time_t *)×tamp, &time); | |
123 | strftime(timestring, 16, "%b %d %T", &time); | |
124 | last_timestamp = timestamp; | |
4ce2e883 DC |
125 | } |
126 | ||
127 | print_to_buf(timestring, 15); | |
128 | } | |
129 | ||
130 | void print_pid(sd_journal *j) { | |
131 | const char *d; | |
132 | size_t l; | |
133 | int r = sd_journal_get_data(j, "_PID", (const void **)&d, &l); | |
134 | if (r < 0) { | |
f41595d3 | 135 | // we sometimes have no pid, e.g., kernel messages |
4a5869e3 | 136 | return; |
4ce2e883 DC |
137 | } |
138 | ||
139 | // remove '_PID=' | |
140 | d += 5; | |
141 | l -= 5; | |
142 | ||
143 | print_to_buf("[", 1); | |
144 | print_to_buf(d, l); | |
145 | print_to_buf("]", 1); | |
146 | } | |
147 | ||
148 | bool print_field(sd_journal *j, const char *field) { | |
149 | const char *d; | |
150 | size_t l; | |
151 | int r = sd_journal_get_data(j, field, (const void **)&d, &l); | |
152 | if (r < 0) { | |
4a5869e3 TL |
153 | // some fields do not exists |
154 | return false; | |
4ce2e883 DC |
155 | } |
156 | ||
157 | int fieldlen = strlen(field)+1; | |
158 | d += fieldlen; | |
159 | l -= fieldlen; | |
160 | print_to_buf(d, l); | |
161 | return true; | |
162 | } | |
163 | ||
164 | ||
165 | void print_line(sd_journal *j) { | |
166 | print_reboot(j); | |
167 | print_timestamp(j); | |
168 | print_to_buf(" ", 1); | |
169 | print_field(j, "_HOSTNAME"); | |
170 | print_to_buf(" ", 1); | |
171 | if (!print_field(j, "SYSLOG_IDENTIFIER") && | |
4a5869e3 TL |
172 | !print_field(j, "_COMM")) { |
173 | print_to_buf("unknown", strlen("unknown") - 1); | |
4ce2e883 DC |
174 | } |
175 | print_pid(j); | |
176 | print_to_buf(": ", 2); | |
177 | print_field(j, "MESSAGE"); | |
178 | print_to_buf("\n", 1); | |
179 | } | |
180 | ||
381acb44 TL |
181 | char *progname; |
182 | ||
183 | void usage(char *error) { | |
184 | if (error) { | |
185 | fprintf(stderr, "ERROR: %s\n", error); | |
186 | } | |
4ce2e883 | 187 | fprintf(stderr, "usage: %s [OPTIONS]\n", progname); |
3805633b TL |
188 | fprintf(stderr, " -b begin\tbegin at this UNIX epoch based timestamp\n"); |
189 | fprintf(stderr, " -e end\tend at this UNIX epoch based timestamp\n"); | |
4ce2e883 DC |
190 | fprintf(stderr, " -d directory\tpath to journal directory\n"); |
191 | fprintf(stderr, " -n number\tprint the last number entries\n"); | |
192 | fprintf(stderr, " -f from\tprint from this cursor\n"); | |
193 | fprintf(stderr, " -t to\tprint to this cursor\n"); | |
194 | fprintf(stderr, " -h \t\tthis help\n"); | |
195 | fprintf(stderr, "\n"); | |
196 | fprintf(stderr, "giving a range conflicts with -n\n"); | |
197 | fprintf(stderr, "-b and -f conflict\n"); | |
198 | fprintf(stderr, "-e and -t conflict\n"); | |
381acb44 | 199 | exit(error ? 1 : 0); |
4ce2e883 DC |
200 | } |
201 | ||
c13468ab TL |
202 | static uint64_t arg_to_uint64(const char *argument) { |
203 | errno = 0; | |
204 | char * end; | |
205 | uint64_t value = strtoull(argument, &end, 10); | |
206 | if (errno != 0 || *end != '\0') { | |
207 | fprintf(stderr, "%s is not a valid integer number\n", argument); | |
208 | exit(1); | |
209 | } | |
210 | ||
211 | return value; | |
212 | } | |
213 | ||
214 | ||
4ce2e883 DC |
215 | int main(int argc, char *argv[]) { |
216 | uint64_t number = 0; | |
217 | const char *directory = NULL; | |
218 | const char *startcursor = NULL; | |
219 | const char *endcursor = NULL; | |
220 | uint64_t begin = 0; | |
221 | uint64_t end = 0; | |
222 | char c; | |
223 | ||
381acb44 TL |
224 | progname = argv[0]; |
225 | ||
4ce2e883 | 226 | while ((c = getopt (argc, argv, "b:e:d:n:f:t:h")) != -1) { |
4a5869e3 TL |
227 | switch (c) { |
228 | case 'b': | |
c13468ab TL |
229 | begin = arg_to_uint64(optarg); |
230 | begin = begin * 1000 * 1000; // µs | |
4a5869e3 TL |
231 | break; |
232 | case 'e': | |
c13468ab TL |
233 | end = arg_to_uint64(optarg); |
234 | end = end * 1000 * 1000; // µs | |
4a5869e3 TL |
235 | break; |
236 | case 'd': | |
237 | directory = optarg; | |
238 | break; | |
239 | case 'n': | |
c13468ab | 240 | number = arg_to_uint64(optarg); |
4a5869e3 TL |
241 | break; |
242 | case 'f': | |
243 | startcursor = optarg; | |
244 | break; | |
245 | case 't': | |
246 | endcursor = optarg; | |
247 | break; | |
248 | case 'h': | |
381acb44 | 249 | usage(NULL); |
4a5869e3 TL |
250 | break; |
251 | case '?': | |
252 | default: | |
381acb44 | 253 | usage("invalid option or missing argument"); |
4a5869e3 | 254 | } |
4ce2e883 DC |
255 | } |
256 | ||
257 | if (number && (begin || startcursor)) { | |
381acb44 | 258 | usage("-n conflicts with -b and/or -f"); |
4ce2e883 DC |
259 | } |
260 | ||
261 | if (begin && startcursor) { | |
381acb44 | 262 | usage("-b and -f conflict"); |
4ce2e883 DC |
263 | } |
264 | ||
265 | if (end && endcursor) { | |
381acb44 | 266 | usage("-e and -t conflict"); |
4ce2e883 DC |
267 | } |
268 | ||
269 | if (argc > optind) { | |
381acb44 | 270 | usage("unkown, or to many arguments"); |
4ce2e883 DC |
271 | } |
272 | ||
273 | // to prevent calling it everytime we generate a timestamp | |
274 | tzset(); | |
275 | ||
276 | int r; | |
277 | sd_journal *j; | |
278 | if (directory == NULL) { | |
4a5869e3 | 279 | r = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY); |
4ce2e883 | 280 | } else { |
4a5869e3 | 281 | r = sd_journal_open_directory(&j, directory, 0); |
4ce2e883 DC |
282 | } |
283 | ||
284 | if (r < 0) { | |
4a5869e3 TL |
285 | fprintf(stderr, "Failed to open journal: %s\n", strerror(-r)); |
286 | return 1; | |
4ce2e883 DC |
287 | } |
288 | ||
289 | // if we want to print the last x entries, seek to cursor or end, | |
290 | // then x entries back, print the cursor and finally print the | |
291 | // entries until end or cursor | |
292 | if (number) { | |
4a5869e3 TL |
293 | if (end) { |
294 | r = sd_journal_seek_realtime_usec(j, end); | |
295 | } else if (endcursor != NULL) { | |
296 | r = sd_journal_seek_cursor(j, endcursor); | |
297 | number++; | |
298 | } else { | |
299 | r = sd_journal_seek_tail(j); | |
300 | } | |
301 | ||
302 | if (r < 0) { | |
303 | fprintf(stderr, "Failed to seek to end/cursor: %s\n", strerror(-r)); | |
304 | exit(1); | |
305 | } | |
306 | ||
307 | // seek back number entries and print cursor | |
308 | r = sd_journal_previous_skip(j, number + 1); | |
309 | if (r < 0) { | |
310 | fprintf(stderr, "Failed to seek back: %s\n", strerror(-r)); | |
311 | exit(1); | |
312 | } | |
4ce2e883 | 313 | } else { |
4a5869e3 TL |
314 | if (begin) { |
315 | r = sd_journal_seek_realtime_usec(j, begin); | |
316 | } else if (startcursor) { | |
317 | r = sd_journal_seek_cursor(j, startcursor); | |
318 | } else { | |
319 | r = sd_journal_seek_head(j); | |
320 | } | |
321 | ||
322 | if (r < 0) { | |
323 | fprintf(stderr, "Failed to seek to begin/cursor: %s\n", strerror(-r)); | |
324 | exit(1); | |
325 | } | |
326 | ||
327 | // if we have a start cursor, we want to skip the first entry | |
328 | if (startcursor) { | |
329 | r = sd_journal_next(j); | |
330 | if (r < 0) { | |
331 | fprintf(stderr, "Failed to seek to begin/cursor: %s\n", strerror(-r)); | |
332 | exit(1); | |
333 | } | |
334 | print_first_cursor(j); | |
335 | } | |
4ce2e883 DC |
336 | } |
337 | ||
338 | ||
339 | while ((r = sd_journal_next(j)) > 0 && (end == 0 || get_timestamp(j) < end)) { | |
4a5869e3 TL |
340 | print_first_cursor(j); |
341 | if (endcursor != NULL && sd_journal_test_cursor(j, endcursor)) { | |
342 | break; | |
343 | } | |
344 | print_line(j); | |
4ce2e883 DC |
345 | } |
346 | ||
347 | // print optional reboot | |
348 | print_reboot(j); | |
349 | ||
350 | // print final cursor | |
351 | print_cursor(j); | |
352 | ||
353 | // print remaining buffer | |
354 | write(1, buf, offset); | |
355 | sd_journal_close(j); | |
356 | ||
357 | return 0; | |
358 | } | |
359 |