Created
May 30, 2014 22:43
-
-
Save mpobrien/7ba57662a2a1e3da3bfc to your computer and use it in GitHub Desktop.
querywatch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "flag" | |
import "labix.org/v2/mgo" | |
import "labix.org/v2/mgo/bson" | |
import "fmt" | |
import "time" | |
import "encoding/json" | |
type ProfileOp struct { | |
Op string `bson:"op"` | |
Ns string `bson:"ns"` | |
Query map[string]interface{} `bson:"query"` | |
Command map[string]interface{} `bson:"command"` | |
NtoReturn int `bson:"ntoreturn"` | |
NtoSkip int `bson:"ntoskip"` | |
KeyUpdates int `bson:"keyupdates"` | |
UpdateObj map[string]interface{} `bson:"updateobj"` | |
NumYields int `bson:"numyields"` | |
Nreturned int `bson:"nreturned"` | |
ResponseLength int `bson:"responseLength"` | |
Millis int `bson:"millis"` | |
Ts time.Time `bson:"ts"` | |
} | |
func (self *ProfileOp) String() (string, error){ | |
part1 := fmt.Sprintf("%dms\t%s\t(%s): ", self.Millis, self.Op, self.Ns) | |
var part2 string | |
if self.Op == "query" || self.Op == "remove" || self.Op == "update"{ | |
queryJson, err := json.Marshal(self.Query) | |
if err != nil { | |
return "", err | |
} | |
if self.Op == "query" { | |
part2 = string(queryJson) | |
}else if self.Op == "update"{ | |
updateJson, err := json.Marshal(self.UpdateObj) | |
if err != nil { | |
return "", err | |
} | |
part2 = string(queryJson) + " update: " +string(updateJson) | |
} | |
} | |
if self.Op == "command"{ | |
commandJson, err := json.Marshal(self.Command) | |
if err != nil { | |
return "", err | |
} | |
part2 = " command: " +string(commandJson) | |
} | |
return part1 + " " + part2, nil | |
} | |
var dbname string | |
var host string | |
func main() { | |
flag.StringVar(&dbname, "d", "test", "database to use") | |
flag.StringVar(&host, "h", "127.0.0.1", "mongo host to connect to") | |
flag.StringVar(&host, "host", "127.0.0.1", "mongo host to connect to in format 'hostname:port'") | |
flag.Parse() | |
session, _ := mgo.Dial(host) | |
c := session.DB(dbname).C("system.profile") | |
pOp := ProfileOp{} | |
iter := c.Find(bson.M{"ts": bson.M{"$gt": time.Now()}, "ns": bson.M{"$ne": dbname + ".system.profile"}}).Tail(5 * time.Second) | |
lastTs := time.Now() | |
for { | |
for iter.Next(&pOp) { | |
opstr, err := pOp.String() | |
if err != nil { | |
fmt.Println(err) | |
return | |
} | |
fmt.Println(opstr) | |
lastTs = pOp.Ts | |
} | |
if err := iter.Close(); err != nil { | |
fmt.Println(err) | |
return | |
} | |
if iter.Timeout(){ | |
// | |
}else{ | |
iter = c.Find(bson.M{"ts": bson.M{"$gt": lastTs}, "ns": bson.M{"$ne": dbname + ".system.profile"}}).Tail(5 * time.Second) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment