2018-08-23 02:10:40 +00:00
|
|
|
package icinga2
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"testing"
|
2019-09-06 19:37:17 +00:00
|
|
|
"time"
|
2018-08-23 02:10:40 +00:00
|
|
|
|
2019-09-06 19:37:17 +00:00
|
|
|
"github.com/influxdata/telegraf"
|
2018-08-23 02:10:40 +00:00
|
|
|
"github.com/influxdata/telegraf/testutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestGatherServicesStatus(t *testing.T) {
|
2019-09-06 19:37:17 +00:00
|
|
|
s := `{
|
|
|
|
"results": [
|
|
|
|
{
|
|
|
|
"attrs": {
|
|
|
|
"check_command": "check-bgp-juniper-netconf",
|
|
|
|
"display_name": "eq-par.dc2.fr",
|
|
|
|
"name": "ef017af8-c684-4f3f-bb20-0dfe9fcd3dbe",
|
|
|
|
"state": 0
|
|
|
|
},
|
|
|
|
"joins": {},
|
|
|
|
"meta": {},
|
|
|
|
"name": "eq-par.dc2.fr!ef017af8-c684-4f3f-bb20-0dfe9fcd3dbe",
|
|
|
|
"type": "Service"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
`
|
2018-08-23 02:10:40 +00:00
|
|
|
|
|
|
|
checks := Result{}
|
|
|
|
json.Unmarshal([]byte(s), &checks)
|
|
|
|
|
|
|
|
icinga2 := new(Icinga2)
|
2019-09-23 22:39:50 +00:00
|
|
|
icinga2.Log = testutil.Logger{}
|
2018-08-23 02:10:40 +00:00
|
|
|
icinga2.ObjectType = "services"
|
|
|
|
icinga2.Server = "https://localhost:5665"
|
2019-09-06 19:37:17 +00:00
|
|
|
|
|
|
|
var acc testutil.Accumulator
|
2018-08-23 02:10:40 +00:00
|
|
|
icinga2.GatherStatus(&acc, checks.Results)
|
2019-09-06 19:37:17 +00:00
|
|
|
|
|
|
|
expected := []telegraf.Metric{
|
|
|
|
testutil.MustMetric(
|
|
|
|
"icinga2_services",
|
|
|
|
map[string]string{
|
|
|
|
"display_name": "eq-par.dc2.fr",
|
|
|
|
"check_command": "check-bgp-juniper-netconf",
|
|
|
|
"state": "ok",
|
|
|
|
"source": "localhost",
|
|
|
|
"port": "5665",
|
|
|
|
"scheme": "https",
|
|
|
|
},
|
|
|
|
map[string]interface{}{
|
|
|
|
"name": "ef017af8-c684-4f3f-bb20-0dfe9fcd3dbe",
|
|
|
|
"state_code": 0,
|
|
|
|
},
|
|
|
|
time.Unix(0, 0),
|
|
|
|
),
|
|
|
|
}
|
|
|
|
|
|
|
|
testutil.RequireMetricsEqual(t, expected, acc.GetTelegrafMetrics(), testutil.IgnoreTime())
|
2018-08-23 02:10:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGatherHostsStatus(t *testing.T) {
|
2019-09-06 19:37:17 +00:00
|
|
|
s := `{
|
|
|
|
"results": [
|
|
|
|
{
|
|
|
|
"attrs": {
|
|
|
|
"address": "192.168.1.1",
|
|
|
|
"check_command": "ping",
|
|
|
|
"display_name": "apache",
|
|
|
|
"name": "webserver",
|
|
|
|
"state": 2.0
|
|
|
|
},
|
|
|
|
"joins": {},
|
|
|
|
"meta": {},
|
|
|
|
"name": "webserver",
|
|
|
|
"type": "Host"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
`
|
2018-08-23 02:10:40 +00:00
|
|
|
|
|
|
|
checks := Result{}
|
|
|
|
json.Unmarshal([]byte(s), &checks)
|
|
|
|
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
|
|
|
|
icinga2 := new(Icinga2)
|
2019-09-23 22:39:50 +00:00
|
|
|
icinga2.Log = testutil.Logger{}
|
2018-08-23 02:10:40 +00:00
|
|
|
icinga2.ObjectType = "hosts"
|
|
|
|
icinga2.Server = "https://localhost:5665"
|
2019-09-06 19:37:17 +00:00
|
|
|
|
2018-08-23 02:10:40 +00:00
|
|
|
icinga2.GatherStatus(&acc, checks.Results)
|
2019-09-06 19:37:17 +00:00
|
|
|
|
|
|
|
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())
|
2018-08-23 02:10:40 +00:00
|
|
|
}
|