Convert check state to an integer in icinga2 input (#6333)

This commit is contained in:
Daniel Nelson 2019-09-06 12:37:17 -07:00 committed by GitHub
parent 76e7b57fcd
commit 7d2cffe056
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 91 additions and 66 deletions

View File

@ -41,7 +41,7 @@ type Attribute struct {
CheckCommand string `json:"check_command"` CheckCommand string `json:"check_command"`
DisplayName string `json:"display_name"` DisplayName string `json:"display_name"`
Name string `json:"name"` Name string `json:"name"`
State int `json:"state"` State float64 `json:"state"`
} }
var levels = []string{"ok", "warning", "critical", "unknown"} var levels = []string{"ok", "warning", "critical", "unknown"}
@ -88,12 +88,14 @@ func (i *Icinga2) GatherStatus(acc telegraf.Accumulator, checks []Object) {
log.Fatal(err) log.Fatal(err)
} }
state := int64(check.Attrs.State)
fields["name"] = check.Attrs.Name fields["name"] = check.Attrs.Name
fields["state_code"] = check.Attrs.State fields["state_code"] = state
tags["display_name"] = check.Attrs.DisplayName tags["display_name"] = check.Attrs.DisplayName
tags["check_command"] = check.Attrs.CheckCommand tags["check_command"] = check.Attrs.CheckCommand
tags["state"] = levels[check.Attrs.State] tags["state"] = levels[state]
tags["source"] = url.Hostname() tags["source"] = url.Hostname()
tags["scheme"] = url.Scheme tags["scheme"] = url.Scheme
tags["port"] = url.Port() tags["port"] = url.Port()

View File

@ -3,13 +3,15 @@ package icinga2
import ( import (
"encoding/json" "encoding/json"
"testing" "testing"
"time"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/testutil" "github.com/influxdata/telegraf/testutil"
) )
func TestGatherServicesStatus(t *testing.T) { func TestGatherServicesStatus(t *testing.T) {
s := `{
s := `{"results":[ "results": [
{ {
"attrs": { "attrs": {
"check_command": "check-bgp-juniper-netconf", "check_command": "check-bgp-juniper-netconf",
@ -22,70 +24,91 @@ func TestGatherServicesStatus(t *testing.T) {
"name": "eq-par.dc2.fr!ef017af8-c684-4f3f-bb20-0dfe9fcd3dbe", "name": "eq-par.dc2.fr!ef017af8-c684-4f3f-bb20-0dfe9fcd3dbe",
"type": "Service" "type": "Service"
} }
]}` ]
}
`
checks := Result{} checks := Result{}
json.Unmarshal([]byte(s), &checks) json.Unmarshal([]byte(s), &checks)
fields := map[string]interface{}{
"name": "ef017af8-c684-4f3f-bb20-0dfe9fcd3dbe", icinga2 := new(Icinga2)
"state_code": 0, icinga2.ObjectType = "services"
} icinga2.Server = "https://localhost:5665"
tags := map[string]string{
var acc testutil.Accumulator
icinga2.GatherStatus(&acc, checks.Results)
expected := []telegraf.Metric{
testutil.MustMetric(
"icinga2_services",
map[string]string{
"display_name": "eq-par.dc2.fr", "display_name": "eq-par.dc2.fr",
"check_command": "check-bgp-juniper-netconf", "check_command": "check-bgp-juniper-netconf",
"state": "ok", "state": "ok",
"source": "localhost", "source": "localhost",
"port": "5665", "port": "5665",
"scheme": "https", "scheme": "https",
},
map[string]interface{}{
"name": "ef017af8-c684-4f3f-bb20-0dfe9fcd3dbe",
"state_code": 0,
},
time.Unix(0, 0),
),
} }
var acc testutil.Accumulator testutil.RequireMetricsEqual(t, expected, acc.GetTelegrafMetrics(), testutil.IgnoreTime())
icinga2 := new(Icinga2)
icinga2.ObjectType = "services"
icinga2.Server = "https://localhost:5665"
icinga2.GatherStatus(&acc, checks.Results)
acc.AssertContainsTaggedFields(t, "icinga2_services", fields, tags)
} }
func TestGatherHostsStatus(t *testing.T) { func TestGatherHostsStatus(t *testing.T) {
s := `{
s := `{"results":[ "results": [
{ {
"attrs": { "attrs": {
"name": "webserver",
"address": "192.168.1.1", "address": "192.168.1.1",
"check_command": "ping", "check_command": "ping",
"display_name": "apache", "display_name": "apache",
"state": 2 "name": "webserver",
"state": 2.0
}, },
"joins": {}, "joins": {},
"meta": {}, "meta": {},
"name": "webserver", "name": "webserver",
"type": "Host" "type": "Host"
} }
]}` ]
}
`
checks := Result{} checks := Result{}
json.Unmarshal([]byte(s), &checks) json.Unmarshal([]byte(s), &checks)
fields := map[string]interface{}{
"name": "webserver",
"state_code": 2,
}
tags := map[string]string{
"display_name": "apache",
"check_command": "ping",
"state": "critical",
"source": "localhost",
"port": "5665",
"scheme": "https",
}
var acc testutil.Accumulator var acc testutil.Accumulator
icinga2 := new(Icinga2) icinga2 := new(Icinga2)
icinga2.ObjectType = "hosts" icinga2.ObjectType = "hosts"
icinga2.Server = "https://localhost:5665" icinga2.Server = "https://localhost:5665"
icinga2.GatherStatus(&acc, checks.Results) icinga2.GatherStatus(&acc, checks.Results)
acc.AssertContainsTaggedFields(t, "icinga2_hosts", fields, tags)
expected := []telegraf.Metric{
testutil.MustMetric(
"icinga2_hosts",
map[string]string{
"display_name": "apache",
"check_command": "ping",
"state": "critical",
"source": "localhost",
"port": "5665",
"scheme": "https",
},
map[string]interface{}{
"name": "webserver",
"state_code": 2,
},
time.Unix(0, 0),
),
}
testutil.RequireMetricsEqual(t, expected, acc.GetTelegrafMetrics(), testutil.IgnoreTime())
} }