3 26fb88e9 2022-01-12 o import "encoding/json"
5 76669f9b 2022-01-11 o func (s Service) name() string {
9 76669f9b 2022-01-11 o func (s Service) path() string {
10 76669f9b 2022-01-11 o return "/objects/services/" + s.Name
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"`
25 f5fde32e 2022-01-18 o type CheckResult struct {
29 76669f9b 2022-01-11 o type ServiceState int
32 76669f9b 2022-01-11 o ServiceOK ServiceState = 0 + iota
34 76669f9b 2022-01-11 o ServiceCritical
38 76669f9b 2022-01-11 o func (s ServiceState) String() string {
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"
49 76669f9b 2022-01-11 o return "unhandled service state"
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
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)
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
72 562d5c1e 2022-01-19 o alias: (*alias)(s),
74 562d5c1e 2022-01-19 o if err := json.Unmarshal(data, &aux); err != nil {
77 562d5c1e 2022-01-19 o if aux.Acknowledgement != 0 {
78 562d5c1e 2022-01-19 o s.Acknowledgement = true