moving mongodb code into toolkit repo

This commit is contained in:
Carlos Salguero
2016-12-11 23:18:56 -03:00
parent 12d65fc180
commit 47c0db3650
67 changed files with 4546 additions and 0 deletions

32
src/go/Makefile Normal file
View File

@@ -0,0 +1,32 @@
GO := GO15VENDOREXPERIMENT=1 go
pkgs = $(shell $(GO) list ./... | grep -v /vendor/)
VERSION=$(git describe --tags)
BUILD=$(date +%FT%T%z)
PREFIX=$(shell pwd)
BIN_DIR=$(shell pwd)
all: format build test
style:
@echo ">> checking code style"
@! gofmt -d $(shell find . -path ./vendor -prune -o -name '*.go' -print) | grep '^'
test:
@echo ">> running tests"
@./runtests.sh
format:
@echo ">> formatting code"
@$(GO) fmt $(pkgs)
vet:
@echo ">> vetting code"
@$(GO) vet $(pkgs)
build:
@echo ">> building binaries"
@$(GO) build -ldflags "-w -s -X main.Version=${VERSION} -X main.Build=${BUILD}"
.PHONY: all style format build test vet tarball

10
src/go/README.md Normal file
View File

@@ -0,0 +1,10 @@
#Percona Toolkit
[![Build Status](https://travis-ci.org/percona/toolkit-go.svg?branch=master)](https://travis-ci.org/percona/toolkit-go)
Percona Toolkit is a collection of advanced command-line tools used by Percona support staff to perform a variety of MySQL and system tasks that are too difficult or complex to perform manually.
These tools are ideal alternatives to private or "one-off" scripts because they are professionally developed, formally tested, and fully documented. They are also fully self-contained, so installation is quick and easy and no libraries are installed.
Percona Toolkit is developed and supported by Percona Inc. For more information and other free, open-source software developed by Percona, visit http://www.percona.com/software/.
This is the repository for new tools, written in Go. Please visit the [main repository](https://github.com/percona/percona-toolkit) for more information.

View File

@@ -0,0 +1,3 @@
#Percona Toolkit
Common libs/utils/structs

View File

@@ -0,0 +1,4 @@
#Percona Toolkit for MongoDB structures
This is a collection of structures used to gather information from MongoDB config and stats.

View File

@@ -0,0 +1,9 @@
package proto
type Asserts struct {
User float64 `bson:"user"`
Warning float64 `bson:"warning"`
Msg float64 `bson:"msg"`
Regular float64 `bson:"regular"`
Rollovers float64 `bson:"rollovers"`
}

View File

@@ -0,0 +1,9 @@
package proto
type BackgroundFlushing struct {
AverageMs float64 `bson:"average_ms"`
Flushes float64 `bson:"flushes"`
LastFinished string `bson:"last_finished"`
LastMs float64 `bson:"last_ms"`
TotalMs float64 `bson:"total_ms"`
}

View File

@@ -0,0 +1,9 @@
package proto
type BalancerStats struct {
Success int64
Failed int64
Splits int64
Drops int64
Settings map[string]interface{}
}

View File

@@ -0,0 +1,13 @@
package proto
// BuildInfo Struct to store results of calling session.BuildInfo()
type BuildInfo struct {
Version string
VersionArray []int32
GitVersion string
OpenSSLVersion string
SysInfo string
Bits int32
Debug bool
MaxObjectSize int64
}

View File

@@ -0,0 +1,94 @@
package proto
type ProcessManagement struct {
Fork bool `bson:"fork"`
}
type Replication struct {
ReplSet string `bson:"replSet"`
}
type Sharding struct {
ClusterRole string `bson:"clusterRole"`
}
type CloStorage struct {
DbPath string `bson:"dbPath"`
Engine string `bson:"engine"`
}
type CloSystemLog struct {
Destination string `bson:"destination"`
Path string `bson:"path"`
}
type Parsed struct {
Sharding Sharding `bson:"sharding"`
Storage CloStorage `bson:"storage"`
SystemLog CloSystemLog `bson:"systemLog"`
Net Net `bson:"net"`
ProcessManagement ProcessManagement `bson:"processManagement"`
Replication Replication `bson:"replication"`
}
// Security is a struct to hold security related configs
type Security struct {
KeyFile string `bson:"keyFile"`
ClusterAuthMode string `bson:"clusterAuthMode"`
Authorization string `bson:"authorization"`
JavascriptEnabled bool `bson:javascriptEnabled"`
Sasl struct {
HostName string `bson:"hostName"`
ServiceName string `bson:"serverName"`
SaslauthdSocketPath string `bson:"saslauthdSocketPath"`
} `bson:"sasl"`
EnableEncryption bool `bson:"enableEncryption"`
EncryptionCipherMode string `bson:"encryptionCipherMode"`
EncryptionKeyFile string `bson:"encryptionKeyFile"`
Kmip struct {
KeyIdentifier string `bson:"keyIdentifier"`
RotateMasterKey bool `bson:"rotateMasterKey"`
ServerName string `bson:"serverName"`
Port string `bson:"port"`
ClientCertificateFile string `bson:"clientCertificateFile"`
ClientCertificatePassword string `bson:"clientCertificatePassword"`
ServerCAFile string `bson:"serverCAFile"`
} `bson:"kmip"`
}
// NET config options. See https://docs.mongodb.com/manual/reference/configuration-options/#net-options
type Net struct {
HTTP HTTP `bson:"http"`
SSL SSL `bson:"ssl"`
}
type HTTP struct {
Enabled bool `bson:"enabled"`
Port float64 `bson:"port"`
JSONPEnabled bool `bson:"JSONPEnabled"`
RESTInterfaceEnabled bool `bson:"RESTInterfaceEnabled"`
}
// SSL config options. See https://docs.mongodb.com/manual/reference/configuration-options/#net-ssl-options
type SSL struct {
SSLOnNormalPorts bool `bson:"sslOnNormalPorts"` // deprecated since 2.6
Mode string `bson:"mode"` // disabled, allowSSL, preferSSL, requireSSL
PEMKeyFile string `bson:"PEMKeyFile"`
PEMKeyPassword string `bson:"PEMKeyPassword"`
ClusterFile string `bson:"clusterFile"`
ClusterPassword string `bson:"clusterPassword"`
CAFile string `bson:"CAFile"`
CRLFile string `bson:"CRLFile"`
AllowConnectionsWithoutCertificates bool `bson:"allowConnectionsWithoutCertificates"`
AllowInvalidCertificates bool `bson:"allowInvalidCertificates"`
AllowInvalidHostnames bool `bson:"allowInvalidHostnames"`
DisabledProtocols string `bson:"disabledProtocols"`
FIPSMode bool `bson:"FIPSMode"`
}
type CommandLineOptions struct {
Argv []string `bson:"argv"`
Ok float64 `bson:"ok"`
Parsed Parsed `bson:"parsed"`
Security Security `bson:"security"`
}

View File

@@ -0,0 +1,7 @@
package proto
type Connections struct {
Available float64 `bson:"available"`
Current float64 `bson:"current"`
TotalCreated float64 `bson:"totalCreated"`
}

View File

@@ -0,0 +1,68 @@
package proto
type Query struct {
CurrentOp float64 `bson:"currentOp"`
}
type Progress struct {
Done float64 `bson:"done"`
Total float64 `bson:"total"`
}
type AcquireCount struct {
Rr float64 `bson:"r"`
Ww float64 `bson:"w"`
R float64 `bson:"R"`
W float64 `bson:"W"`
}
type LockInfo struct {
DeadlockCount AcquireCount `bson:"deadlockCount"`
AcquireCount AcquireCount `bson:"acquireCount"`
AcquireWaitCount AcquireCount `bson:"acquireWaitCount"`
TimeAcquiringMicros AcquireCount `bson:"timeAcquiringMicros"`
}
type CurrentOpLockStats struct {
Global LockInfo `bson:"Global"`
MMAPV1Journal interface{} `bson:"MMAPV1Journal"`
Database interface{} `bson:"Database"`
}
type Locks struct {
Global LockInfo `bson:"Global"`
MMAPV1Journal LockInfo `bson:"MMAPV1Journal"`
Database LockInfo `bson:"Database"`
Collection LockInfo `bson:"Collection"`
Metadata LockInfo `bson:"Metadata"`
Oplog LockInfo `bson:"oplog"`
}
type Inprog struct {
Desc string `bson:"desc"`
ConnectionId float64 `bson:"connectionId"`
Opid float64 `bson:"opid"`
Msg string `bson:"msg"`
NumYields float64 `bson:"numYields"`
Locks Locks `bson:"locks"`
WaitingForLock float64 `bson:"waitingForLock"`
ThreadId string `bson:"threadId"`
Active float64 `bson:"active"`
MicrosecsRunning float64 `bson:"microsecs_running"`
SecsRunning float64 `bson:"secs_running"`
Op string `bson:"op"`
Ns string `bson:"ns"`
Insert interface{} `bson:"insert"`
PlanSummary string `bson:"planSummary"`
Client string `bson:"client"`
Query Query `bson:"query"`
Progress Progress `bson:"progress"`
KillPending float64 `bson:"killPending"`
LockStats CurrentOpLockStats `bson:"lockStats"`
}
type CurrentOp struct {
Info string `bson:"info"`
Inprog []Inprog `bson:"inprog"`
FsyncLock float64 `bson:"fsyncLock"`
}

View File

@@ -0,0 +1,10 @@
package proto
type Cursors struct {
ClientCursorsSize float64 `bson:"clientCursors_size"`
Note string `bson:"note"`
Pinned float64 `bson:"pinned"`
TimedOut float64 `bson:"timedOut"`
TotalNoTimeout float64 `bson:"totalNoTimeout"`
TotalOpen float64 `bson:"totalOpen"`
}

View File

@@ -0,0 +1,13 @@
package proto
// Database item plus struct to hold collections stats
type Database struct {
Name string `bson:"name"`
SizeOnDisk int64 `bson:"sizeOnDisk"`
Empty bool `bson:"empty"`
}
// Database struct for listDatabases command
type Databases struct {
Databases []Database `bson:"databases"`
}

View File

@@ -0,0 +1,21 @@
package proto
type TimeMs struct {
WriteToDataFiles float64 `bson:"writeToDataFiles"`
WriteToJournal float64 `bson:"writeToJournal"`
Commits float64 `bson:"commits"`
CommitsInWriteLock float64 `bson:"commitsInWriteLock"`
Dt float64 `bson:"dt"`
PrepLogBuffer float64 `bson:"prepLogBuffer"`
RemapPrivateView float64 `bson:"remapPrivateView"`
}
type Dur struct {
TimeMs *TimeMs `bson:"timeMs"`
WriteToDataFilesMB float64 `bson:"writeToDataFilesMB"`
Commits float64 `bson:"commits"`
CommitsInWriteLock float64 `bson:"commitsInWriteLock"`
Compression float64 `bson:"compression"`
EarlyCommits float64 `bson:"earlyCommits"`
JournaledMB float64 `bson:"journaledMB"`
}

View File

@@ -0,0 +1,7 @@
package proto
type ExtraInfo struct {
PageFaults float64 `bson:"page_faults"`
HeapUsageBytes float64 `bson:"heap_usage_bytes"`
Note string `bson:"note"`
}

View File

@@ -0,0 +1,19 @@
package proto
type GlobalLock struct {
ActiveClients *ActiveClients `bson:"activeClients"`
CurrentQueue *CurrentQueue `bson:"currentQueue"`
TotalTime int64 `bson:"totalTime"`
}
type ActiveClients struct {
Readers int64 `bson:"readers"`
Total int64 `bson:"total"`
Writers int64 `bson:"writers"`
}
type CurrentQueue struct {
Writers int64 `bson:"writers"`
Readers int64 `bson:"readers"`
Total int64 `bson:"total"`
}

View File

@@ -0,0 +1,41 @@
package proto
type Extra struct {
LibcVersion string `bson:"libcVersion"`
PageSize float64 `bson:"pageSize"`
VersionSignature string `bson:"versionSignature"`
NumPages float64 `bson:"numPages"`
VersionString string `bson:"versionString"`
CpuFeatures string `bson:"cpuFeatures"`
CpuFrequencyMHz string `bson:"cpuFrequencyMHz"`
KernelVersion string `bson:"kernelVersion"`
MaxOpenFiles float64 `bson:"maxOpenFiles"`
}
type Os struct {
Type string `bson:"type"`
Version string `bson:"version"`
Name string `bson:"name"`
}
type System struct {
CurrentTime string `bson:"currentTime"`
Hostname string `bson:"hostname"`
MemSizeMB float64 `bson:"memSizeMB"`
NumCores float64 `bson:"numCores"`
NumaEnabled bool `bson:"numaEnabled"`
CpuAddrSize float64 `bson:"cpuAddrSize"`
CpuArch string `bson:"cpuArch"`
}
// HostInfo has exported field for the 'hostInfo' command plus some other
// fields like Database/Collections count. We are setting those fields into
// a separated function
type HostInfo struct {
Extra *Extra `bson:"extra"`
Os *Os `bson:"os"`
System *System `bson:"system"`
DatabasesCount int
CollectionsCount int
ID int
}

View File

@@ -0,0 +1,7 @@
package proto
type AcquiredLocks struct {
AcquireCount *AcquireCount `bson:"acquireCount"`
AcquireWaitCount float64 `bson:"acquireWaitCount.W"`
TimeAcquiringMicros float64 `bson:"timeAcquiringMicros.W"`
}

View File

@@ -0,0 +1,47 @@
package proto_test
import (
"fmt"
"io/ioutil"
"os"
"testing"
"github.com/percona/toolkit-go/mongolib/proto"
mgo "gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
"gopkg.in/mgo.v2/dbtest"
)
var Server dbtest.DBServer
var session *mgo.Session
func TestMain(m *testing.M) {
// The tempdir is created so MongoDB has a location to store its files.
// Contents are wiped once the server stops
os.Setenv("CHECK_SESSIONS", "0")
tempDir, _ := ioutil.TempDir("", "testing")
Server.SetPath(tempDir)
session = Server.Session()
retCode := m.Run()
Server.Session().Close()
Server.Wipe()
// Stop shuts down the temporary server and removes data on disk.
Server.Stop()
// call with result of m.Run()
os.Exit(retCode)
}
func ExampleServerStatus() {
ss := proto.ServerStatus{}
if err := session.DB("admin").Run(bson.D{{"serverStatus", 1}, {"recordStats", 1}}, &ss); err != nil {
panic(err)
}
fmt.Printf("%+v", ss)
// Output:
}

View File

@@ -0,0 +1,7 @@
package proto
type MasterDoc struct {
SetName interface{} `bson:"setName"`
Hosts interface{} `bson:"hosts"`
Msg string `bson:"msg"`
}

View File

@@ -0,0 +1,10 @@
package proto
type Mem struct {
Bits float64 `bson:"bits"`
Mapped float64 `bson:"mapped"`
MappedWithJournal float64 `bson:"mappedWithJournal"`
Resident float64 `bson:"resident"`
Supported bool `bson:"supported"`
Virtual float64 `bson:"virtual"`
}

View File

@@ -0,0 +1,85 @@
package proto
type Metrics struct {
Commands map[string]CommandStats `bson:"commands"`
Cursor *Cursor `bson:"cursor"`
Document *Document `bson:"document"`
GetLastError *GetLastError `bson:"getLastError"`
Moves float64 `bson:"record.moves"`
Operation *Operation `bson:"operation"`
QueryExecutor *QueryExecutor `bson:"queryExecutor"`
Repl *ReplMetrics `bson:"repl"`
Storage *Storage `bson:"storage"`
Ttl *Ttl `bson:"ttl"`
}
type CommandStats struct {
Failed float64 `bson:"failed"`
Total float64 `bson:"total"`
}
type Cursor struct {
NoTimeout float64 `bson:"open.noTimeout"`
Pinned float64 `bson:"open.pinned"`
TimedOut float64 `bson:"timedOut"`
Total float64 `bson:"open.total"`
}
type Document struct {
Deleted float64 `bson:"deleted"`
Inserted float64 `bson:"inserted"`
Returned float64 `bson:"returned"`
Updated float64 `bson:"updated"`
}
type GetLastError struct {
Wtimeouts float64 `bson:"wtimeouts"`
Num float64 `bson:"wtime.num"`
TotalMillis float64 `bson:"wtime.totalMillis"`
}
type ReplMetrics struct {
Batches *MetricStats `bson:"apply.batches"`
BufferSizeBytes float64 `bson:"buffer.sizeBytes"`
BufferCount float64 `bson:"buffer.count"`
BufferMaxSizeBytes float64 `bson:"buffer.maxSizeBytes"`
Network *ReplNetwork `bson:"network"`
Ops float64 `bson:"apply.ops"`
PreloadDocs *MetricStats `bson:"preload.docs"`
PreloadIndexes *MetricStats `bson:"preload.indexes"`
}
type Storage struct {
BucketExhausted float64 `bson:"freelist.search.bucketExhausted"`
Requests float64 `bson:"freelist.search.requests"`
Scanned float64 `bson:"freelist.search.scanned"`
}
type MetricStats struct {
Num float64 `bson:"num"`
TotalMillis float64 `bson:"totalMillis"`
}
type ReplNetwork struct {
Getmores *MetricStats `bson:"getmores"`
Ops float64 `bson:"ops"`
ReadersCreated float64 `bson:"readersCreated"`
Bytes float64 `bson:"bytes"`
}
type Operation struct {
Fastmod float64 `bson:"fastmod"`
Idhack float64 `bson:"idhack"`
ScanAndOrder float64 `bson:"scanAndOrder"`
WriteConflicts float64 `bson:"writeConflicts"`
}
type QueryExecutor struct {
Scanned float64 `bson:"scanned"`
ScannedObjects float64 `bson:"scannedObjects"`
}
type Ttl struct {
DeletedDocuments float64 `bson:"deletedDocuments"`
Passes float64 `bson:"passes"`
}

View File

@@ -0,0 +1,7 @@
package proto
type Network struct {
BytesIn float64 `bson:"bytesIn"`
BytesOut float64 `bson:"bytesOut"`
NumRequests float64 `bson:"numRequests"`
}

View File

@@ -0,0 +1,10 @@
package proto
type Opcounters struct {
Command float64 `bson:"command"`
Delete float64 `bson:"delete"`
Getmore float64 `bson:"getmore"`
Insert float64 `bson:"insert"`
Query float64 `bson:"query"`
Update float64 `bson:"update"`
}

View File

@@ -0,0 +1,74 @@
package proto
import (
"time"
"gopkg.in/mgo.v2/bson"
)
type OplogEntry struct {
Name string
Options struct {
Capped bool
Size int64
AutoIndexId bool
}
}
type OplogInfo struct {
Hostname string
Size int64
UsedMB int64
TimeDiff int64
TimeDiffHours float64
Running string // TimeDiffHours in human readable format
TFirst time.Time
TLast time.Time
Now time.Time
ElectionTime time.Time
}
type OpLogs []OplogInfo
func (s OpLogs) Len() int {
return len(s)
}
func (s OpLogs) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s OpLogs) Less(i, j int) bool {
return s[i].TimeDiffHours < s[j].TimeDiffHours
}
type OplogRow struct {
H int64 `bson:"h"`
V int64 `bson:"v"`
Op string `bson:"op"`
O bson.M `bson:"o"`
Ts int64 `bson:"ts"`
}
type OplogColStats struct {
NumExtents int
IndexDetails bson.M
Nindexes int
TotalIndexSize int64
Size int64
PaddingFactorNote string
Capped bool
MaxSize int64
IndexSizes bson.M
GleStats struct {
LastOpTime int64
ElectionId string
} `bson:"$gleStats"`
StorageSize int64
PaddingFactor int64
AvgObjSize int64
LastExtentSize int64
UserFlags int64
Max int64
Ok int
Ns string
Count int64
}

View File

@@ -0,0 +1,13 @@
package proto
type Repl struct {
Rbid float64 `bson:"rbid"`
SetVersion float64 `bson:"setVersion"`
ElectionId string `bson:"electionId"`
Primary string `bson:"primary"`
Me string `bson:"me"`
Secondary bool `bson:"secondary"`
SetName string `bson:"setName"`
Hosts []string `bson:"hosts"`
Ismaster bool `bson:"ismaster"`
}

View File

@@ -0,0 +1,34 @@
package proto
type Optime struct {
Ts float64 `bson:"ts"` // the Timestamp of the last operation applied to this member of the replica set from the oplog.
T float64 `bson:"t"` //the term in which the last applied operation was originally generated on the primary.
}
type Members struct {
Optime *Optime `bson:"optime"` // See Optime struct
OptimeDate string `bson:"optimeDate"` //the last entry from the oplog that this member applied.
InfoMessage string `bson:"infoMessage"` // A message
Id int64 `bson:"_id"` // Server ID
Name string `bson:"name"` // server name
Health float64 `bson:"health"` // This field conveys if the member is up (i.e. 1) or down (i.e. 0).
StateStr string `bson:"stateStr"` // A string that describes state.
Uptime float64 `bson:"uptime"` // number of seconds that this member has been online.
ConfigVersion float64 `bson:"configVersion"` // revision # of the replica set configuration object from previous iterations of the configuration.
Self bool `bson:"self"` // true if this is the server we are currently connected
State float64 `bson:"state"` // integer between 0 and 10 that represents the replica state of the member.
ElectionTime int64 `bson:"electionTime"` // For the current primary, information regarding the election Timestamp from the operation log.
ElectionDate string `bson:"electionDate"` // For the current primary, an ISODate formatted date string that reflects the election date
Set string `bson:"-"`
}
// Struct for replSetGetStatus
type ReplicaSetStatus struct {
Date string `bson:"date"` // Current date
MyState float64 `bson:"myState"` // integer between 0 and 10 that represents the replica state of the current member
Term float64 `bson:"term"` // The election count for the replica set, as known to this replica set member. Mongo 3.2+
HeartbeatIntervalMillis float64 `bson:"heartbeatIntervalMillis"` // The frequency in milliseconds of the heartbeats. 3.2+
Members []Members `bson:"members"` //
Ok float64 `bson:"ok"` //
Set string `bson:"set"` // Replica set name
}

View File

@@ -0,0 +1,189 @@
package proto
import "time"
type ServerStatus struct {
Host string `bson:"host"`
Version string `bson:"version"`
Process string `bson:"process"`
Pid int64 `bson:"pid"`
Uptime int64 `bson:"uptime"`
UptimeMillis int64 `bson:"uptimeMillis"`
UptimeEstimate int64 `bson:"uptimeEstimate"`
LocalTime time.Time `bson:"localTime"`
Asserts map[string]int64 `bson:"asserts"`
BackgroundFlushing *FlushStats `bson:"backgroundFlushing"`
ExtraInfo *ExtraInfo `bson:"extra_info"`
Connections *ConnectionStats `bson:"connections"`
Dur *DurStats `bson:"dur"`
GlobalLock *GlobalLockStats `bson:"globalLock"`
Locks map[string]LockStats `bson:"locks,omitempty"`
Network *NetworkStats `bson:"network"`
Opcounters *OpcountStats `bson:"opcounters"`
OpcountersRepl *OpcountStats `bson:"opcountersRepl"`
RecordStats *DBRecordStats `bson:"recordStats"`
Mem *MemStats `bson:"mem"`
Repl *ReplStatus `bson:"repl"`
ShardCursorType map[string]interface{} `bson:"shardCursorType"`
StorageEngine map[string]string `bson:"storageEngine"`
WiredTiger *WiredTiger `bson:"wiredTiger"`
}
// WiredTiger stores information related to the WiredTiger storage engine.
type WiredTiger struct {
Transaction TransactionStats `bson:"transaction"`
Concurrent ConcurrentTransactions `bson:"concurrentTransactions"`
Cache CacheStats `bson:"cache"`
}
type ConcurrentTransactions struct {
Write ConcurrentTransStats `bson:"write"`
Read ConcurrentTransStats `bson:"read"`
}
type ConcurrentTransStats struct {
Out int64 `bson:"out"`
}
// CacheStats stores cache statistics for WiredTiger.
type CacheStats struct {
TrackedDirtyBytes int64 `bson:"tracked dirty bytes in the cache"`
CurrentCachedBytes int64 `bson:"bytes currently in the cache"`
MaxBytesConfigured int64 `bson:"maximum bytes configured"`
}
// TransactionStats stores transaction checkpoints in WiredTiger.
type TransactionStats struct {
TransCheckpoints int64 `bson:"transaction checkpoints"`
}
// ReplStatus stores data related to replica sets.
type ReplStatus struct {
SetName string `bson:"setName"`
IsMaster interface{} `bson:"ismaster"`
Secondary interface{} `bson:"secondary"`
IsReplicaSet interface{} `bson:"isreplicaset"`
ArbiterOnly interface{} `bson:"arbiterOnly"`
Hosts []string `bson:"hosts"`
Passives []string `bson:"passives"`
Me string `bson:"me"`
}
// DBRecordStats stores data related to memory operations across databases.
type DBRecordStats struct {
AccessesNotInMemory int64 `bson:"accessesNotInMemory"`
PageFaultExceptionsThrown int64 `bson:"pageFaultExceptionsThrown"`
DBRecordAccesses map[string]RecordAccesses `bson:",inline"`
}
// RecordAccesses stores data related to memory operations scoped to a database.
type RecordAccesses struct {
AccessesNotInMemory int64 `bson:"accessesNotInMemory"`
PageFaultExceptionsThrown int64 `bson:"pageFaultExceptionsThrown"`
}
// MemStats stores data related to memory statistics.
type MemStats struct {
Bits int64 `bson:"bits"`
Resident int64 `bson:"resident"`
Virtual int64 `bson:"virtual"`
Supported interface{} `bson:"supported"`
Mapped int64 `bson:"mapped"`
MappedWithJournal int64 `bson:"mappedWithJournal"`
}
// FlushStats stores information about memory flushes.
type FlushStats struct {
Flushes int64 `bson:"flushes"`
TotalMs int64 `bson:"total_ms"`
AverageMs float64 `bson:"average_ms"`
LastMs int64 `bson:"last_ms"`
LastFinished time.Time `bson:"last_finished"`
}
// ConnectionStats stores information related to incoming database connections.
type ConnectionStats struct {
Current int64 `bson:"current"`
Available int64 `bson:"available"`
TotalCreated int64 `bson:"totalCreated"`
}
// DurTiming stores information related to journaling.
type DurTiming struct {
Dt int64 `bson:"dt"`
PrepLogBuffer int64 `bson:"prepLogBuffer"`
WriteToJournal int64 `bson:"writeToJournal"`
WriteToDataFiles int64 `bson:"writeToDataFiles"`
RemapPrivateView int64 `bson:"remapPrivateView"`
}
// DurStats stores information related to journaling statistics.
type DurStats struct {
Commits int64 `bson:"commits"`
JournaledMB int64 `bson:"journaledMB"`
WriteToDataFilesMB int64 `bson:"writeToDataFilesMB"`
Compression int64 `bson:"compression"`
CommitsInWriteLock int64 `bson:"commitsInWriteLock"`
EarlyCommits int64 `bson:"earlyCommits"`
TimeMs DurTiming
}
// QueueStats stores the number of queued read/write operations.
type QueueStats struct {
Total int64 `bson:"total"`
Readers int64 `bson:"readers"`
Writers int64 `bson:"writers"`
}
// ClientStats stores the number of active read/write operations.
type ClientStats struct {
Total int64 `bson:"total"`
Readers int64 `bson:"readers"`
Writers int64 `bson:"writers"`
}
// GlobalLockStats stores information related locks in the MMAP storage engine.
type GlobalLockStats struct {
TotalTime int64 `bson:"totalTime"`
LockTime int64 `bson:"lockTime"`
CurrentQueue *QueueStats `bson:"currentQueue"`
ActiveClients *ClientStats `bson:"activeClients"`
}
// NetworkStats stores information related to network traffic.
type NetworkStats struct {
BytesIn int64 `bson:"bytesIn"`
BytesOut int64 `bson:"bytesOut"`
NumRequests int64 `bson:"numRequests"`
}
// OpcountStats stores information related to comamnds and basic CRUD operations.
type OpcountStats struct {
Insert int64 `bson:"insert"`
Query int64 `bson:"query"`
Update int64 `bson:"update"`
Delete int64 `bson:"delete"`
GetMore int64 `bson:"getmore"`
Command int64 `bson:"command"`
}
// ReadWriteLockTimes stores time spent holding read/write locks.
type ReadWriteLockTimes struct {
Read int64 `bson:"R"`
Write int64 `bson:"W"`
ReadLower int64 `bson:"r"`
WriteLower int64 `bson:"w"`
}
// LockStats stores information related to time spent acquiring/holding locks
// for a given database.
type LockStats struct {
TimeLockedMicros ReadWriteLockTimes `bson:"timeLockedMicros"`
TimeAcquiringMicros ReadWriteLockTimes `bson:"timeAcquiringMicros"`
// AcquireCount and AcquireWaitCount are new fields of the lock stats only populated on 3.0 or newer.
// Typed as a pointer so that if it is nil, mongostat can assume the field is not populated
// with real namespace data.
AcquireCount *ReadWriteLockTimes `bson:"acquireCount,omitempty"`
AcquireWaitCount *ReadWriteLockTimes `bson:"acquireWaitCount,omitempty"`
}

View File

@@ -0,0 +1,15 @@
package proto
type ShardingChangelogSummaryId struct {
Event string `bson:"event"`
Note string `bson:"note"`
}
type ShardingChangelogSummary struct {
Id *ShardingChangelogSummaryId `bson:"_id"`
Count float64 `bson:"count"`
}
type ShardingChangelogStats struct {
Items *[]ShardingChangelogSummary
}

View File

@@ -0,0 +1,11 @@
package proto
type Shard struct {
ID string `bson:"_id"`
Host string `bson:"host"`
}
type ShardsInfo struct {
Shards []Shard `bson:"shards"`
OK int `bson:"ok"`
}

View File

@@ -0,0 +1,79 @@
package proto
import "time"
type SystemProfile struct {
//AllUsers []interface{} `bson:"allUsers"`
Client string `bson:"client"`
CursorExhausted bool `bson:"cursorExhausted"`
DocsExamined int `bson:"docsExamined"`
ExecStats struct {
Advanced int `bson:"advanced"`
ExecutionTimeMillisEstimate int `bson:"executionTimeMillisEstimate"`
InputStage struct {
Advanced int `bson:"advanced"`
Direction string `bson:"direction"`
DocsExamined int `bson:"docsExamined"`
ExecutionTimeMillisEstimate int `bson:"executionTimeMillisEstimate"`
Filter struct {
Date struct {
Eq string `bson:"$eq"`
} `bson:"date"`
} `bson:"filter"`
Invalidates int `bson:"invalidates"`
IsEOF int `bson:"isEOF"`
NReturned int `bson:"nReturned"`
NeedTime int `bson:"needTime"`
NeedYield int `bson:"needYield"`
RestoreState int `bson:"restoreState"`
SaveState int `bson:"saveState"`
Stage string `bson:"stage"`
Works int `bson:"works"`
} `bson:"inputStage"`
Invalidates int `bson:"invalidates"`
IsEOF int `bson:"isEOF"`
LimitAmount int `bson:"limitAmount"`
NReturned int `bson:"nReturned"`
NeedTime int `bson:"needTime"`
NeedYield int `bson:"needYield"`
RestoreState int `bson:"restoreState"`
SaveState int `bson:"saveState"`
Stage string `bson:"stage"`
Works int `bson:"works"`
} `bson:"execStats"`
KeyUpdates int `bson:"keyUpdates"`
KeysExamined int `bson:"keysExamined"`
Locks struct {
Collection struct {
AcquireCount struct {
R int `bson:"R"`
} `bson:"acquireCount"`
} `bson:"Collection"`
Database struct {
AcquireCount struct {
R int `bson:"r"`
} `bson:"acquireCount"`
} `bson:"Database"`
Global struct {
AcquireCount struct {
R int `bson:"r"`
} `bson:"acquireCount"`
} `bson:"Global"`
MMAPV1Journal struct {
AcquireCount struct {
R int `bson:"r"`
} `bson:"acquireCount"`
} `bson:"MMAPV1Journal"`
} `bson:"locks"`
Millis int `bson:"millis"`
Nreturned int `bson:"nreturned"`
Ns string `bson:"ns"`
NumYield int `bson:"numYield"`
Op string `bson:"op"`
Protocol string `bson:"protocol"`
Query map[string]interface{} `bson:"query"`
ResponseLength int `bson:"responseLength"`
Ts time.Time `bson:"ts"`
User string `bson:"user"`
WriteConflicts int `bson:"writeConflicts"`
}

View File

@@ -0,0 +1,656 @@
package main
import (
"crypto/md5"
"fmt"
"html/template"
"log"
"os"
"sort"
"strings"
"time"
"github.com/howeyc/gopass"
"github.com/montanaflynn/stats"
"github.com/pborman/getopt"
"github.com/percona/toolkit-go/mongolib/proto"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
var (
Version string
Build string
)
type iter interface {
All(result interface{}) error
Close() error
Err() error
For(result interface{}, f func() error) (err error)
Next(result interface{}) bool
Timeout() bool
}
type options struct {
AuthDB string
Database string
Debug bool
Help bool
Host string
Limit int
OrderBy []string
Password string
User string
Version bool
}
const (
MAX_DEPTH_LEVEL = 10
)
type statsArray []stat
func (a statsArray) Len() int { return len(a) }
func (a statsArray) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a statsArray) Less(i, j int) bool { return a[i].Count < a[j].Count }
type times []time.Time
func (a times) Len() int { return len(a) }
func (a times) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a times) Less(i, j int) bool { return a[i].Before(a[j]) }
type stat struct {
ID string
Fingerprint string
Namespace string
Query map[string]interface{}
Count int
TableScan bool
NScanned []float64
NReturned []float64
QueryTime []float64 // in milliseconds
ResponseLength []float64
LockTime times
BlockedTime times
FirstSeen time.Time
LastSeen time.Time
}
type groupKey struct {
Fingerprint string
Namespace string
}
type statistics struct {
Pct float64
Total float64
Min float64
Max float64
Avg float64
Pct95 float64
StdDev float64
Median float64
}
type queryInfo struct {
Rank int
ID string
Count int
Ratio float64
QPS float64
Fingerprint string
Namespace string
Scanned statistics
Returned statistics
QueryTime statistics
ResponseLength statistics
FirstSeen time.Time
LastSeen time.Time
}
func main() {
opts, err := getOptions()
if err != nil {
log.Printf("error processing commad line arguments: %s", err)
os.Exit(1)
}
if opts.Help {
getopt.Usage()
return
}
if opts.Version {
fmt.Println("pt-mongodb-summary")
fmt.Printf("Version %s\n", Version)
fmt.Printf("Build: %s\n", Build)
return
}
di := getDialInfo(opts)
if di.Database == "" {
log.Printf("must indicate a database")
getopt.PrintUsage(os.Stderr)
os.Exit(2)
}
session, err := mgo.DialWithInfo(di)
if err != nil {
log.Printf("error connecting to the db %s", err)
os.Exit(3)
}
i := session.DB(di.Database).C("system.profile").Find(bson.M{"op": bson.M{"$nin": []string{"getmore", "delete"}}}).Sort("-$natural").Iter()
queries := sortQueries(getData(i), opts.OrderBy)
uptime := uptime(session)
queryTotals := aggregateQueryStats(queries, uptime)
tt, _ := template.New("query").Funcs(template.FuncMap{
"Format": format,
}).Parse(getTotalsTemplate())
tt.Execute(os.Stdout, queryTotals)
queryStats := calcQueryStats(queries, uptime)
t, _ := template.New("query").Funcs(template.FuncMap{
"Format": format,
}).Parse(getQueryTemplate())
if opts.Limit > 0 && len(queryStats) > opts.Limit {
queryStats = queryStats[:opts.Limit]
}
for _, qs := range queryStats {
t.Execute(os.Stdout, qs)
}
}
// format scales a number and returns a string made of the scaled value and unit (K=Kilo, M=Mega, T=Tera)
// using I.F where i is the number of digits for the integer part and F is the number of digits for the
// decimal part
// Examples:
// format(1000, 5.0) will return 1K
// format(1000, 5.2) will return 1.00k
func format(val float64, size float64) string {
units := []string{"K", "M", "T"}
unit := " "
intSize := int64(size)
decSize := int64((size - float64(intSize)) * 10)
for i := 0; i < 3; i++ {
if val > 1000 {
val /= 1000
unit = units[i]
}
}
pfmt := fmt.Sprintf("%% %d.%df", intSize, decSize)
fval := fmt.Sprintf(pfmt, val)
return fmt.Sprintf("%s%s", fval, unit)
}
func uptime(session *mgo.Session) int64 {
ss := proto.ServerStatus{}
if err := session.Ping(); err != nil {
return 0
}
if err := session.DB("admin").Run(bson.D{{"serverStatus", 1}, {"recordStats", 1}}, &ss); err != nil {
return 0
}
return ss.Uptime
}
func aggregateQueryStats(queries []stat, uptime int64) queryInfo {
qi := queryInfo{}
qs := stat{}
_, totalScanned, totalReturned, totalQueryTime, totalBytes := calcTotals(queries)
for _, query := range queries {
qs.NScanned = append(qs.NScanned, query.NScanned...)
qs.NReturned = append(qs.NReturned, query.NReturned...)
qs.QueryTime = append(qs.QueryTime, query.QueryTime...)
qs.ResponseLength = append(qs.ResponseLength, query.ResponseLength...)
qi.Count += query.Count
}
qi.Scanned = calcStats(qs.NScanned)
qi.Returned = calcStats(qs.NReturned)
qi.QueryTime = calcStats(qs.QueryTime)
qi.ResponseLength = calcStats(qs.ResponseLength)
qi.QPS = float64(int64(qs.Count) / uptime)
if totalScanned > 0 {
qi.Scanned.Pct = qi.Scanned.Total * 100 / totalScanned
}
if totalReturned > 0 {
qi.Returned.Pct = qi.Returned.Total * 100 / totalReturned
}
if totalQueryTime > 0 {
qi.QueryTime.Pct = qi.QueryTime.Total * 100 / totalQueryTime
}
if totalBytes > 0 {
qi.ResponseLength.Pct = qi.ResponseLength.Total / totalBytes
}
if qi.Returned.Total > 0 {
qi.Ratio = qi.Scanned.Total / qi.Returned.Total
}
return qi
}
func calcQueryStats(queries []stat, uptime int64) []queryInfo {
queryStats := []queryInfo{}
_, totalScanned, totalReturned, totalQueryTime, totalBytes := calcTotals(queries)
for rank, query := range queries {
qi := queryInfo{
Rank: rank,
Count: query.Count,
ID: query.ID,
Fingerprint: query.Fingerprint,
Scanned: calcStats(query.NScanned),
Returned: calcStats(query.NReturned),
QueryTime: calcStats(query.QueryTime),
ResponseLength: calcStats(query.ResponseLength),
FirstSeen: query.FirstSeen,
LastSeen: query.LastSeen,
Namespace: query.Namespace,
QPS: float64(int64(query.Count) / uptime),
}
if totalScanned > 0 {
qi.Scanned.Pct = qi.Scanned.Total * 100 / totalScanned
}
if totalReturned > 0 {
qi.Returned.Pct = qi.Returned.Total * 100 / totalReturned
}
if totalQueryTime > 0 {
qi.QueryTime.Pct = qi.QueryTime.Total * 100 / totalQueryTime
}
if totalBytes > 0 {
qi.ResponseLength.Pct = qi.ResponseLength.Total / totalBytes
}
if qi.Returned.Total > 0 {
qi.Ratio = qi.Scanned.Total / qi.Returned.Total
}
queryStats = append(queryStats, qi)
}
return queryStats
}
func getTotals(queries []stat) stat {
qt := stat{}
for _, query := range queries {
qt.NScanned = append(qt.NScanned, query.NScanned...)
qt.NReturned = append(qt.NReturned, query.NReturned...)
qt.QueryTime = append(qt.QueryTime, query.QueryTime...)
qt.ResponseLength = append(qt.ResponseLength, query.ResponseLength...)
}
return qt
}
func calcTotals(queries []stat) (totalCount int, totalScanned, totalReturned, totalQueryTime, totalBytes float64) {
for _, query := range queries {
totalCount += query.Count
scanned, _ := stats.Sum(query.NScanned)
totalScanned += scanned
returned, _ := stats.Sum(query.NReturned)
totalReturned += returned
queryTime, _ := stats.Sum(query.QueryTime)
totalQueryTime += queryTime
bytes, _ := stats.Sum(query.ResponseLength)
totalBytes += bytes
}
return
}
func calcStats(samples []float64) statistics {
var s statistics
s.Total, _ = stats.Sum(samples)
s.Min, _ = stats.Min(samples)
s.Max, _ = stats.Max(samples)
s.Avg, _ = stats.Mean(samples)
s.Pct95, _ = stats.PercentileNearestRank(samples, 95)
s.StdDev, _ = stats.StandardDeviation(samples)
s.Median, _ = stats.Median(samples)
return s
}
func getData(i iter) []stat {
var doc proto.SystemProfile
stats := make(map[groupKey]*stat)
for i.Next(&doc) && i.Err() == nil {
if len(doc.Query) > 0 {
query := doc.Query
if squery, ok := doc.Query["$query"]; ok {
if ssquery, ok := squery.(map[string]interface{}); ok {
query = ssquery
}
}
fp := fingerprint(query)
var s *stat
var ok bool
key := groupKey{
Fingerprint: fp,
Namespace: doc.Ns,
}
if s, ok = stats[key]; !ok {
s = &stat{
ID: fmt.Sprintf("%x", md5.Sum([]byte(fp+doc.Ns))),
Fingerprint: fp,
Namespace: doc.Ns,
TableScan: false,
Query: query,
}
stats[key] = s
}
s.Count++
s.NScanned = append(s.NScanned, float64(doc.DocsExamined))
s.NReturned = append(s.NReturned, float64(doc.Nreturned))
s.QueryTime = append(s.QueryTime, float64(doc.Millis))
s.ResponseLength = append(s.ResponseLength, float64(doc.ResponseLength))
var zeroTime time.Time
if s.FirstSeen == zeroTime || s.FirstSeen.After(doc.Ts) {
s.FirstSeen = doc.Ts
}
if s.LastSeen == zeroTime || s.LastSeen.Before(doc.Ts) {
s.LastSeen = doc.Ts
}
}
}
// We need to sort the data but a hash cannot be sorted so, convert the hash having
// the results to a slice
sa := statsArray{}
for _, s := range stats {
sa = append(sa, *s)
}
sort.Sort(sa)
return sa
}
func getOptions() (*options, error) {
opts := &options{Host: "localhost:27017"}
getopt.BoolVarLong(&opts.Help, "help", '?', "Show help")
getopt.BoolVarLong(&opts.Version, "version", 'v', "", "show version & exit")
getopt.IntVarLong(&opts.Limit, "limit", 'l', "show the first n queries")
getopt.ListVarLong(&opts.OrderBy, "order-by", 'o', "comma separated list of order by fields (max values): count,ratio,query-time,docs-scanned,docs-returned. - in front of the field name denotes reverse order.")
getopt.StringVarLong(&opts.AuthDB, "authenticationDatabase", 'a', "admin", "database used to establish credentials and privileges with a MongoDB server")
getopt.StringVarLong(&opts.Database, "database", 'd', "", "database to profile")
getopt.StringVarLong(&opts.Password, "password", 'p', "", "password").SetOptional()
getopt.StringVarLong(&opts.User, "user", 'u', "", "username")
getopt.SetParameters("host[:port][/database]")
getopt.Parse()
if opts.Help {
return opts, nil
}
args := getopt.Args() // host is a positional arg
if len(args) > 0 {
opts.Host = args[0]
}
if getopt.IsSet("order-by") {
validFields := []string{"count", "ratio", "query-time", "docs-scanned", "docs-returned"}
for _, field := range opts.OrderBy {
valid := false
for _, vf := range validFields {
if field == vf || field == "-"+vf {
valid = true
}
}
if !valid {
return nil, fmt.Errorf("invalid sort field '%q'", field)
}
}
} else {
opts.OrderBy = []string{"count"}
}
if getopt.IsSet("password") && opts.Password == "" {
print("Password: ")
pass, err := gopass.GetPasswd()
if err != nil {
return nil, err
}
opts.Password = string(pass)
}
return opts, nil
}
func getDialInfo(opts *options) *mgo.DialInfo {
di, _ := mgo.ParseURL(opts.Host)
di.FailFast = true
if getopt.IsSet("user") {
di.Username = opts.User
}
if getopt.IsSet("password") {
di.Password = opts.Password
}
if getopt.IsSet("authenticationDatabase") {
di.Source = opts.AuthDB
}
if getopt.IsSet("database") {
di.Database = opts.Database
}
return di
}
func fingerprint(query map[string]interface{}) string {
return strings.Join(keys(query, 0), ",")
}
func keys(query map[string]interface{}, level int) []string {
ks := []string{}
for key, value := range query {
ks = append(ks, key)
if m, ok := value.(map[string]interface{}); ok {
level++
if level <= MAX_DEPTH_LEVEL {
ks = append(ks, keys(m, level)...)
}
}
}
sort.Strings(ks)
return ks
}
func getQueryTemplate() string {
t := `
# Query {{.Rank}}: {{printf "% 0.2f" .QPS}} QPS, ID {{.ID}}
# Ratio {{Format .Ratio 7.2}} (docs scanned/returned)
# Time range: {{.FirstSeen}} to {{.LastSeen}}
# Attribute pct total min max avg 95% stddev median
# ================== === ======== ======== ======== ======== ======== ======= ========
# Count (docs) {{printf "% 7d " .Count}}
# Exec Time ms {{printf "% 4.0f" .QueryTime.Pct}} {{printf "% 7.0f " .QueryTime.Total}} {{printf "% 7.0f " .QueryTime.Min}} {{printf "% 7.0f " .QueryTime.Max}} {{printf "% 7.0f " .QueryTime.Avg}} {{printf "% 7.0f " .QueryTime.Pct95}} {{printf "% 7.0f " .QueryTime.StdDev}} {{printf "% 7.0f " .QueryTime.Median}}
# Docs Scanned {{printf "% 4.0f" .Scanned.Pct}} {{Format .Scanned.Total 7.2}} {{Format .Scanned.Min 7.2}} {{Format .Scanned.Max 7.2}} {{Format .Scanned.Avg 7.2}} {{Format .Scanned.Pct95 7.2}} {{Format .Scanned.StdDev 7.2}} {{Format .Scanned.Median 7.2}}
# Docs Returned {{printf "% 4.0f" .Returned.Pct}} {{Format .Returned.Total 7.2}} {{Format .Returned.Min 7.2}} {{Format .Returned.Max 7.2}} {{Format .Returned.Avg 7.2}} {{Format .Returned.Pct95 7.2}} {{Format .Returned.StdDev 7.2}} {{Format .Returned.Median 7.2}}
# Bytes recv {{printf "% 4.0f" .ResponseLength.Pct}} {{Format .ResponseLength.Total 7.2}} {{Format .ResponseLength.Min 7.2}} {{Format .ResponseLength.Max 7.2}} {{Format .ResponseLength.Avg 7.2}} {{Format .ResponseLength.Pct95 7.2}} {{Format .ResponseLength.StdDev 7.2}} {{Format .ResponseLength.Median 7.2}}
# String:
# Namespaces {{.Namespace}}
# Fingerprint {{.Fingerprint}}
`
return t
}
func getTotalsTemplate() string {
t := `
pt-query profile
# Totals
# Ratio {{Format .Ratio 7.2}} (docs scanned/returned)
# Attribute pct total min max avg 95% stddev median
# ================== === ======== ======== ======== ======== ======== ======= ========
# Count (docs) {{printf "% 7d " .Count}}
# Exec Time ms {{printf "% 4.0f" .QueryTime.Pct}} {{printf "% 7.0f " .QueryTime.Total}} {{printf "% 7.0f " .QueryTime.Min}} {{printf "% 7.0f " .QueryTime.Max}} {{printf "% 7.0f " .QueryTime.Avg}} {{printf "% 7.0f " .QueryTime.Pct95}} {{printf "% 7.0f " .QueryTime.StdDev}} {{printf "% 7.0f " .QueryTime.Median}}
# Docs Scanned {{printf "% 4.0f" .Scanned.Pct}} {{Format .Scanned.Total 7.2}} {{Format .Scanned.Min 7.2}} {{Format .Scanned.Max 7.2}} {{Format .Scanned.Avg 7.2}} {{Format .Scanned.Pct95 7.2}} {{Format .Scanned.StdDev 7.2}} {{Format .Scanned.Median 7.2}}
# Docs Returned {{printf "% 4.0f" .Returned.Pct}} {{Format .Returned.Total 7.2}} {{Format .Returned.Min 7.2}} {{Format .Returned.Max 7.2}} {{Format .Returned.Avg 7.2}} {{Format .Returned.Pct95 7.2}} {{Format .Returned.StdDev 7.2}} {{Format .Returned.Median 7.2}}
# Bytes recv {{printf "% 4.0f" .ResponseLength.Pct}} {{Format .ResponseLength.Total 7.2}} {{Format .ResponseLength.Min 7.2}} {{Format .ResponseLength.Max 7.2}} {{Format .ResponseLength.Avg 7.2}} {{Format .ResponseLength.Pct95 7.2}} {{Format .ResponseLength.StdDev 7.2}} {{Format .ResponseLength.Median 7.2}}
#
`
return t
}
type lessFunc func(p1, p2 *stat) bool
type multiSorter struct {
queries []stat
less []lessFunc
}
// Sort sorts the argument slice according to the less functions passed to OrderedBy.
func (ms *multiSorter) Sort(queries []stat) {
ms.queries = queries
sort.Sort(ms)
}
// OrderedBy returns a Sorter that sorts using the less functions, in order.
// Call its Sort method to sort the data.
func OrderedBy(less ...lessFunc) *multiSorter {
return &multiSorter{
less: less,
}
}
// Len is part of sort.Interface.
func (ms *multiSorter) Len() int {
return len(ms.queries)
}
// Swap is part of sort.Interface.
func (ms *multiSorter) Swap(i, j int) {
ms.queries[i], ms.queries[j] = ms.queries[j], ms.queries[i]
}
// Less is part of sort.Interface. It is implemented by looping along the
// less functions until it finds a comparison that is either Less or
// !Less. Note that it can call the less functions twice per call. We
// could change the functions to return -1, 0, 1 and reduce the
// number of calls for greater efficiency: an exercise for the reader.
func (ms *multiSorter) Less(i, j int) bool {
p, q := &ms.queries[i], &ms.queries[j]
// Try all but the last comparison.
var k int
for k = 0; k < len(ms.less)-1; k++ {
less := ms.less[k]
switch {
case less(p, q):
// p < q, so we have a decision.
return true
case less(q, p):
// p > q, so we have a decision.
return false
}
// p == q; try the next comparison.
}
// All comparisons to here said "equal", so just return whatever
// the final comparison reports.
return ms.less[k](p, q)
}
func sortQueries(queries []stat, orderby []string) []stat {
sortFuncs := []lessFunc{}
for _, field := range orderby {
var f lessFunc
switch field {
//
case "count":
f = func(c1, c2 *stat) bool {
return c1.Count < c2.Count
}
case "-count":
f = func(c1, c2 *stat) bool {
return c1.Count > c2.Count
}
case "ratio":
f = func(c1, c2 *stat) bool {
ns1, _ := stats.Max(c1.NScanned)
ns2, _ := stats.Max(c2.NScanned)
nr1, _ := stats.Max(c1.NReturned)
nr2, _ := stats.Max(c2.NReturned)
ratio1 := ns1 / nr1
ratio2 := ns2 / nr2
return ratio1 < ratio2
}
case "-ratio":
f = func(c1, c2 *stat) bool {
ns1, _ := stats.Max(c1.NScanned)
ns2, _ := stats.Max(c2.NScanned)
nr1, _ := stats.Max(c1.NReturned)
nr2, _ := stats.Max(c2.NReturned)
ratio1 := ns1 / nr1
ratio2 := ns2 / nr2
return ratio1 > ratio2
}
//
case "query-time":
f = func(c1, c2 *stat) bool {
qt1, _ := stats.Max(c1.QueryTime)
qt2, _ := stats.Max(c2.QueryTime)
return qt1 < qt2
}
case "-query-time":
f = func(c1, c2 *stat) bool {
qt1, _ := stats.Max(c1.QueryTime)
qt2, _ := stats.Max(c2.QueryTime)
return qt1 > qt2
}
//
case "docs-scanned":
f = func(c1, c2 *stat) bool {
ns1, _ := stats.Max(c1.NScanned)
ns2, _ := stats.Max(c2.NScanned)
return ns1 < ns2
}
case "-docs-scanned":
f = func(c1, c2 *stat) bool {
ns1, _ := stats.Max(c1.NScanned)
ns2, _ := stats.Max(c2.NScanned)
return ns1 > ns2
}
//
case "docs-returned":
f = func(c1, c2 *stat) bool {
nr1, _ := stats.Max(c1.NReturned)
nr2, _ := stats.Max(c2.NReturned)
return nr1 < nr2
}
case "-docs-returned":
f = func(c1, c2 *stat) bool {
nr1, _ := stats.Max(c1.NReturned)
nr2, _ := stats.Max(c2.NReturned)
return nr1 > nr2
}
}
// count,query-time,docs-scanned, docs-returned. - in front of the field name denotes reverse order.")
sortFuncs = append(sortFuncs, f)
}
OrderedBy(sortFuncs...).Sort(queries)
return queries
}

View File

@@ -0,0 +1,290 @@
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"reflect"
"testing"
"time"
"github.com/percona/toolkit-go/mongolib/proto"
"gopkg.in/mgo.v2/dbtest" // mock
)
var Server dbtest.DBServer
func TestMain(m *testing.M) {
// The tempdir is created so MongoDB has a location to store its files.
// Contents are wiped once the server stops
os.Setenv("CHECK_SESSIONS", "0")
tempDir, _ := ioutil.TempDir("", "testing")
Server.SetPath(tempDir)
dat, err := ioutil.ReadFile("test/sample/system.profile.json")
if err != nil {
fmt.Printf("cannot load fixtures: %s", err)
os.Exit(1)
}
var docs []proto.SystemProfile
err = json.Unmarshal(dat, &docs)
c := Server.Session().DB("samples").C("system_profile")
for _, doc := range docs {
c.Insert(doc)
}
retCode := m.Run()
Server.Session().Close()
Server.Session().DB("samples").DropDatabase()
// Stop shuts down the temporary server and removes data on disk.
Server.Stop()
// call with result of m.Run()
os.Exit(retCode)
}
func TestCalcStats(t *testing.T) {
it := Server.Session().DB("samples").C("system_profile").Find(nil).Sort("Ts").Iter()
data := getData(it)
s := calcStats(data[0].NScanned)
want := statistics{Pct: 0, Total: 159, Min: 79, Max: 80, Avg: 79.5, Pct95: 80, StdDev: 0.5, Median: 79.5}
if !reflect.DeepEqual(s, want) {
t.Errorf("error in calcStats: got:\n%#v\nwant:\n%#v\n", s, want)
}
wantTotals := stat{
ID: "",
Fingerprint: "",
Namespace: "",
Query: map[string]interface{}(nil),
Count: 0,
TableScan: false,
NScanned: []float64{79, 80},
NReturned: []float64{79, 80},
QueryTime: []float64{27, 28},
ResponseLength: []float64{109, 110},
LockTime: nil,
BlockedTime: nil,
FirstSeen: time.Time{},
LastSeen: time.Time{},
}
totals := getTotals(data[0:1])
if !reflect.DeepEqual(totals, wantTotals) {
t.Errorf("error in calcStats: got:\n%#v\nwant:\n:%#v\n", totals, wantTotals)
}
var wantTotalCount int = 2
var wantTotalScanned, wantTotalReturned, wantTotalQueryTime, wantTotalBytes float64 = 159, 159, 55, 219
totalCount, totalScanned, totalReturned, totalQueryTime, totalBytes := calcTotals(data[0:1])
if totalCount != wantTotalCount {
t.Errorf("invalid total count. Want %v, got %v\n", wantTotalCount, totalCount)
}
if totalScanned != wantTotalScanned {
t.Errorf("invalid total count. Want %v, got %v\n", wantTotalScanned, totalScanned)
}
if totalReturned != wantTotalReturned {
t.Errorf("invalid total count. Want %v, got %v\n", wantTotalReturned, totalReturned)
}
if totalQueryTime != wantTotalQueryTime {
t.Errorf("invalid total count. Want %v, got %v\n", wantTotalQueryTime, totalQueryTime)
}
if totalBytes != wantTotalBytes {
t.Errorf("invalid total count. Want %v, got %v\n", wantTotalBytes, totalBytes)
}
}
func TestGetData(t *testing.T) {
it := Server.Session().DB("samples").C("system_profile").Find(nil).Iter()
tests := []struct {
name string
i iter
want []stat
}{
{
name: "test 1",
i: it,
want: []stat{
stat{
ID: "6c3fff4804febd156700a06f9a346162",
Fingerprint: "find,limit",
Namespace: "samples.col1",
Query: map[string]interface{}{
"find": "col1",
"limit": float64(2),
},
Count: 2,
TableScan: false,
NScanned: []float64{79, 80},
NReturned: []float64{79, 80},
QueryTime: []float64{27, 28},
ResponseLength: []float64{109, 110},
LockTime: nil,
BlockedTime: nil,
FirstSeen: time.Date(2016, time.November, 8, 13, 46, 27, 0, time.UTC).Local(),
LastSeen: time.Date(2016, time.November, 8, 13, 46, 27, 0, time.UTC).Local(),
},
stat{
ID: "fdcea004122ddb225bc56de417391e25",
Fingerprint: "find",
Namespace: "samples.col1",
Query: map[string]interface{}{"find": "col1"},
Count: 8,
TableScan: false,
NScanned: []float64{71, 72, 73, 74, 75, 76, 77, 78},
NReturned: []float64{71, 72, 73, 74, 75, 76, 77, 78},
QueryTime: []float64{19, 20, 21, 22, 23, 24, 25, 26},
ResponseLength: []float64{101, 102, 103, 104, 105, 106, 107, 108},
LockTime: nil,
BlockedTime: nil,
FirstSeen: time.Date(2016, time.November, 8, 13, 46, 27, 0, time.UTC).Local(),
LastSeen: time.Date(2016, time.November, 8, 13, 46, 27, 0, time.UTC).Local(),
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := getData(tt.i)
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("got\n%#v\nwant\n%#v", got, tt.want)
}
})
}
}
func TestUptime(t *testing.T) {
session := Server.Session()
time.Sleep(1500 * time.Millisecond)
if uptime(session) <= 0 {
t.Error("uptime is 0")
}
session.Close()
}
func TestFingerprint(t *testing.T) {
tests := []struct {
name string
query map[string]interface{}
want string
}{
{
query: map[string]interface{}{"query": map[string]interface{}{}, "orderby": map[string]interface{}{"ts": -1}},
want: "orderby,query,ts",
},
{
query: map[string]interface{}{"find": "system.profile", "filter": map[string]interface{}{}, "sort": map[string]interface{}{"$natural": 1}},
want: "$natural,filter,find,sort",
},
{
query: map[string]interface{}{"collection": "system.profile", "batchSize": 0, "getMore": 18531768265},
want: "batchSize,collection,getMore",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := fingerprint(tt.query); got != tt.want {
t.Errorf("fingerprint() = %v, want %v", got, tt.want)
}
})
}
}
func TestTimesLen(t *testing.T) {
tests := []struct {
name string
a times
want int
}{
{
name: "Times.Len",
a: []time.Time{time.Now()},
want: 1,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.a.Len(); got != tt.want {
t.Errorf("times.Len() = %v, want %v", got, tt.want)
}
})
}
}
func TestTimesSwap(t *testing.T) {
type args struct {
i int
j int
}
t1 := time.Now()
t2 := t1.Add(1 * time.Minute)
tests := []struct {
name string
a times
args args
}{
{
name: "Times.Swap",
a: times{t1, t2},
args: args{i: 0, j: 1},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.a.Swap(tt.args.i, tt.args.j)
if tt.a[0] != t2 || tt.a[1] != t1 {
t.Errorf("%s has (%v, %v) want (%v, %v)", tt.name, tt.a[0], tt.a[1], t2, t1)
}
})
}
}
func TestTimesLess(t *testing.T) {
type args struct {
i int
j int
}
t1 := time.Now()
t2 := t1.Add(1 * time.Minute)
tests := []struct {
name string
a times
args args
want bool
}{
{
name: "Times.Swap",
a: times{t1, t2},
args: args{i: 0, j: 1},
want: true,
},
{
name: "Times.Swap",
a: times{t2, t1},
args: args{i: 0, j: 1},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.a.Less(tt.args.i, tt.args.j); got != tt.want {
t.Errorf("times.Less() = %v, want %v", got, tt.want)
}
})
}
}

