fleet/server/webhooks/host_status.go
Tomas Touceda d0777ccfd3
Change message for text so webhook works on slack (#2838)
* Change message for text so webhook works on slack

* Update preview text for the webhook in the FE
2021-11-08 15:13:02 -03:00

56 lines
1.5 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package webhooks
import (
"context"
"fmt"
"github.com/fleetdm/fleet/v4/server"
"github.com/fleetdm/fleet/v4/server/fleet"
kitlog "github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/pkg/errors"
)
func TriggerHostStatusWebhook(
ctx context.Context,
ds fleet.Datastore,
logger kitlog.Logger,
appConfig *fleet.AppConfig,
) error {
if !appConfig.WebhookSettings.HostStatusWebhook.Enable {
return nil
}
level.Debug(logger).Log("enabled", "true")
total, unseen, err := ds.TotalAndUnseenHostsSince(ctx, appConfig.WebhookSettings.HostStatusWebhook.DaysCount)
if err != nil {
return errors.Wrap(err, "getting total and unseen hosts")
}
percentUnseen := float64(unseen) * 100.0 / float64(total)
if percentUnseen >= appConfig.WebhookSettings.HostStatusWebhook.HostPercentage {
url := appConfig.WebhookSettings.HostStatusWebhook.DestinationURL
message := fmt.Sprintf(
"More than %.2f%% of your hosts have not checked into Fleet for more than %d days. "+
"Youve been sent this message because the Host status webhook is enabled in your Fleet instance.",
percentUnseen, appConfig.WebhookSettings.HostStatusWebhook.DaysCount,
)
payload := map[string]interface{}{
"text": message,
"data": map[string]interface{}{
"unseen_hosts": unseen,
"total_hosts": total,
"days_unseen": appConfig.WebhookSettings.HostStatusWebhook.DaysCount,
},
}
err = server.PostJSONWithTimeout(ctx, url, &payload)
if err != nil {
return errors.Wrapf(err, "posting to %s", url)
}
}
return nil
}