diff --git a/README.md b/README.md index da28edb..f05495d 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,35 @@ Prometheus exporter for the Icinga2 API. The `icinga2_exporter` listens on HTTP port 9665 by default. See the `-help` output for more options. +``` +-collector.apilistener + Include APIListener data +-debug + Enable debug logging +-icinga.api string + Path to the Icinga2 API (default "https://localhost:5665/v1") +-icinga.cafile string + Path to the Icinga2 API TLS CA +-icinga.certfile string + Path to the Icinga2 API TLS cert +-icinga.insecure + Skip TLS verification for Icinga2 API +-icinga.keyfile string + Path to the Icinga2 API TLS key +-icinga.password string + Icinga2 API Password +-icinga.username string + Icinga2 API Username +-version + Print version +-web.cache-ttl uint + Cache lifetime in seconds for the Icinga API responses (default 60) +-web.listen-address string + Address on which to expose metrics and web interface. (default ":9665") +-web.metrics-path string + Path under which to expose metrics. (default "/metrics") +``` + ## Collectors By default only the `CIB` metrics of the status API are collected. diff --git a/internal/icinga/client_test.go b/internal/icinga/client_test.go new file mode 100644 index 0000000..7f09ae8 --- /dev/null +++ b/internal/icinga/client_test.go @@ -0,0 +1,82 @@ +package icinga + +import ( + "net/http" + "net/http/httptest" + "net/url" + "os" + "reflect" + "testing" + "time" +) + +const ( + icingaTestDataCIB1 = "testdata/cib1.json" +) + +func loadTestdata(filepath string) []byte { + data, _ := os.ReadFile(filepath) + return data +} + +func testConfig(ts *httptest.Server) Config { + u, _ := url.Parse(ts.URL) + return Config{ + BasicAuthUsername: "", + BasicAuthPassword: "", + CAFile: "", + CertFile: "", + KeyFile: "", + Insecure: true, + CacheTTL: 0 * time.Second, + IcingaAPIURI: *u, + } +} + +func Test_GetCIBMetrics(t *testing.T) { + testcases := map[string]struct { + expected CIBResult + server *httptest.Server + }{ + "cib": { + server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusOK) + w.Write(loadTestdata(icingaTestDataCIB1)) + })), + expected: CIBResult{ + Results: []struct { + Name string `json:"name"` + Status map[string]float64 `json:"status,omitempty"` + }{ + { + Name: "CIB", + Status: map[string]float64{ + "active_host_checks": 0.123, + "uptime": 123.456, + }, + }, + }, + }, + }, + } + + for name, test := range testcases { + t.Run(name, func(t *testing.T) { + defer test.server.Close() + + cfg := testConfig(test.server) + + cli, _ := NewClient(cfg) + + actual, err := cli.GetCIBMetrics() + + if err != nil { + t.Fatalf("did not expect error got:\n %+v", err) + } + + if !reflect.DeepEqual(test.expected, actual) { + t.Fatalf("expected:\n %+v \ngot:\n %+v", test.expected, actual) + } + }) + } +} diff --git a/internal/icinga/testdata/cib1.json b/internal/icinga/testdata/cib1.json new file mode 100644 index 0000000..784d54d --- /dev/null +++ b/internal/icinga/testdata/cib1.json @@ -0,0 +1,12 @@ +{ + "results": [ + { + "name": "CIB", + "perfdata": [], + "status": { + "active_host_checks": 0.123, + "uptime": 123.456 + } + } + ] +}