-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
141 lines (120 loc) · 3.99 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
package main
import (
"flag"
"net/http"
"os"
"sync"
"time"
"github.com/CoverGenius/cloudwatch-prometheus-exporter/backup"
"github.com/CoverGenius/cloudwatch-prometheus-exporter/ec2"
"github.com/CoverGenius/cloudwatch-prometheus-exporter/elasticache"
"github.com/CoverGenius/cloudwatch-prometheus-exporter/elb"
"github.com/CoverGenius/cloudwatch-prometheus-exporter/elbv2"
"github.com/CoverGenius/cloudwatch-prometheus-exporter/network"
"github.com/CoverGenius/cloudwatch-prometheus-exporter/s3"
"github.com/CoverGenius/cloudwatch-prometheus-exporter/sqs"
"github.com/CoverGenius/cloudwatch-prometheus-exporter/vpc"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/cloudwatch"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/CoverGenius/cloudwatch-prometheus-exporter/base"
h "github.com/CoverGenius/cloudwatch-prometheus-exporter/helpers"
"github.com/CoverGenius/cloudwatch-prometheus-exporter/rds"
log "github.com/sirupsen/logrus"
)
var (
rdd []*base.RegionDescription
aws_session *session.Session
config string
)
func init() {
flag.StringVar(&config, "config", "config.yaml", "Path to config file")
}
func run(nd map[string]*base.NamespaceDescription, cw *cloudwatch.CloudWatch, rd *base.RegionDescription, pi int64) {
var delay int64 = 0
for {
select {
case <-time.After(time.Duration(delay) * time.Second):
var wg sync.WaitGroup
wg.Add(11)
log.Debug("Creating list of resources ...")
go elasticache.CreateResourceList(nd["AWS/ElastiCache"], &wg)
go rds.CreateResourceList(nd["AWS/RDS"], &wg)
go ec2.CreateResourceList(nd["AWS/EC2"], &wg)
go network.CreateResourceList(nd["AWS/NATGateway"], &wg)
go elb.CreateResourceList(nd["AWS/ELB"], &wg)
go elbv2.CreateResourceList(nd["AWS/ApplicationELB"], &wg)
go elbv2.CreateResourceList(nd["AWS/NetworkELB"], &wg)
go s3.CreateResourceList(nd["AWS/S3"], &wg)
go sqs.CreateResourceList(nd["AWS/SQS"], &wg)
go vpc.CreateResourceList(nd["AWS/VPC"], &wg)
go backup.CreateResourceList(nd["AWS/Backup"], &wg)
wg.Wait()
delay = pi
go rd.GatherMetrics(cw)
}
}
}
func processConfig(p *string) *base.Config {
c := base.Config{}
h.YAMLDecode(p, &c)
if c.Listen == "" {
c.Listen = "127.0.0.1:8080"
}
if c.PeriodSeconds == 0 {
c.PeriodSeconds = 60
}
if c.RangeSeconds == 0 {
c.RangeSeconds = 300
}
if c.AccountID == "" {
log.Fatal("Please specify account ID")
}
if c.APIKey != "" && c.APISecret != "" {
log.Info("Using access key ID and secret access key in order to establish a session!")
os.Setenv("AWS_ACCESS_KEY_ID", c.APIKey)
os.Setenv("AWS_SECRET_ACCESS_KEY", c.APISecret)
} else {
log.Info("api_key or api_secret is empty. Trying EC2 role instead!")
}
if len(c.Regions) < 1 {
log.Fatal("No regions specified. Please set at least one!")
}
if c.PollInterval == 0 {
c.PollInterval = 300
}
log.SetOutput(os.Stdout)
log.SetLevel(h.GetLogLevel(c.LogLevel))
return &c
}
func main() {
flag.Parse()
// TODO allow hot reload of config
c := processConfig(&config)
defaults := map[string]map[string]*base.MetricDescription{
"AWS/RDS": rds.Metrics,
"AWS/ElastiCache": elasticache.Metrics,
"AWS/EC2": ec2.Metrics,
"AWS/NATGateway": network.Metrics,
"AWS/ELB": elb.Metrics,
"AWS/ApplicationELB": elbv2.ALBMetrics,
"AWS/NetworkELB": elbv2.NLBMetrics,
"AWS/S3": s3.Metrics,
"AWS/SQS": sqs.Metrics,
"AWS/VPC": vpc.Metrics,
"AWS/Backup": backup.Metrics,
}
mds := c.ConstructMetrics(defaults)
for _, r := range c.Regions {
awsSession := base.CreateAWSSession(c, r)
cw := cloudwatch.New(awsSession)
rd := base.RegionDescription{Region: r}
rdd = append(rdd, &rd)
if err := rd.Init(awsSession, &c.AccountID, c.Tags, mds); err != nil {
log.Fatalf("error initializing region: %s", err)
}
go run(rd.Namespaces, cw, &rd, c.PollInterval)
}
http.Handle("/metrics", promhttp.Handler())
log.Fatal(http.ListenAndServe(c.Listen, nil))
}