From b18a772a376522e257263caf9f17b52176a0be09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel=20Garc=C3=ADa?= Date: Mon, 2 Oct 2017 15:30:56 +0200 Subject: [PATCH 1/5] add flag to choose an specific file --- main.go | 26 ++++++++++++++++++++++++-- src/rdstail.go | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index 78cb237..e127ee9 100644 --- a/main.go +++ b/main.go @@ -13,7 +13,7 @@ import ( "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/rds" "github.com/urfave/cli" - "github.com/litl/rdstail/src" + "github.com/mgar/rdstail/src" ) func fie(e error) { @@ -58,10 +58,19 @@ func watch(c *cli.Context) { r := setupRDS(c) db := parseDB(c) rate := parseRate(c) - + file := string(c.String("file")) stop := make(chan struct{}) go signalListen(stop) + if file != "" { + err := rdstail.WatchFile(r, db, rate, file, func(lines string) error { + fmt.Print(lines) + return nil + }, stop) + + fie(err) + } + err := rdstail.Watch(r, db, rate, func(lines string) error { fmt.Print(lines) return nil @@ -97,7 +106,12 @@ func papertrail(c *cli.Context) { func tail(c *cli.Context) { r := setupRDS(c) db := parseDB(c) + file := string(c.String("file")) numLines := int64(c.Int("lines")) + if file != "" { + err := rdstail.TailFile(r, db, file, numLines) + fie(err) + } err := rdstail.Tail(r, db, numLines) fie(err) } @@ -167,6 +181,10 @@ func main() { Value: "3s", Usage: "rds log polling rate", }, + cli.StringFlag{ + Name: "file, f", + Usage: "name of the logfile to watch", + }, }, }, @@ -180,6 +198,10 @@ func main() { Value: 20, Usage: "output the last n lines. use 0 for a full dump of the most recent file", }, + cli.StringFlag{ + Name: "file, f", + Usage: "name of the logfile to watch", + }, }, }, } diff --git a/src/rdstail.go b/src/rdstail.go index ce2f0d8..de7d096 100644 --- a/src/rdstail.go +++ b/src/rdstail.go @@ -126,6 +126,17 @@ func Tail(r *rds.RDS, db string, numLines int64) error { return nil } +func TailFile(r *rds.RDS, db string, file string, numLines int64) error { + logFile := &rds.DescribeDBLogFilesDetails{LogFileName: aws.String(file)} + + tail, _, err := tailLogFile(r, db, *logFile.LogFileName, numLines, "") + if err != nil { + return err + } + fmt.Println(tail) + return nil +} + func Watch(r *rds.RDS, db string, rate time.Duration, callback func(string) error, stop <-chan struct{}) error { // Periodically check for new log files (unless there is a way to detect the file is done being written to) // Poll that log file, retaining the marker @@ -183,6 +194,42 @@ func Watch(r *rds.RDS, db string, rate time.Duration, callback func(string) erro return nil } +func WatchFile(r *rds.RDS, db string, rate time.Duration, file string, callback func(string) error, stop <-chan struct{}) error { + logFile := &rds.DescribeDBLogFilesDetails{LogFileName: aws.String(file)} + + // Get a marker for the end of the log file by requesting the most recent line + lines, marker, err := tailLogFile(r, db, *logFile.LogFileName, 1, "") + if err != nil { + return err + } + + t := time.NewTicker(rate) + empty := 0 + const checkLogfileRate = 4 + for { + select { + case <-t.C: + lines, marker, err = tailLogFile(r, db, *logFile.LogFileName, 0, marker) + if err != nil { + return err + } + + if lines == "" { + empty++ + } else { + empty = 0 + if err := callback(lines); err != nil { + return err + } + } + case <-stop: + return nil + } + } + + return nil +} + func FeedPapertrail(r *rds.RDS, db string, rate time.Duration, papertrailHost, app, hostname string, stop <-chan struct{}) error { nameSegment := fmt.Sprintf(" %s %s: ", hostname, app) From 371dedc7308471e238ab0d358c0b3acdf81e5635 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel=20Garc=C3=ADa?= Date: Mon, 2 Oct 2017 15:42:19 +0200 Subject: [PATCH 2/5] add parseFile function --- .idea/vcs.xml | 6 ++ .idea/workspace.xml | 226 ++++++++++++++++++++++++++++++++++++++++++++ main.go | 9 +- 3 files changed, 239 insertions(+), 2 deletions(-) create mode 100644 .idea/vcs.xml create mode 100644 .idea/workspace.xml diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000..265b6aa --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,226 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + github.com/litl/rdstail + Println + Print + DescribeDBLogFilesInput + if + os.E + var + Feed + tail + fmt.Pr + WatchFile + file + + + + + + + + + + + + true + DEFINITION_ORDER + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/main.go b/main.go index ec6936a..936d464 100644 --- a/main.go +++ b/main.go @@ -12,8 +12,8 @@ import ( "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/rds" - "github.com/urfave/cli" "github.com/mgar/rdstail/src" + "github.com/urfave/cli" ) func fie(e error) { @@ -187,7 +187,7 @@ func main() { Usage: "rds log polling rate", }, cli.StringFlag{ - Name: "file, f", + Name: "file, f", Usage: "name of the logfile to watch", }, }, @@ -204,7 +204,7 @@ func main() { Usage: "output the last n lines. use 0 for a full dump of the most recent file", }, cli.StringFlag{ - Name: "file, f", + Name: "file, f", Usage: "name of the logfile to watch", }, }, From 728fdbb501617811e5aaf0a28df04c6f32dd4891 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel=20Garc=C3=ADa?= Date: Mon, 2 Oct 2017 16:12:59 +0200 Subject: [PATCH 4/5] change import name --- README.md | 2 ++ main.go | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index e679a01..cf12f74 100644 --- a/README.md +++ b/README.md @@ -67,6 +67,7 @@ USAGE: OPTIONS: --rate, -r "3s" rds log polling rate + --file, -f "trace/alert_DATABASE.log.2017-09-27" name of the logfile ------------------------------------------------------------ ยป ./rdstail tail -h @@ -79,5 +80,6 @@ USAGE: OPTIONS: --lines, -n "20" output the last n lines. use 0 for a full dump of the most recent file + --file, -f "trace/alert_DATABASE.log.2017-09-27" name of the logfile ``` diff --git a/main.go b/main.go index 936d464..a2bd478 100644 --- a/main.go +++ b/main.go @@ -12,7 +12,7 @@ import ( "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/rds" - "github.com/mgar/rdstail/src" + "github.com/litl/rdstail/src" "github.com/urfave/cli" ) @@ -205,7 +205,7 @@ func main() { }, cli.StringFlag{ Name: "file, f", - Usage: "name of the logfile to watch", + Usage: "name of the logfile to tail", }, }, }, From 6cea12aa0c0356ba64f8ad62bdc9891d1cf1a094 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel=20Garc=C3=ADa?= Date: Mon, 2 Oct 2017 16:15:25 +0200 Subject: [PATCH 5/5] remove unreachable code --- src/rdstail.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/rdstail.go b/src/rdstail.go index de7d096..c8edcb8 100644 --- a/src/rdstail.go +++ b/src/rdstail.go @@ -190,8 +190,6 @@ func Watch(r *rds.RDS, db string, rate time.Duration, callback func(string) erro return nil } } - - return nil } func WatchFile(r *rds.RDS, db string, rate time.Duration, file string, callback func(string) error, stop <-chan struct{}) error { @@ -226,8 +224,6 @@ func WatchFile(r *rds.RDS, db string, rate time.Duration, file string, callback return nil } } - - return nil } func FeedPapertrail(r *rds.RDS, db string, rate time.Duration, papertrailHost, app, hostname string, stop <-chan struct{}) error {