Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ See the `-help` output for more options.
```
-collector.apilistener
Include APIListener data
-collector.cib
Include CIB data
-collector.checker
Include CheckerComponent data
-debug
Enable debug logging
-icinga.api string
Expand Down Expand Up @@ -38,14 +42,16 @@ See the `-help` output for more options.

## Collectors

By default only the `CIB` metrics of the status API are collected.
By default only the `IcingaApplication` metrics of the status API are collected.

There are more collectors that can be activated via the CLI.
The tables below list all existing collectors.

| Collector | Flag |
| ------------- | ---------- |
| APIListener | `-collector.apilistener` |
| CIB | `-collector.cib` |
| CheckerComponent | `-collector.checker` |

# Development

Expand Down
13 changes: 12 additions & 1 deletion icinga2_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ func main() {
cliDebugLog bool
cliInsecure bool
cliCollectorApiListener bool
cliCollectorCIB bool
cliCollectorChecker bool
)

flag.StringVar(&cliListenAddress, "web.listen-address", ":9665", "Address on which to expose metrics and web interface.")
Expand All @@ -72,6 +74,8 @@ func main() {
flag.BoolVar(&cliInsecure, "icinga.insecure", false, "Skip TLS verification for Icinga2 API")

flag.BoolVar(&cliCollectorApiListener, "collector.apilistener", false, "Include APIListener data")
flag.BoolVar(&cliCollectorCIB, "collector.cib", false, "Include CIB data")
flag.BoolVar(&cliCollectorChecker, "collector.checker", false, "Include CheckerComponent data")

flag.BoolVar(&cliVersion, "version", false, "Print version")
flag.BoolVar(&cliDebugLog, "debug", false, "Enable debug logging")
Expand Down Expand Up @@ -122,13 +126,20 @@ func main() {
}

// Register Collectors
prometheus.MustRegister(collector.NewIcinga2CIBCollector(c, logger))
prometheus.MustRegister(collector.NewIcinga2ApplicationCollector(c, logger))

if cliCollectorCIB {
prometheus.MustRegister(collector.NewIcinga2CIBCollector(c, logger))
}

if cliCollectorApiListener {
prometheus.MustRegister(collector.NewIcinga2APICollector(c, logger))
}

if cliCollectorChecker {
prometheus.MustRegister(collector.NewIcinga2CheckerCollector(c, logger))
}

// Create a central context to propagate a shutdown
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer cancel()
Expand Down
59 changes: 59 additions & 0 deletions internal/collector/checker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package collector

import (
"log/slog"

"github.com/martialblog/icinga2-exporter/internal/icinga"

"github.com/prometheus/client_golang/prometheus"
)

type Icinga2CheckerCollector struct {
icingaClient *icinga.Client
logger *slog.Logger
checkercomponent_checker_idle *prometheus.Desc
checkercomponent_checker_pending *prometheus.Desc
}

func NewIcinga2CheckerCollector(client *icinga.Client, logger *slog.Logger) *Icinga2CheckerCollector {
return &Icinga2CheckerCollector{
icingaClient: client,
logger: logger,
checkercomponent_checker_idle: prometheus.NewDesc("icinga2_checkercomponent_checker_idle", "CheckerComponent idle", nil, nil),
checkercomponent_checker_pending: prometheus.NewDesc("icinga2_checkercomponent_checker_pending", "CheckerComponent pending", nil, nil),
}
}

func (collector *Icinga2CheckerCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- collector.checkercomponent_checker_idle
ch <- collector.checkercomponent_checker_pending
}

func (collector *Icinga2CheckerCollector) Collect(ch chan<- prometheus.Metric) {
result, err := collector.icingaClient.GetCheckerComponentMetrics()

if err != nil {
collector.logger.Error("Could not retrieve CheckerComponent metrics", "error", err.Error())
return
}

if len(result.Results) < 1 {
collector.logger.Debug("No results for CheckerComponent metrics")
return
}

r := result.Results[0]
// There might be a better way
var perfdata = make(map[string]float64, len(r.Perfdata))
for _, v := range r.Perfdata {
perfdata[v.Label] = v.Value
}

if v, ok := perfdata["checkercomponent_checker_idle"]; ok {
ch <- prometheus.MustNewConstMetric(collector.checkercomponent_checker_idle, prometheus.GaugeValue, v)
}

if v, ok := perfdata["checkercomponent_checker_pending"]; ok {
ch <- prometheus.MustNewConstMetric(collector.checkercomponent_checker_pending, prometheus.GaugeValue, v)
}
}
18 changes: 18 additions & 0 deletions internal/icinga/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,24 @@ func (icinga *Client) GetApplicationMetrics() (ApplicationResult, error) {
return result, nil
}

func (icinga *Client) GetCheckerComponentMetrics() (CheckerComponentResult, error) {
var result CheckerComponentResult

body, errBody := icinga.fetchJSON(endpointCheckerComponent)

if errBody != nil {
return result, fmt.Errorf("error fetching response: %w", errBody)
}

errDecode := json.Unmarshal(body, &result)

if errDecode != nil {
return result, fmt.Errorf("error parsing response: %w", errDecode)
}

return result, nil
}

func (icinga *Client) fetchJSON(endpoint string) ([]byte, error) {
// Lookup data in the cache we go out and bother the Icinga API
if elem, ok := icinga.cache.Get(endpoint); ok {
Expand Down
55 changes: 52 additions & 3 deletions internal/icinga/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ import (
)

const (
icingaTestDataCIB1 = "testdata/cib1.json"
icingaTestDataAPP1 = "testdata/app1.json"
icingaTestDataAPI1 = "testdata/api1.json"
icingaTestDataCIB1 = "testdata/cib1.json"
icingaTestDataAPP1 = "testdata/app1.json"
icingaTestDataAPI1 = "testdata/api1.json"
icingaTestDataCheck1 = "testdata/checker1.json"
)

func loadTestdata(filepath string) []byte {
Expand Down Expand Up @@ -185,3 +186,51 @@ func Test_GetApiListenerMetrics(t *testing.T) {
})
}
}

func Test_GetCheckerMetrics(t *testing.T) {
testcases := map[string]struct {
expected CheckerComponentResult
server *httptest.Server
}{
"application": {
server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write(loadTestdata(icingaTestDataCheck1))
})),
expected: CheckerComponentResult{
Results: []struct {
Name string `json:"name"`
Perfdata []Perfdata `json:"perfdata,omitempty"`
}{
{
Name: "CheckerComponent",
Perfdata: []Perfdata{
{Label: "checkercomponent_checker_idle", Value: 15},
{Label: "checkercomponent_checker_pending", Value: 10},
},
},
},
},
},
}

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.GetCheckerComponentMetrics()

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)
}
})
}
}
7 changes: 7 additions & 0 deletions internal/icinga/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,10 @@ type App struct {
EnableServiceChecks bool `json:"enable_service_checks"`
Version string `json:"version"`
}

type CheckerComponentResult struct {
Results []struct {
Name string `json:"name"`
Perfdata []Perfdata `json:"perfdata,omitempty"`
} `json:"results"`
}
39 changes: 39 additions & 0 deletions internal/icinga/testdata/checker1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"results": [
{
"name": "CheckerComponent",
"perfdata": [
{
"counter": false,
"crit": null,
"label": "checkercomponent_checker_idle",
"max": null,
"min": null,
"type": "PerfdataValue",
"unit": "",
"value": 15,
"warn": null
},
{
"counter": false,
"crit": null,
"label": "checkercomponent_checker_pending",
"max": null,
"min": null,
"type": "PerfdataValue",
"unit": "",
"value": 10,
"warn": null
}
],
"status": {
"checkercomponent": {
"checker": {
"idle": 14,
"pending": 0
}
}
}
}
]
}