Skip to content

Commit

Permalink
Add --disable flag
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinburke committed Aug 29, 2017
1 parent 2f2e660 commit acb24c3
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 14 deletions.
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ immediately start tailing the logs. Example output:
2017-08-29T13:03:52-07:00 "" 127.0.0.1 result: time:0s deleted:0
```

Note: This tool works by setting the database's profiling level to 2, which logs
data about every query to the database's `system.profile` collection. We then
stream the data. Be careful about running this in production, since this will
slow down your database and may log sensitive data to the system profile.

### Usage

The database syntax uses the same syntax as the mongo shell client. To tail the
Expand All @@ -26,17 +31,25 @@ Or on the `foo` database on host 192.168.0.5, port 9999:
read-mongo-logs 192.168.0.5:9999/foo
```

If you enabled query logs and you want to disable them, use the `--disable`
flag.

```
read-mongo-logs --disable 192.168.0.5:9999/foo
```


## Installation

Find your target operating system (darwin, windows, linux) and desired bin
directory, and modify the command below as appropriate:

curl --silent --location https://github.com/kevinburke/read-mongo-logs/releases/download/0.2/read-mongo-logs-linux-amd64 > /usr/local/bin/read-mongo-logs && chmod 755 /usr/local/bin/read-mongo-logs
curl --silent --location https://github.com/kevinburke/read-mongo-logs/releases/download/0.3/read-mongo-logs-linux-amd64 > /usr/local/bin/read-mongo-logs && chmod 755 /usr/local/bin/read-mongo-logs

On Travis, you may want to create `$HOME/bin` and write to that, since
/usr/local/bin isn't writable with their container-based infrastructure.

The latest version is 0.2.
The latest version is 0.3.

If you have a Go development environment, you can also install via source code:

Expand Down
55 changes: 43 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"io"
"log"
"os"
"strconv"
"strings"
"time"

Expand All @@ -25,12 +26,15 @@ type ProfileResult struct {

func init() {
flag.Usage = func() {
os.Stderr.WriteString(`usage: read-mongo-logs [mongo-url]
os.Stderr.WriteString(`usage: read-mongo-logs [mongo-url] [--version] [--disable]
Enable verbose Mongo logs on the provided database, and then tail the logs. We
parse Mongo URL's the same way that the mongo shell client parses them, for
example, specify "read-mongo-logs accounts" to connect to the accounts database
on localhost.
--version: [bool] Print the version and exit
--disable: [bool] Disable Mongo query logging and exit
`)
}
}
Expand Down Expand Up @@ -213,8 +217,32 @@ var query = &bson.M{

const Version = "0.2"

func setProfilingLevel(db *mgo.Database, level int) error {
if level < 0 || level > 2 {
panic("invalid database level " + strconv.Itoa(level))
}
res := new(ProfileResult)
// this is the call underlying db.setProfilingLevel. note setProfilingLevel
// defaults to showing 100ms, we want to show everything.
// https://docs.mongodb.com/manual/reference/method/db.setProfilingLevel/
var slowms int
if level == 0 {
slowms = 100 // reset to default
} else {
slowms = 0
}
if err := db.Run(bson.D{{Name: "profile", Value: level}, {Name: "slowms", Value: slowms}}, res); err != nil {
return err
}
if !res.OK {
return errors.New("Could not enable verbose logging")
}
return nil
}

func main() {
version := flag.Bool("version", false, "Print the version string and exit")
disable := flag.Bool("disable", false, "Disable database profiling and exit")
flag.Parse()
if *version {
fmt.Fprintf(os.Stderr, "read-mongo-logs version %s\n", Version)
Expand Down Expand Up @@ -255,21 +283,24 @@ func main() {
WMode: "majority",
})
db := client.DB(info.Database)
res := new(ProfileResult)
// this is the call underlying db.setProfilingLevel. note setProfilingLevel
// defaults to showing 100ms, we want to show everything.
// https://docs.mongodb.com/manual/reference/method/db.setProfilingLevel/
if err := db.Run(bson.D{{Name: "profile", Value: 2}, {Name: "slowms", Value: 0}}, res); err != nil {
log.Fatal(err)
if *disable {
if err := setProfilingLevel(db, 0); err != nil {
log.Fatal(err.Error() + " on database " + info.Database)
}
os.Stderr.WriteString("Disabled system logging on database " + info.Database + ". Quitting\n")
return
}
if !res.OK {
log.Fatal("Could not enable verbose logging on " + info.Database)
if err := setProfilingLevel(db, 2); err != nil {
log.Fatal(err.Error() + " on database " + info.Database)
}
iter := db.C("system.profile").Find(query).Tail(-1)
if os.Getenv("DEBUG") == "true" {
if err := debugLoop(iter, info.Database, os.Stdout); err != nil {
log.Fatal(err)
}
return
}
if err := loop(iter, info.Database, os.Stdout); err != nil {
log.Fatal(err)
}
//if err := debugLoop(iter, info.Database, os.Stdout); err != nil {
//log.Fatal(err)
//}
}

0 comments on commit acb24c3

Please sign in to comment.