fleet/server/webhooks/host_status.go
Martin Angers 4f4185372d
Add support for context in datastore/mysql layer (#1962)
This is just to pass down the context to the datastore layer, it doesn't
use it just yet - this will be in a follow-up PR.
2021-09-14 08:11:07 -04: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{}{
"message": 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
}