From 58a4e1d36f30244b17bf7749dce8bc90b31951b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Bart=C3=ADk?= Date: Wed, 6 May 2020 17:28:08 +0200 Subject: [PATCH] add datatransfer --- pkg/check/check.go | 9 ++- pkg/worker/data_transfer_huge.go | 100 ++++++++++++++++++++++++++ pkg/worker/data_transfer_huge_test.go | 1 + pkg/worker/worker.go | 2 + 4 files changed, 111 insertions(+), 1 deletion(-) create mode 100644 pkg/worker/data_transfer_huge.go create mode 100644 pkg/worker/data_transfer_huge_test.go diff --git a/pkg/check/check.go b/pkg/check/check.go index 86a5eb6..32672dd 100644 --- a/pkg/check/check.go +++ b/pkg/check/check.go @@ -110,7 +110,14 @@ func GetChecks() []Check { Name: "EC2 instances are not EBS Optimized", Group: "Incorrect service usage", Type: "ebs_opt", - Description: "There are some EC2 instances with EBS attatached but at the same time they have EBS Optimization disabled", + Description: "There are some EC2 instances with EBS attached but at the same time they have EBS Optimization disabled", + }, + { + ID: uuid.New().String(), + Name: "Data Transfer cost is huge", + Group: "Incorrect service usage", + Type: "datatransfer_huge", + Description: "Data Transfer cost is high relative to total spend", }, } diff --git a/pkg/worker/data_transfer_huge.go b/pkg/worker/data_transfer_huge.go new file mode 100644 index 0000000..a6d7365 --- /dev/null +++ b/pkg/worker/data_transfer_huge.go @@ -0,0 +1,100 @@ +package worker + +import ( + "strconv" + + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/service/costexplorer" + "github.com/pipetail/cloudlint/pkg/check" + "github.com/pipetail/cloudlint/pkg/checkcompleted" + log "github.com/sirupsen/logrus" +) + +func getEgressPrice(ce *costexplorer.CostExplorer) (price float64) { + + params := &costexplorer.GetCostAndUsageInput{ + + Granularity: aws.String("MONTHLY"), + Metrics: []*string{aws.String("UnblendedCost")}, + TimePeriod: &costexplorer.DateInterval{ + Start: aws.String(getLastMonthStart()), + End: aws.String(getLastMonthEnd()), + }, + Filter: &costexplorer.Expression{ + + Dimensions: &costexplorer.DimensionValues{ + Key: aws.String("USAGE_TYPE"), + Values: []*string{aws.String("DataTransfer")}, + }, + }, + // GroupBy: []*costexplorer.GroupDefinition{ + // { + // Type: aws.String("DIMENSION"), + // Key: aws.String("RESOURCE_ID"), + // }, + // }, + } + + res, err := ce.GetCostAndUsage(params) + if err != nil { + log.WithFields(log.Fields{ + "costParams": params, + "err": err, + }).Error("calling GetCostAndUsage returned error") + return 0 + } + + log.WithFields(log.Fields{ + "res": res, + }).Info("checking datatransfer_huge") + + //price = 30 + priceString := *res.ResultsByTime[len(res.ResultsByTime)-1].Total["UnblendedCost"].Amount + price, _ = strconv.ParseFloat(priceString, 64) + + log.WithFields(log.Fields{ + "price": price, + "priceString": priceString, + }).Debug("final price res.ResultsByTime") + + return price +} + +func datatransferhuge(event check.Event) (*checkcompleted.Event, error) { + + // prepare the empty report + outputReport := checkcompleted.New(event.Payload.CheckID) + + auth := event.Payload.AWSAuth + + log.WithFields(log.Fields{ + "event": event, + }).Debug("checking datatransfer_huge") + + var totalMonthlyPrice float64 = 0 + + // cost explorer is available only in us-east-1 + ce := NewCEClient(auth) + + price := getEgressPrice(ce) + + // count the price + totalMonthlyPrice = price + + // TODO: make this relative to total spend + severity := checkcompleted.INFO + if totalMonthlyPrice > 0 { + severity = checkcompleted.WARNING + } + + // set check details + outputReport.Payload.Check.Severity = severity + outputReport.Payload.Check.Impact = int(totalMonthlyPrice) + + log.WithFields(log.Fields{ + "checkCompleted": outputReport, + }).Info("datatransfer_huge check finished") + + return &outputReport, nil + +} diff --git a/pkg/worker/data_transfer_huge_test.go b/pkg/worker/data_transfer_huge_test.go new file mode 100644 index 0000000..4df0094 --- /dev/null +++ b/pkg/worker/data_transfer_huge_test.go @@ -0,0 +1 @@ +package worker diff --git a/pkg/worker/worker.go b/pkg/worker/worker.go index 80fa9cb..35e64a9 100644 --- a/pkg/worker/worker.go +++ b/pkg/worker/worker.go @@ -145,6 +145,8 @@ func Handler(message check.Event) *checkcompleted.Event { outputReport, err = amiOld(message) case check.GetChecks()[10].Type: outputReport, err = ebsopt(message) + case check.GetChecks()[11].Type: + outputReport, err = datatransferhuge(message) default: log.WithFields(log.Fields{ "err": err,