summaryrefslogtreecommitdiffstats
path: root/src/collectors/log2journal/log2journal-rewrite.c
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-07-24 09:54:23 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-07-24 09:54:44 +0000
commit836b47cb7e99a977c5a23b059ca1d0b5065d310e (patch)
tree1604da8f482d02effa033c94a84be42bc0c848c3 /src/collectors/log2journal/log2journal-rewrite.c
parentReleasing debian version 1.44.3-2. (diff)
downloadnetdata-836b47cb7e99a977c5a23b059ca1d0b5065d310e.tar.xz
netdata-836b47cb7e99a977c5a23b059ca1d0b5065d310e.zip
Merging upstream version 1.46.3.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/collectors/log2journal/log2journal-rewrite.c')
-rw-r--r--src/collectors/log2journal/log2journal-rewrite.c51
1 files changed, 51 insertions, 0 deletions
diff --git a/src/collectors/log2journal/log2journal-rewrite.c b/src/collectors/log2journal/log2journal-rewrite.c
new file mode 100644
index 000000000..112391bf0
--- /dev/null
+++ b/src/collectors/log2journal/log2journal-rewrite.c
@@ -0,0 +1,51 @@
+// SPDX-License-Identifier: GPL-3.0-or-later
+
+#include "log2journal.h"
+
+void rewrite_cleanup(REWRITE *rw) {
+ hashed_key_cleanup(&rw->key);
+
+ if(rw->flags & RW_MATCH_PCRE2)
+ search_pattern_cleanup(&rw->match_pcre2);
+ else if(rw->flags & RW_MATCH_NON_EMPTY)
+ replace_pattern_cleanup(&rw->match_non_empty);
+
+ replace_pattern_cleanup(&rw->value);
+ rw->flags = RW_NONE;
+}
+
+bool log_job_rewrite_add(LOG_JOB *jb, const char *key, RW_FLAGS flags, const char *search_pattern, const char *replace_pattern) {
+ if(jb->rewrites.used >= MAX_REWRITES) {
+ log2stderr("Error: too many rewrites. You can add up to %d rewrite rules.", MAX_REWRITES);
+ return false;
+ }
+
+ if((flags & (RW_MATCH_PCRE2|RW_MATCH_NON_EMPTY)) && (!search_pattern || !*search_pattern)) {
+ log2stderr("Error: rewrite for key '%s' does not specify a search pattern.", key);
+ return false;
+ }
+
+ REWRITE *rw = &jb->rewrites.array[jb->rewrites.used++];
+ rw->flags = flags;
+
+ hashed_key_set(&rw->key, key);
+
+ if((flags & RW_MATCH_PCRE2) && !search_pattern_set(&rw->match_pcre2, search_pattern, strlen(search_pattern))) {
+ rewrite_cleanup(rw);
+ jb->rewrites.used--;
+ return false;
+ }
+ else if((flags & RW_MATCH_NON_EMPTY) && !replace_pattern_set(&rw->match_non_empty, search_pattern)) {
+ rewrite_cleanup(rw);
+ jb->rewrites.used--;
+ return false;
+ }
+
+ if(replace_pattern && *replace_pattern && !replace_pattern_set(&rw->value, replace_pattern)) {
+ rewrite_cleanup(rw);
+ jb->rewrites.used--;
+ return false;
+ }
+
+ return true;
+}