|
| 1 | +package lwaws |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/csv" |
| 5 | + "fmt" |
| 6 | + log "github.com/sirupsen/logrus" |
| 7 | + "github.com/spf13/cobra" |
| 8 | + "io" |
| 9 | + "lw-billing/helpers" |
| 10 | + "os" |
| 11 | + "strconv" |
| 12 | + "strings" |
| 13 | +) |
| 14 | + |
| 15 | +func Run(billingCSV string, debug bool) { |
| 16 | + if debug { |
| 17 | + log.SetLevel(log.DebugLevel) |
| 18 | + } |
| 19 | + |
| 20 | + instanceTypes := loadAWSInstanceTypes() |
| 21 | + instances := getVMVCPU(billingCSV, instanceTypes) |
| 22 | + lambdas := getLambdaVCPU(billingCSV) |
| 23 | + getFargateVCPU(billingCSV) |
| 24 | + |
| 25 | + vmvcpus := countUpVMs(instances) |
| 26 | + println("VM vCPUS:", vmvcpus) |
| 27 | + |
| 28 | + lambdavcpus := countUpLambdas(lambdas) |
| 29 | + println("Lambda vCPUS:", lambdavcpus) |
| 30 | + |
| 31 | + println("Total AWS vCPUS:", vmvcpus+lambdavcpus) |
| 32 | +} |
| 33 | + |
| 34 | +const ( |
| 35 | + hoursPerMonth = 720 |
| 36 | + recordID = 4 |
| 37 | + linkedAccountID = 2 |
| 38 | + usageType = 15 |
| 39 | + usageQty = 21 |
| 40 | + productCode = 12 |
| 41 | + payerAccountID = 1 |
| 42 | +) |
| 43 | + |
| 44 | +type InstanceType struct { |
| 45 | + Name string |
| 46 | + VCPU int |
| 47 | +} |
| 48 | + |
| 49 | +type BillingInstance struct { |
| 50 | + Name string |
| 51 | + Hours float64 |
| 52 | + AccountID string |
| 53 | + VCPU int |
| 54 | +} |
| 55 | + |
| 56 | +func check(e error) { |
| 57 | + if e != nil { |
| 58 | + panic(e) |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +func countUpVMs(instances map[string][]BillingInstance) int { |
| 63 | + var totalVCPUs float64 |
| 64 | + |
| 65 | + for account, instance := range instances { |
| 66 | + var accountvCPUs float64 |
| 67 | + for _, it := range instance { |
| 68 | + numInstances := it.Hours / hoursPerMonth |
| 69 | + vcpus := numInstances * float64(it.VCPU) |
| 70 | + accountvCPUs += vcpus |
| 71 | + totalVCPUs += vcpus |
| 72 | + } |
| 73 | + fmt.Printf("Account %s - VM vCPUs: %.2f\n", account, accountvCPUs) |
| 74 | + } |
| 75 | + |
| 76 | + return int(totalVCPUs) |
| 77 | +} |
| 78 | + |
| 79 | +func countUpLambdas(instances map[string]float64) int { |
| 80 | + var totalVCPUs float64 |
| 81 | + |
| 82 | + for account, vcpus := range instances { |
| 83 | + var accountvCPUs float64 |
| 84 | + accountvCPUs += vcpus |
| 85 | + totalVCPUs += vcpus |
| 86 | + fmt.Printf("Account %s - Lambda vCPUs: %.2f\n", account, accountvCPUs) |
| 87 | + } |
| 88 | + return int(totalVCPUs) |
| 89 | +} |
| 90 | + |
| 91 | +func getVMVCPU(filename string, instanceTypes []InstanceType) map[string][]BillingInstance { |
| 92 | + readFile, err := os.Open(filename) |
| 93 | + check(err) |
| 94 | + defer readFile.Close() |
| 95 | + |
| 96 | + r := csv.NewReader(readFile) |
| 97 | + |
| 98 | + instances := make(map[string][]BillingInstance) |
| 99 | + for { |
| 100 | + row, err := r.Read() |
| 101 | + if err == io.EOF { |
| 102 | + break |
| 103 | + } |
| 104 | + if err != nil { |
| 105 | + log.Fatal(err) |
| 106 | + } |
| 107 | + |
| 108 | + for _, instanceType := range instanceTypes { |
| 109 | + if !strings.Contains(row[recordID], "AccountTotal") && row[linkedAccountID] != "" && strings.Contains(row[usageType], instanceType.Name) { |
| 110 | + hours, _ := strconv.ParseFloat(row[usageQty], 64) |
| 111 | + accountID := row[linkedAccountID] |
| 112 | + if accountID == "" { |
| 113 | + accountID = row[payerAccountID] |
| 114 | + } |
| 115 | + instances[accountID] = append(instances[accountID], BillingInstance{ |
| 116 | + Name: instanceType.Name, |
| 117 | + AccountID: accountID, |
| 118 | + Hours: hours, |
| 119 | + VCPU: instanceType.VCPU, |
| 120 | + }) |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + return instances |
| 126 | +} |
| 127 | + |
| 128 | +func getLambdaVCPU(filename string) map[string]float64 { |
| 129 | + readFile, err := os.Open(filename) |
| 130 | + check(err) |
| 131 | + defer readFile.Close() |
| 132 | + |
| 133 | + r := csv.NewReader(readFile) |
| 134 | + |
| 135 | + lambdas := make(map[string]float64) |
| 136 | + for { |
| 137 | + row, err := r.Read() |
| 138 | + if err == io.EOF { |
| 139 | + break |
| 140 | + } |
| 141 | + if err != nil { |
| 142 | + log.Fatal(err) |
| 143 | + } |
| 144 | + |
| 145 | + if !strings.Contains(row[recordID], "AccountTotal") && row[linkedAccountID] != "" && row[productCode] == "AWSLambda" && strings.Contains(row[usageType], "Lambda-GB-Second") { |
| 146 | + accountID := row[linkedAccountID] |
| 147 | + if accountID == "" { |
| 148 | + accountID = row[payerAccountID] |
| 149 | + } |
| 150 | + seconds, _ := strconv.ParseFloat(row[usageQty], 64) |
| 151 | + |
| 152 | + lambdas[accountID] += seconds / 3600 / 1024 / hoursPerMonth |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + return lambdas |
| 157 | +} |
| 158 | + |
| 159 | +func getFargateVCPU(filename string) map[string]float64 { |
| 160 | + readFile, err := os.Open(filename) |
| 161 | + check(err) |
| 162 | + defer readFile.Close() |
| 163 | + |
| 164 | + r := csv.NewReader(readFile) |
| 165 | + |
| 166 | + lambdas := make(map[string]float64) |
| 167 | + for { |
| 168 | + row, err := r.Read() |
| 169 | + if err == io.EOF { |
| 170 | + break |
| 171 | + } |
| 172 | + if err != nil { |
| 173 | + log.Fatal(err) |
| 174 | + } |
| 175 | + |
| 176 | + if !strings.Contains(row[recordID], "AccountTotal") && row[linkedAccountID] != "" && row[productCode] == "AmazonECS" && strings.Contains(row[usageType], "Fargate-vCPU-Hours:perCPU") { |
| 177 | + accountID := row[linkedAccountID] |
| 178 | + if accountID == "" { |
| 179 | + accountID = row[payerAccountID] |
| 180 | + } |
| 181 | + hours, _ := strconv.ParseFloat(row[usageQty], 64) |
| 182 | + fmt.Printf("Fargate %s, %.2f\n", accountID, hours) |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + return lambdas |
| 187 | +} |
| 188 | + |
| 189 | +func loadAWSInstanceTypes() []InstanceType { |
| 190 | + var instanceTypes []InstanceType |
| 191 | + for instance, vcpu := range helpers.Aws_instances { |
| 192 | + instanceTypes = append(instanceTypes, InstanceType{ |
| 193 | + Name: instance, |
| 194 | + VCPU: vcpu, |
| 195 | + }) |
| 196 | + } |
| 197 | + |
| 198 | + return instanceTypes |
| 199 | +} |
| 200 | + |
| 201 | +func ParseBilling(cmd *cobra.Command) string { |
| 202 | + billingCSV := helpers.GetFlagEnvironmentString(cmd, "billing", "billing", "Missing Billing CSV", true) |
| 203 | + return billingCSV |
| 204 | +} |
0 commit comments