Example #1
0
func TestRun(t *T) {
	ctx := context.Context{
		Name: "foo",
	}
	code := `return ctx.Name == "foo"`

	ret, ok := RunInline(ctx, code)
	assert.True(t, ok)
	assert.Equal(t, true, ret)

	ctx.Name = "bar"
	ret, ok = RunInline(ctx, code)
	assert.True(t, ok)
	assert.Equal(t, false, ret)

	f, err := ioutil.TempFile("", "")
	require.Nil(t, err)
	filename := f.Name()
	defer os.Remove(filename)
	_, err = io.WriteString(f, code)
	require.Nil(t, err)
	f.Close()

	ctx = context.Context{
		Name: "foo",
	}
	ret, ok = RunFile(ctx, filename)
	assert.True(t, ok)
	assert.Equal(t, true, ret)

	ctx.Name = "bar"
	ret, ok = RunFile(ctx, filename)
	assert.True(t, ok)
	assert.Equal(t, false, ret)
}
Example #2
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
		}
	}
}