/
UnderutilizedEBSVolumes.go
164 lines (138 loc) · 5.82 KB
/
UnderutilizedEBSVolumes.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
package cost
import (
"context"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/cloudwatch"
cloudWatchTypes "github.com/aws/aws-sdk-go-v2/service/cloudwatch/types"
"github.com/aws/aws-sdk-go-v2/service/ec2"
"github.com/aws/aws-sdk-go-v2/service/ec2/types"
"github.com/brittandeyoung/ckia/internal/client"
"github.com/brittandeyoung/ckia/internal/common"
)
const (
UnderutilizedEBSVolumesCheckId = "ckia:aws:cost:UnderutilizedEBSVolumes"
UnderutilizedEBSVolumesCheckName = "Underutilized Amazon EBS Volumes"
UnderutilizedEBSVolumesCheckDescription = "Checks Amazon Elastic Block Store (Amazon EBS) volume configurations and warns when volumes appear to be underutilized. Charges begin when a volume is created. If a volume remains unattached or has very low write activity (excluding boot volumes) for a period of time, the volume is underutilized. We recommend that you remove underutilized volumes to reduce costs."
UnderutilizedEBSVolumesCheckCriteria = "A volume is unattached or had less than 1 IOPS per day for the past 7 days."
UnderutilizedEBSVolumesCheckRecommendedAction = "Consider creating a snapshot and deleting the volume to reduce costs."
UnderutilizedEBSVolumesCheckAdditionalResources = "See comparable AWS Trusted advisor check: https://docs.aws.amazon.com/awssupport/latest/user/cost-optimization-checks.html#underutilized-amazon-ebs-volumes"
)
type UnderutilizedEBSVolume struct {
Region string `json:"region"`
VolumeId string `json:"volumeId"`
VolumeName string `json:"volumeName"`
VolumeType string `json:"volumeType"`
VolumeSize int `json:"volumeSize"`
MonthlyStorageCost int `json:"monthlyStorageCost"`
SnapshotId string `json:"snapshotId"`
SnapshotName string `json:"snapshotName"`
SnapshotAge int `json:"snapshotAge"`
}
type UnderutilizedEBSVolumesCheck struct {
common.Check
UnderutilizedEBSVolumes []UnderutilizedEBSVolume `json:"underutilizedVolumes"`
}
func (v UnderutilizedEBSVolumesCheck) List() *UnderutilizedEBSVolumesCheck {
check := &UnderutilizedEBSVolumesCheck{
Check: common.Check{
Id: UnderutilizedEBSVolumesCheckId,
Name: UnderutilizedEBSVolumesCheckName,
Description: UnderutilizedEBSVolumesCheckDescription,
Criteria: UnderutilizedEBSVolumesCheckCriteria,
RecommendedAction: UnderutilizedEBSVolumesCheckRecommendedAction,
AdditionalResources: UnderutilizedEBSVolumesCheckAdditionalResources,
},
}
return check
}
func (v UnderutilizedEBSVolumesCheck) Run(ctx context.Context, conn client.AWSClient) (*UnderutilizedEBSVolumesCheck, error) {
check := new(UnderutilizedEBSVolumesCheck).List()
currentTime := time.Now()
in := &ec2.DescribeVolumesInput{}
var volumes []types.Volume
paginator := ec2.NewDescribeVolumesPaginator(conn.EC2, in, func(o *ec2.DescribeVolumesPaginatorOptions) {})
for paginator.HasMorePages() {
output, err := paginator.NextPage(ctx)
if err != nil {
return nil, err
}
volumes = append(volumes, output.Volumes...)
}
if len(volumes) == 0 {
return nil, nil
}
var underutilizedVolumes []UnderutilizedEBSVolume
for _, volume := range volumes {
var underutilizedVolume UnderutilizedEBSVolume
metrics, err := conn.Cloudwatch.GetMetricStatistics(ctx, &cloudwatch.GetMetricStatisticsInput{
MetricName: aws.String("VolumeReadOps"),
Period: aws.Int32(3600),
Namespace: aws.String("AWS/EBS"),
Statistics: []cloudWatchTypes.Statistic{cloudWatchTypes.StatisticSum},
Dimensions: []cloudWatchTypes.Dimension{
{
Name: aws.String("DBInstanceIdentifier"),
Value: volume.VolumeId,
},
},
StartTime: aws.Time(currentTime.AddDate(0, 0, -14)),
EndTime: aws.Time(currentTime),
})
if err != nil {
return nil, err
}
underutilizedVolume = expandUnderutilizedVolume(conn, volume, metrics.Datapoints)
if underutilizedVolume.SnapshotId != "" {
snapshots, err := conn.EC2.DescribeSnapshots(ctx, &ec2.DescribeSnapshotsInput{
SnapshotIds: []string{underutilizedVolume.SnapshotId},
})
if err != nil {
return nil, err
}
underutilizedVolume = expandSnapshot(snapshots.Snapshots, underutilizedVolume)
// Still trying to figure out how to get the proper pricing via the API
// underutilizedVolume.MonthlyStorageCost = 0
}
if underutilizedVolume.VolumeId != "" {
underutilizedVolumes = append(underutilizedVolumes, underutilizedVolume)
}
}
check.UnderutilizedEBSVolumes = underutilizedVolumes
return check, nil
}
func expandUnderutilizedVolume(conn client.AWSClient, volume types.Volume, dataPoints []cloudWatchTypes.Datapoint) UnderutilizedEBSVolume {
var underutilizedVolume UnderutilizedEBSVolume
iopsFound := false
for _, dataPoint := range dataPoints {
if aws.ToFloat64(dataPoint.Average) != 0 {
iopsFound = true
}
}
if volume.State == types.VolumeStateAvailable && !iopsFound {
underutilizedVolume.Region = conn.Region
underutilizedVolume.VolumeId = aws.ToString(volume.VolumeId)
for _, tag := range volume.Tags {
if aws.ToString(tag.Key) == "Name" {
underutilizedVolume.VolumeName = aws.ToString(tag.Value)
}
}
underutilizedVolume.VolumeType = aws.ToString((*string)(&volume.VolumeType))
underutilizedVolume.VolumeSize = int(aws.ToInt32(volume.Size))
underutilizedVolume.SnapshotId = aws.ToString(volume.SnapshotId)
}
return underutilizedVolume
}
func expandSnapshot(snapshots []types.Snapshot, volume UnderutilizedEBSVolume) UnderutilizedEBSVolume {
if len(snapshots) > 0 {
snapshot := snapshots[0]
duration := time.Since(aws.ToTime(snapshot.StartTime))
volume.SnapshotAge = int(duration.Hours() / 24)
for _, tag := range snapshot.Tags {
if aws.ToString(tag.Key) == "Name" {
volume.SnapshotName = aws.ToString(tag.Value)
}
}
}
return volume
}