/
ebs.go
289 lines (251 loc) · 8.58 KB
/
ebs.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
package ebs
import (
"fmt"
"time"
"github.com/VictoriaMetrics/metrics"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/cloudwatch"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/sirupsen/logrus"
"github.com/thunderbottom/ebs-exporter/pkg/config"
"github.com/thunderbottom/ebs-exporter/pkg/exporter"
"golang.org/x/sync/errgroup"
)
const (
namespace = "ebs"
)
// EBSExporter is a struct that holds an instance
// of EC2 client and the job details required to
// scrape EBS metrics
type EBSExporter struct {
client *ec2.EC2
cloudwatch *cloudwatch.CloudWatch
filters []*ec2.Filter
job *config.Job
logger *logrus.Logger
metrics *metrics.Set
}
// New returns a new instance of EBSExporter
func New(j *config.Job, log *logrus.Logger, m *metrics.Set, rc *aws.Config, sess *session.Session) *EBSExporter {
// create instances of ec2 and cloudwatch clients
var (
client *ec2.EC2
cw *cloudwatch.CloudWatch
)
// RoleARN config overrides Access Key and Secret
if rc != nil {
client = ec2.New(sess, rc)
cw = cloudwatch.New(sess, rc)
} else {
client = ec2.New(sess)
cw = cloudwatch.New(sess)
}
filters := make([]*ec2.Filter, 0, len(j.Filters))
for _, tag := range j.Filters {
if tag.Name != "" || tag.Value != "" {
filters = append(filters, &ec2.Filter{
Name: aws.String(tag.Name),
Values: []*string{aws.String(tag.Value)},
})
}
}
log.Debugf("%s: setting up a new EBS exporter", j.Name)
return &EBSExporter{
client: client,
cloudwatch: cw,
filters: filters,
job: j,
logger: log,
metrics: m,
}
}
// Collect calls methods to collect metrics from AWS
func (e *EBSExporter) Collect() error {
var g errgroup.Group
g.Go(e.getSnapshotMetrics)
g.Go(e.getVolumeStatusMetrics)
g.Go(e.getVolumeUsageMetrics)
// Return if any of the errgroup
// goroutines returns an error
if err := g.Wait(); err != nil {
return err
}
return nil
}
// getSnapshotMetrics scrapes EBS volume snapshot metrics from AWS
func (e *EBSExporter) getSnapshotMetrics() error {
input := &ec2.DescribeSnapshotsInput{}
// Check whether there are filters defined in the config
if len(e.filters) != 0 {
input.Filters = e.filters
}
// Fetch only private snapshots
input.OwnerIds = []*string{aws.String("self")}
snapshots, err := e.client.DescribeSnapshots(input)
if err != nil {
e.logger.Errorf("an error occurred while retrieving snapshot data: %s", err)
return err
}
e.logger.Debugf("%s: got %d Volume Snapshots", e.job.Name, len(snapshots.Snapshots))
for _, s := range snapshots.Snapshots {
// Default labels to attach to all metrics
labels := fmt.Sprintf(`job="%s",region="%s",snapshot_id="%s",vol_id="%s",progress="%s",state="%s"`,
e.job.Name, e.job.AWS.Region, *s.SnapshotId, *s.VolumeId, *s.Progress, *s.State)
// Check whether the snapshot has any tags
// that we want to export
for _, et := range e.job.Tags {
for _, t := range s.Tags {
if *t.Key == et.Tag {
// Ensure that the tags are correct by replacing
// unsupported characters with underscore
labels = labels + fmt.Sprintf(`,%s="%s"`, exporter.FormatTag(et.ExportedTag), *t.Value)
}
}
}
// Total number of EBS snapshots
snapTotal := fmt.Sprintf(`%s_snapshots_total{job="%s",region="%s",state="%s"}`,
namespace, e.job.Name, e.job.AWS.Region, *s.State)
e.metrics.GetOrCreateCounter(snapTotal).Add(1)
// Size of the volume associated with the EBS snapshot
volSize := fmt.Sprintf(`%s_snapshots_volume_size{%s}`, namespace, labels)
vsize := float64(*s.VolumeSize)
e.metrics.GetOrCreateGauge(volSize, func() float64 {
return vsize
})
// Start Time of the EBS Snapshot (UNIX Time)
snapStartTime := fmt.Sprintf(`%s_snapshots_start_time{%s}`, namespace, labels)
sstart := float64(s.StartTime.Unix())
e.metrics.GetOrCreateGauge(snapStartTime, func() float64 {
return sstart
})
}
return nil
}
// getVolumeStatusMetrics scrapes EBS volume status metrics from AWS
func (e *EBSExporter) getVolumeStatusMetrics() error {
input := &ec2.DescribeVolumeStatusInput{}
if len(e.filters) != 0 {
input.Filters = e.filters
}
volumes, err := e.client.DescribeVolumeStatus(input)
if err != nil {
e.logger.Errorf("an error occurred while retrieving volume status data: %s", err)
return err
}
e.logger.Debugf("%s: Got %d Volume Statuses", e.job.Name, len(volumes.VolumeStatuses))
for _, v := range volumes.VolumeStatuses {
// Default labels to attach to all metrics
labels := fmt.Sprintf(`job="%s",region="%s",vol_id="%s"`,
e.job.Name, e.job.AWS.Region, *v.VolumeId)
// Convert volume status to numbers
// ok => 0, warning => 1, impaired => 2, insufficient-data => 3
var status int
switch *v.VolumeStatus.Status {
case "ok":
status = 0
case "warning":
status = 1
case "impaired":
status = 2
case "insufficient-data":
status = 3
}
// Total number of volumes by status
statTotal := fmt.Sprintf(`%s_volume_total{job="%s",region="%s",status="%s"}`,
namespace, e.job.Name, e.job.AWS.Region, *v.VolumeStatus.Status)
e.metrics.GetOrCreateCounter(statTotal).Add(1)
// EBS volume status
volStatus := fmt.Sprintf(`%s_volume_status{%s}`, namespace, labels)
e.metrics.GetOrCreateGauge(volStatus, func() float64 {
return float64(status)
})
}
return nil
}
// getVolumeUsageMetrics scrapes EBS volume usage metrics from AWS
func (e *EBSExporter) getVolumeUsageMetrics() error {
input := &ec2.DescribeVolumesInput{}
if len(e.filters) != 0 {
input.Filters = e.filters
}
volumes, err := e.client.DescribeVolumes(input)
if err != nil {
e.logger.Errorf("An error occurred while retrieving volume usage data: %s", err)
return err
}
e.logger.Debugf("%s: Got %d Volumes", e.job.Name, len(volumes.Volumes))
for _, v := range volumes.Volumes {
// Default labels to attach to all metrics
labels := fmt.Sprintf(`job="%s",region="%s",vol_id="%s"`,
e.job.Name, e.job.AWS.Region, *v.VolumeId)
// Check whether the volume contains any tags
// that we want to export
for _, et := range e.job.Tags {
for _, t := range v.Tags {
if *t.Key == et.Tag {
// Ensure that the tags are correct by replacing
// unsupported characters with underscore
labels = labels + fmt.Sprintf(`,%s="%s"`, exporter.FormatTag(et.ExportedTag), *t.Value)
}
}
}
// Total number of volumes by volume-type and availability zone
typeTotal := fmt.Sprintf(`%s_volume_type_total{job="%s",region="%s",type="%s",availability_zone="%s"}`,
namespace, e.job.Name, e.job.AWS.Region, *v.VolumeType, *v.AvailabilityZone)
e.metrics.GetOrCreateCounter(typeTotal).Add(1)
// Total number of volumes by usage and availability,
// and volume availability zone
usageTotal := fmt.Sprintf(`%s_volume_usage_status_total{job="%s",region="%s",status="%s",availability_zone="%s"}`,
namespace, e.job.Name, e.job.AWS.Region, *v.State, *v.AvailabilityZone)
e.metrics.GetOrCreateCounter(usageTotal).Add(1)
// Get EBS BurstBalance metrics
balance, err := e.getIOPSBalance(*v.VolumeId)
if err != nil {
e.logger.Errorf("an error occurred while retrieving volume IOPS data: %s", err)
return err
}
volIOPS := fmt.Sprintf(`%s_volume_iops_credit{job="%s",region="%s",vol_id="%s"}`,
namespace, e.job.Name, e.job.AWS.Region, *v.VolumeId)
e.metrics.GetOrCreateGauge(volIOPS, func() float64 {
return balance
})
}
return nil
}
// getIOPSBalance gets last 5-minute average IOPS BurstBalance
// for an EBS volume using AWS Cloudwatch
func (e *EBSExporter) getIOPSBalance(volumeID string) (float64, error) {
input := &cloudwatch.GetMetricStatisticsInput{
Dimensions: []*cloudwatch.Dimension{
{
Name: aws.String("VolumeId"),
Value: aws.String(volumeID),
},
},
MetricName: aws.String("BurstBalance"),
Namespace: aws.String("AWS/EBS"),
// Get IOPS average for the last 5 minutes
// Setting Period to 5 minutes gives a single
// average value for the entire duration
Statistics: []*string{aws.String("Average")},
Period: aws.Int64(5 * 60),
StartTime: aws.Time(time.Now().Add(time.Duration(-5) * time.Minute)),
EndTime: aws.Time(time.Now()),
}
metrics, err := e.cloudwatch.GetMetricStatistics(input)
if err != nil {
return 0, err
}
// Some volumes do not have any IOPS value
if metrics != nil && metrics.Datapoints != nil && len(metrics.Datapoints) >= 1 {
var avgIOPS, totalIOPS float64
for _, datapoint := range metrics.Datapoints {
totalIOPS += *datapoint.Average
}
avgIOPS = totalIOPS / float64(len(metrics.Datapoints))
return avgIOPS, nil
}
e.logger.Debugf("%s: volume %s has no IOPS BurstBalance", e.job.Name, volumeID)
return 0, nil
}