From fc1c2a1ded8be2960543850d837c3b6f78d35426 Mon Sep 17 00:00:00 2001 From: k1LoW Date: Wed, 9 Oct 2019 18:12:35 +0900 Subject: [PATCH] Support parse 'unixtime' --- README.md | 2 +- client/client.go | 3 +++ parser/parser.go | 7 ++++++- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 95ab3e2..4952015 100644 --- a/README.md +++ b/README.md @@ -50,7 +50,7 @@ targetSets: description: app log type: regexp regexp: 'time:([^\t]+)' - timeFormat: 'Jan 02 15:04:05' + timeFormat: 'Jan 02 15:04:05' # Golang time format and 'unixtime' timeZone: '+0900' sources: - 'ssh://app-1.example.com/var/log/ltsv.log*' diff --git a/client/client.go b/client/client.go index d7e4baa..1a5befa 100644 --- a/client/client.go +++ b/client/client.go @@ -55,6 +55,9 @@ func buildReadCommand(path string, st, et *time.Time, timeFormat, timeZone strin findStart := st.Format("2006-01-02 15:04:05 MST") cmd := fmt.Sprintf("sudo find %s/ -type f -name '%s' -newermt '%s' | xargs sudo ls -tr | xargs sudo zcat -f | grep -a '%s'", dir, base, findStart, string(matches)) + if timeFormat == "unixtime" { + cmd = fmt.Sprintf("sudo find %s/ -type f -name '%s' -newermt '%s' | xargs sudo ls -tr | xargs sudo zcat -f", dir, base, findStart) + } return cmd } diff --git a/parser/parser.go b/parser/parser.go index 7101b38..dea73dc 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -3,6 +3,7 @@ package parser import ( "context" "fmt" + "strconv" "time" "github.com/k1LoW/harvest/client" @@ -27,8 +28,12 @@ type Parser interface { Parse(ctx context.Context, cancel context.CancelFunc, lineChan <-chan client.Line, tz string, st *time.Time, et *time.Time) <-chan Log } -// parseTime ... func parseTime(tf string, tz string, content string) (*time.Time, error) { + if tf == "unixtime" { + ui, _ := strconv.ParseInt(content, 10, 64) + ut := time.Unix(ui, 0) + return &ut, nil + } if tz == "" { t, err := time.Parse(fmt.Sprintf("2006-01-02 %s", tf), fmt.Sprintf("%s %s", time.Now().Format("2006-01-02"), content)) return &t, err