-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
74 lines (63 loc) · 1.81 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
package main
import (
"flag"
"fmt"
"net/http"
"strconv"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/prometheus/client_golang/prometheus"
)
var (
addr = flag.String("listen-address", ":9090", "The address to listen on for HTTP requests.")
sleep = flag.Int("sleep", 15, "sleep between polls")
aws_az = flag.String("az", "eu-west-1", "AWS AvailabilityZone")
price_gauge = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "spot_price",
Help: "Spot price of aws instance type.",
}, []string{"AvailabilityZone", "InstanceType", "ProductDescription"})
)
func init() {
prometheus.MustRegister(price_gauge)
}
func main() {
flag.Parse()
go func() {
for {
fetch_prices()
time.Sleep(time.Duration(*sleep) * time.Second)
}
}()
// Expose the registered metrics via HTTP.
http.Handle("/metrics", prometheus.Handler())
http.ListenAndServe(*addr, nil)
}
func fetch_prices() {
svc := ec2.New(session.New(), &aws.Config{Region: aws.String(*aws_az)})
params := &ec2.DescribeSpotPriceHistoryInput{
StartTime: aws.Time(time.Now()),
EndTime: aws.Time(time.Now()),
}
err := svc.DescribeSpotPriceHistoryPages(params,
func(resp *ec2.DescribeSpotPriceHistoryOutput, lastPage bool) bool {
prices := resp.SpotPriceHistory
for _, price := range prices {
spot_p, err := strconv.ParseFloat(*price.SpotPrice, 64)
if err == nil {
price_gauge.WithLabelValues(
*price.AvailabilityZone,
*price.InstanceType,
*price.ProductDescription).Set(spot_p)
} else {
fmt.Println(err)
}
}
return len(prices) > 0
})
if err != nil {
fmt.Println(err.Error())
return
}
}