Skip to content

Commit

Permalink
icinga2, add host groups to services
Browse files Browse the repository at this point in the history
  • Loading branch information
BuJo committed Dec 18, 2023
1 parent 42c6b35 commit 4581e94
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions pkg/connectors/icinga2/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,20 @@ func (c *Connector) Collect(ctx context.Context) ([]connectors.Alert, error) {
continue
}

var hostgroups []string
if host, ok := hosts[service.HostName]; ok {
hostgroups = host.Host.Groups
}

sec, dec := math.Modf(service.LastStateChange)
alert := connectors.Alert{
Labels: map[string]string{
"Hostname": service.HostName,
"Zone": service.Zone,
"Source": c.config.URL,
"groups": strings.Join(service.Groups, ","),
"Type": "Service",
"Hostname": service.HostName,
"Zone": service.Zone,
"Source": c.config.URL,
"groups": strings.Join(service.Groups, ","),
"hostgroups": strings.Join(hostgroups, ","),
"Type": "Service",
},
Start: time.Unix(int64(sec), int64(dec*(1e9))),
State: connectors.State(service.State),
Expand All @@ -111,6 +117,7 @@ func (c *Connector) Collect(ctx context.Context) ([]connectors.Alert, error) {
html.HTML("<a href=\"" + c.config.DashboardURL + "/dashboard#!/monitoring/host/show?host=" + service.HostName + "&service=" + service.Name + "\" target=\"_blank\" alt=\"Home\">🏠</a>"),
},
}

alert.Silence = c.createSilencer(alert)
alerts = append(alerts, alert)
}
Expand Down Expand Up @@ -140,7 +147,7 @@ func (c *Connector) collectServices(ctx context.Context) ([]serviceAttrs, error)
return response.Results, nil
}

func (c *Connector) collectHosts(ctx context.Context) ([]HostAttrs, error) {
func (c *Connector) collectHosts(ctx context.Context) (map[string]HostAttrs, error) {
body, err := c.get("/v1/objects/hosts", ctx)
if err != nil {
return nil, err
Expand All @@ -155,7 +162,12 @@ func (c *Connector) collectHosts(ctx context.Context) ([]HostAttrs, error) {
return nil, err
}

return response.Results, nil
var results map[string]HostAttrs
for _, host := range response.Results {
results[host.Host.DisplayName] = host
}

return results, nil
}

func (c *Connector) get(endpoint string, ctx context.Context) (io.ReadCloser, error) {
Expand Down

0 comments on commit 4581e94

Please sign in to comment.