-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathcheck_command.go
94 lines (78 loc) · 2.2 KB
/
check_command.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
93
94
package plugin
import (
"fmt"
"sort"
"strings"
api "github.com/appscode/searchlight/apis/monitoring/v1alpha1"
"github.com/appscode/searchlight/plugins/check_webhook"
)
var checkCommandTemplate = `object CheckCommand "%s" {
import "plugin-check-command"
command = [ PluginDir + %s]
arguments = {
%s
}
}`
func GenerateCheckCommand(plugin *api.SearchlightPlugin) string {
type arg struct {
key string
val string
}
args := make([]arg, 0)
args = append(args, arg{
key: "icinga.checkInterval",
val: "$service.check_interval$",
})
webhook := plugin.Spec.Webhook
if plugin.Spec.Arguments.Vars != nil {
for key := range plugin.Spec.Arguments.Vars.Fields {
args = append(args, arg{
key: key,
val: fmt.Sprintf("$%s$", key),
})
}
}
for key, val := range plugin.Spec.Arguments.Host {
args = append(args, arg{
key: key,
val: fmt.Sprintf("$host.%s$", val),
})
}
sort.Slice(args, func(i, j int) bool {
return args[i].key < args[j].key
})
var command string
flagList := make([]string, 0)
if webhook == nil {
// Command in CheckCommand
parts := strings.Split(plugin.Spec.Command, " ")
for i, part := range parts {
if i == 0 {
command = command + fmt.Sprintf(`"/%s"`, part)
} else {
command = command + fmt.Sprintf(`, "%s"`, part)
}
}
// Arguments in CheckCommand
for _, f := range args {
flagList = append(flagList, fmt.Sprintf(`"--%s" = "%s"`, f.key, f.val))
}
} else {
// Command in CheckCommand
command = `"/hyperalert", "check_webhook"`
// URL for webhook
namespace := "default"
if webhook.Namespace != "" {
namespace = webhook.Namespace
}
url := fmt.Sprintf("http://%s.%s.svc/%s", webhook.Name, namespace, plugin.Name)
flagList = append(flagList, fmt.Sprintf(`"--%s" = "%s"`, check_webhook.FlagWebhookURL, url))
flagList = append(flagList, fmt.Sprintf(`"--%s" = "%s"`, check_webhook.FlagCheckCommand, plugin.Name))
// Arguments in CheckCommand
for i, f := range args {
flagList = append(flagList, fmt.Sprintf(`"--key.%d" = "%s"`, i, f.key))
flagList = append(flagList, fmt.Sprintf(`"--val.%d" = "%s"`, i, f.val))
}
}
return fmt.Sprintf(checkCommandTemplate, plugin.Name, command, strings.Join(flagList, "\n\t"))
}