forked from banzaicloud/spot-price-exporter
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
127 lines (113 loc) · 4.28 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
package main
import (
"context"
"flag"
"net/http"
"strings"
"github.com/AndreZiviani/ec2-price-exporter/exporter"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/ec2"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
log "github.com/sirupsen/logrus"
)
var (
addr = flag.String("listen-address", ":8080", "The address to listen on for HTTP requests.")
metricsPath = flag.String("metrics-path", "/metrics", "path to metrics endpoint")
rawLevel = flag.String("log-level", "info", "log level")
productDescriptions = flag.String("product-descriptions", "Linux/UNIX", "Comma separated list of product descriptions, used to filter spot instances. Accepted values: Linux/UNIX, SUSE Linux, Windows, Linux/UNIX (Amazon VPC), SUSE Linux (Amazon VPC), Windows (Amazon VPC)")
operatingSystems = flag.String("operating-systems", "Linux", "Comma separated list of operating systems, used to filter ondemand instances. Accepted values: Linux, RHEL, SUSE, Windows")
regions = flag.String("regions", "", "Comma separated list of AWS regions to get pricing for (defaults to *all*)")
lifecycle = flag.String("lifecycle", "", "Comma separated list of Lifecycles (spot or ondemand) to get pricing for (defaults to *all*)")
cache = flag.Int("cache", 0, "How long should the results be cached, in seconds (defaults to *0*)")
)
func init() {
flag.Parse()
parsedLevel, err := log.ParseLevel(*rawLevel)
if err != nil {
log.WithError(err).Warnf("Couldn't parse log level, using default: %s", log.GetLevel())
} else {
log.SetLevel(parsedLevel)
log.Debugf("Set log level to %s", parsedLevel)
}
}
func main() {
log.Infof("Starting AWS EC2 Price exporter. [log-level=%s, product-descriptions=%s, operating-systems=%s, cache=%d, lifecycle=%s]", *rawLevel, *productDescriptions, *operatingSystems, *cache, *lifecycle)
var reg []string
if len(*regions) == 0 {
cfg, err := config.LoadDefaultConfig(context.TODO())
if err != nil {
log.WithError(err).Errorf("error while initializing aws client to list available regions")
return
}
ec2Svc := ec2.NewFromConfig(cfg)
r, err := ec2Svc.DescribeRegions(context.TODO(), &ec2.DescribeRegionsInput{AllRegions: aws.Bool(false)})
if err != nil {
log.Fatal(err)
return
}
for _, region := range r.Regions {
reg = append(reg, *region.RegionName)
}
} else {
reg = splitAndTrim(*regions)
}
pds := splitAndTrim(*productDescriptions)
oss := splitAndTrim(*operatingSystems)
lc := splitAndTrim(*lifecycle)
if len(lc) == 0 {
lc = []string{"spot", "ondemand"}
}
validateProductDesc(pds)
validateOperatingSystems(oss)
exporter, err := exporter.NewExporter(pds, oss, reg, lc, *cache)
if err != nil {
log.Fatal(err)
}
prometheus.MustRegister(exporter)
log.Infof("Starting metric http endpoint [address=%s, path=%s]", *addr, *metricsPath)
http.Handle(*metricsPath, promhttp.Handler())
http.HandleFunc("/", rootHandler)
log.Fatal(http.ListenAndServe(*addr, nil))
}
func splitAndTrim(str string) []string {
if str == "" {
return []string{}
}
parts := strings.Split(str, ",")
for i := range parts {
parts[i] = strings.TrimSpace(parts[i])
}
return parts
}
func validateProductDesc(pds []string) {
for _, desc := range pds {
if desc != "Linux/UNIX" && desc != "Linux/UNIX (Amazon VPC)" &&
desc != "SUSE Linux" && desc != "SUSE Linux (Amazon VPC)" &&
desc != "Windows" && desc != "Windows (Amazon VPC)" {
log.Fatalf("product description '%s' is not recognized. Available product descriptions: Linux/UNIX, SUSE Linux, Windows, Linux/UNIX (Amazon VPC), SUSE Linux (Amazon VPC), Windows (Amazon VPC)", desc)
}
}
}
func validateOperatingSystems(oss []string) {
for _, os := range oss {
if os != "Linux" &&
os != "RHEL" &&
os != "SUSE" &&
os != "Windows" {
log.Fatalf("Operating System '%s' is not recognized. Available operating system: Linux, RHEL, SUSE, Windows", os)
}
}
}
func rootHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte(`<html>
<head><title>AWS EC2 Price Exporter</title></head>
<body>
<h1>AWS EC2 Price Exporter</h1>
<p><a href="` + *metricsPath + `">Metrics</a></p>
</body>
</html>
`))
}