This repository has been archived by the owner on Oct 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
298 lines (252 loc) · 7.37 KB
/
main.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
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"strconv"
"strings"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/aws/external"
"github.com/aws/aws-sdk-go-v2/service/costexplorer"
)
var (
date string
keyID string
secretKey string
configFile string
resultPath string
exact bool
logLevel string
tagsList []string
defaultTag = "Unknown"
resultFile *os.File
debugLogger *log.Logger
traceLogger *log.Logger
)
type settings struct {
AWSKeyID string `json:"aws_key_id"`
AWSSecretKey string `json:"aws_secret_key"`
Date string `json:"date"`
}
type Config struct {
Accounts []struct {
Name string `json:"name"`
ID string `json:"id"`
Tags map[string]string `json:"tags,omitempty"`
} `json:"accounts"`
}
type serviceCost struct {
AccountID string `json:"account_id"`
ServiceName string `json:"service_name"`
ServiceCost string `json:"service_cost"`
Timestamp string `json:"timestamp"`
}
func init() {
var err error
flag.StringVar(&date, "date", "", "date in format yyyy-MM-dd, (by default will be set as yesterday)")
flag.StringVar(&keyID, "key-id", "", "AWS key ID(by default will be taken from env AWS_ACCESS_KEY_ID)")
flag.StringVar(&secretKey, "secret", "", "AWS secret key(by default will be taken from env AWS_SECRET_KEY)")
flag.StringVar(&configFile, "config", "", "config file")
flag.StringVar(&resultPath, "result", "", "result file")
flag.StringVar(&logLevel, "log", "", "log level(only 'debug' is supported right now)")
flag.BoolVar(&exact, "exact", false, "show only accounts from config file")
flag.Parse()
if exact && configFile == "" {
flag.PrintDefaults()
os.Exit(1)
}
if keyID == "" {
keyID = os.Getenv("AWS_ACCESS_KEY_ID")
}
if secretKey == "" {
secretKey = os.Getenv("AWS_SECRET_KEY")
}
if keyID == "" || secretKey == "" {
flag.PrintDefaults()
os.Exit(1)
}
if date == "" {
yesterday := time.Now().AddDate(0, 0, -1)
date = yesterday.Format("2006-01-02")
}
resultFile = os.Stdout
if resultPath != "" {
_ = os.Remove(resultPath)
resultFile, err = os.OpenFile(resultPath, os.O_CREATE|os.O_WRONLY, 0666)
if err != nil {
log.Fatalf("failed opening file: %s", err)
}
}
debugLogger = log.New(ioutil.Discard, "", -1)
traceLogger = log.New(ioutil.Discard, "", -1)
if logLevel == "debug" {
debugLogger = log.New(os.Stdout, "DEBUG:", log.Ldate|log.Ltime|log.Lshortfile)
} else if logLevel == "trace" {
debugLogger = log.New(os.Stdout, "DEBUG:", log.Ldate|log.Ltime|log.Lshortfile)
traceLogger = log.New(os.Stdout, "TRACE:", log.Ldate|log.Lmicroseconds|log.Lshortfile)
}
}
func loadConfig(path string) (Config, error) {
var c Config
var err error
debugLogger.Printf("load config: %v\n", path)
file, err := ioutil.ReadFile(path)
if err != nil {
return c, fmt.Errorf("can't load config: %v", err)
}
err = json.Unmarshal(file, &c)
if err != nil {
return c, fmt.Errorf("can't unmarshal config: %v", err)
}
for _, acc := range c.Accounts {
if len(acc.Tags) > 0 {
for t := range acc.Tags {
_ = addTags(t)
}
}
}
debugLogger.Printf("config loaded\n")
return c, err
}
func addTags(tag string) bool {
for _, t := range tagsList {
if t == tag {
return false
}
}
tagsList = append(tagsList, tag)
return true
}
func getDataFromAWS(a *settings) (*[]costexplorer.ResultByTime, error) {
var err error
groupDefinitions := []string{"SERVICE", "LINKED_ACCOUNT"}
debugLogger.Printf("collecting data from AWS\n")
t, _ := time.Parse("2006-01-02", date)
end := t.AddDate(0, 0, 1).Format("2006-01-02")
cfg, err := external.LoadDefaultAWSConfig(
external.WithCredentialsValue(aws.Credentials{
AccessKeyID: a.AWSKeyID,
SecretAccessKey: a.AWSSecretKey,
}),
)
if err != nil {
return nil, fmt.Errorf("failed to load config, %v", err)
}
ce := costexplorer.New(cfg)
request := ce.GetCostAndUsageRequest(&costexplorer.GetCostAndUsageInput{
Granularity: "DAILY",
Metrics: []string{"UnblendedCost"},
GroupBy: []costexplorer.GroupDefinition{{
Type: "DIMENSION",
Key: &groupDefinitions[0],
}, {
Type: "DIMENSION",
Key: &groupDefinitions[1],
}},
TimePeriod: &costexplorer.DateInterval{
Start: &date,
End: &end,
},
})
data, err := request.Send()
if err != nil {
return nil, fmt.Errorf("failed to do request, %v", err)
}
debugLogger.Printf("collected %v items from AWS\n", len(data.ResultsByTime[0].Groups))
return &data.ResultsByTime, err
}
func getServiceCost(results *[]costexplorer.ResultByTime) []serviceCost {
sc := []serviceCost{}
t, _ := time.Parse("2006-01-02", date)
timestamp := strconv.FormatInt(t.UnixNano(), 10)
for _, timePeriod := range *results {
for _, group := range timePeriod.Groups {
sc = append(sc, serviceCost{
AccountID: group.Keys[1],
ServiceName: strings.Replace(group.Keys[0], " ", "_", -1),
ServiceCost: *group.Metrics["UnblendedCost"].Amount,
Timestamp: timestamp,
})
}
}
return sc
}
func printInfluxLineProtocol(servicesFromAWS []serviceCost, c Config) {
debugLogger.Printf("printing result in Influx Line Protocol to %s\n", resultFile.Name())
if len(c.Accounts) == 0 {
for _, s := range servicesFromAWS {
fmt.Fprintf(resultFile, "aws-cost,account_id=%v,service_name=%v cost=%v %v\n", s.AccountID, s.ServiceName, s.ServiceCost, s.Timestamp)
}
} else {
for _, s := range servicesFromAWS {
if exact {
if ok, accountName, accountTags := checkElementInArray(c, s.AccountID); ok {
fmt.Fprintf(resultFile, "aws-cost,account_id=%v,account_name=%v,service_name=%v%v cost=%v %v\n", s.AccountID, accountName, s.ServiceName, accountTags, s.ServiceCost, s.Timestamp)
}
} else {
if ok, accountName, accountTags := checkElementInArray(c, s.AccountID); ok {
fmt.Fprintf(resultFile, "aws-cost,account_id=%v,account_name=%v,service_name=%v%v cost=%v %v\n", s.AccountID, accountName, s.ServiceName, accountTags, s.ServiceCost, s.Timestamp)
} else {
fmt.Fprintf(resultFile, "aws-cost,account_id=%v,account_name=%v,service_name=%v cost=%v %v\n", s.AccountID, accountName, s.ServiceName, s.ServiceCost, s.Timestamp)
}
}
}
}
}
func checkElementInArray(config Config, element string) (bool, string, string) {
if element[0] == '0' {
element = element[1:]
}
for _, acc := range config.Accounts {
if acc.ID == element {
currentTags := acc.Tags
if currentTags == nil {
currentTags = make(map[string]string)
}
if len(currentTags) != len(tagsList) {
for _, t := range tagsList {
if _, elementExist := currentTags[t]; !elementExist {
//debugLogger.Println(currentTags, t, defaultTag)
currentTags[t] = defaultTag
}
}
}
name := strings.Replace(acc.Name, " ", "_", -1)
tags := getStringWithTags(currentTags)
return true, name, tags
}
}
return false, "", ""
}
func getStringWithTags(accountTags map[string]string) string {
tags := ""
for tag := range accountTags {
tags += fmt.Sprintf(",%s=%s", tag, accountTags[tag])
}
ret := strings.Replace(tags, " ", "_", -1)
return ret
}
func main() {
var c Config
var err error
defer resultFile.Close()
if configFile != "" {
c, err = loadConfig(configFile)
if err != nil {
log.Fatal(err)
}
}
data, err := getDataFromAWS(&settings{
AWSKeyID: keyID,
AWSSecretKey: secretKey,
Date: date,
})
if err != nil {
log.Fatal(err)
}
printInfluxLineProtocol(getServiceCost(data), c)
}