-
Notifications
You must be signed in to change notification settings - Fork 80
/
main.go
120 lines (99 loc) · 3.13 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
package main
import (
"fmt"
"github.com/Optum/dce/pkg/account"
"github.com/Optum/dce/pkg/config"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/cloudwatch"
"github.com/aws/aws-sdk-go/service/cloudwatch/cloudwatchiface"
"log"
)
var (
Services *config.ServiceBuilder
)
func initConfig() {
log.Println("Cold start; initializing lambda")
// load up the values into the various settings...
cfgBldr := &config.ConfigurationBuilder{}
_ = cfgBldr.
WithEnv("AWS_CURRENT_REGION", "AWS_CURRENT_REGION", "us-east-1").
Build()
svcBuilder := &config.ServiceBuilder{Config: cfgBldr}
_, err := svcBuilder.
WithAccountService().
WithCloudWatchService().
Build()
if err != nil {
errorMessage := fmt.Sprintf("Failed to initialize account service: %s", err)
log.Fatal(errorMessage)
}
Services = svcBuilder
}
// Hardcoding until pagination function is implemented
var QueryLimit int64 = 20000
func getMetric(status account.Status) CountMetric {
query := account.Account{
Status: &status,
Limit: &QueryLimit,
}
accounts, err := Services.AccountService().List(&query)
if err != nil {
log.Fatal("failed to query accounts by name, ", err)
}
return CountMetric{
name: status.String(),
count: len(*accounts),
}
}
type CountMetric struct {
name string
count int
}
func publishMetrics(namespace string, countMetrics CountMetric) {
log.Println("Publishing metrics to cloudwatch")
var cloudWatchSvc cloudwatchiface.CloudWatchAPI
if err := Services.Config.GetService(&cloudWatchSvc); err != nil {
panic(err)
}
metricData := []*cloudwatch.MetricDatum{
{
MetricName: aws.String(string(countMetrics.name) + "Accounts"),
Unit: aws.String("Count"),
Value: aws.Float64(float64(countMetrics.count)),
},
}
_, err := cloudWatchSvc.PutMetricData(&cloudwatch.PutMetricDataInput{
Namespace: aws.String(namespace),
MetricData: metricData,
})
if err != nil {
log.Fatalln(err)
}
}
// Handler - Handle the lambda function
func Handler(_ events.CloudWatchEvent) {
log.Printf("Initializing account pool metrics lambda")
initConfig()
Ready := getMetric(account.StatusReady)
NotReady := getMetric(account.StatusNotReady)
Leased := getMetric(account.StatusLeased)
Orphaned := getMetric(account.StatusOrphaned)
log.Println("Found ", Ready.count, Ready.name, " accounts")
log.Println("Found ", NotReady.count, NotReady.name, " accounts")
log.Println("Found ", Leased.count, Leased.name, " accounts")
log.Println("Found ", Orphaned.count, Orphaned.name, " accounts")
publishMetrics("DCE/AccountPool", Ready)
publishMetrics("DCE/AccountPool", NotReady)
publishMetrics("DCE/AccountPool", Leased)
publishMetrics("DCE/AccountPool", Orphaned)
log.Println("Published ReadyAccount Metric: ", float64(Ready.count))
log.Println("Published NotReadyAccounts Metric: ", float64(NotReady.count))
log.Println("Published LeasedAccounts Metric: ", float64(Leased.count))
log.Println("Published OrphanedAccounts Metric: ", float64(Orphaned.count))
log.Print("Account pool metrics lambda complete")
}
func main() {
lambda.Start(Handler)
}