View File

@@ -0,0 +1,414 @@
[
{
"Query": {
"find": "col1"
},
"Ts": "2016-11-08T13:46:27.000+00:00",
"Client": "127.0.0.1",
"ExecStats": {
"ExecutionTimeMillisEstimate": 0,
"IsEOF": 0,
"NReturned": 71,
"NeedTime": 1,
"RestoreState": 2,
"Works": 78,
"DocsExamined": 71,
"Direction": "forward",
"Invalidates": 0,
"NeedYield": 2,
"SaveState": 3,
"Stage": "COLLSCAN",
"Advanced": 70
},
"Ns": "samples.col1",
"Op": "query",
"WriteConflicts": 0,
"KeyUpdates": 0,
"KeysExamined": 0,
"Locks": {
"Global": "",
"MMAPV1Journal": "",
"Database": "",
"Collection": "",
"Metadata": "",
"Oplog": ""
},
"Nreturned": 71,
"ResponseLength": 101,
"DocsExamined": 71,
"Millis": 19,
"NumYield": 2,
"User": ""
},
{
"Query": {
"find": "col1"
},
"Ts": "2016-11-08T13:46:27.000+00:00",
"Client": "127.0.0.1",
"ExecStats": {
"ExecutionTimeMillisEstimate": 0,
"IsEOF": 0,
"NReturned": 72,
"NeedTime": 1,
"RestoreState": 2,
"Works": 78,
"DocsExamined": 72,
"Direction": "forward",
"Invalidates": 0,
"NeedYield": 2,
"SaveState": 3,
"Stage": "COLLSCAN",
"Advanced": 70
},
"Ns": "samples.col1",
"Op": "query",
"WriteConflicts": 0,
"KeyUpdates": 0,
"KeysExamined": 0,
"Locks": {
"Global": "",
"MMAPV1Journal": "",
"Database": "",
"Collection": "",
"Metadata": "",
"Oplog": ""
},
"Nreturned": 72,
"ResponseLength": 102,
"DocsExamined": 72,
"Millis": 20,
"NumYield": 2,
"User": ""
},
{
"Query": {
"find": "col1"
},
"Ts": "2016-11-08T13:46:27.000+00:00",
"Client": "127.0.0.1",
"ExecStats": {
"ExecutionTimeMillisEstimate": 0,
"IsEOF": 0,
"NReturned": 73,
"NeedTime": 1,
"RestoreState": 2,
"Works": 78,
"DocsExamined": 73,
"Direction": "forward",
"Invalidates": 0,
"NeedYield": 2,
"SaveState": 3,
"Stage": "COLLSCAN",
"Advanced": 70
},
"Ns": "samples.col1",
"Op": "query",
"WriteConflicts": 0,
"KeyUpdates": 0,
"KeysExamined": 0,
"Locks": {
"Global": "",
"MMAPV1Journal": "",
"Database": "",
"Collection": "",
"Metadata": "",
"Oplog": ""
},
"Nreturned": 73,
"ResponseLength": 103,
"DocsExamined": 73,
"Millis": 21,
"NumYield": 2,
"User": ""
},
{
"Query": {
"find": "col1"
},
"Ts": "2016-11-08T13:46:27.000+00:00",
"Client": "127.0.0.1",
"ExecStats": {
"ExecutionTimeMillisEstimate": 0,
"IsEOF": 0,
"NReturned": 74,
"NeedTime": 1,
"RestoreState": 2,
"Works": 78,
"DocsExamined": 74,
"Direction": "forward",
"Invalidates": 0,
"NeedYield": 2,
"SaveState": 3,
"Stage": "COLLSCAN",
"Advanced": 70
},
"Ns": "samples.col1",
"Op": "query",
"WriteConflicts": 0,
"KeyUpdates": 0,
"KeysExamined": 0,
"Locks": {
"Global": "",
"MMAPV1Journal": "",
"Database": "",
"Collection": "",
"Metadata": "",
"Oplog": ""
},
"Nreturned": 74,
"ResponseLength": 104,
"DocsExamined": 74,
"Millis": 22,
"NumYield": 2,
"User": ""
},
{
"Query": {
"find": "col1"
},
"Ts": "2016-11-08T13:46:27.000+00:00",
"Client": "127.0.0.1",
"ExecStats": {
"ExecutionTimeMillisEstimate": 0,
"IsEOF": 0,
"NReturned": 75,
"NeedTime": 1,
"RestoreState": 2,
"Works": 78,
"DocsExamined": 75,
"Direction": "forward",
"Invalidates": 0,
"NeedYield": 2,
"SaveState": 3,
"Stage": "COLLSCAN",
"Advanced": 70
},
"Ns": "samples.col1",
"Op": "query",
"WriteConflicts": 0,
"KeyUpdates": 0,
"KeysExamined": 0,
"Locks": {
"Global": "",
"MMAPV1Journal": "",
"Database": "",
"Collection": "",
"Metadata": "",
"Oplog": ""
},
"Nreturned": 75,
"ResponseLength": 105,
"DocsExamined": 75,
"Millis": 23,
"NumYield": 2,
"User": ""
},
{
"Query": {
"find": "col1"
},
"Ts": "2016-11-08T13:46:27.000+00:00",
"Client": "127.0.0.1",
"ExecStats": {
"ExecutionTimeMillisEstimate": 0,
"IsEOF": 0,
"NReturned": 76,
"NeedTime": 1,
"RestoreState": 2,
"Works": 78,
"DocsExamined": 76,
"Direction": "forward",
"Invalidates": 0,
"NeedYield": 2,
"SaveState": 3,
"Stage": "COLLSCAN",
"Advanced": 70
},
"Ns": "samples.col1",
"Op": "query",
"WriteConflicts": 0,
"KeyUpdates": 0,
"KeysExamined": 0,
"Locks": {
"Global": "",
"MMAPV1Journal": "",
"Database": "",
"Collection": "",
"Metadata": "",
"Oplog": ""
},
"Nreturned": 76,
"ResponseLength": 106,
"DocsExamined": 76,
"Millis": 24,
"NumYield": 2,
"User": ""
},
{
"Query": {
"find": "col1"
},
"Ts": "2016-11-08T13:46:27.000+00:00",
"Client": "127.0.0.1",
"ExecStats": {
"ExecutionTimeMillisEstimate": 0,
"IsEOF": 0,
"NReturned": 77,
"NeedTime": 1,
"RestoreState": 2,
"Works": 78,
"DocsExamined": 77,
"Direction": "forward",
"Invalidates": 0,
"NeedYield": 2,
"SaveState": 3,
"Stage": "COLLSCAN",
"Advanced": 70
},
"Ns": "samples.col1",
"Op": "query",
"WriteConflicts": 0,
"KeyUpdates": 0,
"KeysExamined": 0,
"Locks": {
"Global": "",
"MMAPV1Journal": "",
"Database": "",
"Collection": "",
"Metadata": "",
"Oplog": ""
},
"Nreturned": 77,
"ResponseLength": 107,
"DocsExamined": 77,
"Millis": 25,
"NumYield": 2,
"User": ""
},
{
"Query": {
"find": "col1"
},
"Ts": "2016-11-08T13:46:27.000+00:00",
"Client": "127.0.0.1",
"ExecStats": {
"ExecutionTimeMillisEstimate": 0,
"IsEOF": 0,
"NReturned": 78,
"NeedTime": 1,
"RestoreState": 2,
"Works": 78,
"DocsExamined": 78,
"Direction": "forward",
"Invalidates": 0,
"NeedYield": 2,
"SaveState": 3,
"Stage": "COLLSCAN",
"Advanced": 70
},
"Ns": "samples.col1",
"Op": "query",
"WriteConflicts": 0,
"KeyUpdates": 0,
"KeysExamined": 0,
"Locks": {
"Global": "",
"MMAPV1Journal": "",
"Database": "",
"Collection": "",
"Metadata": "",
"Oplog": ""
},
"Nreturned": 78,
"ResponseLength": 108,
"DocsExamined": 78,
"Millis": 26,
"NumYield": 2,
"User": ""
},
{
"Query": {
"find": "col1",
"limit": 2
},
"Ts": "2016-11-08T13:46:27.000+00:00",
"Client": "127.0.0.1",
"ExecStats": {
"ExecutionTimeMillisEstimate": 0,
"IsEOF": 0,
"NReturned": 79,
"NeedTime": 1,
"RestoreState": 2,
"Works": 78,
"DocsExamined": 79,
"Direction": "forward",
"Invalidates": 0,
"NeedYield": 2,
"SaveState": 3,
"Stage": "COLLSCAN",
"Advanced": 70
},
"Ns": "samples.col1",
"Op": "query",
"WriteConflicts": 0,
"KeyUpdates": 0,
"KeysExamined": 0,
"Locks": {
"Global": "",
"MMAPV1Journal": "",
"Database": "",
"Collection": "",
"Metadata": "",
"Oplog": ""
},
"Nreturned": 79,
"ResponseLength": 109,
"DocsExamined": 79,
"Millis": 27,
"NumYield": 2,
"User": ""
},
{
"Query": {
"find": "col1",
"limit": 2
},
"Ts": "2016-11-08T13:46:27.000+00:00",
"Client": "127.0.0.1",
"ExecStats": {
"ExecutionTimeMillisEstimate": 0,
"IsEOF": 0,
"NReturned": 80,
"NeedTime": 1,
"RestoreState": 2,
"Works": 78,
"DocsExamined": 80,
"Direction": "forward",
"Invalidates": 0,
"NeedYield": 2,
"SaveState": 3,
"Stage": "COLLSCAN",
"Advanced": 70
},
"Ns": "samples.col1",
"Op": "query",
"WriteConflicts": 0,
"KeyUpdates": 0,
"KeysExamined": 0,
"Locks": {
"Global": "",
"MMAPV1Journal": "",
"Database": "",
"Collection": "",
"Metadata": "",
"Oplog": ""
},
"Nreturned": 80,
"ResponseLength": 110,
"DocsExamined": 80,
"Millis": 28,
"NumYield": 2,
"User": ""
}
]

