Blob


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