forked from nerdalert/nflow-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen_targets_file.go
57 lines (42 loc) · 1.11 KB
/
gen_targets_file.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
package main
import (
"encoding/json"
"fmt"
"os"
)
type PromTargetLabels struct {
Ip string `json:"ip"`
Name string `json:"name"`
}
type PromTarget struct {
Targets []string `json:"targets"`
Labels PromTargetLabels `json:"labels"`
}
func GenTargetsFile(filename string, config ConfigFile) error {
targetsFile, err := os.Create(filename)
if err != nil {
return fmt.Errorf("failed to create targets file %s: %v", filename, err)
}
defer targetsFile.Close()
var targets []PromTarget
for _, hostConfig := range config.Hosts {
targets = append(targets, PromTarget{
Targets: []string{hostConfig.Ip + ":" + PROM_PORT},
Labels: PromTargetLabels{
Ip: hostConfig.Ip,
Name: hostConfig.Name,
},
})
}
targetsJson, err := json.MarshalIndent(targets, "", "\t")
if err != nil {
return fmt.Errorf("failed to marshal targets: %v", err)
}
targetsFile.WriteString(string(targetsJson))
err = targetsFile.Sync()
if err != nil {
return fmt.Errorf("failed to sync targets file %s: %v", filename, err)
}
fmt.Println("Successfully generated targets file: " + filename)
return nil
}