Skip to content

Commit

Permalink
Fix source field for icinga2 plugin (influxdata#7651)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
abracadv8 authored and idohalevi committed Sep 23, 2020
1 parent 4045c34 commit c798410
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
13 changes: 7 additions & 6 deletions plugins/inputs/icinga2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,13 @@ services and hosts. You can read Icinga2's documentation for their remote API
### Tags:

- All measurements have the following tags:
- check_command
- display_name
- state
- source
- port
- scheme
- check_command - The short name of the check command
- display_name - The name of the service or host
- state - The state: UP/DOWN for hosts, OK/WARNING/CRITICAL/UNKNOWN for services
- source - The icinga2 host
- port - The icinga2 port
- scheme - The icinga2 protocol (http/https)
- server - The server the check_command is running for

### Sample Queries:

Expand Down
20 changes: 18 additions & 2 deletions plugins/inputs/icinga2/icinga2.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type Attribute struct {
DisplayName string `json:"display_name"`
Name string `json:"name"`
State float64 `json:"state"`
HostName string `json:"host_name"`
}

var levels = []string{"ok", "warning", "critical", "unknown"}
Expand Down Expand Up @@ -94,11 +95,18 @@ func (i *Icinga2) GatherStatus(acc telegraf.Accumulator, checks []Object) {
"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{
"display_name": check.Attrs.DisplayName,
"check_command": check.Attrs.CheckCommand,
"source": source,
"state": levels[state],
"source": url.Hostname(),
"server": url.Hostname(),
"scheme": url.Scheme,
"port": url.Port(),
}
Expand Down Expand Up @@ -136,7 +144,15 @@ func (i *Icinga2) Gather(acc telegraf.Accumulator) error {
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)
if err != nil {
Expand Down
7 changes: 5 additions & 2 deletions plugins/inputs/icinga2/icinga2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func TestGatherServicesStatus(t *testing.T) {
"attrs": {
"check_command": "check-bgp-juniper-netconf",
"display_name": "eq-par.dc2.fr",
"host_name": "someserverfqdn.net",
"name": "ef017af8-c684-4f3f-bb20-0dfe9fcd3dbe",
"state": 0
},
Expand Down Expand Up @@ -46,7 +47,8 @@ func TestGatherServicesStatus(t *testing.T) {
"display_name": "eq-par.dc2.fr",
"check_command": "check-bgp-juniper-netconf",
"state": "ok",
"source": "localhost",
"source": "someserverfqdn.net",
"server": "localhost",
"port": "5665",
"scheme": "https",
},
Expand Down Expand Up @@ -100,7 +102,8 @@ func TestGatherHostsStatus(t *testing.T) {
"display_name": "apache",
"check_command": "ping",
"state": "critical",
"source": "localhost",
"source": "webserver",
"server": "localhost",
"port": "5665",
"scheme": "https",
},
Expand Down

0 comments on commit c798410

Please sign in to comment.