systemd-cat-native negative timeout (#18729)
fixed negative timeout; added verbose mode
Costa Tsaousis committed
Oct 8, 2024 at 20:39 UTC
ce4a91110c5577a48d030cc2cf74c57177fc812f
3 files changed
+48
-29
CMakeLists.txt
+4
-1
@@ -2401,7 +2401,10 @@ set(SYSTEMD_CAT_NATIVE_FILES src/libnetdata/log/systemd-cat-native.c
2401
src/libnetdata/log/systemd-cat-native.h)
2402
2403
add_executable(systemd-cat-native ${SYSTEMD_CAT_NATIVE_FILES})
2404
-target_link_libraries(systemd-cat-native libnetdata)
2404
+target_link_libraries(systemd-cat-native
2405
+ libnetdata
2406
+ "$<$<BOOL:${CURL_FOUND}>:PkgConfig::CURL>"
2407
+)
2408
2409
install(TARGETS systemd-cat-native
2410
COMPONENT netdata
src/libnetdata/log/systemd-cat-native.c
+43
-27
@@ -11,7 +11,9 @@
11
#include <machine/endian.h>
12
#endif
13
14
-static inline void log_message_to_stderr(BUFFER *msg) {
14
+bool verbose = false;
15
+
16
+static inline void log_message_to_stderr(BUFFER *msg, const char *scope) {
17
CLEAN_BUFFER *tmp = buffer_create(0, NULL);
18
19
for(size_t i = 0; i < msg->len ;i++) {
@@ -24,13 +26,13 @@ static inline void log_message_to_stderr(BUFFER *msg) {
26
}
27
}
28
27
- fprintf(stderr, "SENDING: %s\n", buffer_tostring(tmp));
29
+ fprintf(stderr, "SENDING %s: %s\n", scope, buffer_tostring(tmp));
30
}
31
32
static inline buffered_reader_ret_t get_next_line(struct buffered_reader *reader, BUFFER *line, int timeout_ms) {
33
while(true) {
34
if(unlikely(!buffered_reader_next_line(reader, line))) {
33
- buffered_reader_ret_t ret = buffered_reader_read_timeout(reader, STDIN_FILENO, timeout_ms, false);
35
+ buffered_reader_ret_t ret = buffered_reader_read_timeout(reader, STDIN_FILENO, timeout_ms, verbose);
36
if(unlikely(ret != BUFFERED_READER_READ_OK))
37
return ret;
38
@@ -126,7 +128,7 @@ static inline void buffer_memcat_replacing_newlines(BUFFER *wb, const char *src,
128
// ----------------------------------------------------------------------------
129
// log to a systemd-journal-remote
130
129
-#ifdef HAVE_CURL
131
+#ifdef HAVE_LIBCURL
132
#include <curl/curl.h>
133
134
#ifndef HOST_NAME_MAX
@@ -203,8 +205,8 @@ static void journal_remote_complete_event(BUFFER *msg, usec_t *monotonic_ut) {
205
206
buffer_sprintf(msg,
207
""
206
- "__REALTIME_TIMESTAMP=%llu\n"
207
- "__MONOTONIC_TIMESTAMP=%llu\n"
208
+ "__REALTIME_TIMESTAMP=%"PRIu64"\n"
209
+ "__MONOTONIC_TIMESTAMP=%"PRIu64"\n"
210
"_MACHINE_ID=%s\n"
211
"_BOOT_ID=%s\n"
212
"_HOSTNAME=%s\n"
@@ -226,7 +228,8 @@ static void journal_remote_complete_event(BUFFER *msg, usec_t *monotonic_ut) {
228
229
static CURLcode journal_remote_send_buffer(CURL* curl, BUFFER *msg) {
230
229
- // log_message_to_stderr(msg);
231
+ if(verbose)
232
+ log_message_to_stderr(msg, "REMOTE");
233
234
struct upload_data upload = {0};
235
@@ -260,8 +263,8 @@ static log_to_journal_remote_ret_t log_input_to_journal_remote(const char *url,
263
264
global_boot_id[0] = '\0';
265
char buffer[1024];
263
- if(read_file(BOOT_ID_PATH, buffer, sizeof(buffer)) == 0) {
264
- uuid_t uuid;
266
+ if(read_txt_file(BOOT_ID_PATH, buffer, sizeof(buffer)) == 0) {
267
+ nd_uuid_t uuid;
268
if(uuid_parse_flexi(buffer, uuid) == 0)
269
uuid_unparse_lower_compact(uuid, global_boot_id);
270
else
@@ -270,13 +273,13 @@ static log_to_journal_remote_ret_t log_input_to_journal_remote(const char *url,
273
274
if(global_boot_id[0] == '\0') {
275
fprintf(stderr, "WARNING: cannot read '%s'. Will generate a random _BOOT_ID.\n", BOOT_ID_PATH);
273
- uuid_t uuid;
276
+ nd_uuid_t uuid;
277
uuid_generate_random(uuid);
278
uuid_unparse_lower_compact(uuid, global_boot_id);
279
}
280
278
- if(read_file(MACHINE_ID_PATH, buffer, sizeof(buffer)) == 0) {
279
- uuid_t uuid;
281
+ if(read_txt_file(MACHINE_ID_PATH, buffer, sizeof(buffer)) == 0) {
282
+ nd_uuid_t uuid;
283
if(uuid_parse_flexi(buffer, uuid) == 0)
284
uuid_unparse_lower_compact(uuid, global_machine_id);
285
else
@@ -285,13 +288,13 @@ static log_to_journal_remote_ret_t log_input_to_journal_remote(const char *url,
288
289
if(global_machine_id[0] == '\0') {
290
fprintf(stderr, "WARNING: cannot read '%s'. Will generate a random _MACHINE_ID.\n", MACHINE_ID_PATH);
288
- uuid_t uuid;
291
+ nd_uuid_t uuid;
292
uuid_generate_random(uuid);
293
uuid_unparse_lower_compact(uuid, global_boot_id);
294
}
295
296
if(global_stream_id[0] == '\0') {
294
- uuid_t uuid;
297
+ nd_uuid_t uuid;
298
uuid_generate_random(uuid);
299
uuid_unparse_lower_compact(uuid, global_stream_id);
300
}
@@ -456,10 +459,11 @@ static int help(void) {
459
"Usage:\n"
460
"\n"
461
" %s\n"
462
+ " [--verbose|-v]\n"
463
" [--newline=STRING]\n"
464
" [--log-as-netdata|-N]\n"
465
" [--namespace=NAMESPACE] [--socket=PATH]\n"
462
-#ifdef HAVE_CURL
466
+#ifdef HAVE_LIBCURL
467
" [--url=URL [--key=FILENAME] [--cert=FILENAME] [--trust=FILENAME|all]]\n"
468
#endif
469
"\n"
@@ -488,7 +492,7 @@ static int help(void) {
492
" the log destination. Only log fields defined by Netdata are accepted.\n"
493
" If the environment variables expected by Netdata are not found, it\n"
494
" falls back to stderr logging in logfmt format.\n"
491
-#ifdef HAVE_CURL
495
+#ifdef HAVE_LIBCURL
496
"\n"
497
" * Log to a systemd-journal-remote TCP socket, enabled with --url=URL\n"
498
"\n"
@@ -585,15 +589,16 @@ static int log_input_as_netdata(const char *newline, int timeout_ms) {
589
ND_LOG_STACK_PUSH(lgs);
590
lgs_reset(lgs);
591
592
+ ND_LOG_SOURCES source = NDLS_HEALTH;
593
+ ND_LOG_FIELD_PRIORITY priority = NDLP_INFO;
594
size_t fields_added = 0;
595
size_t messages_logged = 0;
590
- ND_LOG_FIELD_PRIORITY priority = NDLP_INFO;
596
597
while(get_next_line(&reader, line, timeout_ms) == BUFFERED_READER_READ_OK) {
598
if(!line->len) {
599
// an empty line - we are done for this message
600
596
- nd_log(NDLS_HEALTH, priority,
601
+ nd_log(source, priority,
602
"added %zu fields", // if the user supplied a MESSAGE, this will be ignored
603
fields_added);
604
@@ -625,7 +630,7 @@ static int log_input_as_netdata(const char *newline, int timeout_ms) {
630
struct log_stack_entry backup = lgs[NDF_MESSAGE];
631
lgs[NDF_MESSAGE] = ND_LOG_FIELD_TXT(NDF_MESSAGE, NULL);
632
628
- nd_log(NDLS_COLLECTORS, NDLP_ERR,
633
+ nd_log(source, NDLP_ERR,
634
"Field '%.*s' is not a Netdata field. Ignoring it.",
635
(int)field_len, field);
636
@@ -636,7 +641,7 @@ static int log_input_as_netdata(const char *newline, int timeout_ms) {
641
struct log_stack_entry backup = lgs[NDF_MESSAGE];
642
lgs[NDF_MESSAGE] = ND_LOG_FIELD_TXT(NDF_MESSAGE, NULL);
643
639
- nd_log(NDLS_COLLECTORS, NDLP_ERR,
644
+ nd_log(source, NDLP_ERR,
645
"Line does not contain an = sign; ignoring it: %s",
646
line->buffer);
647
@@ -648,7 +653,7 @@ static int log_input_as_netdata(const char *newline, int timeout_ms) {
653
}
654
655
if(fields_added) {
651
- nd_log(NDLS_HEALTH, priority, "added %zu fields", fields_added);
656
+ nd_log(source, priority, "added %zu fields", fields_added);
657
messages_logged++;
658
}
659
@@ -659,7 +664,8 @@ static int log_input_as_netdata(const char *newline, int timeout_ms) {
664
// log to a local systemd-journald
665
666
static bool journal_local_send_buffer(int fd, BUFFER *msg) {
662
- // log_message_to_stderr(msg);
667
+ if(verbose)
668
+ log_message_to_stderr(msg, "LOCAL");
669
670
bool ret = journal_direct_send(fd, msg->buffer, msg->len);
671
if (!ret)
@@ -720,6 +726,13 @@ static int log_input_to_journal(const char *socket, const char *namespace, const
726
}
727
728
cleanup:
729
+ if(verbose) {
730
+ if(failed_messages)
731
+ fprintf(stderr, "%zu messages failed to be logged\n", failed_messages);
732
+ if(!messages_logged)
733
+ fprintf(stderr, "No messages were logged!\n");
734
+ }
735
+
736
return !failed_messages && messages_logged ? 0 : 1;
737
}
738
@@ -727,12 +740,12 @@ int main(int argc, char *argv[]) {
740
clocks_init();
741
nd_log_initialize_for_external_plugins(argv[0]);
742
730
- int timeout_ms = -1; // wait forever
743
+ int timeout_ms = 0; // wait forever
744
bool log_as_netdata = false;
745
const char *newline = NULL;
746
const char *namespace = NULL;
747
const char *socket = getenv("NETDATA_SYSTEMD_JOURNAL_PATH");
735
-#ifdef HAVE_CURL
748
+#ifdef HAVE_LIBCURL
749
const char *url = NULL;
750
const char *key = NULL;
751
const char *cert = NULL;
@@ -746,6 +759,9 @@ int main(int argc, char *argv[]) {
759
if(strcmp(k, "--help") == 0 || strcmp(k, "-h") == 0)
760
return help();
761
762
+ else if(strcmp(k, "--verbose") == 0 || strcmp(k, "-v") == 0)
763
+ verbose = true;
764
+
765
else if(strcmp(k, "--log-as-netdata") == 0 || strcmp(k, "-N") == 0)
766
log_as_netdata = true;
767
@@ -758,7 +774,7 @@ int main(int argc, char *argv[]) {
774
else if(strncmp(k, "--newline=", 10) == 0)
775
newline = &k[10];
776
761
-#ifdef HAVE_CURL
777
+#ifdef HAVE_LIBCURL
778
else if (strncmp(k, "--url=", 6) == 0)
779
url = &k[6];
780
@@ -780,7 +796,7 @@ int main(int argc, char *argv[]) {
796
}
797
}
798
783
-#ifdef HAVE_CURL
799
+#ifdef HAVE_LIBCURL
800
if(log_as_netdata && url) {
801
fprintf(stderr, "Cannot log to a systemd-journal-remote URL as Netdata. "
802
"Please either give --url or --log-as-netdata, not both.\n");
@@ -804,7 +820,7 @@ int main(int argc, char *argv[]) {
820
if(log_as_netdata)
821
return log_input_as_netdata(newline, timeout_ms);
822
807
-#ifdef HAVE_CURL
823
+#ifdef HAVE_LIBCURL
824
if(url) {
825
if(url && namespace && *namespace)
826
snprintfz(global_namespace, sizeof(global_namespace), "_NAMESPACE=%s\n", namespace);
src/libnetdata/socket/socket.c
+1
-1
@@ -1210,7 +1210,7 @@ inline int wait_on_socket_or_cancel_with_timeout(
1210
.revents = 0,
1211
};
1212
1213
- bool forever = (timeout_ms == 0);
1213
+ bool forever = (timeout_ms <= 0);
1214
1215
while (timeout_ms > 0 || forever) {
1216
if(nd_thread_signaled_to_cancel()) {