-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnetapp_exporter.go
385 lines (332 loc) · 10.3 KB
/
netapp_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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
package main
import (
"fmt"
"io/ioutil"
"net/http"
"os"
"strconv"
"time"
"github.com/pepabo/go-netapp/netapp"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"gopkg.in/alecthomas/kingpin.v2"
"gopkg.in/yaml.v2"
)
const namespace string = "netapp"
var (
diskLimit = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "quota_disk_limit_kbytes"),
"Qtree disk soft limit in kbytes",
[]string{"qtree", "volume", "vserver"},
nil,
)
diskUsed = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "quota_disk_use_kbytes"),
"Qtree disk current use in kbytes",
[]string{"qtree", "volume", "vserver"},
nil,
)
fileLimit = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "quota_file_limit"),
"Qtree number of file soft limit",
[]string{"qtree", "volume", "vserver"},
nil,
)
fileUsed = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "quota_file_use"),
"Qtree number of file soft limit",
[]string{"qtree", "volume", "vserver"},
nil,
)
status = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "quota_status"),
"Quota status of volume",
[]string{"volume", "vserver", "status"},
nil,
)
volumeTotalUsedBytes = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "volume_total_used_kbytes"),
"Total usage of volume (kbytes)",
[]string{"volume", "vserver"},
nil,
)
volumeTotalUseRate = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "volume_total_use_rate"),
"Total use rate of volume",
[]string{"volume", "vserver"},
nil,
)
volumePhysicalUsedBytes = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "volume_physical_used_kbytes"),
"Physical usage of volume (kbytes)",
[]string{"volume", "vserver"},
nil,
)
volumePhysicalUseRate = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "volume_physical_use_rate"),
"Physical use rate of volume",
[]string{"volume", "vserver"},
nil,
)
volumeUserUsedBytes = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "volume_user_used_kbytes"),
"User usage of volume (kbytes)",
[]string{"volume", "vserver"},
nil,
)
volumeUserUseRate = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "volume_user_use_rate"),
"User use rate of volume",
[]string{"volume", "vserver"},
nil,
)
volumeFilesystemMetadataUsedBytes = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "volume_filesystem_metadata_used_kbytes"),
"FilesystemMetadata usage of volume (kbytes)",
[]string{"volume", "vserver"},
nil,
)
volumeFilesystemMetadataUseRate = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "volume_filesystem_metadata_use_rate"),
"FilesystemMetadata use rate of volume",
[]string{"volume", "vserver"},
nil,
)
volumePerformanceMetadataUsedBytes = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "volume_performance_metadata_used_kbytes"),
"PerformanceMetadata usage of volume (kbytes)",
[]string{"volume", "vserver"},
nil,
)
volumePerformanceMetadataUseRate = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "volume_performance_metadata_use_rate"),
"PerformanceMetadata use rate of volume",
[]string{"volume", "vserver"},
nil,
)
volumeSnapshotReserveUsedBytes = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "volume_snapshot_reserve_used_kbytes"),
"Snapshot reserve usage of volume (kbytes)",
[]string{"volume", "vserver"},
nil,
)
volumeSnapshotReserveUseRate = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "volume_snapshot_reserve_use_rate"),
"Snapshot reserve use rate of volume",
[]string{"volume", "vserver"},
nil,
)
)
type quotaCollector struct {
client *netapp.Client
conditions []QuotaSeachCondition
}
func (m *quotaCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- diskLimit
ch <- diskUsed
ch <- fileLimit
ch <- fileUsed
ch <- status
}
// go-netappからの取得値はint or stringなので、exporter用にfloat64に変換する
func toFloat(val interface{}) (float64, error) {
floatVal, isFloat := val.(float64)
if isFloat {
// already float
return floatVal, nil
}
intVal, isInt := val.(int)
if isInt {
return (float64)(intVal), nil
}
strVal, isStr := val.(string)
if isStr {
intVal, err := strconv.Atoi(strVal)
if err != nil {
return 0.0, err
}
return (float64)(intVal), nil
}
return 0.0, fmt.Errorf("value (%v) is neither Int or String or Float", val)
}
func sendMetric(desc *prometheus.Desc, value interface{}, labels []string, ch chan<- prometheus.Metric) {
metricVal, err := toFloat(value)
if err != nil {
return
}
ch <- prometheus.MustNewConstMetric(
desc,
prometheus.GaugeValue,
metricVal,
labels...,
)
}
func sendQuotaMetric(desc *prometheus.Desc, value interface{}, quota netapp.QuotaReportEntry, ch chan<- prometheus.Metric) {
sendMetric(desc, value, []string{quota.Tree, quota.Volume, quota.Vserver}, ch)
}
func sendVolumeMetric(desc *prometheus.Desc, value interface{}, volume netapp.VolumeSpaceInfo, ch chan<- prometheus.Metric) {
sendMetric(desc, value, []string{volume.Volume, volume.Vserver}, ch)
}
func sendVolumeRateMetric(desc *prometheus.Desc, value interface{}, volume netapp.VolumeSpaceInfo, ch chan<- prometheus.Metric) {
floatVal, err := toFloat(value)
if err != nil {
return
}
// go-netappからはパーセントで取得されるので、比率に変換する(exporterのベストプラクティス)
sendVolumeMetric(desc, floatVal/100, volume, ch)
}
func (c quotaCollector) Collect(ch chan<- prometheus.Metric) {
volumes, err := c.GetVolumeSpaces()
if err == nil {
for _, v := range volumes {
// export volume value
sendVolumeMetric(volumeTotalUsedBytes, v.TotalUsed, v, ch)
sendVolumeMetric(volumePhysicalUsedBytes, v.PhysicalUsed, v, ch)
sendVolumeMetric(volumeUserUsedBytes, v.UserData, v, ch)
sendVolumeMetric(volumeFilesystemMetadataUsedBytes, v.FilesystemMetadata, v, ch)
sendVolumeMetric(volumePerformanceMetadataUsedBytes, v.PerformanceMetadata, v, ch)
sendVolumeMetric(volumeSnapshotReserveUsedBytes, v.SnapshotReserve, v, ch)
// export volume use rate
sendVolumeRateMetric(volumeTotalUseRate, v.TotalUsedPercent, v, ch)
sendVolumeRateMetric(volumePhysicalUseRate, v.PhysicalUsedPercent, v, ch)
sendVolumeRateMetric(volumeUserUseRate, v.UserDataPercent, v, ch)
sendVolumeRateMetric(volumeFilesystemMetadataUseRate, v.FilesystemMetadataPercent, v, ch)
sendVolumeRateMetric(volumePerformanceMetadataUseRate, v.PerformanceMetadataPercent, v, ch)
sendVolumeRateMetric(volumeSnapshotReserveUseRate, v.SnapshotReservePercent, v, ch)
s, err := c.GetQuotaStatus(v)
if err != nil {
continue
}
if s == "" {
continue
}
ch <- prometheus.MustNewConstMetric(status, prometheus.GaugeValue, 1, v.Volume, v.Vserver, s)
}
}
for _, condition := range c.conditions {
quotas, err := c.GetQuotas(condition)
if err != nil {
fmt.Println(err.Error())
return
}
for _, q := range quotas {
// export quota metrics
sendQuotaMetric(diskLimit, q.DiskLimit, q, ch)
sendQuotaMetric(diskUsed, q.DiskUsed, q, ch)
sendQuotaMetric(fileLimit, q.FileLimit, q, ch)
sendQuotaMetric(fileUsed, q.FilesUsed, q, ch)
}
}
}
func (c quotaCollector) GetQuotas(q QuotaSeachCondition) ([]netapp.QuotaReportEntry, error) {
nextTag := ""
qtrees := []netapp.QuotaReportEntry{}
for {
qRes, _, err := c.client.QuotaReport.Report(&netapp.QuotaReportOptions{
MaxRecords: 1000,
Query: &netapp.QuotaReportEntryQuery{
QuotaReportEntry: &netapp.QuotaReportEntry{
Volume: q.Volume,
Tree: q.Qtree,
Vserver: q.Vserver,
},
},
Tag: nextTag,
})
if err != nil {
return nil, err
}
qtrees = append(qtrees, qRes.Results.AttributesList.QuotaReportEntry...)
nextTag = qRes.Results.NextTag
if nextTag == "" {
break
}
}
return qtrees, nil
}
func (c quotaCollector) GetVolumeSpaces() ([]netapp.VolumeSpaceInfo, error) {
r, _, err := c.client.VolumeSpace.List(nil)
if err != nil {
return nil, err
}
return r.Results.AttributesList.SpaceInfo, nil
}
func (c quotaCollector) GetQuotaStatus(v netapp.VolumeSpaceInfo) (string, error) {
r, _, err := c.client.Quota.Status(v.Vserver, v.Volume)
if err != nil {
return "", err
}
return r.Results.QuotaStatus, nil
}
type QuotaSeachCondition struct {
Qtree string
Volume string
Vserver string
}
func NewQuotaCollector(endpoint, user, password string, conditions []QuotaSeachCondition) (*quotaCollector, error) {
client, err := netapp.NewClient(
endpoint,
"1.20",
&netapp.ClientOptions{
BasicAuthUser: user,
BasicAuthPassword: password,
SSLVerify: true,
Timeout: 10 * time.Second,
},
)
if err != nil {
return nil, err
}
if len(conditions) == 0 {
conditions = []QuotaSeachCondition{{}}
}
return "aCollector{
client: client,
conditions: conditions,
}, nil
}
type SearchConfig struct {
QuotaSearchCondition []QuotaSeachCondition `yaml:"quota_search_condition"`
}
func loadSearchConfig(path string) (*SearchConfig, error) {
config := &SearchConfig{}
b, err := ioutil.ReadFile(path)
if err != nil {
switch err.(type) {
case *os.PathError:
// skip if notfound
default:
return nil, err
}
} else {
if err := yaml.Unmarshal(b, config); err != nil {
return nil, err
}
}
if len(config.QuotaSearchCondition) == 0 {
config.QuotaSearchCondition = []QuotaSeachCondition{{}}
}
return config, nil
}
func main() {
configPath := kingpin.Flag("api.search-config", "search Config file path").Default("/etc/netapp_quota_exporter.conf").String()
endpoint := kingpin.Flag("api.endpoint", "NetApp API endpoint").String()
user := kingpin.Flag("api.user", "NetApp API auth user").String()
password := kingpin.Flag("api.password", "NetApp API auth password").String()
listenAddress := kingpin.Flag("web.listen-address", "Address to listen on for web interface and telemetry.").Default(":9797").String()
metricsPath := kingpin.Flag("web.telemetry-path", "Path under which to expose metrics.").Default("/metrics").String()
kingpin.Parse()
sconf, err := loadSearchConfig(*configPath)
if err != nil {
fmt.Println(err)
os.Exit(2)
}
c, err := NewQuotaCollector(*endpoint, *user, *password, sconf.QuotaSearchCondition)
if err != nil {
fmt.Println(err)
os.Exit(3)
}
prometheus.Register(c)
http.Handle(*metricsPath, promhttp.Handler())
http.ListenAndServe(*listenAddress, nil)
}