Blame


1 76669f9b 2022-01-11 o package icinga
2 76669f9b 2022-01-11 o
3 8f08828e 2022-02-11 o import (
4 8f08828e 2022-02-11 o "encoding/json"
5 8f08828e 2022-02-11 o "strings"
6 8f08828e 2022-02-11 o "time"
7 8f08828e 2022-02-11 o )
8 76669f9b 2022-01-11 o
9 76669f9b 2022-01-11 o func (s Service) name() string {
10 76669f9b 2022-01-11 o return s.Name
11 76669f9b 2022-01-11 o }
12 76669f9b 2022-01-11 o
13 76669f9b 2022-01-11 o func (s Service) path() string {
14 76669f9b 2022-01-11 o return "/objects/services/" + s.Name
15 76669f9b 2022-01-11 o }
16 76669f9b 2022-01-11 o
17 76669f9b 2022-01-11 o // Service represents a Service object.
18 76669f9b 2022-01-11 o type Service struct {
19 c26e0f9d 2022-02-02 o Name string `json:"-"`
20 c26e0f9d 2022-02-02 o Groups []string `json:"groups,omitempty"`
21 c26e0f9d 2022-02-02 o State ServiceState `json:"state,omitempty"`
22 c26e0f9d 2022-02-02 o StateType StateType `json:"state_type,omitempty"`
23 c26e0f9d 2022-02-02 o CheckCommand string `json:"check_command"`
24 c26e0f9d 2022-02-02 o DisplayName string `json:"display_name,omitempty"`
25 8f08828e 2022-02-11 o LastCheck time.Time `json:",omitempty"`
26 a17d3f9d 2022-02-11 o LastCheckResult CheckResult `json:"last_check_result,omitempty"`
27 c26e0f9d 2022-02-02 o Acknowledgement bool `json:",omitempty"`
28 001c466e 2023-03-11 o Notes string `json:"notes,omitempty"`
29 001c466e 2023-03-11 o NotesURL string `json:"notes_url,omitempty"`
30 76669f9b 2022-01-11 o }
31 76669f9b 2022-01-11 o
32 f5fde32e 2022-01-18 o type CheckResult struct {
33 8f08828e 2022-02-11 o CheckSource string `json:"check_source"`
34 8f08828e 2022-02-11 o Command interface{}
35 8f08828e 2022-02-11 o Output string
36 f5fde32e 2022-01-18 o }
37 f5fde32e 2022-01-18 o
38 76669f9b 2022-01-11 o type ServiceState int
39 76669f9b 2022-01-11 o
40 76669f9b 2022-01-11 o const (
41 76669f9b 2022-01-11 o ServiceOK ServiceState = 0 + iota
42 76669f9b 2022-01-11 o ServiceWarning
43 76669f9b 2022-01-11 o ServiceCritical
44 76669f9b 2022-01-11 o ServiceUnknown
45 76669f9b 2022-01-11 o )
46 76669f9b 2022-01-11 o
47 dca9442c 2022-02-02 o func (state ServiceState) String() string {
48 dca9442c 2022-02-02 o switch state {
49 76669f9b 2022-01-11 o case ServiceOK:
50 d65c457c 2023-04-22 o return "OK"
51 76669f9b 2022-01-11 o case ServiceWarning:
52 d65c457c 2023-04-22 o return "Warning"
53 76669f9b 2022-01-11 o case ServiceCritical:
54 d65c457c 2023-04-22 o return "Critical"
55 76669f9b 2022-01-11 o }
56 d65c457c 2023-04-22 o return "Unknown"
57 76669f9b 2022-01-11 o }
58 76669f9b 2022-01-11 o
59 562d5c1e 2022-01-19 o // UnmarshalJSON unmarshals service attributes into more meaningful Service field types.
60 562d5c1e 2022-01-19 o func (s *Service) UnmarshalJSON(data []byte) error {
61 562d5c1e 2022-01-19 o type alias Service
62 562d5c1e 2022-01-19 o aux := &struct {
63 f21c51c7 2022-11-01 o Acknowledgement interface{} `json:"acknowledgement"`
64 f21c51c7 2022-11-01 o State interface{} `json:"state"`
65 f21c51c7 2022-11-01 o StateType interface{} `json:"state_type"`
66 f21c51c7 2022-11-01 o LastCheck float64 `json:"last_check"`
67 562d5c1e 2022-01-19 o *alias
68 562d5c1e 2022-01-19 o }{
69 562d5c1e 2022-01-19 o alias: (*alias)(s),
70 562d5c1e 2022-01-19 o }
71 562d5c1e 2022-01-19 o if err := json.Unmarshal(data, &aux); err != nil {
72 562d5c1e 2022-01-19 o return err
73 562d5c1e 2022-01-19 o }
74 f21c51c7 2022-11-01 o switch v := aux.Acknowledgement.(type) {
75 f21c51c7 2022-11-01 o case int:
76 f21c51c7 2022-11-01 o if v != 0 {
77 f21c51c7 2022-11-01 o s.Acknowledgement = true
78 f21c51c7 2022-11-01 o }
79 f21c51c7 2022-11-01 o case float64:
80 f21c51c7 2022-11-01 o if int(v) != 0 {
81 f21c51c7 2022-11-01 o s.Acknowledgement = true
82 f21c51c7 2022-11-01 o }
83 562d5c1e 2022-01-19 o }
84 f21c51c7 2022-11-01 o switch v := aux.State.(type) {
85 f21c51c7 2022-11-01 o case int:
86 f21c51c7 2022-11-01 o s.State = ServiceState(v)
87 f21c51c7 2022-11-01 o case float64:
88 f21c51c7 2022-11-01 o s.State = ServiceState(v)
89 f21c51c7 2022-11-01 o }
90 f21c51c7 2022-11-01 o switch v := aux.StateType.(type) {
91 f21c51c7 2022-11-01 o case int:
92 f21c51c7 2022-11-01 o s.StateType = StateType(v)
93 f21c51c7 2022-11-01 o case float64:
94 f21c51c7 2022-11-01 o s.StateType = StateType(v)
95 f21c51c7 2022-11-01 o }
96 8f08828e 2022-02-11 o s.LastCheck = time.Unix(int64(aux.LastCheck), 0)
97 562d5c1e 2022-01-19 o return nil
98 562d5c1e 2022-01-19 o }
99 8f08828e 2022-02-11 o
100 8f08828e 2022-02-11 o func (s Service) Host() string {
101 8f08828e 2022-02-11 o return strings.SplitN(s.Name, "!", 2)[0]
102 8f08828e 2022-02-11 o }
103 8f08828e 2022-02-11 o
104 8f08828e 2022-02-11 o func (cr CheckResult) RawCommand() string {
105 8f08828e 2022-02-11 o switch v := cr.Command.(type) {
106 8f08828e 2022-02-11 o case string:
107 8f08828e 2022-02-11 o return v
108 8f08828e 2022-02-11 o case []interface{}:
109 8f08828e 2022-02-11 o var cmd []string
110 8f08828e 2022-02-11 o for i := range v {
111 8f08828e 2022-02-11 o if arg, ok := v[i].(string); ok {
112 8f08828e 2022-02-11 o cmd = append(cmd, arg)
113 8f08828e 2022-02-11 o }
114 8f08828e 2022-02-11 o }
115 8f08828e 2022-02-11 o return strings.Join(cmd, " ")
116 8f08828e 2022-02-11 o }
117 8f08828e 2022-02-11 o return "no command"
118 8f08828e 2022-02-11 o }