Commit Diff


commit - dd0af5f789c1bac0444c33fcc4434922be48d1e7
commit + 7cb145ba970416743d12a36bb0a4df59264203da
blob - 243d6d9e4fe762f6290d47900923d6519ef11d7a
blob + bb803fd93dae42f336a709dbcffefd36df670a84
--- checker.go
+++ checker.go
@@ -40,6 +40,12 @@ func (h Host) Check(c *Client) error {
 	return c.check(h)
 }
 
+// Check reschedules the checks for all hosts in the HostGroup hg via the
+// provided Client.
+func (hg HostGroup) Check(c *Client) error {
+	return c.check(hg)
+}
+
 func splitServiceName(name string) []string {
 	return strings.SplitN(name, "!", 2)
 }
@@ -62,6 +68,9 @@ func (c *Client) check(ch checker) error {
 		host := a[0]
 		service := a[1]
 		filter.Expr = fmt.Sprintf("host.name == %q && service.name == %q", host, service)
+	case HostGroup:
+		filter.Type = "Host"
+		filter.Expr = fmt.Sprintf("%q in host.groups", v.Name)
 	default:
 		return fmt.Errorf("cannot check %T", v)
 	}
@@ -74,12 +83,18 @@ func (c *Client) check(ch checker) error {
 	if err != nil {
 		return fmt.Errorf("check %s: %w", ch.name(), err)
 	}
-	switch resp.StatusCode {
-	case http.StatusOK:
+	if resp.StatusCode == http.StatusOK {
 		return nil
-	case http.StatusNotFound:
+	} else if resp.StatusCode == http.StatusNotFound {
 		return fmt.Errorf("check %s: %w", ch.name(), ErrNotExist)
-	default:
-		return fmt.Errorf("check %s: %s", ch.name(), resp.Status)
 	}
+	defer resp.Body.Close()
+	iresp, err := parseResponse(resp.Body)
+	if err != nil {
+		return fmt.Errorf("check %s: parse response: %v", ch.name(), err)
+	}
+	if iresp.Error != nil {
+		return fmt.Errorf("check %s: %v", ch.name(), iresp.Error)
+	}
+	return fmt.Errorf("check %s: %s", ch.name(), resp.Status)
 }
blob - 61b38ff5a9a35520693db11b03b0607ab7b557e5
blob + 6706b881d34f7c9f765071850524e5c24304ee91
--- icinga_test.go
+++ icinga_test.go
@@ -21,6 +21,27 @@ func randomHostname() string {
 	return string(b) + ".example.org"
 }
 
+func createTestHosts(c *icinga.Client) ([]icinga.Host, error) {
+	hostgroup := icinga.HostGroup{Name: "test", DisplayName: "Test Group"}
+	if err := c.CreateHostGroup(hostgroup); err != nil && !errors.Is(err, icinga.ErrExist) {
+		return nil, err
+	}
+
+	var hosts []icinga.Host
+	for i := 0; i < 5; i++ {
+		h := icinga.Host{
+			Name:         randomHostname(),
+			CheckCommand: "random",
+			Groups:       []string{hostgroup.Name},
+		}
+		hosts = append(hosts, h)
+		if err := c.CreateHost(h); err != nil && !errors.Is(err, icinga.ErrExist) {
+			return nil, err
+		}
+	}
+	return hosts, nil
+}
+
 func newTestClient() (*icinga.Client, error) {
 	tp := http.DefaultTransport.(*http.Transport)
 	tp.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
@@ -47,40 +68,22 @@ func TestFilter(t *testing.T) {
 		t.Skipf("no local test icinga? got: %v", err)
 	}
 
-	hostgroup := icinga.HostGroup{Name: "examples", DisplayName: "Test Group"}
-	if err := client.CreateHostGroup(hostgroup); err != nil {
-		t.Error(err)
-	}
-	hostgroup, err = client.LookupHostGroup(hostgroup.Name)
-	if err != nil {
-		t.Error(err)
-	}
-	defer client.DeleteHostGroup(hostgroup.Name, false)
-
 	var want, got []string
-	for i := 0; i < 5; i++ {
-		h := icinga.Host{
-			Name:         randomHostname(),
-			CheckCommand: "hostalive",
-			Groups:       []string{hostgroup.Name},
-		}
+	hosts, err := createTestHosts(client)
+	if err != nil {
+		t.Fatal(err)
+	}
+	for _, h := range hosts {
 		want = append(want, h.Name)
-		if err := client.CreateHost(h); err != nil {
-			if !errors.Is(err, icinga.ErrExist) {
-				t.Error(err)
-			}
-			continue
-		}
-		t.Logf("created host %s", h.Name)
 	}
 	defer func() {
-		for _, name := range want {
-			if err := client.DeleteHost(name, false); err != nil {
+		for _, h := range hosts {
+			if err := client.DeleteHost(h.Name, false); err != nil {
 				t.Log(err)
 			}
 		}
 	}()
-	hosts, err := client.Hosts("match(\"*example.org\", host.name)")
+	hosts, err = client.Hosts("match(\"*example.org\", host.name)")
 	if err != nil {
 		t.Fatal(err)
 	}
@@ -135,6 +138,31 @@ func TestChecker(t *testing.T) {
 	t.Logf("%+v\n", s)
 }
 
+func TestCheckHostGroup(t *testing.T) {
+	client, err := newTestClient()
+	if err != nil {
+		t.Skipf("no local test icinga? got: %v", err)
+	}
+	hosts, err := createTestHosts(client)
+	if err != nil {
+		t.Fatal(err)
+	}
+	defer func() {
+		for _, h := range hosts {
+			if err := client.DeleteHost(h.Name, true); err != nil {
+				t.Error(err)
+			}
+		}
+	}()
+	hostgroup, err := client.LookupHostGroup("test")
+	if err != nil {
+		t.Fatal(err)
+	}
+	if err := hostgroup.Check(client); err != nil {
+		t.Fatal(err)
+	}
+}
+
 func TestCreateService(t *testing.T) {
 	client, err := newTestClient()
 	if err != nil {