-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathmain.go
92 lines (82 loc) · 3.11 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
package main
import (
"flag"
"net/http"
"strings"
"github.com/banzaicloud/spot-price-exporter/exporter"
"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")
partitions = flag.String("partitions", "aws", "Comma separated list of AWS partitions. Accepted values: aws, aws-cn, aws-us-gov")
productDescriptions = flag.String("product-descriptions", "Linux/UNIX", "Comma separated list of product descriptions. Accepted values: Linux/UNIX, SUSE Linux, Windows, Linux/UNIX (Amazon VPC), SUSE Linux (Amazon VPC), Windows (Amazon VPC)")
regions = flag.String("regions", "", "Comma separated list of AWS regions to get pricing for (defaults to *all*)")
)
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 Spot Price exporter. [log-level=%s, partitions=%s, product-descriptions=%s]", *rawLevel, *partitions, *productDescriptions)
parts := splitAndTrim(*partitions)
pds := splitAndTrim(*productDescriptions)
regions := splitAndTrim(*regions)
validatePartitions(parts)
validateProductDesc(pds)
exporter, err := exporter.NewExporter(parts, pds, regions)
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 validatePartitions(parts []string) {
for _, p := range parts {
if p != "aws" && p != "aws-cn" && p != "aws-us-gov" {
log.Fatalf("partition '%s' is not recognized. Available partitions: aws, aws-cn, aws-us-gov", p)
}
}
}
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 rootHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte(`<html>
<head><title>AWS Spot Price Exporter</title></head>
<body>
<h1>AWS Spot Price Exporter</h1>
<p><a href="` + *metricsPath + `">Metrics</a></p>
</body>
</html>
`))
}