-
Notifications
You must be signed in to change notification settings - Fork 82
/
main.go
311 lines (283 loc) · 8.19 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
299
300
301
302
303
304
305
306
307
308
309
310
311
// aws-billing project main.go
package main
import (
"encoding/csv"
"encoding/json"
"flag"
"fmt"
"io"
"math"
"net"
"os"
"regexp"
"runtime"
"strconv"
"strings"
"sync"
"time"
)
var (
tagMatcher = regexp.MustCompile("(user|aws):.*")
dateMonthMatcher = regexp.MustCompile(`\d{4}-\d{2}`)
monthlyCostAllocationMatcher = regexp.MustCompile(`.*aws-cost-allocation.*`)
detailedBillingWithResourcesMatcher = regexp.MustCompile(`.*billing-report.*`)
ReportMatchers = []*regexp.Regexp{monthlyCostAllocationMatcher, detailedBillingWithResourcesMatcher}
wg = sync.WaitGroup{}
// Flag variables
file = flag.String("file", "billing_report_2016-05.csv", "CSV billing file to load.")
logstashAddress = flag.String("logstash-address", "logstash:5140", "Address and port for the logstash tcp listener.")
concurrency = flag.Int("concurrency", 2, "Number of cores to use.")
//accountsFile = flag.String("accounts-file", "aws-cost-allocation-2014-06.csv", "CSV file containing LinkedAccountId and account LinkedAccountName")
)
var FieldTypes = map[string]func(s string, report *BillingReport) interface{} {
"PayerAccountId": ParseInt,
"RateId": ParseInt,
"SubscriptionId": ParseInt,
"PricingPlanId": ParseInt,
"UsageQuantity": ParseFloat,
"BlendedRate": ParseFloat,
"BlendedCost": ParseFloat,
"UnBlendedRate": ParseFloat,
"UnBlendedCost": ParseFloat,
"TotalCost": ParseFloat,
"Cost": ParseFloat,
"CostBeforeTax": ParseFloat,
"Credits": ParseFloat,
"UsageStartDate": ParseDate,
"UsageEndDate": ParseDate,
"BillingPeriodStartDate": ParseBillingPeriodDate,
"BillingPeriodEndDate": ParseBillingPeriodDate,
}
type DetailedLineItem struct {
InvoiceID string
PayerAccountId int
LinkedAccountId int
}
const (
DetailedBillingWithResourcesAndTags = iota
MonthlyCostAllocation = iota
)
type FieldMapper map[string]map[interface{}]map[string]interface{}
type BillingReport struct {
CsvFileName string
InvoicePeriod time.Time
ReportType int
Fields []string
csvReader *csv.Reader
Mapper FieldMapper
}
func main() {
flag.Parse()
fmt.Println("AWS billing CSV report parser!")
var out = make(chan []byte)
fmt.Println("out after make(chan []byte) is %v", out)
go PublishRecord(out)
//var csvFileName = "376681487066-aws-billing-detailed-line-items-with-resources-and-tags-2014-06.csv"
report := OpenBillingReport(*file)
/*if *accountsFile != "" {
accountReport := OpenBillingReport(*accountsFile)
report.Mapper = ReportToAccountFieldMap(accountReport)
}*/
valuesSink := make(chan []string)
reportSinks := make([]chan map[string]interface{}, 0, 0)
jsonSinks := make([]chan []byte, 0, 0)
for i := 0; i < runtime.GOMAXPROCS(*concurrency); i++ {
reportSink := make(chan map[string]interface{})
jsonSink := make(chan []byte)
reportSinks = append(reportSinks, reportSink)
jsonSinks = append(jsonSinks, jsonSink)
go ParseRecord(valuesSink, reportSink, report)
go RecordToJson(reportSink, jsonSink)
go PublishRecord(jsonSink)
wg.Add(3)
}
var recordNum int
for {
values, err := report.csvReader.Read()
if err == io.EOF {
break
}
valuesSink <- values
recordNum += 1
if math.Mod(float64(recordNum), 10000) == 0 {
fmt.Println(recordNum)
}
}
close(valuesSink)
wg.Wait()
}
func ReportToAccountFieldMap(report *BillingReport) FieldMapper {
mapper := FieldMapper{}
mapper["LinkedAccountId"] = make(map[interface{}]map[string]interface{})
for {
values, err := report.csvReader.Read()
if err == io.EOF {
break
} else if err != nil {
panic(err.Error())
}
var linkedAccountId string
var linkedAccountName string
for i, field := range report.Fields {
if field == "LinkedAccountId" {
linkedAccountId = values[i]
}
if field == "LinkedAccountName" {
linkedAccountName = values[i]
}
}
if _, ok := mapper["LinkedAccountId"][linkedAccountId]; !ok {
fmt.Println("Creating map for: ", linkedAccountId)
mapper["LinkedAccountId"][linkedAccountId] = make(map[string]interface{})
}
mapper["LinkedAccountId"][linkedAccountId]["LinkedAccountName"] = linkedAccountName
}
return mapper
}
func ParseRecord(in chan []string, out chan map[string]interface{}, report *BillingReport) {
var parsedValue interface{}
for values := range in {
var record = make(map[string]interface{})
record["Tags"] = make(map[string]interface{})
record["type"] = report.TypeString()
for i, field := range report.Fields {
if f, ok := FieldTypes[field]; ok {
parsedValue = f(values[i], report)
record[field] = parsedValue
} else if tagMatcher.MatchString(field) {
record["Tags"].(map[string]interface{})[ParseTag(field, report).(string)] = strings.ToLower(values[i])
parsedValue = strings.ToLower(values[i])
} else {
record[field] = values[i]
parsedValue = values[i]
}
if valueMap, ok := report.Mapper[field][parsedValue]; ok {
for k, v := range valueMap {
record[k] = v
}
}
}
out <- record
}
close(out)
wg.Done()
}
func RecordToJson(in chan map[string]interface{}, out chan []byte) {
for record := range in {
jbRecord, err := json.Marshal(record)
if err != nil {
panic(err.Error())
}
out <- jbRecord
}
close(out)
wg.Done()
}
func (report *BillingReport) TypeString() string {
switch report.ReportType {
case DetailedBillingWithResourcesAndTags:
return "aws_billing_hourly"
case MonthlyCostAllocation:
return "aws_billing_monthly"
}
return ""
}
func OpenBillingReport(csvFileName string) *BillingReport {
fmt.Println("Opening billing report filename", csvFileName)
/*
AWS billing report filename must contain at least yyyy-mm
*/
invoicePeriod, err := time.Parse("2006-01", dateMonthMatcher.FindString(csvFileName))
if err != nil {
panic(err.Error())
}
file, err := os.Open(csvFileName)
if err != nil {
panic(err.Error())
}
reader := csv.NewReader(file)
report := &BillingReport{
CsvFileName: csvFileName,
InvoicePeriod: invoicePeriod,
csvReader: reader}
switch {
case detailedBillingWithResourcesMatcher.MatchString(csvFileName):
report.ReportType = DetailedBillingWithResourcesAndTags
case monthlyCostAllocationMatcher.MatchString(csvFileName):
report.ReportType = MonthlyCostAllocation
}
if report.ReportType == MonthlyCostAllocation {
//Burn one due to comment
fmt.Println("Monthly cost allocation, skipping comment:")
fmt.Println(reader.Read())
reader.FieldsPerRecord = 0
}
fields, err := reader.Read()
if err != nil {
panic(err.Error())
}
report.Fields = fields
return report
}
func ParseTag(s string, report *BillingReport) interface{} {
tagParts := strings.Split(s, ":")
return strings.Join(tagParts, "_")
}
func ParseInt(s string, report *BillingReport) interface{} {
value, _ := strconv.ParseInt(s, 0, 0)
return value
}
func ParseUint(s string, report *BillingReport) interface{} {
value, _ := strconv.ParseUint(s, 0, 0)
return value
}
func ParseFloat(s string, report *BillingReport) interface{} {
value, _ := strconv.ParseFloat(s, 0)
return value
}
func ParseDate(s string, report *BillingReport) interface{} {
var returnTime time.Time
var err error
switch s {
case "":
returnTime = report.InvoicePeriod
default:
returnTime, err = time.Parse("2006-01-02 15:04:05", s)
if err != nil {
panic(err.Error())
}
}
return returnTime.Format(time.RFC3339)
}
func ParseBillingPeriodDate(s string, report *BillingReport) interface{} {
var returnTime time.Time
var err error
switch s {
case "":
returnTime = report.InvoicePeriod
default:
returnTime, err = time.Parse("2006/01/02 15:04:05", s)
if err != nil {
panic(err.Error())
}
}
return returnTime.Format(time.RFC3339)
}
func PublishRecord(in chan []byte) {
fmt.Println("Publishing record to logstash %v", in)
con, err := net.DialTimeout("tcp", *logstashAddress, (5 * time.Second))
if err != nil {
panic(err.Error())
}
for record := range in {
_, err = con.Write(record)
if err != nil {
panic(err.Error())
}
_, err = con.Write([]byte("\n"))
if err != nil {
panic(err.Error())
}
}
wg.Done()
}