|
| 1 | +package collector_mongod |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/prometheus/client_golang/prometheus" |
| 5 | + "github.com/prometheus/common/log" |
| 6 | + "gopkg.in/mgo.v2" |
| 7 | + "gopkg.in/mgo.v2/bson" |
| 8 | +) |
| 9 | + |
| 10 | +var ( |
| 11 | + collectionSize = prometheus.NewGaugeVec(prometheus.GaugeOpts{ |
| 12 | + Namespace: Namespace, |
| 13 | + Subsystem: "db_coll", |
| 14 | + Name: "size", |
| 15 | + Help: "The total size in memory of all records in a collection", |
| 16 | + }, []string{"db", "coll"}) |
| 17 | + collectionObjectCount = prometheus.NewGaugeVec(prometheus.GaugeOpts{ |
| 18 | + Namespace: Namespace, |
| 19 | + Subsystem: "db_coll", |
| 20 | + Name: "count", |
| 21 | + Help: "The number of objects or documents in this collection", |
| 22 | + }, []string{"db", "coll"}) |
| 23 | + collectionAvgObjSize = prometheus.NewGaugeVec(prometheus.GaugeOpts{ |
| 24 | + Namespace: Namespace, |
| 25 | + Subsystem: "db_coll", |
| 26 | + Name: "avgobjsize", |
| 27 | + Help: "The average size of an object in the collection (plus any padding)", |
| 28 | + }, []string{"db", "coll"}) |
| 29 | + collectionStorageSize = prometheus.NewGaugeVec(prometheus.GaugeOpts{ |
| 30 | + Namespace: Namespace, |
| 31 | + Subsystem: "db_coll", |
| 32 | + Name: "storage_size", |
| 33 | + Help: "The total amount of storage allocated to this collection for document storage", |
| 34 | + }, []string{"db", "coll"}) |
| 35 | + collectionIndexes = prometheus.NewGaugeVec(prometheus.GaugeOpts{ |
| 36 | + Namespace: Namespace, |
| 37 | + Subsystem: "db_coll", |
| 38 | + Name: "indexes", |
| 39 | + Help: "The number of indexes on the collection", |
| 40 | + }, []string{"db", "coll"}) |
| 41 | + collectionIndexesSize = prometheus.NewGaugeVec(prometheus.GaugeOpts{ |
| 42 | + Namespace: Namespace, |
| 43 | + Subsystem: "db_coll", |
| 44 | + Name: "indexes_size", |
| 45 | + Help: "The total size of all indexes", |
| 46 | + }, []string{"db", "coll"}) |
| 47 | +) |
| 48 | + |
| 49 | +// CollectionStatList contains stats from all collections |
| 50 | +type CollectionStatList struct { |
| 51 | + Members []CollectionStatus |
| 52 | +} |
| 53 | + |
| 54 | +// CollectionStatus represents stats about a collection in database (mongod and raw from mongos) |
| 55 | +type CollectionStatus struct { |
| 56 | + Database string |
| 57 | + Name string |
| 58 | + Size int `bson:"size,omitempty"` |
| 59 | + Count int `bson:"count,omitempty"` |
| 60 | + AvgObjSize int `bson:"avgObjSize,omitempty"` |
| 61 | + StorageSize int `bson:"storageSize,omitempty"` |
| 62 | + Indexes int `bson:"indexSizes,omitempty"` |
| 63 | + IndexesSize int `bson:"totalIndexSize,omitempty"` |
| 64 | +} |
| 65 | + |
| 66 | +// Export exports database stats to prometheus |
| 67 | +func (collStatList *CollectionStatList) Export(ch chan<- prometheus.Metric) { |
| 68 | + for _, member := range collStatList.Members { |
| 69 | + ls := prometheus.Labels{ |
| 70 | + "db": member.Database, |
| 71 | + "coll": member.Name, |
| 72 | + } |
| 73 | + collectionSize.With(ls).Set(float64(member.Size)) |
| 74 | + collectionObjectCount.With(ls).Set(float64(member.Count)) |
| 75 | + collectionAvgObjSize.With(ls).Set(float64(member.AvgObjSize)) |
| 76 | + collectionStorageSize.With(ls).Set(float64(member.StorageSize)) |
| 77 | + collectionIndexes.With(ls).Set(float64(member.Indexes)) |
| 78 | + collectionIndexesSize.With(ls).Set(float64(member.IndexesSize)) |
| 79 | + } |
| 80 | + collectionSize.Collect(ch) |
| 81 | + collectionObjectCount.Collect(ch) |
| 82 | + collectionAvgObjSize.Collect(ch) |
| 83 | + collectionStorageSize.Collect(ch) |
| 84 | + collectionIndexes.Collect(ch) |
| 85 | + collectionIndexesSize.Collect(ch) |
| 86 | +} |
| 87 | + |
| 88 | +// Describe describes database stats for prometheus |
| 89 | +func (collStatList *CollectionStatList) Describe(ch chan<- *prometheus.Desc) { |
| 90 | + collectionSize.Describe(ch) |
| 91 | + collectionObjectCount.Describe(ch) |
| 92 | + collectionAvgObjSize.Describe(ch) |
| 93 | + collectionStorageSize.Describe(ch) |
| 94 | + collectionIndexes.Describe(ch) |
| 95 | + collectionIndexesSize.Describe(ch) |
| 96 | +} |
| 97 | + |
| 98 | +// GetDatabaseStatus returns stats for a given database |
| 99 | +func GetCollectionStatList(session *mgo.Session) *CollectionStatList { |
| 100 | + collectionStatList := &CollectionStatList{} |
| 101 | + database_names, err := session.DatabaseNames() |
| 102 | + if err != nil { |
| 103 | + log.Error("Failed to get database names") |
| 104 | + return nil |
| 105 | + } |
| 106 | + for _, db := range database_names { |
| 107 | + collection_names, err := session.DB(db).CollectionNames() |
| 108 | + if err != nil { |
| 109 | + log.Error("Failed to get collection names for db=" + db) |
| 110 | + return nil |
| 111 | + } |
| 112 | + for _, collection_name := range collection_names { |
| 113 | + collStatus := CollectionStatus{} |
| 114 | + err := session.DB(db).Run(bson.D{{"collStats", collection_name}, {"scale", 1}}, &collStatus) |
| 115 | + collStatus.Database = db |
| 116 | + collStatus.Name = collection_name |
| 117 | + if err != nil { |
| 118 | + log.Error("Failed to get collection status.") |
| 119 | + return nil |
| 120 | + } |
| 121 | + collectionStatList.Members = append(collectionStatList.Members, collStatus) |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + return collectionStatList |
| 126 | +} |
0 commit comments