|
| 1 | +// Package alert alert rules and templatize. |
| 2 | +package alert |
| 3 | + |
| 4 | +import ( |
| 5 | + "bytes" |
| 6 | + "fmt" |
| 7 | + "text/template" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/Akagi201/utilgo/jobber" |
| 11 | + "esalert/action" |
| 12 | + "esalert/context" |
| 13 | + "esalert/luautil" |
| 14 | + "esalert/search" |
| 15 | + log "github.com/sirupsen/logrus" |
| 16 | + yaml "gopkg.in/yaml.v2" |
| 17 | +) |
| 18 | + |
| 19 | +// Alert encompasses a search query which will be run periodically, the results |
| 20 | +// of which will be checked against a condition. If the condition returns true a |
| 21 | +// set of actions will be performed |
| 22 | +type Alert struct { |
| 23 | + Name string `yaml:"name"` |
| 24 | + Interval string `yaml:"interval"` |
| 25 | + SearchIndex string `yaml:"search_index"` |
| 26 | + SearchType string `yaml:"search_type"` |
| 27 | + Search search.Dict `yaml:"search"` |
| 28 | + SearchQuery string `yaml:"search_query"` |
| 29 | + Process luautil.LuaRunner `yaml:"process"` |
| 30 | + |
| 31 | + Jobber *jobber.FullTimeSpec |
| 32 | + SearchIndexTPL *template.Template |
| 33 | + SearchTypeTPL *template.Template |
| 34 | + SearchTPL *template.Template |
| 35 | +} |
| 36 | + |
| 37 | +func templatizeHelper(i interface{}, lastErr error) (*template.Template, error) { |
| 38 | + if lastErr != nil { |
| 39 | + return nil, lastErr |
| 40 | + } |
| 41 | + var str string |
| 42 | + if s, ok := i.(string); ok { |
| 43 | + str = s |
| 44 | + } else { |
| 45 | + b, err := yaml.Marshal(i) |
| 46 | + if err != nil { |
| 47 | + return nil, err |
| 48 | + } |
| 49 | + str = string(b) |
| 50 | + } |
| 51 | + |
| 52 | + return template.New("").Parse(str) |
| 53 | +} |
| 54 | + |
| 55 | +// Init initializes some internal data inside the Alert, and must be called |
| 56 | +// after the Alert is unmarshaled from yaml (or otherwise created) |
| 57 | +func (a *Alert) Init() error { |
| 58 | + var err error |
| 59 | + a.SearchIndexTPL, err = templatizeHelper(a.SearchIndex, err) |
| 60 | + a.SearchTypeTPL, err = templatizeHelper(a.SearchType, err) |
| 61 | + if a.Search == nil { |
| 62 | + a.SearchTPL, err = templatizeHelper(a.SearchQuery, err) |
| 63 | + } else { |
| 64 | + a.SearchTPL, err = templatizeHelper(&a.Search, err) |
| 65 | + } |
| 66 | + if err != nil { |
| 67 | + return err |
| 68 | + } |
| 69 | + |
| 70 | + jb, err := jobber.ParseFullTimeSpec(a.Interval) |
| 71 | + if err != nil { |
| 72 | + return fmt.Errorf("parsing interval: %s", err) |
| 73 | + } |
| 74 | + a.Jobber = jb |
| 75 | + |
| 76 | + return nil |
| 77 | +} |
| 78 | + |
| 79 | +func (a Alert) Run() { |
| 80 | + kv := log.Fields{ |
| 81 | + "name": a.Name, |
| 82 | + } |
| 83 | + log.WithFields(kv).Infoln("running alert") |
| 84 | + |
| 85 | + now := time.Now() |
| 86 | + c := context.Context{ |
| 87 | + Name: a.Name, |
| 88 | + StartedTS: uint64(now.Unix()), |
| 89 | + Time: now, |
| 90 | + } |
| 91 | + |
| 92 | + searchIndex, searchType, searchQuery, err := a.CreateSearch(c) |
| 93 | + if err != nil { |
| 94 | + kv["err"] = err |
| 95 | + log.WithFields(kv).Errorln("failed to create search data") |
| 96 | + return |
| 97 | + } |
| 98 | + |
| 99 | + log.WithFields(kv).Debugln("running search step") |
| 100 | + res, err := search.Search(searchIndex, searchType, searchQuery) |
| 101 | + if err != nil { |
| 102 | + kv["err"] = err |
| 103 | + log.WithFields(kv).Errorln("failed at search step") |
| 104 | + return |
| 105 | + } |
| 106 | + c.Result = res |
| 107 | + |
| 108 | + log.WithFields(kv).Debugln("running process step") |
| 109 | + processRes, ok := a.Process.Do(c) |
| 110 | + if !ok { |
| 111 | + log.WithFields(kv).Errorln("failed at process step") |
| 112 | + return |
| 113 | + } |
| 114 | + |
| 115 | + // if processRes isn't an []interface{}, actionsRaw will be the nil value of |
| 116 | + // []interface{}, which has a length of 0, so either way this works |
| 117 | + actionsRaw, _ := processRes.([]interface{}) |
| 118 | + if len(actionsRaw) == 0 { |
| 119 | + log.WithFields(kv).Debugln("no actions returned") |
| 120 | + } |
| 121 | + |
| 122 | + actions := make([]action.Action, len(actionsRaw)) |
| 123 | + for i := range actionsRaw { |
| 124 | + a, err := action.ToActioner(actionsRaw[i]) |
| 125 | + if err != nil { |
| 126 | + kv["err"] = err |
| 127 | + log.WithFields(kv).Errorln("error unpacking action") |
| 128 | + return |
| 129 | + } |
| 130 | + actions[i] = a |
| 131 | + } |
| 132 | + |
| 133 | + for i := range actions { |
| 134 | + kv["action"] = actions[i].Type |
| 135 | + log.WithFields(kv).Infoln("performing action") |
| 136 | + if err := actions[i].Do(c); err != nil { |
| 137 | + kv["err"] = err |
| 138 | + log.WithFields(kv).Errorln("failed to complete action") |
| 139 | + return |
| 140 | + } |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +func (a Alert) CreateSearch(c context.Context) (string, string, interface{}, error) { |
| 145 | + buf := bytes.NewBuffer(make([]byte, 0, 1024)) |
| 146 | + if err := a.SearchIndexTPL.Execute(buf, &c); err != nil { |
| 147 | + return "", "", nil, err |
| 148 | + } |
| 149 | + searchIndex := buf.String() |
| 150 | + |
| 151 | + buf.Reset() |
| 152 | + if err := a.SearchTypeTPL.Execute(buf, &c); err != nil { |
| 153 | + return "", "", nil, err |
| 154 | + } |
| 155 | + searchType := buf.String() |
| 156 | + |
| 157 | + buf.Reset() |
| 158 | + if err := a.SearchTPL.Execute(buf, &c); err != nil { |
| 159 | + return "", "", nil, err |
| 160 | + } |
| 161 | + searchRaw := buf.Bytes() |
| 162 | + |
| 163 | + var search search.Dict |
| 164 | + if err := yaml.Unmarshal(searchRaw, &search); err != nil { |
| 165 | + return "", "", nil, err |
| 166 | + } |
| 167 | + |
| 168 | + return searchIndex, searchType, search, nil |
| 169 | +} |
0 commit comments