Skip to content
This repository has been archived by the owner on Aug 20, 2022. It is now read-only.

add datatransfer #12

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion pkg/check/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
}

Expand Down
100 changes: 100 additions & 0 deletions pkg/worker/data_transfer_huge.go
Original file line number Diff line number Diff line change
@@ -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

}
1 change: 1 addition & 0 deletions pkg/worker/data_transfer_huge_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package worker
2 changes: 2 additions & 0 deletions pkg/worker/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down