接第一篇,之前已经创建好规则了,这篇主要讲如何把安装的elastalert用起来。
http://kexiaoke.blog.51cto.com/5530023/1977481
创建规则
每个规则定义要执行的查询,触发匹配的参数,以及每个匹配的触发警报列表。 我们将使用example_rules / example_frequency.yaml作为模板:
# (Required) # Rule name, must be unique name: Example frequency rule # (Required) # Type of alert. # the frequency rule type alerts when num_events events occur with timeframe time type: frequency # (Required) # Index to search, wildcard supported index: logstash-* # (Required, frequency specific) # Alert when this many documents matching the query occur within a timeframe num_events: 50 # (Required, frequency specific) # num_events must occur within this amount of time to trigger an alert timeframe: hours: 4 # (Required) # A list of Elasticsearch filters used for find events # These filters are joined with AND and nested in a filtered query # For more info: http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl.html filter: - term: some_field: "some_value" # (Required) # The alert is use when a match is found alert: - "email" # (required, email specific) # a list of email addresses to send alerts to email: - "405942850@qq.com"
es_host和es_port应该指向我们要查询的Elasticsearch集群。
name是此规则的唯一名称。如果两个规则共享相同的名称,ElastAlert将不会启动。
类型:每个规则具有不同的类型,可能会采用不同的参数。频率类型表示“在时间范围内超过num_event发生时的警报”。有关其他类型的信息,请参阅规则类型。
index:要查询的索引的名称。如果您使用Logstash,默认情况下索引将匹配“logstash- *”。
num_events:此参数特定于频率类型,是触发警报时的阈值。
时间段是必须发生num_event的时间段。
过滤器是用于过滤结果的Elasticsearch过滤器列表。这里我们有一个单字段过滤器用于some_field匹配some_value的文档。有关详细信息,请参阅编写过滤规则。如果不需要过滤器,则应将其指定为空列表:filter:[]
警报是每个匹配上运行的警报的列表。有关警报类型的更多信息,请参阅警报。电子邮件警报需要SMTP服务器才能发送邮件。默认情况下,它将尝试使用localhost。这可以通过smtp_host选项更改。
电子邮件是要发送警报的地址列表。
还有许多其他可选配置选项,请参阅常见配置选项。
所有文档必须具有时间戳字段。 ElastAlert将默认使用@timestamp,但可以使用timestamp_field选项更改。默认情况下,ElastAlert使用ISO8601时间戳,但是通过设置timestamp_type支持unix times-tamps。
就这样,这个规则是指“在4小时内有50个以上的some_field == some_value的文件时,发送电子邮件至405942850@qq.com。
测试规则
elastalert-test-rule --alert example_rules/example_frequency.yaml 官方的例子是没有加--alert的,如果不加就真的是只显示结果,不会发出邮件,如果想看结果邮件 ,需要加上--alert参数!!!
运行ElastAlert
python -m elastalert.elastalert --verbose --rule example_frequency.yaml
以上命令代表启动后每隔1分钟(这个值是在config.yaml的
run_every:
minutes: 1
指定的)查询一下最近4个小时内有没有50个以上的some_field == some_value的状态,如果有,则发出报警。千万注意是自启动以来!!!
转载于:https://blog.51cto.com/kexiaoke/1977815