Fix source field for icinga2 plugin (#7651)

The plugin uses the icinga2 server name, but does not provide the actual host the check is running on.

This fixes that by adding a new field called `server`, where `server` is the icinga2 server and `source` is hostname of the service or host object that icinga2 performing the checks on.
This commit is contained in:
Chase Diem 2020-06-08 13:11:01 -04:00 committed by GitHub
parent bfaecf62b8
commit 073cb9b7db
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 10 deletions

View File

@ -41,12 +41,13 @@ services and hosts. You can read Icinga2's documentation for their remote API
### Tags: ### Tags:
- All measurements have the following tags: - All measurements have the following tags:
- check_command - check_command - The short name of the check command
- display_name - display_name - The name of the service or host
- state - state - The state: UP/DOWN for hosts, OK/WARNING/CRITICAL/UNKNOWN for services
- source - source - The icinga2 host
- port - port - The icinga2 port
- scheme - scheme - The icinga2 protocol (http/https)
- server - The server the check_command is running for
### Sample Queries: ### Sample Queries:

View File

@ -43,6 +43,7 @@ type Attribute struct {
DisplayName string `json:"display_name"` DisplayName string `json:"display_name"`
Name string `json:"name"` Name string `json:"name"`
State float64 `json:"state"` State float64 `json:"state"`
HostName string `json:"host_name"`
} }
var levels = []string{"ok", "warning", "critical", "unknown"} var levels = []string{"ok", "warning", "critical", "unknown"}
@ -94,11 +95,18 @@ func (i *Icinga2) GatherStatus(acc telegraf.Accumulator, checks []Object) {
"state_code": state, "state_code": state,
} }
// source is dependent on 'services' or 'hosts' check
source := check.Attrs.Name
if i.ObjectType == "services" {
source = check.Attrs.HostName
}
tags := map[string]string{ tags := map[string]string{
"display_name": check.Attrs.DisplayName, "display_name": check.Attrs.DisplayName,
"check_command": check.Attrs.CheckCommand, "check_command": check.Attrs.CheckCommand,
"source": source,
"state": levels[state], "state": levels[state],
"source": url.Hostname(), "server": url.Hostname(),
"scheme": url.Scheme, "scheme": url.Scheme,
"port": url.Port(), "port": url.Port(),
} }
@ -136,7 +144,15 @@ func (i *Icinga2) Gather(acc telegraf.Accumulator) error {
i.client = client i.client = client
} }
url := fmt.Sprintf("%s/v1/objects/%s?attrs=name&attrs=display_name&attrs=state&attrs=check_command", i.Server, i.ObjectType) requestUrl := "%s/v1/objects/%s?attrs=name&attrs=display_name&attrs=state&attrs=check_command"
// Note: attrs=host_name is only valid for 'services' requests, using check.Attrs.HostName for the host
// 'hosts' requests will need to use attrs=name only, using check.Attrs.Name for the host
if i.ObjectType == "services" {
requestUrl += "&attrs=host_name"
}
url := fmt.Sprintf(requestUrl, i.Server, i.ObjectType)
req, err := http.NewRequest("GET", url, nil) req, err := http.NewRequest("GET", url, nil)
if err != nil { if err != nil {

View File

@ -16,6 +16,7 @@ func TestGatherServicesStatus(t *testing.T) {
"attrs": { "attrs": {
"check_command": "check-bgp-juniper-netconf", "check_command": "check-bgp-juniper-netconf",
"display_name": "eq-par.dc2.fr", "display_name": "eq-par.dc2.fr",
"host_name": "someserverfqdn.net",
"name": "ef017af8-c684-4f3f-bb20-0dfe9fcd3dbe", "name": "ef017af8-c684-4f3f-bb20-0dfe9fcd3dbe",
"state": 0 "state": 0
}, },
@ -46,7 +47,8 @@ func TestGatherServicesStatus(t *testing.T) {
"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": "someserverfqdn.net",
"server": "localhost",
"port": "5665", "port": "5665",
"scheme": "https", "scheme": "https",
}, },
@ -100,7 +102,8 @@ func TestGatherHostsStatus(t *testing.T) {
"display_name": "apache", "display_name": "apache",
"check_command": "ping", "check_command": "ping",
"state": "critical", "state": "critical",
"source": "localhost", "source": "webserver",
"server": "localhost",
"port": "5665", "port": "5665",
"scheme": "https", "scheme": "https",
}, },