12
src/go/pt-mongodb-summary/.gitignore vendored Normal file
View File

@@ -0,0 +1,12 @@
pt-mongodb-summary
pt-mongodb-summary_darwin_amd64
pt-mongodb-summary_freebsd_386
pt-mongodb-summary_freebsd_amd64
pt-mongodb-summary_linux_386
pt-mongodb-summary_linux_amd64
pt-mongodb-summary_linux_arm
pt-mongodb-summary_windows_386.exe
pt-mongodb-summary_windows_amd64.exe
coverage.out
vendor/*
!vendor/vendor.json

View File

@@ -0,0 +1,91 @@
#pt-mongodb-summary
pt-mongodb-summary collects information about a MongoDB cluster.
##Usage
pt-mongodb-summary [options] [host:[port]]
Default host:port is `localhost:27017`.
For better results, host must be a **mongos** server.
##Binaries
Please check the [releases](https://github.com/percona/toolkit-go/releases) tab to download the binaries.
###Paramters
|Short|Long|default||
|---|---|---|---|
|u|user|empty|user name to use when connecting if DB auth is enabled|
|p|password|empty|password to use when connecting if DB auth is enabled|
|a|auth-db|admin|database used to establish credentials and privileges with a MongoDB server|
`-p` is an optional parameter. If it is used it shouldn't have a blank between the parameter and its value: `-p<password>`
It can be also used as `-p` without specifying a password; in that case, the program will ask the password to avoid using a password in the command line.
###Output example
```
# Instances ####################################################################################
ID Host Type ReplSet Engine Status
0 localhost:17001 PRIMARY r1
1 localhost:17002 SECONDARY r1
2 localhost:17003 SECONDARY r1
0 localhost:18001 PRIMARY r2
1 localhost:18002 SECONDARY r2
2 localhost:18003 SECONDARY r2
# This host
# Mongo Executable #############################################################################
Path to executable | /home/karl/tmp/MongoDB32Labs/3.0/bin/mongos
Has symbols | No
# Report On 0 ########################################
User | karl
PID Owner | mongos
Time | 2016-10-30 00:18:49 -0300 ART
Hostname | karl-HP-ENVY
Version | 3.0.11
Built On | Linux x86_64
Started | 2016-10-30 00:18:49 -0300 ART
Databases | 0
Collections | 0
Datadir | /data/db
Processes | 0
Process Type | mongos
# Running Ops ##################################################################################
Type Min Max Avg
Insert 0 0 0/5s
Query 0 0 0/5s
Update 0 0 0/5s
Delete 0 0 0/5s
GetMore 0 0 0/5s
Command 0 22 16/5s
# Security #####################################################################################
Users 0
Roles 0
Auth disabled
SSL disabled
# Oplog ########################################################################################
Oplog Size 18660 Mb
Oplog Used 55 Mb
Oplog Length 0.91 hours
Last Election 2016-10-30 00:18:44 -0300 ART
# Cluster wide #################################################################################
Databases: 3
Collections: 17
Sharded Collections: 1
Unsharded Collections: 16
Sharded Data Size: 68 GB
Unsharded Data Size: 0 KB
# Balancer (per day)
Success: 6
Failed: 0
Splits: 0
Drops: 0
```

View File

@@ -0,0 +1,658 @@
package main
import (
"fmt"
"html/template"
"log"
"os"
"strings"
"time"
"github.com/howeyc/gopass"
"github.com/pborman/getopt"
"github.com/percona/pmgo"
"github.com/percona/toolkit-go/mongolib/proto"
"github.com/percona/toolkit-go/pt-mongodb-summary/templates"
"github.com/pkg/errors"
"github.com/shirou/gopsutil/process"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
var (
Version string
Build string
)
type hostInfo struct {
ThisHostID int
Hostname string
HostOsType string
HostSystemCPUArch string
HostDatabases int
HostCollections int
DBPath string
ProcPath string
ProcUserName string
ProcCreateTime time.Time
ProcProcessCount int
// Server Status
ProcessName string
ReplicasetName string
Version string
NodeType string
}
type procInfo struct {
CreateTime time.Time
Path string
UserName string
Error error
}
type security struct {
Users int
Roles int
Auth string
SSL string
}
type timedStats struct {
Min int64
Max int64
Total int64
Avg int64
}
type opCounters struct {
Insert timedStats
Query timedStats
Update timedStats
Delete timedStats
GetMore timedStats
Command timedStats
SampleRate time.Duration
}
type databases struct {
Databases []struct {
Name string `bson:"name"`
SizeOnDisk int64 `bson:"sizeOnDisk"`
Empty bool `bson:"empty"`
Shards map[string]int64 `bson:"shards"`
} `bson:"databases"`
TotalSize int64 `bson:"totalSize"`
TotalSizeMb int64 `bson:"totalSizeMb"`
OK bool `bson:"ok"`
}
type clusterwideInfo struct {
TotalDBsCount int
TotalCollectionsCount int
ShardedColsCount int
UnshardedColsCount int
ShardedDataSize int64 // bytes
ShardedDataSizeScaled float64
ShardedDataSizeScale string
UnshardedDataSize int64 // bytes
UnshardedDataSizeScaled float64
UnshardedDataSizeScale string
}
type options struct {
Host string
User string
Password string
AuthDB string
Debug bool
Version bool
}
func main() {
opts := options{Host: "localhost:27017"}
help := getopt.BoolLong("help", '?', "Show help")
getopt.BoolVarLong(&opts.Version, "version", 'v', "", "show version & exit")
getopt.StringVarLong(&opts.User, "user", 'u', "", "username")
getopt.StringVarLong(&opts.Password, "password", 'p', "", "password").SetOptional()
getopt.StringVarLong(&opts.AuthDB, "authenticationDatabase", 'a', "admin", "database used to establish credentials and privileges with a MongoDB server")
getopt.SetParameters("host[:port]")
getopt.Parse()
if *help {
getopt.Usage()
return
}
args := getopt.Args() // positional arg
if len(args) > 0 {
opts.Host = args[0]
}
if opts.Version {
fmt.Println("pt-mongodb-summary")
fmt.Printf("Version %s\n", Version)
fmt.Printf("Build: %s\n", Build)
return
}
if getopt.IsSet("password") && opts.Password == "" {
print("Password: ")
pass, err := gopass.GetPasswd()
if err != nil {
fmt.Println(err)
os.Exit(2)
}
opts.Password = string(pass)
}
di := &mgo.DialInfo{
Username: opts.User,
Password: opts.Password,
Addrs: []string{opts.Host},
FailFast: true,
Source: opts.AuthDB,
}
dialer := pmgo.NewDialer()
hostnames, err := getHostnames(dialer, di)
session, err := dialer.DialWithInfo(di)
if err != nil {
log.Printf("cannot connect to the db: %s", err)
os.Exit(1)
}
defer session.Close()
if replicaMembers, err := GetReplicasetMembers(dialer, hostnames, di); err != nil {
log.Printf("[Error] cannot get replicaset members: %v\n", err)
} else {
t := template.Must(template.New("replicas").Parse(templates.Replicas))
t.Execute(os.Stdout, replicaMembers)
}
//
if hostInfo, err := GetHostinfo(session); err != nil {
log.Printf("[Error] cannot get host info: %v\n", err)
} else {
t := template.Must(template.New("hosttemplateData").Parse(templates.HostInfo))
t.Execute(os.Stdout, hostInfo)
}
var sampleCount int64 = 5
var sampleRate time.Duration = 1 // in seconds
if rops, err := GetOpCountersStats(session, sampleCount, sampleRate); err != nil {
log.Printf("[Error] cannot get Opcounters stats: %v\n", err)
} else {
t := template.Must(template.New("runningOps").Parse(templates.RunningOps))
t.Execute(os.Stdout, rops)
}
if security, err := GetSecuritySettings(session); err != nil {
log.Printf("[Error] cannot get security settings: %v\n", err)
} else {
t := template.Must(template.New("ssl").Parse(templates.Security))
t.Execute(os.Stdout, security)
}
if oplogInfo, err := GetOplogInfo(hostnames, di); err != nil {
log.Printf("[Error] cannot get Oplog info: %v\n", err)
} else {
if len(oplogInfo) > 0 {
t := template.Must(template.New("oplogInfo").Parse(templates.Oplog))
t.Execute(os.Stdout, oplogInfo[0])
}
}
if cwi, err := GetClusterwideInfo(session); err != nil {
log.Printf("[Error] cannot get cluster wide info: %v\n", err)
} else {
t := template.Must(template.New("clusterwide").Parse(templates.Clusterwide))
t.Execute(os.Stdout, cwi)
}
if bs, err := GetBalancerStats(session); err != nil {
log.Printf("[Error] cannot get balancer stats: %v\n", err)
} else {
t := template.Must(template.New("balancer").Parse(templates.BalancerStats))
t.Execute(os.Stdout, bs)
}
}
func GetHostinfo2(session pmgo.SessionManager) (*hostInfo, error) {
hi := proto.HostInfo{}
if err := session.Run(bson.M{"hostInfo": 1}, &hi); err != nil {
return nil, errors.Wrap(err, "GetHostInfo.hostInfo")
}
cmdOpts := proto.CommandLineOptions{}
err := session.DB("admin").Run(bson.D{{"getCmdLineOpts", 1}, {"recordStats", 1}}, &cmdOpts)
if err != nil {
return nil, errors.Wrap(err, "cannot get command line options")
}
ss := proto.ServerStatus{}
if err := session.DB("admin").Run(bson.D{{"serverStatus", 1}, {"recordStats", 1}}, &ss); err != nil {
return nil, errors.Wrap(err, "GetHostInfo.serverStatus")
}
pi := procInfo{}
if err := getProcInfo(int32(ss.Pid), &pi); err != nil {
pi.Error = err
}
nodeType, _ := getNodeType(session)
i := &hostInfo{
Hostname: hi.System.Hostname,
HostOsType: hi.Os.Type,
HostSystemCPUArch: hi.System.CpuArch,
HostDatabases: hi.DatabasesCount,
HostCollections: hi.CollectionsCount,
DBPath: "", // Sets default. It will be overriden later if necessary
ProcessName: ss.Process,
Version: ss.Version,
NodeType: nodeType,
ProcPath: pi.Path,
ProcUserName: pi.UserName,
ProcCreateTime: pi.CreateTime,
}
if ss.Repl != nil {
i.ReplicasetName = ss.Repl.SetName
}
if cmdOpts.Parsed.Storage.DbPath != "" {
i.DBPath = cmdOpts.Parsed.Storage.DbPath
}
return i, nil
}
func GetHostinfo(session pmgo.SessionManager) (*hostInfo, error) {
hi := proto.HostInfo{}
if err := session.Run(bson.M{"hostInfo": 1}, &hi); err != nil {
return nil, errors.Wrap(err, "GetHostInfo.hostInfo")
}
cmdOpts := proto.CommandLineOptions{}
err := session.DB("admin").Run(bson.D{{"getCmdLineOpts", 1}, {"recordStats", 1}}, &cmdOpts)
if err != nil {
return nil, errors.Wrap(err, "cannot get command line options")
}
ss := proto.ServerStatus{}
if err := session.DB("admin").Run(bson.D{{"serverStatus", 1}, {"recordStats", 1}}, &ss); err != nil {
return nil, errors.Wrap(err, "GetHostInfo.serverStatus")
}
pi := procInfo{}
if err := getProcInfo(int32(ss.Pid), &pi); err != nil {
pi.Error = err
}
nodeType, _ := getNodeType(session)
i := &hostInfo{
Hostname: hi.System.Hostname,
HostOsType: hi.Os.Type,
HostSystemCPUArch: hi.System.CpuArch,
HostDatabases: hi.DatabasesCount,
HostCollections: hi.CollectionsCount,
DBPath: "", // Sets default. It will be overriden later if necessary
ProcessName: ss.Process,
Version: ss.Version,
NodeType: nodeType,
ProcPath: pi.Path,
ProcUserName: pi.UserName,
ProcCreateTime: pi.CreateTime,
}
if ss.Repl != nil {
i.ReplicasetName = ss.Repl.SetName
}
if cmdOpts.Parsed.Storage.DbPath != "" {
i.DBPath = cmdOpts.Parsed.Storage.DbPath
}
return i, nil
}
func getHostnames(dialer pmgo.Dialer, di *mgo.DialInfo) ([]string, error) {
session, err := dialer.DialWithInfo(di)
if err != nil {
return nil, err
}
defer session.Close()
shardsInfo := &proto.ShardsInfo{}
err = session.Run("listShards", shardsInfo)
if err != nil {
return nil, errors.Wrap(err, "cannot list shards")
}
hostnames := []string{di.Addrs[0]}
if shardsInfo != nil {
for _, shardInfo := range shardsInfo.Shards {
m := strings.Split(shardInfo.Host, "/")
h := strings.Split(m[1], ",")
hostnames = append(hostnames, h[0])
}
}
return hostnames, nil
}
func GetClusterwideInfo(session pmgo.SessionManager) (*clusterwideInfo, error) {
var databases databases
err := session.Run(bson.M{"listDatabases": 1}, &databases)
if err != nil {
return nil, errors.Wrap(err, "GetClusterwideInfo.listDatabases ")
}
cwi := &clusterwideInfo{
TotalDBsCount: len(databases.Databases),
}
configDB := session.DB("config")
for _, db := range databases.Databases {
collections, err := session.DB(db.Name).CollectionNames()
if err != nil {
continue
}
cwi.TotalCollectionsCount += len(collections)
if len(db.Shards) == 1 {
cwi.UnshardedDataSize += db.SizeOnDisk
continue
}
cwi.ShardedDataSize += db.SizeOnDisk
colsCount, _ := configDB.C("collections").Find(bson.M{"_id": bson.RegEx{"^" + db.Name, ""}}).Count()
cwi.ShardedColsCount += colsCount
}
cwi.UnshardedColsCount = cwi.TotalCollectionsCount - cwi.ShardedColsCount
cwi.ShardedDataSizeScaled, cwi.ShardedDataSizeScale = sizeAndUnit(cwi.ShardedDataSize)
cwi.UnshardedDataSizeScaled, cwi.UnshardedDataSizeScale = sizeAndUnit(cwi.UnshardedDataSize)
return cwi, nil
}
func sizeAndUnit(size int64) (float64, string) {
unit := []string{"KB", "MB", "GB", "TB"}
idx := 0
newSize := float64(size)
for newSize > 1024 {
newSize /= 1024
idx++
}
newSize = float64(int64(newSize*100) / 1000)
return newSize, unit[idx]
}
func GetReplicasetMembers(dialer pmgo.Dialer, hostnames []string, di *mgo.DialInfo) ([]proto.Members, error) {
replicaMembers := []proto.Members{}
for _, hostname := range hostnames {
di.Addrs = []string{hostname}
session, err := dialer.DialWithInfo(di)
if err != nil {
return nil, errors.Wrapf(err, "getReplicasetMembers. cannot connect to %s", hostname)
}
defer session.Close()
rss := proto.ReplicaSetStatus{}
err = session.Run(bson.M{"replSetGetStatus": 1}, &rss)
if err != nil {
continue // If a host is a mongos we cannot get info but is not a real error
}
for _, m := range rss.Members {
m.Set = rss.Set
replicaMembers = append(replicaMembers, m)
}
}
return replicaMembers, nil
}
func GetSecuritySettings(session pmgo.SessionManager) (*security, error) {
s := security{
Auth: "disabled",
SSL: "disabled",
}
cmdOpts := proto.CommandLineOptions{}
err := session.DB("admin").Run(bson.D{{"getCmdLineOpts", 1}, {"recordStats", 1}}, &cmdOpts)
if err != nil {
return nil, errors.Wrap(err, "cannot get command line options")
}
if cmdOpts.Security.Authorization != "" || cmdOpts.Security.KeyFile != "" {
s.Auth = "enabled"
}
if cmdOpts.Parsed.Net.SSL.Mode != "" && cmdOpts.Parsed.Net.SSL.Mode != "disabled" {
s.SSL = cmdOpts.Parsed.Net.SSL.Mode
}
s.Users, err = session.DB("admin").C("system.users").Count()
if err != nil {
return nil, errors.Wrap(err, "cannot get users count")
}
s.Roles, err = session.DB("admin").C("system.roles").Count()
if err != nil {
return nil, errors.Wrap(err, "cannot get roles count")
}
return &s, nil
}
func getNodeType(session pmgo.SessionManager) (string, error) {
md := proto.MasterDoc{}
err := session.Run("isMaster", &md)
if err != nil {
return "", err
}
if md.SetName != nil || md.Hosts != nil {
return "replset", nil
} else if md.Msg == "isdbgrid" {
// isdbgrid is always the msg value when calling isMaster on a mongos
// see http://docs.mongodb.org/manual/core/sharded-cluster-query-router/
return "mongos", nil
}
return "mongod", nil
}
func GetOpCountersStats(session pmgo.SessionManager, count int64, sleep time.Duration) (*opCounters, error) {
oc := &opCounters{}
ss := proto.ServerStatus{}
err := session.DB("admin").Run(bson.D{{"serverStatus", 1}, {"recordStats", 1}}, &ss)
if err != nil {
return nil, errors.Wrap(err, "GetOpCountersStats.ServerStatus")
}
ticker := time.NewTicker(sleep)
for i := int64(0); i < count-1; i++ {
<-ticker.C
err := session.DB("admin").Run(bson.D{{"serverStatus", 1}, {"recordStats", 1}}, &ss)
if err != nil {
continue
}
// Insert
if ss.Opcounters.Insert > oc.Insert.Max {
oc.Insert.Max = ss.Opcounters.Insert
}
if ss.Opcounters.Insert < oc.Insert.Min {
oc.Insert.Min = ss.Opcounters.Insert
}
oc.Insert.Total += ss.Opcounters.Insert
// Query ---------------------------------------
if ss.Opcounters.Query > oc.Query.Max {
oc.Query.Max = ss.Opcounters.Query
}
if ss.Opcounters.Query < oc.Query.Min {
oc.Query.Min = ss.Opcounters.Query
}
oc.Query.Total += ss.Opcounters.Query
// Command -------------------------------------
if ss.Opcounters.Command > oc.Command.Max {
oc.Command.Max = ss.Opcounters.Command
}
if ss.Opcounters.Command < oc.Command.Min {
oc.Command.Min = ss.Opcounters.Command
}
oc.Command.Total += ss.Opcounters.Command
// Update --------------------------------------
if ss.Opcounters.Update > oc.Update.Max {
oc.Update.Max = ss.Opcounters.Update
}
if ss.Opcounters.Update < oc.Update.Min {
oc.Update.Min = ss.Opcounters.Update
}
oc.Update.Total += ss.Opcounters.Update
// Delete --------------------------------------
if ss.Opcounters.Delete > oc.Delete.Max {
oc.Delete.Max = ss.Opcounters.Delete
}
if ss.Opcounters.Delete < oc.Delete.Min {
oc.Delete.Min = ss.Opcounters.Delete
}
oc.Delete.Total += ss.Opcounters.Delete
// GetMore -------------------------------------
if ss.Opcounters.GetMore > oc.GetMore.Max {
oc.GetMore.Max = ss.Opcounters.GetMore
}
if ss.Opcounters.GetMore < oc.GetMore.Min {
oc.GetMore.Min = ss.Opcounters.GetMore
}
oc.GetMore.Total += ss.Opcounters.GetMore
}
ticker.Stop()
oc.Insert.Avg = oc.Insert.Total / count
oc.Query.Avg = oc.Query.Total / count
oc.Update.Avg = oc.Update.Total / count
oc.Delete.Avg = oc.Delete.Total / count
oc.GetMore.Avg = oc.GetMore.Total / count
oc.Command.Avg = oc.Command.Total / count
//
oc.SampleRate = time.Duration(count) * time.Second * sleep
return oc, nil
}
func getProcInfo(pid int32, templateData *procInfo) error {
//proc, err := process.NewProcess(templateData.ServerStatus.Pid)
proc, err := process.NewProcess(pid)
if err != nil {
return fmt.Errorf("cannot get process %d\n", pid)
}
ct, err := proc.CreateTime()
if err != nil {
return err
}
templateData.CreateTime = time.Unix(ct/1000, 0)
templateData.Path, err = proc.Exe()
if err != nil {
return err
}
templateData.UserName, err = proc.Username()
if err != nil {
return err
}
return nil
}
func getDbsAndCollectionsCount(hostnames []string) (int, int, error) {
dbnames := make(map[string]bool)
colnames := make(map[string]bool)
for _, hostname := range hostnames {
session, err := mgo.Dial(hostname)
if err != nil {
continue
}
dbs, err := session.DatabaseNames()
if err != nil {
continue
}
for _, dbname := range dbs {
dbnames[dbname] = true
cols, err := session.DB(dbname).CollectionNames()
if err != nil {
continue
}
for _, colname := range cols {
colnames[dbname+"."+colname] = true
}
}
}
return len(dbnames), len(colnames), nil
}
func GetBalancerStats(session pmgo.SessionManager) (*proto.BalancerStats, error) {
scs, err := GetShardingChangelogStatus(session)
if err != nil {
return nil, err
}
s := &proto.BalancerStats{}
for _, item := range *scs.Items {
event := item.Id.Event
note := item.Id.Note
count := item.Count
switch event {
case "moveChunk.to", "moveChunk.from", "moveChunk.commit":
if note == "success" || note == "" {
s.Success += int64(count)
} else {
s.Failed += int64(count)
}
case "split", "multi-split":
s.Splits += int64(count)
case "dropCollection", "dropCollection.start", "dropDatabase", "dropDatabase.start":
s.Drops++
}
}
return s, nil
}
func GetShardingChangelogStatus(session pmgo.SessionManager) (*proto.ShardingChangelogStats, error) {
var qresults []proto.ShardingChangelogSummary
coll := session.DB("config").C("changelog")
match := bson.M{"time": bson.M{"$gt": time.Now().Add(-240 * time.Hour)}}
group := bson.M{"_id": bson.M{"event": "$what", "note": "$details.note"}, "count": bson.M{"$sum": 1}}
err := coll.Pipe([]bson.M{{"$match": match}, {"$group": group}}).All(&qresults)
if err != nil {
return nil, errors.Wrap(err, "GetShardingChangelogStatus.changelog.find")
}
return &proto.ShardingChangelogStats{
Items: &qresults,
}, nil
}

View File

@@ -0,0 +1,369 @@
package main
import (
"fmt"
"reflect"
"testing"
"time"
mgo "gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
"github.com/golang/mock/gomock"
"github.com/percona/pmgo/pmgomock"
"github.com/percona/toolkit-go/mongolib/proto"
"github.com/percona/toolkit-go/pt-mongodb-summary/test"
)
func TestGetOpCounterStats(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
session := pmgomock.NewMockSessionManager(ctrl)
database := pmgomock.NewMockDatabaseManager(ctrl)
ss := proto.ServerStatus{}
test.LoadJson("test/sample/serverstatus.json", &ss)
// serverStatus for getOpCountersStats
session.EXPECT().DB("admin").Return(database)
database.EXPECT().Run(bson.D{{"serverStatus", 1}, {"recordStats", 1}}, gomock.Any()).SetArg(1, ss)
session.EXPECT().DB("admin").Return(database)
database.EXPECT().Run(bson.D{{"serverStatus", 1}, {"recordStats", 1}}, gomock.Any()).SetArg(1, ss)
session.EXPECT().DB("admin").Return(database)
database.EXPECT().Run(bson.D{{"serverStatus", 1}, {"recordStats", 1}}, gomock.Any()).SetArg(1, ss)
session.EXPECT().DB("admin").Return(database)
database.EXPECT().Run(bson.D{{"serverStatus", 1}, {"recordStats", 1}}, gomock.Any()).SetArg(1, ss)
session.EXPECT().DB("admin").Return(database)
database.EXPECT().Run(bson.D{{"serverStatus", 1}, {"recordStats", 1}}, gomock.Any()).SetArg(1, ss)
var sampleCount int64 = 5
var sampleRate time.Duration = 10 * time.Millisecond // in seconds
expect := timedStats{Min: 0, Max: 473, Total: 1892, Avg: 378}
os, err := GetOpCountersStats(session, sampleCount, sampleRate)
if err != nil {
t.Error(err)
}
if !reflect.DeepEqual(expect, os.Command) {
t.Errorf("getOpCountersStats. got: %+v\nexpect: %+v\n", os.Command, expect)
}
}
func TestSecurityOpts(t *testing.T) {
cmdopts := []proto.CommandLineOptions{
// 1
proto.CommandLineOptions{
Parsed: proto.Parsed{
Net: proto.Net{
SSL: proto.SSL{
Mode: "",
},
},
},
Security: proto.Security{
KeyFile: "",
Authorization: "",
},
},
// 2
proto.CommandLineOptions{
Parsed: proto.Parsed{
Net: proto.Net{
SSL: proto.SSL{
Mode: "",
},
},
},
Security: proto.Security{
KeyFile: "a file",
Authorization: "",
},
},
// 3
proto.CommandLineOptions{
Parsed: proto.Parsed{
Net: proto.Net{
SSL: proto.SSL{
Mode: "",
},
},
},
Security: proto.Security{
KeyFile: "",
Authorization: "something here",
},
},
// 4
proto.CommandLineOptions{
Parsed: proto.Parsed{
Net: proto.Net{
SSL: proto.SSL{
Mode: "super secure",
},
},
},
Security: proto.Security{
KeyFile: "",
Authorization: "",
},
},
}
expect := []*security{
// 1
&security{
Users: 1,
Roles: 2,
Auth: "disabled",
SSL: "disabled",
},
// 2
&security{
Users: 1,
Roles: 2,
Auth: "enabled",
SSL: "disabled",
},
// 3
&security{
Users: 1,
Roles: 2,
Auth: "enabled",
SSL: "disabled",
},
// 4
&security{
Users: 1,
Roles: 2,
Auth: "disabled",
SSL: "super secure",
},
}
ctrl := gomock.NewController(t)
defer ctrl.Finish()
session := pmgomock.NewMockSessionManager(ctrl)
database := pmgomock.NewMockDatabaseManager(ctrl)
usersCol := pmgomock.NewMockCollectionManager(ctrl)
rolesCol := pmgomock.NewMockCollectionManager(ctrl)
for i, cmd := range cmdopts {
session.EXPECT().DB("admin").Return(database)
database.EXPECT().Run(bson.D{{"getCmdLineOpts", 1}, {"recordStats", 1}}, gomock.Any()).SetArg(1, cmd)
session.EXPECT().DB("admin").Return(database)
database.EXPECT().C("system.users").Return(usersCol)
usersCol.EXPECT().Count().Return(1, nil)
session.EXPECT().DB("admin").Return(database)
database.EXPECT().C("system.roles").Return(rolesCol)
rolesCol.EXPECT().Count().Return(2, nil)
got, err := GetSecuritySettings(session)
if err != nil {
t.Errorf("cannot get sec settings: %v", err)
}
if !reflect.DeepEqual(got, expect[i]) {
t.Errorf("got: %+v, expect: %+v\n", got, expect[i])
}
}
}
func TestGetNodeType(t *testing.T) {
md := []struct {
in proto.MasterDoc
out string
}{
{proto.MasterDoc{SetName: "name"}, "replset"},
{proto.MasterDoc{Msg: "isdbgrid"}, "mongos"},
{proto.MasterDoc{Msg: "a msg"}, "mongod"},
}
ctrl := gomock.NewController(t)
defer ctrl.Finish()
session := pmgomock.NewMockSessionManager(ctrl)
for _, m := range md {
session.EXPECT().Run("isMaster", gomock.Any()).SetArg(1, m.in)
nodeType, err := getNodeType(session)
if err != nil {
t.Errorf("cannot get node type: %+v, error: %s\n", m.in, err)
}
if nodeType != m.out {
t.Errorf("invalid node type. got %s, expect: %s\n", nodeType, m.out)
}
}
session.EXPECT().Run("isMaster", gomock.Any()).Return(fmt.Errorf("some fake error"))
nodeType, err := getNodeType(session)
if err == nil {
t.Errorf("error expected, got nil")
}
if nodeType != "" {
t.Errorf("expected blank node type, got %s", nodeType)
}
}
func TestGetReplicasetMembers(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
dialer := pmgomock.NewMockDialer(ctrl)
session := pmgomock.NewMockSessionManager(ctrl)
mockrss := proto.ReplicaSetStatus{
Date: "",
MyState: 1,
Term: 0,
HeartbeatIntervalMillis: 0,
Members: []proto.Members{
proto.Members{
Optime: nil,
OptimeDate: "",
InfoMessage: "",
Id: 0,
Name: "localhost:17001",
Health: 1,
StateStr: "PRIMARY",
Uptime: 113287,
ConfigVersion: 1,
Self: true,
State: 1,
ElectionTime: 6340960613392449537,
ElectionDate: "",
Set: ""},
proto.Members{
Optime: nil,
OptimeDate: "",
InfoMessage: "",
Id: 1,
Name: "localhost:17002",
Health: 1,
StateStr: "SECONDARY",
Uptime: 113031,
ConfigVersion: 1,
Self: false,
State: 2,
ElectionTime: 0,
ElectionDate: "",
Set: ""},
proto.Members{
Optime: nil,
OptimeDate: "",
InfoMessage: "",
Id: 2,
Name: "localhost:17003",
Health: 1,
StateStr: "SECONDARY",
Uptime: 113031,
ConfigVersion: 1,
Self: false,
State: 2,
ElectionTime: 0,
ElectionDate: "",
Set: ""}},
Ok: 1,
Set: "r1",
}
expect := []proto.Members{
proto.Members{
Optime: nil,
OptimeDate: "",
InfoMessage: "",
Id: 0,
Name: "localhost:17001",
Health: 1,
StateStr: "PRIMARY",
Uptime: 113287,
ConfigVersion: 1,
Self: true,
State: 1,
ElectionTime: 6340960613392449537,
ElectionDate: "",
Set: "r1"},
proto.Members{Optime: (*proto.Optime)(nil),
OptimeDate: "",
InfoMessage: "",
Id: 1,
Name: "localhost:17002",
Health: 1,
StateStr: "SECONDARY",
Uptime: 113031,
ConfigVersion: 1,
Self: false,
State: 2,
ElectionTime: 0,
ElectionDate: "",
Set: "r1"},
proto.Members{Optime: (*proto.Optime)(nil),
OptimeDate: "",
InfoMessage: "",
Id: 2,
Name: "localhost:17003",
Health: 1,
StateStr: "SECONDARY",
Uptime: 113031,
ConfigVersion: 1,
Self: false,
State: 2,
ElectionTime: 0,
ElectionDate: "",
Set: "r1",
}}
dialer.EXPECT().DialWithInfo(gomock.Any()).Return(session, nil)
session.EXPECT().Run(bson.M{"replSetGetStatus": 1}, gomock.Any()).SetArg(1, mockrss)
session.EXPECT().Close()
di := &mgo.DialInfo{Addrs: []string{"localhost"}}
rss, err := GetReplicasetMembers(dialer, []string{"localhost"}, di)
if err != nil {
t.Errorf("getReplicasetMembers: %v", err)
}
if !reflect.DeepEqual(rss, expect) {
t.Errorf("getReplicasetMembers: got %+v, expected: %+v\n", rss, expect)
}
}
func TestGetHostnames(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
dialer := pmgomock.NewMockDialer(ctrl)
session := pmgomock.NewMockSessionManager(ctrl)
mockShardsInfo := proto.ShardsInfo{
Shards: []proto.Shard{
proto.Shard{
ID: "r1",
Host: "r1/localhost:17001,localhost:17002,localhost:17003",
},
proto.Shard{
ID: "r2",
Host: "r2/localhost:18001,localhost:18002,localhost:18003",
},
},
OK: 1,
}
dialer.EXPECT().DialWithInfo(gomock.Any()).Return(session, nil)
session.EXPECT().Run("listShards", gomock.Any()).SetArg(1, mockShardsInfo)
session.EXPECT().Close()
expect := []string{"localhost", "localhost:17001", "localhost:18001"}
di := &mgo.DialInfo{Addrs: []string{"localhost"}}
rss, err := getHostnames(dialer, di)
if err != nil {
t.Errorf("getHostnames: %v", err)
}
if !reflect.DeepEqual(rss, expect) {
t.Errorf("getHostnames: got %+v, expected: %+v\n", rss, expect)
}
}

View File

@@ -0,0 +1,121 @@
package main
import (
"fmt"
"sort"
"time"
"github.com/percona/toolkit-go/mongolib/proto"
"github.com/pkg/errors"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
func GetOplogInfo(hostnames []string, di *mgo.DialInfo) ([]proto.OplogInfo, error) {
results := proto.OpLogs{}
for _, hostname := range hostnames {
result := proto.OplogInfo{
Hostname: hostname,
}
di.Addrs = []string{hostname}
session, err := mgo.DialWithInfo(di)
if err != nil {
return nil, errors.Wrapf(err, "cannot connect to %s", hostname)
}
defer session.Close()
oplogCol, err := getOplogCollection(session)
if err != nil {
continue
}
olEntry, err := getOplogEntry(session, oplogCol)
if err != nil {
return nil, errors.Wrap(err, "getOplogInfo -> GetOplogEntry")
}
result.Size = olEntry.Options.Size / (1024 * 1024)
var colStats proto.OplogColStats
err = session.DB("local").Run(bson.M{"collStats": oplogCol}, &colStats)
if err != nil {
return nil, errors.Wrapf(err, "cannot get collStats for collection %s", oplogCol)
}
result.UsedMB = colStats.Size / (1024 * 1024)
var firstRow, lastRow proto.OplogRow
err = session.DB("local").C(oplogCol).Find(nil).Sort("$natural").One(&firstRow)
if err != nil {
return nil, errors.Wrap(err, "cannot read first oplog row")
}
err = session.DB("local").C(oplogCol).Find(nil).Sort("-$natural").One(&lastRow)
if err != nil {
return nil, errors.Wrap(err, "cannot read last oplog row")
}
// https://docs.mongodb.com/manual/reference/bson-types/#timestamps
tfirst := firstRow.Ts >> 32
tlast := lastRow.Ts >> 32
result.TimeDiff = tlast - tfirst
result.TimeDiffHours = float64(result.TimeDiff) / 3600
result.TFirst = time.Unix(tfirst, 0)
result.TLast = time.Unix(tlast, 0)
result.Now = time.Now().UTC()
if result.TimeDiffHours > 24 {
result.Running = fmt.Sprintf("%0.2f days", result.TimeDiffHours/24)
} else {
result.Running = fmt.Sprintf("%0.2f hours", result.TimeDiffHours)
}
replSetStatus := proto.ReplicaSetStatus{}
err = session.Run(bson.M{"replSetGetStatus": 1}, &replSetStatus)
if err != nil {
return nil, errors.Wrap(err, "cannot get ReplicaSetStatus")
}
for _, member := range replSetStatus.Members {
if member.State == 1 {
result.ElectionTime = time.Unix(member.ElectionTime>>32, 0)
break
}
}
results = append(results, result)
}
sort.Sort(results)
return results, nil
}
func getOplogCollection(session *mgo.Session) (string, error) {
oplog := "oplog.rs"
db := session.DB("local")
nsCol := db.C("system.namespaces")
var res interface{}
if err := nsCol.Find(bson.M{"name": "local." + oplog}).One(&res); err == nil {
return oplog, nil
}
oplog = "oplog.$main"
if err := nsCol.Find(bson.M{"name": "local." + oplog}).One(&res); err != nil {
return "", fmt.Errorf("neither master/slave nor replica set replication detected")
}
return oplog, nil
}
func getOplogEntry(session *mgo.Session, oplogCol string) (*proto.OplogEntry, error) {
olEntry := &proto.OplogEntry{}
err := session.DB("local").C("system.namespaces").Find(bson.M{"name": "local." + oplogCol}).One(&olEntry)
if err != nil {
return nil, fmt.Errorf("local.%s, or its options, not found in system.namespaces collection", oplogCol)
}
return olEntry, nil
}

View File

@@ -0,0 +1,9 @@
package templates
const BalancerStats = `
# Balancer (per day)
Success: {{.Success}}
Failed: {{.Failed}}
Splits: {{.Splits}}
Drops: {{.Drops}}
`

View File

@@ -0,0 +1,10 @@
package templates
const Clusterwide = `
# Cluster wide #################################################################################
Databases: {{.TotalDBsCount}}
Collections: {{.TotalCollectionsCount}}
Sharded Collections: {{.ShardedColsCount}}
Unsharded Collections: {{.UnshardedColsCount}}
Sharded Data Size: {{.ShardedDataSizeScaled}} {{.ShardedDataSizeScale}}
Unsharded Data Size: {{.UnshardedDataSizeScaled}} {{.UnshardedDataSizeScale}}`

View File

@@ -0,0 +1,26 @@
package templates
const HostInfo = `# This host
# Mongo Executable #############################################################################
Path to executable | {{.ProcPath}}
Has symbols | No
# Report On {{.ThisHostID}} ########################################
User | {{.ProcUserName}}
PID Owner | {{.ProcessName}}
Time | {{.ProcCreateTime}}
Hostname | {{.Hostname}}
Version | {{.Version}}
Built On | {{.HostOsType}} {{.HostSystemCPUArch}}
Started | {{.ProcCreateTime}}
Databases | {{.HostDatabases}}
Collections | {{.HostCollections}}
{{- if .DBPath }}
Datadir | {{.DBPath}}
{{- end }}
Processes | {{.ProcProcessCount}}
Process Type | {{.NodeType}}
{{ if .ReplicaSetName -}}
ReplSet | {{.ReplicasetName}}
Repl Status |
{{- end -}}
`

View File

@@ -0,0 +1,10 @@
package templates
const Oplog = `
# Oplog ########################################################################################
Oplog Size {{.Size}} Mb
Oplog Used {{.UsedMB}} Mb
Oplog Length {{.Running}}
Last Election {{.ElectionTime}}
`

View File

@@ -0,0 +1,13 @@
package templates
const Replicas = `
# Instances ####################################################################################
ID Host Type ReplSet Engine Status
{{- if . -}}
{{- range . }}
{{printf "% 3d" .Id}} {{printf "%-30s" .Name}} {{printf "%-30s" .StateStr}} {{printf "%10s" .Set -}}
{{end}}
{{else}}
No replica sets found
{{end}}
`

View File

@@ -0,0 +1,13 @@
package templates
const RunningOps = `
# Running Ops ##################################################################################
Type Min Max Avg
Insert {{printf "% 8d" .Insert.Min}} {{printf "% 8d" .Insert.Max}} {{printf "% 8d" .Insert.Avg}}/{{.SampleRate}}
Query {{printf "% 8d" .Query.Min}} {{printf "% 8d" .Query.Max}} {{printf "% 8d" .Query.Avg}}/{{.SampleRate}}
Update {{printf "% 8d" .Update.Min}} {{printf "% 8d" .Update.Max}} {{printf "% 8d" .Update.Avg}}/{{.SampleRate}}
Delete {{printf "% 8d" .Delete.Min}} {{printf "% 8d" .Delete.Max}} {{printf "% 8d" .Delete.Avg}}/{{.SampleRate}}
GetMore {{printf "% 8d" .GetMore.Min}} {{printf "% 8d" .GetMore.Max}} {{printf "% 8d" .GetMore.Avg}}/{{.SampleRate}}
Command {{printf "% 8d" .Command.Min}} {{printf "% 8d" .Command.Max}} {{printf "% 8d" .Command.Avg}}/{{.SampleRate}}
`

View File

@@ -0,0 +1,10 @@
package templates
const Security = `
# Security #####################################################################################
Users {{.Users}}
Roles {{.Roles}}
Auth {{.Auth}}
SSL {{.SSL}}
`

View File

@@ -0,0 +1,10 @@
{
"Version": "3.0.11",
"VersionArray": [ 3, 0, 11, 0 ],
"GitVersion": "48f8b49dc30cc2485c6c1f3db31b723258fcbf39",
"SysInfo": "Linux build2.ny.cbi.10gen.cc 2.6.32-431.3.1.el6.x86_64 #1 SMP Fri Jan 3 21:39:27 UTC 2014 x86_64 BOOST_LIB_VERSION=1_49",
"Bits": 64,
"Debug": false,
"MaxObjectSize": 16777216
}

View File

@@ -0,0 +1,69 @@
{
"Argv": null,
"Ok": 0,
"Parsed": {
"Sharding": {
"ClusterRole": ""
},
"Storage": {
"DbPath": "",
"Engine": ""
},
"SystemLog": {
"Destination": "",
"Path": ""
},
"Net": {
"HTTP": {
"Enabled": false,
"Port": 0,
"JSONPEnabled": false,
"RESTInterfaceEnabled": false
},
"SSL": {
"SSLOnNormalPorts": false,
"Mode": "requireSSL",
"PEMKeyFile": "",
"PEMKeyPassword": "",
"ClusterFile": "",
"ClusterPassword": "",
"CAFile": "",
"CRLFile": "",
"AllowConnectionsWithoutCertificates": false,
"AllowInvalidCertificates": false,
"AllowInvalidHostnames": false,
"DisabledProtocols": "",
"FIPSMode": false
}
},
"ProcessManagement": {
"Fork": false
},
"Replication": {
"ReplSet": ""
}
},
"Security": {
"KeyFile": "",
"ClusterAuthMode": "",
"Authorization": "enabled",
"JavascriptEnabled": false,
"Sasl": {
"HostName": "",
"ServiceName": "",
"SaslauthdSocketPath": ""
},
"EnableEncryption": false,
"EncryptionCipherMode": "",
"EncryptionKeyFile": "",
"Kmip": {
"KeyIdentifier": "",
"RotateMasterKey": false,
"ServerName": "",
"Port": "",
"ClientCertificateFile": "",
"ClientCertificatePassword": "",
"ServerCAFile": ""
}
}
}

View File

@@ -0,0 +1,219 @@
{
"Info": "",
"Inprog": [
{
"Desc": "conn49",
"ConnectionId": 49,
"Opid": 1481,
"Msg": "",
"NumYields": 93,
"Locks": {
"Global": "",
"MMAPV1Journal": "",
"Database": "",
"Collection": "",
"Metadata": "",
"Oplog": ""
},
"WaitingForLock": 0,
"ThreadId": "0x2e60220",
"Active": 1,
"MicrosecsRunning": 857422,
"SecsRunning": 0,
"Op": "getmore",
"Ns": "samples.col1",
"Insert": null,
"PlanSummary": "",
"Client": "127.0.0.1:46290",
"Query": {
"CurrentOp": 0
},
"Progress": {
"Done": 0,
"Total": 0
},
"KillPending": 0,
"LockStats": {
"Global": {
"DeadlockCount": {
"Rr": 0,
"Ww": 0,
"R": 0,
"W": 0
},
"AcquireCount": {
"Rr": 186,
"Ww": 0,
"R": 0,
"W": 0
},
"AcquireWaitCount": {
"Rr": 0,
"Ww": 0,
"R": 0,
"W": 0
},
"TimeAcquiringMicros": {
"Rr": 0,
"Ww": 0,
"R": 0,
"W": 0
}
},
"MMAPV1Journal": {
"acquireCount": {
"r": 93
}
},
"Database": {
"acquireCount": {
"r": 93
}
}
}
},
{
"Desc": "conn53",
"ConnectionId": 53,
"Opid": 1442,
"Msg": "",
"NumYields": 0,
"Locks": {
"Global": "",
"MMAPV1Journal": "",
"Database": "",
"Collection": "",
"Metadata": "",
"Oplog": ""
},
"WaitingForLock": 0,
"ThreadId": "0x2e618e0",
"Active": 1,
"MicrosecsRunning": 4.668058e+06,
"SecsRunning": 4,
"Op": "getmore",
"Ns": "local.oplog.rs",
"Insert": null,
"PlanSummary": "",
"Client": "127.0.0.1:46300",
"Query": {
"CurrentOp": 0
},
"Progress": {
"Done": 0,
"Total": 0
},
"KillPending": 0,
"LockStats": {
"Global": {
"DeadlockCount": {
"Rr": 0,
"Ww": 0,
"R": 0,
"W": 0
},
"AcquireCount": {
"Rr": 10,
"Ww": 0,
"R": 0,
"W": 0
},
"AcquireWaitCount": {
"Rr": 0,
"Ww": 0,
"R": 0,
"W": 0
},
"TimeAcquiringMicros": {
"Rr": 0,
"Ww": 0,
"R": 0,
"W": 0
}
},
"MMAPV1Journal": {
"acquireCount": {
"r": 5
}
},
"Database": {
"acquireCount": {
"r": 5
}
}
}
},
{
"Desc": "conn51",
"ConnectionId": 51,
"Opid": 1433,
"Msg": "",
"NumYields": 0,
"Locks": {
"Global": "",
"MMAPV1Journal": "",
"Database": "",
"Collection": "",
"Metadata": "",
"Oplog": ""
},
"WaitingForLock": 0,
"ThreadId": "0x2e603c0",
"Active": 1,
"MicrosecsRunning": 4.689207e+06,
"SecsRunning": 4,
"Op": "getmore",
"Ns": "local.oplog.rs",
"Insert": null,
"PlanSummary": "",
"Client": "127.0.0.1:46296",
"Query": {
"CurrentOp": 0
},
"Progress": {
"Done": 0,
"Total": 0
},
"KillPending": 0,
"LockStats": {
"Global": {
"DeadlockCount": {
"Rr": 0,
"Ww": 0,
"R": 0,
"W": 0
},
"AcquireCount": {
"Rr": 10,
"Ww": 0,
"R": 0,
"W": 0
},
"AcquireWaitCount": {
"Rr": 0,
"Ww": 0,
"R": 0,
"W": 0
},
"TimeAcquiringMicros": {
"Rr": 0,
"Ww": 0,
"R": 0,
"W": 0
}
},
"MMAPV1Journal": {
"acquireCount": {
"r": 5
}
},
"Database": {
"acquireCount": {
"r": 5
}
}
}
}
],
"FsyncLock": 0
}

View File

@@ -0,0 +1 @@
null

View File

@@ -0,0 +1,14 @@
[
"actionlog",
"changelog",
"chunks",
"databases",
"lockpings",
"locks",
"mongos",
"settings",
"shards",
"system.indexes",
"tags",
"version"
]

View File

@@ -0,0 +1,6 @@
[
"col1",
"col2",
"system.indexes",
"system.profile"
]

View File

@@ -0,0 +1,5 @@
[
"system.indexes",
"testcol02",
"testcol2"
]

View File

@@ -0,0 +1 @@
null

View File

@@ -0,0 +1,30 @@
{
"Extra": {
"LibcVersion": "2.24",
"PageSize": 4096,
"VersionSignature": "Ubuntu 4.8.0-22.24-generic 4.8.0",
"NumPages": 4.090087e+06,
"VersionString": "Linux version 4.8.0-22-generic (buildd@lgw01-11) (gcc version 6.2.0 20161005 (Ubuntu 6.2.0-5ubuntu12) ) #24-Ubuntu SMP Sat Oct 8 09:15:00 UTC 2016",
"CpuFeatures": "fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc aperfmperf eagerfpu pni pclmulqdq dtes64 monitor ds_cpl vmx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm epb tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid xsaveopt dtherm ida arat pln pts",
"CpuFrequencyMHz": "2500.000",
"KernelVersion": "4.8.0-22-generic",
"MaxOpenFiles": 1024
},
"Os": {
"Type": "Linux",
"Version": "16.10",
"Name": "Ubuntu"
},
"System": {
"CurrentTime": "",
"Hostname": "karl-HP-ENVY",
"MemSizeMB": 15976,
"NumCores": 8,
"NumaEnabled": false,
"CpuAddrSize": 64,
"CpuArch": "x86_64"
},
"DatabasesCount": 0,
"CollectionsCount": 0,
"ID": 0
}

View File

@@ -0,0 +1,5 @@
{
"SetName": null,
"Hosts": null,
"Msg": "isdbgrid"
}

View File

@@ -0,0 +1,38 @@
{
"Databases": [
{
"Name": "samples",
"SizeOnDisk": 704643072,
"Empty": false,
"Shards": {
"r1": 218103808,
"r2": 486539264
}
},
{
"Name": "testdb",
"SizeOnDisk": 83886080,
"Empty": false,
"Shards": {
"r1": 83886080
}
},
{
"Name": "testdb02",
"SizeOnDisk": 83886080,
"Empty": false,
"Shards": {
"r2": 83886080
}
},
{
"Name": "config",
"SizeOnDisk": 67108864,
"Empty": false,
"Shards": null
}
],
"TotalSize": 872415232,
"TotalSizeMb": 832,
"OK": true
}

View File

@@ -0,0 +1,58 @@
{
"Date": "",
"MyState": 1,
"Term": 0,
"HeartbeatIntervalMillis": 0,
"Members": [
{
"Optime": null,
"OptimeDate": "",
"InfoMessage": "",
"Id": 0,
"Name": "localhost:18001",
"Health": 1,
"StateStr": "PRIMARY",
"Uptime": 13502,
"ConfigVersion": 1,
"Self": true,
"State": 1,
"ElectionTime": 6341767496013447169,
"ElectionDate": "",
"Set": ""
},
{
"Optime": null,
"OptimeDate": "",
"InfoMessage": "",
"Id": 1,
"Name": "localhost:18002",
"Health": 1,
"StateStr": "SECONDARY",
"Uptime": 13499,
"ConfigVersion": 1,
"Self": false,
"State": 2,
"ElectionTime": 0,
"ElectionDate": "",
"Set": ""
},
{
"Optime": null,
"OptimeDate": "",
"InfoMessage": "",
"Id": 2,
"Name": "localhost:18003",
"Health": 1,
"StateStr": "SECONDARY",
"Uptime": 13497,
"ConfigVersion": 1,
"Self": false,
"State": 2,
"ElectionTime": 0,
"ElectionDate": "",
"Set": ""
}
],
"Ok": 1,
"Set": "r2"
}

View File

@@ -0,0 +1,9 @@
{
"Date": "",
"MyState": 0,
"Term": 0,
"HeartbeatIntervalMillis": 0,
"Members": null,
"Ok": 0,
"Set": ""
}

View File

@@ -0,0 +1,58 @@
{
"Date": "",
"MyState": 1,
"Term": 0,
"HeartbeatIntervalMillis": 0,
"Members": [
{
"Optime": null,
"OptimeDate": "",
"InfoMessage": "",
"Id": 0,
"Name": "localhost:17001",
"Health": 1,
"StateStr": "PRIMARY",
"Uptime": 15959,
"ConfigVersion": 1,
"Self": true,
"State": 1,
"ElectionTime": 6341767483128545281,
"ElectionDate": "",
"Set": ""
},
{
"Optime": null,
"OptimeDate": "",
"InfoMessage": "",
"Id": 1,
"Name": "localhost:17002",
"Health": 1,
"StateStr": "SECONDARY",
"Uptime": 15955,
"ConfigVersion": 1,
"Self": false,
"State": 2,
"ElectionTime": 0,
"ElectionDate": "",
"Set": ""
},
{
"Optime": null,
"OptimeDate": "",
"InfoMessage": "",
"Id": 2,
"Name": "localhost:17003",
"Health": 1,
"StateStr": "SECONDARY",
"Uptime": 15953,
"ConfigVersion": 1,
"Self": false,
"State": 2,
"ElectionTime": 0,
"ElectionDate": "",
"Set": ""
}
],
"Ok": 1,
"Set": "r1"
}

View File

@@ -0,0 +1,58 @@
{
"Date": "",
"MyState": 1,
"Term": 0,
"HeartbeatIntervalMillis": 0,
"Members": [
{
"Optime": null,
"OptimeDate": "",
"InfoMessage": "",
"Id": 0,
"Name": "localhost:18001",
"Health": 1,
"StateStr": "PRIMARY",
"Uptime": 15955,
"ConfigVersion": 1,
"Self": true,
"State": 1,
"ElectionTime": 6341767496013447169,
"ElectionDate": "",
"Set": ""
},
{
"Optime": null,
"OptimeDate": "",
"InfoMessage": "",
"Id": 1,
"Name": "localhost:18002",
"Health": 1,
"StateStr": "SECONDARY",
"Uptime": 15952,
"ConfigVersion": 1,
"Self": false,
"State": 2,
"ElectionTime": 0,
"ElectionDate": "",
"Set": ""
},
{
"Optime": null,
"OptimeDate": "",
"InfoMessage": "",
"Id": 2,
"Name": "localhost:18003",
"Health": 1,
"StateStr": "SECONDARY",
"Uptime": 15950,
"ConfigVersion": 1,
"Self": false,
"State": 2,
"ElectionTime": 0,
"ElectionDate": "",
"Set": ""
}
],
"Ok": 1,
"Set": "r2"
}

View File

@@ -0,0 +1,58 @@
{
"Host": "karl-HP-ENVY",
"Version": "3.2.4",
"Process": "mongos",
"Pid": 2658,
"Uptime": 86400,
"UptimeMillis": 86399860,
"UptimeEstimate": 85467,
"LocalTime": "2016-10-19T11:29:55.826-03:00",
"Asserts": {
"msg": 0,
"regular": 0,
"rollovers": 0,
"user": 28,
"warning": 0
},
"BackgroundFlushing": null,
"ExtraInfo": {
"PageFaults": 10,
"HeapUsageBytes": 2.524536e+06,
"Note": "fields vary by platform"
},
"Connections": {
"Current": 3,
"Available": 816,
"TotalCreated": 65
},
"Dur": null,
"GlobalLock": null,
"Locks": null,
"Network": {
"BytesIn": 33608,
"BytesOut": 678809,
"NumRequests": 522
},
"Opcounters": {
"Insert": 0,
"Query": 22,
"Update": 0,
"Delete": 0,
"GetMore": 0,
"Command": 473
},
"OpcountersRepl": null,
"RecordStats": null,
"Mem": {
"Bits": 64,
"Resident": 21,
"Virtual": 228,
"Supported": true,
"Mapped": 0,
"MappedWithJournal": 0
},
"Repl": null,
"ShardCursorType": null,
"StorageEngine": null,
"WiredTiger": null
}

View File

@@ -0,0 +1,14 @@
{
"Shards": [
{
"ID": "r1",
"Host": "r1/localhost:17001,localhost:17002,localhost:17003"
},
{
"ID": "r2",
"Host": "r2/localhost:18001,localhost:18002,localhost:18003"
}
],
"OK": 1
}

View File

@@ -0,0 +1,8 @@
{
"name": "local.oplog.rs",
"options": {
"autoIndexId": false,
"capped": true,
"size": 1.7267414016e+10
}
}

View File

@@ -0,0 +1,69 @@
package test
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
)
func RootDir() (string, error) {
out, err := exec.Command("git", "rev-parse", "--show-toplevel").Output()
if err != nil {
rootdir, err := searchDir()
if err != nil {
return "", err
}
return rootdir, nil
}
return strings.Replace(string(out), "\n", "", -1), nil
}
func searchDir() (string, error) {
rootdir := ""
dir, err := os.Getwd()
if err != nil {
return "", err
}
for i := 0; i < 3; i++ {
if FileExists(dir + "/.git") {
rootdir = filepath.Clean(dir + "test")
break
}
dir = dir + "/.."
}
if rootdir == "" {
return "", fmt.Errorf("cannot detect root dir")
}
return rootdir, nil
}
func FileExists(file string) bool {
_, err := os.Lstat(file)
if err == nil {
return true
}
if os.IsNotExist(err) {
return false
}
return true
}
func LoadJson(filename string, destination interface{}) error {
dat, err := ioutil.ReadFile(filename)
if err != nil {
return err
}
err = json.Unmarshal(dat, &destination)
if err != nil {
return err
}
return nil
}

View File

@@ -0,0 +1,139 @@
{
"comment": "",
"ignore": "test",
"package": [
{
"checksumSHA1": "9NR0rrcAT5J76C5xMS4AVksS9o0=",
"path": "github.com/StackExchange/wmi",
"revision": "e54cbda6595d7293a7a468ccf9525f6bc8887f99",
"revisionTime": "2016-08-11T21:45:55Z"
},
{
"checksumSHA1": "wDZdTaY9JiqqqnF4c3pHP71nWmk=",
"path": "github.com/go-ole/go-ole",
"revision": "7dfdcf409020452e29b4babcbb22f984d2aa308a",
"revisionTime": "2016-07-29T03:38:29Z"
},
{
"checksumSHA1": "qLYVTQDhgrVIeZ2KI9eZV51mmug=",
"path": "github.com/go-ole/go-ole/oleutil",
"revision": "7dfdcf409020452e29b4babcbb22f984d2aa308a",
"revisionTime": "2016-07-29T03:38:29Z"
},
{
"checksumSHA1": "JSHl8b3nI8EWvzm+uyrIqj2Hiu4=",
"path": "github.com/golang/mock/gomock",
"revision": "bd3c8e81be01eef76d4b503f5e687d2d1354d2d9",
"revisionTime": "2016-01-21T18:51:14Z"
},
{
"checksumSHA1": "lJDwzzEBuS9sjxVOSsq7+rvw+cA=",
"path": "github.com/howeyc/gopass",
"revision": "f5387c492211eb133053880d23dfae62aa14123d",
"revisionTime": "2016-10-03T13:09:00Z"
},
{
"checksumSHA1": "nBmwoMSG67LapH7e79RepVUFS/I=",
"path": "github.com/pborman/getopt",
"revision": "e5fda1850301b0c7c7c38959116cbc7913bb00f3",
"revisionTime": "2016-08-19T15:59:08Z"
},
{
"checksumSHA1": "eBc6L91Na66Z5Avo4qloBpPcJFo=",
"path": "github.com/percona/toolkit-go/mongolib/proto",
"revision": "3cd401c8852ad074dbeac7efbef68422c16bfff7",
"revisionTime": "2016-10-30T21:11:33Z"
},
{
"checksumSHA1": "Hky3u+8Rqum+wB5BHMj0A8ZmT4g=",
"path": "github.com/pkg/errors",
"revision": "17b591df37844cde689f4d5813e5cea0927d8dd2",
"revisionTime": "2016-08-22T09:00:10Z"
},
{
"checksumSHA1": "G1oy2AGv5pCnNL0hRfg+mlX4Uq4=",
"path": "github.com/shirou/gopsutil/cpu",
"revision": "4d0c402af66c78735c5ccf820dc2ca7de5e4ff08",
"revisionTime": "2016-07-25T08:59:33Z"
},
{
"checksumSHA1": "/Pngfnhd3BiUzWRjrVOfsWpORRA=",
"path": "github.com/shirou/gopsutil/host",
"revision": "4d0c402af66c78735c5ccf820dc2ca7de5e4ff08",
"revisionTime": "2016-07-25T08:59:33Z"
},
{
"checksumSHA1": "jc2EYr0yhTJIdYJafcNs1ZEXk/o=",
"path": "github.com/shirou/gopsutil/internal/common",
"revision": "4d0c402af66c78735c5ccf820dc2ca7de5e4ff08",
"revisionTime": "2016-07-25T08:59:33Z"
},
{
"checksumSHA1": "LUipApIqOLcKbZfwXknNCifGles=",
"path": "github.com/shirou/gopsutil/mem",
"revision": "4d0c402af66c78735c5ccf820dc2ca7de5e4ff08",
"revisionTime": "2016-07-25T08:59:33Z"
},
{
"checksumSHA1": "k57srhFXKSysrFdEh3b6oiO48hw=",
"path": "github.com/shirou/gopsutil/net",
"revision": "4d0c402af66c78735c5ccf820dc2ca7de5e4ff08",
"revisionTime": "2016-07-25T08:59:33Z"
},
{
"checksumSHA1": "QBFpNvR0Z1r1ojPBgn6b8/PTbm0=",
"path": "github.com/shirou/gopsutil/process",
"revision": "4d0c402af66c78735c5ccf820dc2ca7de5e4ff08",
"revisionTime": "2016-07-25T08:59:33Z"
},
{
"checksumSHA1": "Nve7SpDmjsv6+rhkXAkfg/UQx94=",
"path": "github.com/shirou/w32",
"revision": "bb4de0191aa41b5507caa14b0650cdbddcd9280b",
"revisionTime": "2016-09-30T03:27:40Z"
},
{
"checksumSHA1": "9C4Av3ypK5pi173F76ogJT/d8x4=",
"path": "golang.org/x/crypto/ssh/terminal",
"revision": "b2fa06b6af4b7c9bfeb8569ab7b17f04550717bf",
"revisionTime": "2016-10-28T17:07:08Z"
},
{
"checksumSHA1": "u4rtURsE9Cn7TK4OPwH8axBMK6M=",
"path": "golang.org/x/sys/unix",
"revision": "8d1157a435470616f975ff9bb013bea8d0962067",
"revisionTime": "2016-10-06T02:47:49Z"
},
{
"checksumSHA1": "1D8GzeoFGUs5FZOoyC2DpQg8c5Y=",
"path": "gopkg.in/mgo.v2",
"revision": "3f83fa5005286a7fe593b055f0d7771a7dce4655",
"revisionTime": "2016-08-18T02:01:20Z"
},
{
"checksumSHA1": "YsB2DChSV9HxdzHaKATllAUKWSI=",
"path": "gopkg.in/mgo.v2/bson",
"revision": "3f83fa5005286a7fe593b055f0d7771a7dce4655",
"revisionTime": "2016-08-18T02:01:20Z"
},
{
"checksumSHA1": "XQsrqoNT1U0KzLxOFcAZVvqhLfk=",
"path": "gopkg.in/mgo.v2/internal/json",
"revision": "3f83fa5005286a7fe593b055f0d7771a7dce4655",
"revisionTime": "2016-08-18T02:01:20Z"
},
{
"checksumSHA1": "LEvMCnprte47qdAxWvQ/zRxVF1U=",
"path": "gopkg.in/mgo.v2/internal/sasl",
"revision": "3f83fa5005286a7fe593b055f0d7771a7dce4655",
"revisionTime": "2016-08-18T02:01:20Z"
},
{
"checksumSHA1": "+1WDRPaOphSCmRMxVPIPBV4aubc=",
"path": "gopkg.in/mgo.v2/internal/scram",
"revision": "3f83fa5005286a7fe593b055f0d7771a7dce4655",
"revisionTime": "2016-08-18T02:01:20Z"
}
],
"rootPath": "github.com/percona/toolkit-go/pt-mongodb-summary"
}

18
src/go/runtests.sh Executable file
View File

@@ -0,0 +1,18 @@
#!/bin/bash
set -e
export BASEDIR=$(git rev-parse --show-toplevel)
export CHECK_SESSIONS=0
cd $BASEDIR
for dir in $(ls -d pt-*)
do
echo "Running tests at $BASEDIR/$dir"
cd $BASEDIR/$dir
go get ./...
go test -v -coverprofile=coverage.out
if [ -f coverage.out ]
then
go tool cover -func=coverage.out
rm coverage.out
fi
done