/
report_cost_rds.go
195 lines (169 loc) · 5.14 KB
/
report_cost_rds.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
package chanute
import (
"fmt"
"sort"
"strconv"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/rds"
"github.com/aws/aws-sdk-go/service/sts"
"github.com/olekukonko/tablewriter"
"github.com/richardwilkes/toolbox/errs"
)
type RDSReport struct {
Instances []*RDSInstance
Aggregated []*RDSAggregate
}
type RDSAggregate struct {
Key string
Instances []*RDSInstance
StorageProvisionedGB int
EstimatedMonthlySavings int
}
type RDSInstance struct {
Region string
Name string
Type string
MultiAZ bool
StorageProvisionedGB int
DaysSinceLastConnection int
EstimatedMonthlySavings int
Tags map[string]string
}
func (r *RDSReport) AsciiReport() string {
if len(r.Instances) == 0 {
return "RDS: No issues"
}
o := &strings.Builder{}
o.WriteString("RDS\n")
w := tablewriter.NewWriter(o)
w.SetHeader([]string{"Name", "MultiAZ", "Days Since Connection", "Storage Size (in GB)", "Monthly Cost"})
if r.Aggregated == nil {
for _, i := range r.Instances {
w.Append([]string{i.Name, strconv.FormatBool(i.MultiAZ), strconv.Itoa(i.DaysSinceLastConnection), strconv.Itoa(i.StorageProvisionedGB), PrintDollars(i.EstimatedMonthlySavings)})
}
w.Render()
return o.String()
}
for _, agg := range r.Aggregated {
w.Append([]string{agg.Key, "", "", strconv.Itoa(agg.StorageProvisionedGB), PrintDollars(agg.EstimatedMonthlySavings)})
if len(agg.Instances) > 0 {
for _, i := range agg.Instances {
w.Append([]string{i.Name, strconv.FormatBool(i.MultiAZ), strconv.Itoa(i.DaysSinceLastConnection), strconv.Itoa(i.StorageProvisionedGB), PrintDollars(i.EstimatedMonthlySavings)})
}
w.Append([]string{"", "", "", ""})
}
}
w.Render()
return o.String()
}
func rdsIdleInstances(config *Config, sess *session.Session, checks []*TrustedAdvisorCheck) (*RDSReport, error) {
m := checksToMaps(checks)
instances := make(map[string]*RDSInstance, len(checks))
var names []*string
for _, instance := range m {
name := instance["DB Instance Name"]
// names = append(names, aws.String(name))
ri := &RDSInstance{
Region: instance["Region"],
Name: name,
MultiAZ: instance["Multi-AZ"] == "Yes",
Type: instance["Instance Type"],
StorageProvisionedGB: parseAmount(instance["Storage Provisioned (GB)"]),
DaysSinceLastConnection: parseDays(instance["Days Since Last Connection"]),
EstimatedMonthlySavings: parseAmount(instance["Estimated Monthly Savings (On Demand)"]),
}
instances[ri.Name] = ri
names = append(names, &ri.Name)
}
if config.GetTags {
tags, err := GetRDSTags(sess, names)
if err != nil {
return nil, errs.Wrap(err)
}
for name, instanceTags := range tags {
i, ok := instances[name]
if !ok {
continue
}
i.Tags = instanceTags
}
}
r := &RDSReport{}
for _, instance := range instances {
r.Instances = append(r.Instances, instance)
}
sort.Slice(r.Instances, func(i, j int) bool {
return r.Instances[i].EstimatedMonthlySavings > r.Instances[j].EstimatedMonthlySavings
})
if config.Aggregator != nil {
aggregated := map[string]*RDSAggregate{}
for _, i := range r.Instances {
key := config.Aggregator(i.Tags)
if key == "" {
key = i.Name
}
if _, ok := aggregated[key]; !ok {
aggregated[key] = &RDSAggregate{
Key: key,
}
}
if !config.HideResourceDetails {
aggregated[key].Instances = append(aggregated[key].Instances, i)
}
aggregated[key].EstimatedMonthlySavings += i.EstimatedMonthlySavings
aggregated[key].StorageProvisionedGB += i.StorageProvisionedGB
}
for _, agg := range aggregated {
r.Aggregated = append(r.Aggregated, agg)
}
sort.Slice(r.Aggregated, func(i, j int) bool {
return r.Aggregated[i].EstimatedMonthlySavings > r.Aggregated[j].EstimatedMonthlySavings
})
}
return r, nil
}
func GetRDSTags(sess *session.Session, names []*string) (TagMap, error) {
c := rds.New(sess)
svc := sts.New(sess)
input := &sts.GetCallerIdentityInput{}
result, err := svc.GetCallerIdentity(input)
if err != nil {
return nil, errs.Wrap(err)
}
tags := map[string]map[string]string{}
for _, n := range names {
arn := fmt.Sprintf("arn:aws:rds:%s:%s:db:%s", *sess.Config.Region, *result.Account, *n)
resp, err := c.ListTagsForResource(&rds.ListTagsForResourceInput{
ResourceName: &arn,
})
if err != nil {
if strings.HasPrefix(err.Error(), "DBInstanceNotFound") {
continue
}
fmt.Println(err)
continue
}
tags[aws.StringValue(n)] = map[string]string{}
for _, t := range resp.TagList {
tags[aws.StringValue(n)][aws.StringValue(t.Key)] = aws.StringValue(t.Value)
}
}
return tags, nil
}
func (r *RDSReport) AggregateRows(a Aggregator) []*AggregateRow {
var o []*AggregateRow
for _, i := range r.Instances {
o = append(o, i.AggregateRow(a))
}
return o
}
func (i *RDSInstance) AggregateRow(a Aggregator) *AggregateRow {
return &AggregateRow{
Env: "",
Service: "EC2",
Key: a(i.Tags),
MonthlySavings: i.EstimatedMonthlySavings,
}
}