Example #1
0
func (a Alert) Run() {
	kv := llog.KV{
		"name": a.Name,
	}
	llog.Info("running alert", kv)

	now := time.Now()
	c := context.Context{
		Name:      a.Name,
		StartedTS: uint64(now.Unix()),
		Time:      now,
	}

	searchIndex, searchType, searchQuery, err := a.createSearch(c)
	if err != nil {
		kv["err"] = err
		llog.Error("failed to create search data", kv)
		return
	}

	llog.Debug("running search step", kv)
	res, err := search.Search(searchIndex, searchType, searchQuery)
	if err != nil {
		kv["err"] = err
		llog.Error("failed at search step", kv)
		return
	}
	c.Result = res

	llog.Debug("running process step", kv)
	processRes, ok := a.Process.Do(c)
	if !ok {
		llog.Error("failed at process step", kv)
		return
	}

	// if processRes isn't an []interface{}, actionsRaw will be the nil value of
	// []interface{}, which has a length of 0, so either way this works
	actionsRaw, _ := processRes.([]interface{})
	if len(actionsRaw) == 0 {
		llog.Debug("no actions returned", kv)
	}

	actions := make([]action.Action, len(actionsRaw))
	for i := range actionsRaw {
		a, err := action.ToActioner(actionsRaw[i])
		if err != nil {
			kv["err"] = err
			llog.Error("error unpacking action", kv)
			return
		}
		actions[i] = a
	}

	for i := range actions {
		kv["action"] = actions[i].Type
		llog.Info("performing action", kv)
		if err := actions[i].Do(c); err != nil {
			kv["err"] = err
			llog.Error("failed to complete action", kv)
			return
		}
	}
}