|
| 1 | +# esalert-plus |
| 2 | + |
| 3 | +Package esalert-plus forked from [Akagi201/esalert](https://github.com/Akagi201/esalert), a simple framework for real-time alerts on data in Elasticsearch. esalter-plus append more features like Elasticsearch X-Watcher and support for Elasticsearch 7.6.2+。 |
| 4 | + |
| 5 | +## Runtime config |
| 6 | +* Esalert's runtime configs. |
| 7 | +* Configs can be passed from command-line, environment or config file. |
| 8 | + |
| 9 | +### Config file |
| 10 | +``` |
| 11 | +alerts = "configs/esalert.d" |
| 12 | +es-addr = "10.41.41.145:9200, 10.41.41.145:9201, 10.41.41.145:9202" |
| 13 | +es-user = "elastic" |
| 14 | +es-pass = "123456" |
| 15 | +lua-init = "" |
| 16 | +lua-vms = "" |
| 17 | +log-level = "debug" |
| 18 | +log-dir = "logs" |
| 19 | +dingding-webhook = "" |
| 20 | +slack-webhook = "" |
| 21 | +force-run = "" |
| 22 | +``` |
| 23 | + |
| 24 | +### Config file Description |
| 25 | + |
| 26 | +#### alerts |
| 27 | + |
| 28 | +A yaml file, or directory with yaml files, containing alert definitions。 |
| 29 | + |
| 30 | +#### es-addr |
| 31 | + |
| 32 | +Address to find an elasticsearch instance on. support for multi discover server.default to `127.0.0.1:9200` |
| 33 | + |
| 34 | +#### es-user |
| 35 | + |
| 36 | +Username for the elasticsearch. default to `elastic` |
| 37 | + |
| 38 | +#### es-pass |
| 39 | + |
| 40 | +Password for the elasticsearch. default to `changeme` |
| 41 | + |
| 42 | +#### lua-init |
| 43 | + |
| 44 | +If set the given lua script file will be executed at the initialization of every lua vm |
| 45 | + |
| 46 | +#### lua-vms |
| 47 | + |
| 48 | +How many lua vms should be used. Each vm is completely independent of the other, and requests are executed on whatever vm is available at that moment. Allows lua scripts to not all be blocked on the same os thread. default to `1` |
| 49 | + |
| 50 | +#### log-level |
| 51 | + |
| 52 | +Adjust the log level. Valid options are: error, warn, info, debug. default to `info` |
| 53 | + |
| 54 | + |
| 55 | +#### log-dir |
| 56 | + |
| 57 | +log dir, default to `stdout`. |
| 58 | + |
| 59 | +#### slack-webhook |
| 60 | + |
| 61 | +Slack webhook url, required if using any Slack actions |
| 62 | + |
| 63 | +#### dingding-webhook |
| 64 | + |
| 65 | +dingding webhook url, required if using any Dingding action |
| 66 | + |
| 67 | + |
| 68 | +## Alert config |
| 69 | +* Alert configs contain all the data processing which should be performed. |
| 70 | +* Esalert runs with one or more alerts defined in its configuration, each one operating independant of the others. |
| 71 | +* Alert configs can be in one file or a directory of files. |
| 72 | +* Alert configs use yaml format. Each file contains an array of alerts. |
| 73 | + |
| 74 | +### Alert rule file(s) |
| 75 | + |
| 76 | +``` |
| 77 | +# esalert.yml |
| 78 | +- name: alert_foo |
| 79 | + # other alert parameters |
| 80 | +
|
| 81 | +- name: alert_bar |
| 82 | + # other alert parameters |
| 83 | +``` |
| 84 | + |
| 85 | +OR |
| 86 | + |
| 87 | +``` |
| 88 | +# esalert.d/foo.yml |
| 89 | +- name: alert_foo |
| 90 | + # other alert parameters |
| 91 | +
|
| 92 | +- name: alert_foo2 |
| 93 | + # other alert parameters |
| 94 | +``` |
| 95 | + |
| 96 | +and |
| 97 | + |
| 98 | +``` |
| 99 | +# esalert.d/bar.yml |
| 100 | +- name: alert_bar |
| 101 | + # other alert parameters |
| 102 | +
|
| 103 | +- name: alert_bar2 |
| 104 | + # other alert parameters |
| 105 | +``` |
| 106 | + |
| 107 | +### Alert document |
| 108 | + |
| 109 | +A single alert has the following fields in its document (all are required): |
| 110 | + |
| 111 | +``` |
| 112 | +- name: something_unique |
| 113 | + interval: "*/5 * * * * *" |
| 114 | + search_index: # see the search subsection |
| 115 | + search_type: # see the search subsection |
| 116 | + search: # see the search subsection |
| 117 | + metadata: # see the metadata subsection |
| 118 | + throttle_period: # see the throttle_period |
| 119 | + process: # see the process subsection |
| 120 | +``` |
| 121 | + |
| 122 | +#### name |
| 123 | + |
| 124 | +This is an arbitrary string to identify the alert. It must be unique amongst all of the defined alerts. |
| 125 | + |
| 126 | +#### interval |
| 127 | + |
| 128 | +A [jobber-style](https://github.com/Akagi201/utilgo/tree/master/jobber) interval string describing when the search should be run and have the process run on the results. |
| 129 | + |
| 130 | +#### search |
| 131 | + |
| 132 | +The search which should be performed against elasticsearch. The results are simply held onto for the process step, nothing else is done with them at this point. |
| 133 | + |
| 134 | +``` |
| 135 | +search_index: filebeat-{{.Format "2006.01.02"}} |
| 136 | +search_type: logs |
| 137 | +# conveniently, json is valid yaml |
| 138 | +search: { |
| 139 | + "query": { |
| 140 | + "query_string": { |
| 141 | + "query":"severity:fatal" |
| 142 | + } |
| 143 | + } |
| 144 | +} |
| 145 | +``` |
| 146 | + |
| 147 | +* See [query dsl](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl.html) docs for more on how to formulate query objects. |
| 148 | +* See [query string](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html#query-string-syntax) docs for more on how to formulate query strings. |
| 149 | +* All three fields(`search_index`, `search_type` and `search`) can have go templating applied. |
| 150 | +* See the alert context subsection for more information on what fields/methods are available to use. |
| 151 | + |
| 152 | + |
| 153 | +#### metadata |
| 154 | + |
| 155 | +The metadata that can share between `search` and `process` phase. |
| 156 | + |
| 157 | +#### throttle_period |
| 158 | + |
| 159 | +A threshold alert periodically checks when your data is above, below, equals, or is in between a certain threshold within a given time interval. |
| 160 | + |
| 161 | +#### process |
| 162 | + |
| 163 | +Once the search is performed the results are kept in the context, which is then passed into this step. The process lua script then checks these results against whatever conditions are desired, and may optionally return a list of actions to take. See the alert context section for all available fields in ctx. |
| 164 | + |
| 165 | +``` |
| 166 | +process: |
| 167 | + lua_file: ./foo-process.yml |
| 168 | +``` |
| 169 | + |
| 170 | +OR |
| 171 | + |
| 172 | +``` |
| 173 | +process: |
| 174 | + lua_inline: | |
| 175 | + if ctx.HitCount > 10 then |
| 176 | + return { |
| 177 | + { |
| 178 | + type = "log", |
| 179 | + message = "got " .. ctx.HitCount .. " hits", |
| 180 | + } |
| 181 | + } |
| 182 | + end |
| 183 | + -- To indicate no actions, you can return an empty table, nil, or simply |
| 184 | + -- don't return at all |
| 185 | + return {} |
| 186 | +``` |
| 187 | + |
| 188 | + |
| 189 | +##### actions |
| 190 | + |
| 191 | +The table returned by process is a list of actions which should be taken. Each action has a type and subsequent fields based on that type. |
| 192 | + |
| 193 | +##### log |
| 194 | + |
| 195 | +Simply logs an INFO message to the console or the `log-dir` you have configured. Useful if you're testing an alert and don't want to set up any real actions yet or write the monitor info in the disk for the next step. |
| 196 | + |
| 197 | +``` |
| 198 | +{ |
| 199 | + type = "log", |
| 200 | + message = "Performing action for alert " .. ctx.Name, |
| 201 | +} |
| 202 | +``` |
| 203 | + |
| 204 | +##### http |
| 205 | + |
| 206 | +Create and execute an http command. A warning is logged if anything except a 2xx response code is returned. |
| 207 | + |
| 208 | +``` |
| 209 | +{ |
| 210 | + type = "http", |
| 211 | + method = "POST", -- optional, defaults to GET |
| 212 | + url = "http://example.com/some/endpoint?ARG1=foo", |
| 213 | + headers = { -- optional |
| 214 | + "X-FOO" = "something", |
| 215 | + }, |
| 216 | + body = "some body for " .. ctx.Name, -- optional |
| 217 | +} |
| 218 | +``` |
| 219 | + |
| 220 | +##### slack |
| 221 | + |
| 222 | +Triggers an event in slack. The --slack-key param must be set in the runtime configuration in order to use this action type. |
| 223 | + |
| 224 | +``` |
| 225 | +{ |
| 226 | + type = "slack", |
| 227 | + text = "some text" |
| 228 | +} |
| 229 | +``` |
| 230 | + |
| 231 | +##### dingding |
| 232 | + |
| 233 | +Triggers an event in DingDing. You must write the text format by yourself. |
| 234 | + |
| 235 | +``` |
| 236 | +{ |
| 237 | + type="dingding", |
| 238 | + text="{\"msgtype\": \"text\", \"text\": { \"content\": \"" .. msg .. "\"}}" |
| 239 | +} |
| 240 | +``` |
| 241 | + |
| 242 | +## Alert context |
| 243 | + |
| 244 | +Through its lifecycle each alert has a context object attached to it. The results from the search step are included in it, as well as other data. Here is a description of the available data in the context, as well as how to use it. |
| 245 | + |
| 246 | +NOTE THAT THE CONTEXT IS READ-ONLY IN ALL CASES |
| 247 | + |
| 248 | +### Context fields |
| 249 | + |
| 250 | +``` |
| 251 | +{ |
| 252 | + Name string // The alert's name |
| 253 | + StartedTS uint64 // The timestamp the alert started at |
| 254 | +
|
| 255 | + // The following are filled in by the search step |
| 256 | + TookMS uint64 // Time search took to complete, in milliseconds |
| 257 | + HitCount interface{} // The total number of documents matched |
| 258 | + HitMaxScore float64 // The maximum score of all the documents matched |
| 259 | +
|
| 260 | + // Array of actual documents matched. Keep in mind that unless you manually |
| 261 | + // define a limit in your search query this will be capped at 10 by |
| 262 | + // elasticsearch. Usually HitCount is the important data point anyway |
| 263 | + Hits []{ |
| 264 | + Index string // The index the hit came from |
| 265 | + Type string // The type the document is |
| 266 | + ID string // The unique id of the document |
| 267 | + Score float64 // The document's score relative to the query |
| 268 | + Source object // The actual document |
| 269 | + } |
| 270 | +
|
| 271 | + // If an aggregation was defined in the search query, the results will be |
| 272 | + // set here |
| 273 | + Aggregations object |
| 274 | +} |
| 275 | +``` |
| 276 | + |
| 277 | +### In lua |
| 278 | + |
| 279 | +Within lua scripts the context is made available as a global variable called `ctx`. Fields on it are directly addressable using the above names, for example `ctx.HitCount` and `ctx.Hits[1].ID`. |
| 280 | + |
| 281 | +### In go template |
| 282 | + |
| 283 | +In some areas go templates, provided by the template/text package, are used to add some dynamic capabilities to otherwise static configuration fields. In these places the context is made available as the root object. For example, {{.HitCount}}. |
| 284 | + |
| 285 | +In addition to the fields defined above, the root template object also has some methods on it which may be helpful for working with dates. All methods defined on go's time.Time object are available. For example, to format a string into the filebeat index for the current day: |
| 286 | + |
| 287 | +``` |
| 288 | +filebeat-{{.Format "2006.01.02"}} |
| 289 | +``` |
| 290 | + |
| 291 | +And to do the same, but for yesterday: |
| 292 | + |
| 293 | +``` |
| 294 | +filebeat-{{(.AddDate 0 0 -1).Format "2006.01.02"}} |
| 295 | +``` |
0 commit comments