-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.go
37 lines (30 loc) · 853 Bytes
/
helpers.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
package main
import (
"fmt"
"os"
"gopkg.in/yaml.v2"
)
func generatePrometheusConfig(publicIPs []string, jobName string, listeningPort int, outputFileName string) error {
var config PrometheusConfig
// Prepare the targets with the port
var targets []string
for _, ip := range publicIPs {
target := fmt.Sprintf("%s:%d", ip, listeningPort)
targets = append(targets, target)
}
config = append(config, TargetGroup{
Targets: targets,
Labels: map[string]string{"job": jobName},
})
// Marshal the configuration to YAML
data, err := yaml.Marshal(&config)
if err != nil {
return fmt.Errorf("error marshaling YAML: %w", err)
}
// Write the YAML to the specified output file
err = os.WriteFile(outputFileName, data, 0644)
if err != nil {
return fmt.Errorf("error writing to file '%s': %w", outputFileName, err)
}
return nil
}