-
Notifications
You must be signed in to change notification settings - Fork 24
/
types.go
123 lines (108 loc) · 3.04 KB
/
types.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
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
package icinga
import (
"fmt"
"os"
"strings"
api "github.com/appscode/searchlight/apis/monitoring/v1alpha1"
cs "github.com/appscode/searchlight/client/clientset/versioned"
"github.com/pkg/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
const (
internalIP = "InternalIP"
TypePod = "pod"
TypeNode = "node"
TypeCluster = "cluster"
)
type IcingaHost struct {
Type string
AlertNamespace string
ObjectName string
IP string
}
func IsValidHostType(t string) bool {
switch t {
case TypePod, TypeNode, TypeCluster:
return true
}
return false
}
func (kh IcingaHost) Name() (string, error) {
switch kh.Type {
case TypePod:
return kh.AlertNamespace + "@" + kh.Type + "@" + kh.ObjectName, nil
case TypeNode:
return kh.AlertNamespace + "@" + kh.Type + "@" + kh.ObjectName, nil
case TypeCluster:
return kh.AlertNamespace + "@" + kh.Type, nil
}
return "", errors.Errorf("unknown host type %s", kh.Type)
}
func (kh IcingaHost) GetAlert(extClient cs.Interface, alertName string) (api.Alert, error) {
switch kh.Type {
case TypePod:
return extClient.MonitoringV1alpha1().PodAlerts(kh.AlertNamespace).Get(alertName, metav1.GetOptions{})
case TypeNode:
return extClient.MonitoringV1alpha1().NodeAlerts(kh.AlertNamespace).Get(alertName, metav1.GetOptions{})
case TypeCluster:
return extClient.MonitoringV1alpha1().ClusterAlerts(kh.AlertNamespace).Get(alertName, metav1.GetOptions{})
}
return nil, errors.Errorf("unknown host type %s", kh.Type)
}
func ParseHost(name string) (*IcingaHost, error) {
parts := strings.SplitN(name, "@", 3)
if !(len(parts) == 2 || len(parts) == 3) {
return nil, errors.Errorf("host %s has a bad format", name)
}
t := parts[1]
switch t {
case TypePod, TypeNode:
if len(parts) != 3 {
return nil, errors.Errorf("host %s has a bad format", name)
}
return &IcingaHost{
AlertNamespace: parts[0],
Type: t,
ObjectName: parts[2],
}, nil
case TypeCluster:
if len(parts) != 2 {
return nil, errors.Errorf("host %s has a bad format", name)
}
return &IcingaHost{
AlertNamespace: parts[0],
Type: t,
}, nil
}
return nil, errors.Errorf("unknown host type %s", t)
}
type IcingaObject struct {
Templates []string `json:"templates,omitempty"`
Attrs map[string]interface{} `json:"attrs"`
}
type ResponseObject struct {
Results []struct {
Attrs struct {
Name string `json:"name"`
CheckInterval float64 `json:"check_interval"`
Vars map[string]interface{} `json:"vars"`
LastState float64 `json:"last_state"`
Acknowledgement float64 `json:"acknowledgement"`
} `json:"attrs"`
Name string `json:"name"`
} `json:"results"`
}
func IVar(value string) string {
return "vars." + value
}
type State int32
const (
OK State = iota // 0
Warning // 1
Critical // 2
Unknown // 3
)
func Output(s State, message interface{}) {
fmt.Fprintln(os.Stdout, s, ":", message)
os.Exit(int(s))
}