11 191337d0 2022-01-18 o type checker interface {
13 191337d0 2022-01-18 o Check(*Client) error
16 191337d0 2022-01-18 o type StateType int
19 191337d0 2022-01-18 o StateSoft StateType = 0 + iota
23 191337d0 2022-01-18 o func (st StateType) String() string {
25 191337d0 2022-01-18 o case StateSoft:
26 191337d0 2022-01-18 o return "StateSoft"
27 191337d0 2022-01-18 o case StateHard:
28 191337d0 2022-01-18 o return "StateHard"
30 191337d0 2022-01-18 o return "unsupported state type"
33 191337d0 2022-01-18 o func (s Service) Check(c *Client) error {
34 191337d0 2022-01-18 o return c.check(s)
37 191337d0 2022-01-18 o func (h Host) Check(c *Client) error {
38 191337d0 2022-01-18 o return c.check(h)
41 191337d0 2022-01-18 o func splitServiceName(name string) []string {
42 191337d0 2022-01-18 o return strings.SplitN(name, "!", 2)
45 191337d0 2022-01-18 o func (c *Client) check(ch checker) error {
46 191337d0 2022-01-18 o var filter struct {
47 191337d0 2022-01-18 o Type string `json:"type"`
48 191337d0 2022-01-18 o Expr string `json:"filter"`
50 191337d0 2022-01-18 o switch v := ch.(type) {
52 191337d0 2022-01-18 o filter.Type = "Host"
53 191337d0 2022-01-18 o filter.Expr = fmt.Sprintf("host.name == %q", v.Name)
55 191337d0 2022-01-18 o filter.Type = "Service"
56 191337d0 2022-01-18 o a := splitServiceName(v.Name)
57 191337d0 2022-01-18 o if len(a) != 2 {
58 191337d0 2022-01-18 o return fmt.Errorf("check %s: invalid service name", v.Name)
61 191337d0 2022-01-18 o service := a[1]
62 191337d0 2022-01-18 o filter.Expr = fmt.Sprintf("host.name == %q && service.name == %q", host, service)
64 191337d0 2022-01-18 o return fmt.Errorf("cannot check %T", v)
67 191337d0 2022-01-18 o buf := &bytes.Buffer{}
68 191337d0 2022-01-18 o if err := json.NewEncoder(buf).Encode(filter); err != nil {
71 191337d0 2022-01-18 o resp, err := c.post("/actions/reschedule-check", buf)
72 191337d0 2022-01-18 o if err != nil {
73 191337d0 2022-01-18 o return fmt.Errorf("check %s: %w", ch.name(), err)
75 191337d0 2022-01-18 o switch resp.StatusCode {
76 191337d0 2022-01-18 o case http.StatusOK:
78 191337d0 2022-01-18 o case http.StatusNotFound:
79 191337d0 2022-01-18 o return fmt.Errorf("check %s: %w", ch.name(), ErrNotExist)
81 191337d0 2022-01-18 o return fmt.Errorf("check %s: %s", ch.name(), resp.Status)