forked from kaz/conoha_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exporter.go
132 lines (114 loc) · 4.76 KB
/
exporter.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
126
127
128
129
130
131
132
package main
import (
"log"
"sync"
"time"
"github.com/prometheus/client_golang/prometheus"
)
type ConohaCollector struct {
*ConohaClient
*sync.RWMutex
describes []*prometheus.Desc
metrics []prometheus.Metric
databases []*Database
}
func NewConohaCollector(client *ConohaClient) (*ConohaCollector, error) {
// データベース一覧を取得
databases, err := client.Databases()
if err != nil {
return nil, err
}
// 提供するメトリクスの情報などを定義
return &ConohaCollector{
client,
&sync.RWMutex{},
[]*prometheus.Desc{
// NewDescの3番目の引数は可変ラベル(NewConstMetricの最後の可変長引数に対応してる)
// 4番目のnilには、固定ラベルをprometheus.Labelsで渡せる
prometheus.NewDesc("object_storage_requests", "Requests to Object Storage", []string{"method"}, nil),
prometheus.NewDesc("object_storage_quota", "Object Storage Quota", []string{}, nil),
prometheus.NewDesc("object_storage_total_usage", "Total Object Storage Usage", []string{}, nil),
prometheus.NewDesc("object_storage_usage", "Usage of Object Storage", []string{"container"}, nil),
prometheus.NewDesc("object_storage_count", "Object Counts of Object Storage", []string{"container"}, nil),
prometheus.NewDesc("database_usage", "Usage of Database (GB)", []string{"database"}, nil),
prometheus.NewDesc("database_quota", "Database Quota (GB)", []string{"service"}, nil),
prometheus.NewDesc("database_total_usage", "Total Database Usage (GB)", []string{"service"}, nil),
prometheus.NewDesc("total_deposit_amount", "Total Deposit Amount", []string{}, nil),
},
[]prometheus.Metric{},
databases,
}, nil
}
func (cc *ConohaCollector) AutoUpdate() {
for {
metrics := make([]prometheus.Metric, 0)
// オブジェクトストレージへのリクエスト数を取得
requests, err := cc.ObjectStorageRequests()
if err != nil {
log.Printf("ObjectStorageRequests error: %v\n", err)
} else {
metrics = append(metrics, prometheus.MustNewConstMetric(cc.describes[0], prometheus.GaugeValue, requests["get"], "get"))
metrics = append(metrics, prometheus.MustNewConstMetric(cc.describes[0], prometheus.GaugeValue, requests["put"], "put"))
metrics = append(metrics, prometheus.MustNewConstMetric(cc.describes[0], prometheus.GaugeValue, requests["delete"], "delete"))
}
// オブジェクトストレージ使用容量を取得
usage, err := cc.ObjectStorageUsage()
if err != nil {
log.Printf("ObjectStorageUsage error: %v\n", err)
} else {
metrics = append(metrics, prometheus.MustNewConstMetric(cc.describes[1], prometheus.GaugeValue, usage.quota))
metrics = append(metrics, prometheus.MustNewConstMetric(cc.describes[2], prometheus.GaugeValue, usage.totalUsage))
for _, container := range usage.containers {
metrics = append(metrics, prometheus.MustNewConstMetric(cc.describes[3], prometheus.GaugeValue, float64(container.Bytes), container.Name))
metrics = append(metrics, prometheus.MustNewConstMetric(cc.describes[4], prometheus.GaugeValue, float64(container.Count), container.Name))
}
}
serviceIDs := make(map[string]bool)
for _, db := range cc.databases {
// データベース使用状況を取得
info, err := cc.DatabaseInfo(db.DatabaseID)
if err != nil {
log.Printf("DatabaseInfo(%s) error: %v\n", db.DatabaseID, err)
} else {
metrics = append(metrics, prometheus.MustNewConstMetric(cc.describes[5], prometheus.GaugeValue, info.DbSize, db.DbName))
serviceIDs[db.ServiceID] = true
}
}
for serviceID := range serviceIDs {
// データベース上限値/合計使用量取得
quota, err := cc.DatabaseQuota(serviceID)
if err != nil {
log.Printf("DatabaseQuota(%s) error: %v\n", serviceID, err)
} else {
metrics = append(metrics, prometheus.MustNewConstMetric(cc.describes[6], prometheus.GaugeValue, float64(quota.Quota), serviceID))
metrics = append(metrics, prometheus.MustNewConstMetric(cc.describes[7], prometheus.GaugeValue, float64(quota.TotalUsage), serviceID))
}
}
deposit, err := cc.BillingPaymentSummary()
if err != nil {
log.Printf("BillingPaymentSumarry error: %v\n", err)
} else {
metrics = append(metrics, prometheus.MustNewConstMetric(cc.describes[8], prometheus.GaugeValue, float64(deposit.Deposit)))
}
// メトリクスデータ更新
cc.Lock()
cc.metrics = metrics
cc.Unlock()
log.Println("Metrics updated.")
// 70秒間待機(ConoHa API側の更新間隔)
<-time.NewTimer(70 * time.Second).C
}
}
// 内部で保持しているデータを返す
func (cc *ConohaCollector) Describe(ch chan<- *prometheus.Desc) {
for _, d := range cc.describes {
ch <- d
}
}
func (cc *ConohaCollector) Collect(ch chan<- prometheus.Metric) {
cc.RLock()
defer cc.RUnlock()
for _, m := range cc.metrics {
ch <- m
}
}