Blame


1 76669f9b 2022-01-11 o package icinga
2 76669f9b 2022-01-11 o
3 26fb88e9 2022-01-12 o import "encoding/json"
4 76669f9b 2022-01-11 o
5 76669f9b 2022-01-11 o func (s Service) name() string {
6 76669f9b 2022-01-11 o return s.Name
7 76669f9b 2022-01-11 o }
8 76669f9b 2022-01-11 o
9 76669f9b 2022-01-11 o func (s Service) path() string {
10 76669f9b 2022-01-11 o return "/objects/services/" + s.Name
11 76669f9b 2022-01-11 o }
12 76669f9b 2022-01-11 o
13 76669f9b 2022-01-11 o // Service represents a Service object.
14 76669f9b 2022-01-11 o type Service struct {
15 46ca0f68 2022-02-01 o Name string `json:"-"`
16 46ca0f68 2022-02-01 o Groups []string `json:"groups,omitempty"`
17 f5fde32e 2022-01-18 o State ServiceState
18 f5fde32e 2022-01-18 o StateType StateType `json:"state_type"`
19 f5fde32e 2022-01-18 o CheckCommand string `json:"check_command"`
20 46ca0f68 2022-02-01 o DisplayName string `json:"display_name,omitempty"`
21 46ca0f68 2022-02-01 o LastCheckResult CheckResult `json:"last_check_result,omitempty"`
22 46ca0f68 2022-02-01 o Acknowledgement bool `json:",omitempty"`
23 76669f9b 2022-01-11 o }
24 76669f9b 2022-01-11 o
25 f5fde32e 2022-01-18 o type CheckResult struct {
26 f5fde32e 2022-01-18 o Output string
27 f5fde32e 2022-01-18 o }
28 f5fde32e 2022-01-18 o
29 76669f9b 2022-01-11 o type ServiceState int
30 76669f9b 2022-01-11 o
31 76669f9b 2022-01-11 o const (
32 76669f9b 2022-01-11 o ServiceOK ServiceState = 0 + iota
33 76669f9b 2022-01-11 o ServiceWarning
34 76669f9b 2022-01-11 o ServiceCritical
35 76669f9b 2022-01-11 o ServiceUnknown
36 76669f9b 2022-01-11 o )
37 76669f9b 2022-01-11 o
38 76669f9b 2022-01-11 o func (s ServiceState) String() string {
39 76669f9b 2022-01-11 o switch s {
40 76669f9b 2022-01-11 o case ServiceOK:
41 76669f9b 2022-01-11 o return "ServiceOK"
42 76669f9b 2022-01-11 o case ServiceWarning:
43 76669f9b 2022-01-11 o return "ServiceWarning"
44 76669f9b 2022-01-11 o case ServiceCritical:
45 76669f9b 2022-01-11 o return "ServiceCritical"
46 76669f9b 2022-01-11 o case ServiceUnknown:
47 76669f9b 2022-01-11 o return "ServiceUnknown"
48 76669f9b 2022-01-11 o }
49 76669f9b 2022-01-11 o return "unhandled service state"
50 76669f9b 2022-01-11 o }
51 76669f9b 2022-01-11 o
52 76669f9b 2022-01-11 o func (s Service) MarshalJSON() ([]byte, error) {
53 76669f9b 2022-01-11 o attrs := make(map[string]interface{})
54 76669f9b 2022-01-11 o if len(s.Groups) > 0 {
55 76669f9b 2022-01-11 o attrs["groups"] = s.Groups
56 76669f9b 2022-01-11 o }
57 76669f9b 2022-01-11 o attrs["check_command"] = s.CheckCommand
58 76669f9b 2022-01-11 o attrs["display_name"] = s.DisplayName
59 76669f9b 2022-01-11 o jservice := &struct {
60 76669f9b 2022-01-11 o Attrs map[string]interface{} `json:"attrs"`
61 76669f9b 2022-01-11 o }{Attrs: attrs}
62 76669f9b 2022-01-11 o return json.Marshal(jservice)
63 76669f9b 2022-01-11 o }
64 562d5c1e 2022-01-19 o
65 562d5c1e 2022-01-19 o // UnmarshalJSON unmarshals service attributes into more meaningful Service field types.
66 562d5c1e 2022-01-19 o func (s *Service) UnmarshalJSON(data []byte) error {
67 562d5c1e 2022-01-19 o type alias Service
68 562d5c1e 2022-01-19 o aux := &struct {
69 562d5c1e 2022-01-19 o Acknowledgement int
70 562d5c1e 2022-01-19 o *alias
71 562d5c1e 2022-01-19 o }{
72 562d5c1e 2022-01-19 o alias: (*alias)(s),
73 562d5c1e 2022-01-19 o }
74 562d5c1e 2022-01-19 o if err := json.Unmarshal(data, &aux); err != nil {
75 562d5c1e 2022-01-19 o return err
76 562d5c1e 2022-01-19 o }
77 562d5c1e 2022-01-19 o if aux.Acknowledgement != 0 {
78 562d5c1e 2022-01-19 o s.Acknowledgement = true
79 562d5c1e 2022-01-19 o }
80 562d5c1e 2022-01-19 o return nil
81 562d5c1e 2022-01-19 o }