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

Add option to watch/tail a single file #7

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

```
31 changes: 29 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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/litl/rdstail/src"
"github.com/urfave/cli"
)

func fie(e error) {
Expand Down Expand Up @@ -54,14 +54,28 @@ func parseDB(c *cli.Context) string {
return db
}

func parseFile(c *cli.Context) string {
file := c.String("file")
return file
}

func watch(c *cli.Context) {
r := setupRDS(c)
db := parseDB(c)
rate := parseRate(c)

file := parseFile(c)
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
Expand Down Expand Up @@ -97,7 +111,12 @@ func papertrail(c *cli.Context) {
func tail(c *cli.Context) {
r := setupRDS(c)
db := parseDB(c)
file := parseFile(c)
numLines := int64(c.Int("lines"))
if file != "" {
err := rdstail.TailFile(r, db, file, numLines)
fie(err)
}
err := rdstail.Tail(r, db, numLines)
fie(err)
}
Expand Down Expand Up @@ -167,6 +186,10 @@ func main() {
Value: "3s",
Usage: "rds log polling rate",
},
cli.StringFlag{
Name: "file, f",
Usage: "name of the logfile to watch",
},
},
},

Expand All @@ -180,6 +203,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 tail",
},
},
},
}
Expand Down
45 changes: 44 additions & 1 deletion src/rdstail.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -179,8 +190,40 @@ 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 {
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
}
}
}

func FeedPapertrail(r *rds.RDS, db string, rate time.Duration, papertrailHost, app, hostname string, stop <-chan struct{}) error {
Expand Down