Skip to content

Commit

Permalink
lxd/network/acl: Read OVN logs from systemd journal
Browse files Browse the repository at this point in the history
In the case of an OVN controller being deployed as part of a MicroOVN deployment,
the OVN controller logs are stored in MicroOVN's snap syslog. The LXD snap should have root access,
which means that it should be authorized (this is being tested) to read the OVN controller logs.

Signed-off-by: Gabriel Mougard <[email protected]>
  • Loading branch information
gabrielmougard committed Dec 2, 2024
1 parent b4964b6 commit b7badc1
Showing 1 changed file with 58 additions and 3 deletions.
61 changes: 58 additions & 3 deletions lxd/network/acl/acl_ovn.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"context"
"encoding/json"
"fmt"
"io"
"net"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -1064,7 +1066,8 @@ func parseLogTime(timestamp string, fields []string) (time.Time, error) {
}

// ovnParseLogEntry takes a log line and expected ACL prefix and returns a re-formated log entry if matching.
func ovnParseLogEntry(input string, prefix string) string {
// The 'timestamp' string is in microseconds format. If empty, the timestamp is extracted from the log entry.
func ovnParseLogEntry(input string, timestamp string, prefix string) string {
fields := strings.Split(input, "|")

// Skip unknown formatting.
Expand Down Expand Up @@ -1093,8 +1096,7 @@ func ovnParseLogEntry(input string, prefix string) string {
return ""
}

// Parse the timestamp.
logTime, err := time.Parse(time.RFC3339, fields[0])
logTime, err := parseLogTime(timestamp, fields)
if err != nil {
return ""
}
Expand Down Expand Up @@ -1153,3 +1155,56 @@ func ovnParseLogEntry(input string, prefix string) string {

return string(out)
}

// ovnParseLogEntriesFromJournald reads the OVN log entries from the systemd journal and returns them as a list of string entries.
func ovnParseLogEntriesFromJournald(systemdUnitName string, prefix string) ([]string, error) {
var logEntries []string
cmd := []string{
"/usr/bin/journalctl",
"--unit", systemdUnitName,
"--directory", shared.HostPath("/var/log/journal"),
"--no-pager",
"--boot", "0",
"--case-sensitive",
"--grep", prefix,
"--output-fields", "MESSAGE",
"-n", "1000",
"-o", "json",
}

stdout := strings.Builder{}
err := shared.RunCommandWithFds(context.TODO(), nil, &stdout, cmd[0], cmd[1:]...)
if err != nil {
return nil, fmt.Errorf("Failed to run journalctl to fetch OVN ACL logs: %w", err)
}

decoder := json.NewDecoder(strings.NewReader(stdout.String()))
for {
var sdLogEntry map[string]any
err = decoder.Decode(&sdLogEntry)
if err == io.EOF {
break
} else if err != nil {
return nil, fmt.Errorf("Failed to parse log entry: %w", err)
}

message, ok := sdLogEntry["MESSAGE"].(string)
if !ok {
continue
}

timestamp, ok := sdLogEntry["__REALTIME_TIMESTAMP"].(string)
if !ok {
continue
}

logEntry := ovnParseLogEntry(message, timestamp, prefix)
if logEntry == "" {
continue
}

logEntries = append(logEntries, logEntry)
}

return logEntries, nil
}

0 comments on commit b7badc1

Please sign in to comment.