-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.go
More file actions
152 lines (137 loc) · 3.43 KB
/
main.go
File metadata and controls
152 lines (137 loc) · 3.43 KB
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
142
143
144
145
146
147
148
149
150
151
152
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"github.com/exoscale/egoscale"
"io/ioutil"
"log"
"os"
"os/signal"
"strconv"
"syscall"
"time"
)
type TargetGroup struct {
Targets []string `json:"targets"`
Labels map[string]string `json:"labels"`
}
type StaticSDConfig []TargetGroup
func getInstancePoolInstanceIps(client *egoscale.Client, zoneId *egoscale.UUID, poolId *egoscale.UUID) ([]string, error) {
ctx := context.Background()
resp, err := client.RequestWithContext(ctx, egoscale.GetInstancePool{
ZoneID: zoneId,
ID: poolId,
})
if err != nil {
log.Printf("failed to get instance pool from Exoscale (%v)", err)
// Ignore error. next run will hopefully work better
return []string{}, err
}
response := resp.(*egoscale.GetInstancePoolResponse)
if len(response.InstancePools) == 0 {
log.Fatalf("instance pool not found")
} else if len(response.InstancePools) > 1 {
//This should never happen
log.Fatalf("more than one instance pool returned")
}
instancePool := response.InstancePools[0]
var ips []string
for _, vm := range instancePool.VirtualMachines {
ips = append(ips, vm.Nic[0].IPAddress.String())
}
return ips, nil
}
func main() {
instancePoolId := ""
exoscaleEndpoint := "https://api.exoscale.ch/v1/"
exoscaleZoneId := ""
exoscaleApiKey := ""
exoscaleApiSecret := ""
prometheusFile := "/var/run/prometheus-sd-exoscale-instance-pools/config.json"
prometheusPort := 9100
flag.StringVar(
&instancePoolId,
"instance-pool-id",
instancePoolId,
"ID of the instance pool to query",
)
flag.StringVar(
&exoscaleZoneId,
"exoscale-zone-id",
exoscaleZoneId,
"Exoscale zone ID",
)
flag.StringVar(
&exoscaleEndpoint,
"exoscale-endpoint",
exoscaleEndpoint,
"Endpoint URL of the Exoscale API",
)
flag.StringVar(
&exoscaleApiKey,
"exoscale-api-key",
exoscaleApiKey,
"API key for Exoscale",
)
flag.StringVar(
&exoscaleApiSecret,
"exoscale-api-secret",
exoscaleApiSecret,
"API secret for Exoscale",
)
flag.StringVar(
&prometheusFile,
"prometheus-file",
prometheusFile,
"Static service discovery file for Prometheus",
)
flag.IntVar(
&prometheusPort,
"prometheus-port",
prometheusPort,
"Port the metrics service runs on",
)
flag.Parse()
zoneId, err := egoscale.ParseUUID(exoscaleZoneId)
if err != nil {
log.Fatalf("invalid zone ID (%v)", err)
}
poolId, err := egoscale.ParseUUID(instancePoolId)
if err != nil {
log.Fatalf("invalid pool ID (%v)", err)
}
exoscaleClient := egoscale.NewClient(exoscaleEndpoint, exoscaleApiKey, exoscaleApiSecret)
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
for {
ips, err := getInstancePoolInstanceIps(exoscaleClient, zoneId, poolId)
if err != nil {
log.Fatalf("failed to fetch instance pool IPs, crashing out (%v)", err)
}
var targets []string
for _, ip := range ips {
targets = append(targets, ip + ":" + strconv.Itoa(prometheusPort))
}
config := StaticSDConfig{TargetGroup{
Targets: targets,
Labels: map[string]string{},
}}
file, err := json.MarshalIndent(config, "", " ")
if err != nil {
log.Fatalf("failed to create JSON (%v)", err)
}
err = ioutil.WriteFile(prometheusFile, file, 0644)
if err != nil {
log.Fatalf("failed to write Prometheus file %s (%v)", prometheusFile, err)
}
select {
case _, _ = <-sigs:
fmt.Println("interrupt received, shutting down")
break
default:
time.Sleep(10 * time.Second)
}
}
}