-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongostatus.go
60 lines (53 loc) · 1.24 KB
/
mongostatus.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package main
// DASTools: mongostatus returns string with basic MongoDB stats
// Copyright (c) 2018 - Valentin Kuznetsov <vkuznet AT gmail dot com>
import (
"flag"
"fmt"
"os"
"runtime"
"time"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
func info() string {
goVersion := runtime.Version()
tstamp := time.Now()
return fmt.Sprintf("git={{VERSION}} go=%s date=%s", goVersion, tstamp)
}
func main() {
var version bool
flag.BoolVar(&version, "version", false, "Show version")
var port int
flag.IntVar(&port, "port", 8230, "MongoDB port")
flag.Parse()
if version {
fmt.Println(info())
return
}
uri := fmt.Sprintf("mongodb://localhost:%d", port)
session, err := mgo.Dial(uri)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer session.Close()
r := bson.M{}
if err := session.DB("admin").Run(bson.D{{"serverStatus", 1}}, &r); err != nil {
fmt.Println(err)
os.Exit(1)
}
proc, _ := r["process"]
pid, _ := r["pid"]
ver, _ := r["version"]
host, _ := r["host"]
c, _ := r["connections"]
con := c.(bson.M)
cons := con["totalCreated"]
s, _ := r["ok"]
status := fmt.Sprintf("%v", s)
if s == 1. {
status = "ok"
}
fmt.Printf("Process:%s, PID:%d, Version:%s, Host:%s, Connections:%d, Status:%s\n", proc, pid, ver, host, cons, status)
}