Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stanza parse journal #1

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions pkg/stanza/operator/input/journald/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"strings"
"sync"
"time"
"unicode/utf8"

jsoniter "github.com/json-iterator/go"
"go.uber.org/zap"
Expand Down Expand Up @@ -177,6 +178,9 @@ func (operator *Input) parseJournalEntry(line []byte) (*entry.Entry, string, err
if err != nil {
return nil, "", err
}
if err := parseJournalJSONObject(&body); err != nil {
return nil, "", err
}

timestamp, ok := body["__REALTIME_TIMESTAMP"]
if !ok {
Expand Down Expand Up @@ -222,3 +226,57 @@ func (operator *Input) Stop() error {
operator.wg.Wait()
return nil
}

func parseJournalJSONObject(o *map[string]any) error {
if o == nil {
return nil
}
for k, v := range *o {
if s, ok := v.([]any); ok {
newV, err := parseJournalJSONArray(s)
if err != nil {
return err
}
(*o)[k] = newV
}
}
return nil
}

func parseJournalJSONArray(s []any) (any, error) {
if str, ok := parseJournalBinaryString(s); ok {
return str, nil
}
res := make([]any, 0, len(s))
for _, v := range s {
if vs, ok := v.([]any); ok {
newV, err := parseJournalJSONArray(vs)
if err != nil {
return nil, err
}
res = append(res, newV)
} else {
res = append(res, v)
}
}
return res, nil
}

func parseJournalBinaryString(s []any) (string, bool) {
b := make([]byte, len(s))
for i, v := range s {
f, ok := v.(float64)
if !ok {
return "", false
}
c := byte(f)
if float64(c) != f {
return "", false
}
b[i] = c
}
if !utf8.Valid(b) {
return "", false
}
return string(b), true
}
3 changes: 2 additions & 1 deletion pkg/stanza/operator/input/journald/input_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (f *fakeJournaldCmd) Start() error {
}

func (f *fakeJournaldCmd) StdoutPipe() (io.ReadCloser, error) {
response := `{ "_BOOT_ID": "c4fa36de06824d21835c05ff80c54468", "_CAP_EFFECTIVE": "0", "_TRANSPORT": "journal", "_UID": "1000", "_EXE": "/usr/lib/systemd/systemd", "_AUDIT_LOGINUID": "1000", "MESSAGE": "run-docker-netns-4f76d707d45f.mount: Succeeded.", "_PID": "13894", "_CMDLINE": "/lib/systemd/systemd --user", "_MACHINE_ID": "d777d00e7caf45fbadedceba3975520d", "_SELINUX_CONTEXT": "unconfined\n", "CODE_FUNC": "unit_log_success", "SYSLOG_IDENTIFIER": "systemd", "_HOSTNAME": "myhostname", "MESSAGE_ID": "7ad2d189f7e94e70a38c781354912448", "_SYSTEMD_CGROUP": "/user.slice/user-1000.slice/[email protected]/init.scope", "_SOURCE_REALTIME_TIMESTAMP": "1587047866229317", "USER_UNIT": "run-docker-netns-4f76d707d45f.mount", "SYSLOG_FACILITY": "3", "_SYSTEMD_SLICE": "user-1000.slice", "_AUDIT_SESSION": "286", "CODE_FILE": "../src/core/unit.c", "_SYSTEMD_USER_UNIT": "init.scope", "_COMM": "systemd", "USER_INVOCATION_ID": "88f7ca6bbf244dc8828fa901f9fe9be1", "CODE_LINE": "5487", "_SYSTEMD_INVOCATION_ID": "83f7fc7799064520b26eb6de1630429c", "PRIORITY": "6", "_GID": "1000", "__REALTIME_TIMESTAMP": "1587047866229555", "_SYSTEMD_UNIT": "[email protected]", "_SYSTEMD_USER_SLICE": "-.slice", "__CURSOR": "s=b1e713b587ae4001a9ca482c4b12c005;i=1eed30;b=c4fa36de06824d21835c05ff80c54468;m=9f9d630205;t=5a369604ee333;x=16c2d4fd4fdb7c36", "__MONOTONIC_TIMESTAMP": "685540311557", "_SYSTEMD_OWNER_UID": "1000" }
response := `{ "_BOOT_ID": "c4fa36de06824d21835c05ff80c54468", "_CAP_EFFECTIVE": "0", "_TRANSPORT": "journal", "_UID": "1000", "_EXE": "/usr/lib/systemd/systemd", "_AUDIT_LOGINUID": "1000", "MESSAGE": "run-docker-netns-4f76d707d45f.mount: Succeeded.", "_PID": "13894", "_CMDLINE": "/lib/systemd/systemd --user", "_MACHINE_ID": "d777d00e7caf45fbadedceba3975520d", "_SELINUX_CONTEXT": "unconfined\n", "CODE_FUNC": "unit_log_success", "SYSLOG_IDENTIFIER": "systemd", "_HOSTNAME": "myhostname", "MESSAGE_ID": "7ad2d189f7e94e70a38c781354912448", "_SYSTEMD_CGROUP": "/user.slice/user-1000.slice/[email protected]/init.scope", "_SOURCE_REALTIME_TIMESTAMP": "1587047866229317", "USER_UNIT": "run-docker-netns-4f76d707d45f.mount", "SYSLOG_FACILITY": "3", "_SYSTEMD_SLICE": "user-1000.slice", "_AUDIT_SESSION": "286", "CODE_FILE": "../src/core/unit.c", "_SYSTEMD_USER_UNIT": "init.scope", "_COMM": "systemd", "USER_INVOCATION_ID": "88f7ca6bbf244dc8828fa901f9fe9be1", "CODE_LINE": "5487", "_SYSTEMD_INVOCATION_ID": "83f7fc7799064520b26eb6de1630429c", "PRIORITY": "6", "_GID": "1000", "__REALTIME_TIMESTAMP": "1587047866229555", "_SYSTEMD_UNIT": "[email protected]", "_SYSTEMD_USER_SLICE": "-.slice", "__CURSOR": "s=b1e713b587ae4001a9ca482c4b12c005;i=1eed30;b=c4fa36de06824d21835c05ff80c54468;m=9f9d630205;t=5a369604ee333;x=16c2d4fd4fdb7c36", "__MONOTONIC_TIMESTAMP": "685540311557", "_SYSTEMD_OWNER_UID": "1000", "BINARY": [116,101,115,116,13,10] }
`
reader := bytes.NewReader([]byte(response))
return io.NopCloser(reader), nil
Expand Down Expand Up @@ -114,6 +114,7 @@ func TestInputJournald(t *testing.T) {
"__CURSOR": "s=b1e713b587ae4001a9ca482c4b12c005;i=1eed30;b=c4fa36de06824d21835c05ff80c54468;m=9f9d630205;t=5a369604ee333;x=16c2d4fd4fdb7c36",
"__MONOTONIC_TIMESTAMP": "685540311557",
"_SYSTEMD_OWNER_UID": "1000",
"BINARY": "test\r\n",
}

select {
Expand Down
Loading