Skip to content

Commit

Permalink
use regexp to parse Systemd version
Browse files Browse the repository at this point in the history
  • Loading branch information
belimawr committed Jun 3, 2024
1 parent b5766a7 commit c9e178c
Showing 1 changed file with 15 additions and 25 deletions.
40 changes: 15 additions & 25 deletions filebeat/input/journald/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import (
"errors"
"flag"
"fmt"
"regexp"
"strconv"
"strings"
"time"

"github.com/coreos/go-systemd/v22/sdjournal"
Expand Down Expand Up @@ -353,33 +353,23 @@ func (r *readerAdapter) Next() (reader.Message, error) {
}

// parseSystemdVersion parses the string version from Systemd fetched via D-Bus.
//
// The expected format is:
//
// - 255.6-1-arch
// - 252.16-1.amzn2023.0.2
//
// The function will parse and return the integer before the full stop.
// The function will parse and return the 3 digit major version, minor version
// and patch are ignored.
func parseSystemdVersion(ver string) (int, error) {
// First try, it's just the version number
version, err := strconv.Atoi(ver)
if err == nil {
return version, nil
}

separators := []string{" ", "."}
// Second try, it's separated by '.' like: 255.6-1-arch
for _, sep := range separators {
parts := strings.Split(ver, sep)
if len(parts) >= 2 {
version, err := strconv.Atoi(parts[0])
if err == nil {
return version, nil
}
}
re := regexp.MustCompile(`(v)?(?P<version>\d\d\d)(\.)?`)
matches := re.FindStringSubmatch(ver)
if len(matches) == 0 {
return 0, fmt.Errorf("unsupported Systemd version format '%s'", ver)
}

return 0, fmt.Errorf("unknown format for Systemd version: '%s'", ver)
// This should never fail because the regexp ensures we're getting a 3-digt
// integer, however, better safe than sorry.
version, err := strconv.Atoi(matches[2])
if err != nil {
return 0, fmt.Errorf("could not convert '%s' to int: %w", matches[2], err)
}

return version, nil
}

// getSystemdVersionViaDBus gets the Systemd version from D-Bus
Expand Down

0 comments on commit c9e178c

Please sign in to comment.