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 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
|
package collector
import (
"time"
"github.com/golang/glog"
"github.com/prometheus/client_golang/prometheus"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
var (
instanceUptimeSeconds = prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: Namespace,
Subsystem: "instance",
Name: "uptime_seconds",
Help: "The value of the uptime field corresponds to the number of seconds that the mongos or mongod process has been active.",
})
instanceUptimeEstimateSeconds = prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: Namespace,
Subsystem: "instance",
Name: "uptime_estimate_seconds",
Help: "uptimeEstimate provides the uptime as calculated from MongoDB's internal course-grained time keeping system.",
})
instanceLocalTime = prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: Namespace,
Subsystem: "instance",
Name: "local_time",
Help: "The localTime value is the current time, according to the server, in UTC specified in an ISODate format.",
})
)
// ServerStatus keeps the data returned by the serverStatus() method.
type ServerStatus struct {
Uptime float64 `bson:"uptime"`
UptimeEstimate float64 `bson:"uptimeEstimate"`
LocalTime time.Time `bson:"localTime"`
Asserts *AssertsStats `bson:"asserts"`
Dur *DurStats `bson:"dur"`
BackgroundFlushing *FlushStats `bson:"backgroundFlushing"`
Connections *ConnectionStats `bson:"connections"`
ExtraInfo *ExtraInfo `bson:"extra_info"`
GlobalLock *GlobalLockStats `bson:"globalLock"`
IndexCounter *IndexCounterStats `bson:"indexCounters"`
Locks LockStatsMap `bson:"locks,omitempty"`
Network *NetworkStats `bson:"network"`
Opcounters *OpcountersStats `bson:"opcounters"`
OpcountersRepl *OpcountersReplStats `bson:"opcountersRepl"`
Mem *MemStats `bson:"mem"`
Metrics *MetricsStats `bson:"metrics"`
Cursors *Cursors `bson:"cursors"`
StorageEngine *StorageEngineStats `bson:"storageEngine"`
WiredTiger *WiredTigerStats `bson:"wiredTiger"`
}
// Export exports the server status to be consumed by prometheus.
func (status *ServerStatus) Export(ch chan<- prometheus.Metric) {
instanceUptimeSeconds.Set(status.Uptime)
instanceUptimeEstimateSeconds.Set(status.Uptime)
instanceLocalTime.Set(float64(status.LocalTime.Unix()))
instanceUptimeSeconds.Collect(ch)
instanceUptimeEstimateSeconds.Collect(ch)
instanceLocalTime.Collect(ch)
if status.Asserts != nil {
status.Asserts.Export(ch)
}
if status.Dur != nil {
status.Dur.Export(ch)
}
if status.BackgroundFlushing != nil {
status.BackgroundFlushing.Export(ch)
}
if status.Connections != nil {
status.Connections.Export(ch)
}
if status.ExtraInfo != nil {
status.ExtraInfo.Export(ch)
}
if status.GlobalLock != nil {
status.GlobalLock.Export(ch)
}
if status.IndexCounter != nil {
status.IndexCounter.Export(ch)
}
if status.Network != nil {
status.Network.Export(ch)
}
if status.Opcounters != nil {
status.Opcounters.Export(ch)
}
if status.OpcountersRepl != nil {
status.OpcountersRepl.Export(ch)
}
if status.Mem != nil {
status.Mem.Export(ch)
}
if status.Locks != nil {
status.Locks.Export(ch)
}
if status.Metrics != nil {
status.Metrics.Export(ch)
}
if status.Cursors != nil {
status.Cursors.Export(ch)
}
if status.WiredTiger != nil {
status.WiredTiger.Export(ch)
}
// If db.serverStatus().storageEngine does not exist (3.0+ only) and status.BackgroundFlushing does (MMAPv1 only), default to mmapv1
// https://docs.mongodb.com/v3.0/reference/command/serverStatus/#storageengine
if status.StorageEngine == nil && status.BackgroundFlushing != nil {
status.StorageEngine = &StorageEngineStats{
Name: "mmapv1",
}
}
if status.StorageEngine != nil {
status.StorageEngine.Export(ch)
}
}
// Describe describes the server status for prometheus.
func (status *ServerStatus) Describe(ch chan<- *prometheus.Desc) {
instanceUptimeSeconds.Describe(ch)
instanceUptimeEstimateSeconds.Describe(ch)
instanceLocalTime.Describe(ch)
if status.Asserts != nil {
status.Asserts.Describe(ch)
}
if status.Dur != nil {
status.Dur.Describe(ch)
}
if status.BackgroundFlushing != nil {
status.BackgroundFlushing.Describe(ch)
}
if status.Connections != nil {
status.Connections.Describe(ch)
}
if status.ExtraInfo != nil {
status.ExtraInfo.Describe(ch)
}
if status.GlobalLock != nil {
status.GlobalLock.Describe(ch)
}
if status.IndexCounter != nil {
status.IndexCounter.Describe(ch)
}
if status.Network != nil {
status.Network.Describe(ch)
}
if status.Opcounters != nil {
status.Opcounters.Describe(ch)
}
if status.OpcountersRepl != nil {
status.OpcountersRepl.Describe(ch)
}
if status.Mem != nil {
status.Mem.Describe(ch)
}
if status.Locks != nil {
status.Locks.Describe(ch)
}
if status.Metrics != nil {
status.Metrics.Describe(ch)
}
if status.Cursors != nil {
status.Cursors.Describe(ch)
}
if status.WiredTiger != nil {
status.WiredTiger.Describe(ch)
}
if status.StorageEngine != nil {
status.StorageEngine.Describe(ch)
}
}
// GetServerStatus returns the server status info.
func GetServerStatus(session *mgo.Session) *ServerStatus {
result := &ServerStatus{}
err := session.DB("admin").Run(bson.D{{"serverStatus", 1}, {"recordStats", 0}}, result)
if err != nil {
glog.Error("Failed to get server status.")
return nil
}
return result
}
|