Blob


1 package icinga
3 import (
4 "encoding/json"
5 "fmt"
6 )
8 func (s Service) name() string {
9 return s.Name
10 }
12 func (s Service) path() string {
13 return "/objects/services/" + s.Name
14 }
16 func (s Service) attrs() map[string]interface{} {
17 m := make(map[string]interface{})
18 m["display_name"] = s.DisplayName
19 return m
20 }
22 // Service represents a Service object.
23 type Service struct {
24 Name string `json:"__name"`
25 Groups []string
26 State ServiceState
27 CheckCommand string `json:"check_command"`
28 DisplayName string `json:"display_name:"`
29 }
31 type ServiceState int
33 const (
34 ServiceOK ServiceState = 0 + iota
35 ServiceWarning
36 ServiceCritical
37 ServiceUnknown
38 )
40 func (s ServiceState) String() string {
41 switch s {
42 case ServiceOK:
43 return "ServiceOK"
44 case ServiceWarning:
45 return "ServiceWarning"
46 case ServiceCritical:
47 return "ServiceCritical"
48 case ServiceUnknown:
49 return "ServiceUnknown"
50 }
51 return "unhandled service state"
52 }
54 func (s Service) MarshalJSON() ([]byte, error) {
55 attrs := make(map[string]interface{})
56 if len(s.Groups) > 0 {
57 attrs["groups"] = s.Groups
58 }
59 attrs["check_command"] = s.CheckCommand
60 attrs["display_name"] = s.DisplayName
61 jservice := &struct {
62 Attrs map[string]interface{} `json:"attrs"`
63 }{Attrs: attrs}
64 return json.Marshal(jservice)
65 }
67 func (c *Client) CreateService(service Service) error {
68 if err := c.createObject(service); err != nil {
69 return fmt.Errorf("create service %s: %w", service.Name, err)
70 }
71 return nil
72 }
74 func (c *Client) LookupService(name string) (Service, error) {
75 obj, err := c.lookupObject("/objects/services/" + name)
76 if err != nil {
77 return Service{}, fmt.Errorf("lookup %s: %w", name, err)
78 }
79 v, ok := obj.(Service)
80 if !ok {
81 return Service{}, fmt.Errorf("lookup %s: result type %T is not service", name, obj)
82 }
83 return v, nil
84 }