forked from percona/mongodb_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
database_status.go
125 lines (113 loc) · 3.83 KB
/
database_status.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package mongos
import (
"strings"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/log"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
var (
indexSize = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: Namespace,
Subsystem: "db",
Name: "index_size_bytes",
Help: "The total size in bytes of all indexes created on this database",
}, []string{"db", "shard"})
dataSize = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: Namespace,
Subsystem: "db",
Name: "data_size_bytes",
Help: "The total size in bytes of the uncompressed data held in this database",
}, []string{"db", "shard"})
collectionsTotal = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: Namespace,
Subsystem: "db",
Name: "collections_total",
Help: "Contains a count of the number of collections in that database",
}, []string{"db", "shard"})
indexesTotal = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: Namespace,
Subsystem: "db",
Name: "indexes_total",
Help: "Contains a count of the total number of indexes across all collections in the database",
}, []string{"db", "shard"})
objectsTotal = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: Namespace,
Subsystem: "db",
Name: "objects_total",
Help: "Contains a count of the number of objects (i.e. documents) in the database across all collections",
}, []string{"db", "shard"})
)
// DatabaseStatList contains stats from all databases
type DatabaseStatList struct {
Members []DatabaseStatus
}
// DatabaseStatus represents stats about a database (mongod and raw from mongos)
type DatabaseStatus struct {
RawStatus // embed to collect top-level attributes
Shards map[string]*RawStatus `bson:"raw,omitempty"`
}
// RawStatus represents stats about a database from Mongos side
type RawStatus struct {
Name string `bson:"db,omitempty"`
IndexSize int `bson:"indexSize,omitempty"`
DataSize int `bson:"dataSize,omitempty"`
Collections int `bson:"collections,omitempty"`
Objects int `bson:"objects,omitempty"`
Indexes int `bson:"indexes,omitempty"`
}
// Export exports database stats to prometheus
func (dbStatList *DatabaseStatList) Export(ch chan<- prometheus.Metric) {
for _, member := range dbStatList.Members {
if len(member.Shards) > 0 {
for shard, stats := range member.Shards {
ls := prometheus.Labels{
"db": stats.Name,
"shard": strings.Split(shard, "/")[0],
}
indexSize.With(ls).Set(float64(stats.IndexSize))
dataSize.With(ls).Set(float64(stats.DataSize))
collectionsTotal.With(ls).Set(float64(stats.Collections))
indexesTotal.With(ls).Set(float64(stats.Indexes))
objectsTotal.With(ls).Set(float64(stats.Objects))
}
}
}
indexSize.Collect(ch)
dataSize.Collect(ch)
collectionsTotal.Collect(ch)
indexesTotal.Collect(ch)
objectsTotal.Collect(ch)
indexSize.Reset()
dataSize.Reset()
collectionsTotal.Reset()
indexesTotal.Reset()
objectsTotal.Reset()
}
// Describe describes database stats for prometheus
func (dbStatList *DatabaseStatList) Describe(ch chan<- *prometheus.Desc) {
indexSize.Describe(ch)
dataSize.Describe(ch)
collectionsTotal.Describe(ch)
indexesTotal.Describe(ch)
objectsTotal.Describe(ch)
}
// GetDatabaseStatList returns stats for all databases
func GetDatabaseStatList(session *mgo.Session) *DatabaseStatList {
dbStatList := &DatabaseStatList{}
database_names, err := session.DatabaseNames()
if err != nil {
log.Error("Failed to get database names")
return nil
}
for _, db := range database_names {
dbStatus := DatabaseStatus{}
err := session.DB(db).Run(bson.D{{"dbStats", 1}, {"scale", 1}}, &dbStatus)
if err != nil {
log.Error("Failed to get database status.")
return nil
}
dbStatList.Members = append(dbStatList.Members, dbStatus)
}
return dbStatList
}