diff --git a/client.go b/client.go index cd849d4..3fe3db2 100644 --- a/client.go +++ b/client.go @@ -157,6 +157,7 @@ type Usage struct { Data [][]float64 `json:"data"` } +// オブジェクトストレージへのリクエスト数を取得 func (cc *ConohaClient) ObjectStorageRequests() (map[string]float64, error) { // メトリクス取得 resp, err := cc.get(cc.accountEndpoint + "/object-storage/rrd/request") @@ -185,6 +186,7 @@ type ObjectStorageSizeResponse struct { Size Usage `json:"size"` } +// オブジェクトストレージの使用容量を取得 func (cc *ConohaClient) ObjectStorageUsage() (map[string]float64, error) { // メトリクス取得 resp, err := cc.get(cc.accountEndpoint + "/object-storage/rrd/size") @@ -227,8 +229,8 @@ type Database struct { Type string `json:"type"` } +// データベース一覧取得 func (cc *ConohaClient) Databases() ([]*Database, error) { - // データベース一覧取得 resp, err := cc.get(cc.databaseHostingEndpoint + "/databases") if err != nil { return nil, err @@ -259,9 +261,9 @@ type Quota struct { Quota int `json:"quota"` } -func (cc *ConohaClient) DatabaseQuota(d *Database) (*Quota, error) { - // データベース使用容量/上限値取得(GB単位) - resp, err := cc.get(cc.databaseHostingEndpoint + "/services/" + d.ServiceID + "/quotas") +// データベース上限値取得(GB単位) +func (cc *ConohaClient) DatabaseQuota(serviceID string) (*Quota, error) { + resp, err := cc.get(cc.databaseHostingEndpoint + "/services/" + serviceID + "/quotas") if err != nil { return nil, err } @@ -274,3 +276,24 @@ func (cc *ConohaClient) DatabaseQuota(d *Database) (*Quota, error) { return &uResp.Quota, nil } + +// JSON 受け取り用 +type DatabaseInfoResponse struct { + Database Database `json:"database"` +} + +// データベース情報取得(GB単位) +func (cc *ConohaClient) DatabaseInfo(databaseID string) (*Database, error) { + resp, err := cc.get(cc.databaseHostingEndpoint + "/databases/" + databaseID) + if err != nil { + return nil, err + } + + // JSONを読む + var uResp DatabaseInfoResponse + if err := json.Unmarshal(resp, &uResp); err != nil { + return nil, err + } + + return &uResp.Database, nil +} diff --git a/exporter.go b/exporter.go index 1a57e48..992965f 100644 --- a/exporter.go +++ b/exporter.go @@ -32,7 +32,7 @@ func NewConohaCollector(client *ConohaClient) (*ConohaCollector, error) { // 4番目のnilには、固定ラベルをprometheus.Labelsで渡せる prometheus.NewDesc("object_storage_requests", "Requests to Object Storage", []string{"method"}, nil), prometheus.NewDesc("object_storage_usage", "Usage of Object Storage", []string{}, nil), - prometheus.NewDesc("database_usage", "Usage of Database Server (GB)", []string{"database"}, nil), + prometheus.NewDesc("database_usage", "Usage of Database (GB)", []string{"database"}, nil), }, []prometheus.Metric{}, databases, @@ -56,13 +56,26 @@ func (cc *ConohaCollector) AutoUpdate() { usage, err := cc.ObjectStorageUsage() metrics = append(metrics, prometheus.MustNewConstMetric(cc.describes[1], prometheus.GaugeValue, usage["value"])) + serviceIDs := make(map[string]bool) + for _, db := range cc.databases { // データベース使用状況を取得 - quota, err := cc.DatabaseQuota(db) + info, err := cc.DatabaseInfo(db.DatabaseID) + if err != nil { + log.Fatal(err) + } + metrics = append(metrics, prometheus.MustNewConstMetric(cc.describes[2], prometheus.GaugeValue, info.DbSize, db.DbName)) + + serviceIDs[db.ServiceID] = true + } + + for serviceID := range serviceIDs { + // データベース上限値取得 + quota, err := cc.DatabaseQuota(serviceID) if err != nil { log.Fatal(err) } - metrics = append(metrics, prometheus.MustNewConstMetric(cc.describes[2], prometheus.GaugeValue, quota.TotalUsage, db.DbName)) + metrics = append(metrics, prometheus.MustNewConstMetric(cc.describes[2], prometheus.GaugeValue, float64(quota.Quota), "Quota "+serviceID)) } // メトリクスデータ更新