部署 ElastAlert
yum -y install wget openssl openssl-devel gcc gcc-c++
git clone https://github.com/Yelp/elastalert.git
pip install "setuptools>=11.3"
python setup.py install
./create_index.py --host ${ES_IP} --port ${ES_PORT} --username 'xxx' --password 'xxx' --config ./config.yaml
配置文件说明
rules_folder: myrules
scan_subdirectories:true
run_every:
minutes: 5
buffer_time:
minutes: 30
es_host: localhost
es_port: 9200
es_username: elasticsearch
es_password: changeme
es_conn_timeout: 20
use_ssl: "false"
writeback_index: elastalert_status
./elastalert-test-rule --config ./config.yaml ./example_rules/example.yaml
python elastalert/elastalert.py --config config.yaml \
[ --verbose|--debug ] \
[ --start YYYY-MM-DDTHH:MM:SS ] \
[ --end YYYY-MM-DDTHH:MM:SS ] \
告警规则的类型
frequency:
spike:
flatline:
blacklist/whitelist:
any:
change:
doc = {
"@timestamp": get_now(),
"codec": "nodejs",
"tags": "31",
"level": "high",
"server": "nginx",
"status": "anystatus",
"message": ">>> [ xxx ]: valid id error ."
}
name: any_rule
type: any
index: testalert-*
timeframe:
minutes: 1
filter:
- term:
status: "anystatus"
alert: post
http_post_url: "http://localhost:8088/alertapi"
http_post_static_payload:
rule_name: any_rule
rule_level: medium
name: blacklist_rule
type: blacklist
index: testalert
timeframe:
minutes: 1
compare_key: status
blacklist:
- "hacker"
- "huahua"
- "!file /tmp/blacklist1.txt"
- "!file /tmp/blacklist2.txt"
alert: post
http_post_url: "http://localhost:8088/alertapi"
http_post_static_payload:
rule_name: blacklist_rule
rule_level: medium
name: change_rule
type: change
index: testalert
timeframe:
minutes: 1
compare_key: codec
query_key: server
ignore_null: true
alert: post
http_post_url: "http://localhost:8088/alertapi"
http_post_static_payload:
rule_name: change_rule
rule_level: medium
name: frequency_rule
type: frequency
index: testalert
num_events: 3
timeframe:
minutes: 1
filter:
- term:
status: "frequency"
alert: post
http_post_url: "http://localhost:8088/alertapi"
http_post_static_payload:
rule_name: frequency_rule
rule_level: medium
name: spike_rule
type: spike
index: testalert
timeframe:
minutes: 1
threshold_cur: 3
spike_height: 1
spike_type: "up"
filter:
- term:
status: "spike"
alert: post
http_post_url: "http://localhost:8088/alertapi"
http_post_static_payload:
rule_name: spike_rule
rule_level: medium
name: flatline_rule
type: flatline
index: testalert
timeframe:
minutes: 1
threshold: 3
alert: post
http_post_url: "http://localhost:8088/alertapi"
http_post_static_payload:
rule_name: flatline_rule
rule_level: medium
name: test_rule
index: testalert
type: cardinality
timeframe:
minutes: 1
cardinality_field: level
max_cardinality: 2
alert: post
http_post_url: "http://localhost:8088/api/alert"
http_post_static_payload:
rule_name: test_rule
rule_level: medium
name: percentage_match_rule
type: percentage_match
index: testalert
buffer_time:
minutes: 1
max_percentage: 95
match_bucket_filter:
- term:
level: high
doc_type: mydata
alert: post
http_post_url: "http://localhost:8088/alertapi"
http_post_static_payload:
rule_name: percentage_match_rule
rule_level: medium
new_term
metric_aggregation
告警方式 ( 类型 )
alert:
- command
command: ["/bin/send_alert", "--username", "%(username)s"]
alert: post
http_post_url: "http://example.com/api"
http_post_payload:
ip: clientip
http_post_static_payload:
apikey: abc123
http_post_headers:
authorization: Basic 123dr3234
以 python 模块方式运行 elastalert
https://elastalert.readthedocs.io/en/latest/elastalert.html
pip install elastalert
./elastalert-create-index --host <ES_IP> --port <ES_PORT> --username 账号 --password '密码' --config ./config.yaml
./elastalert-test-rule ./123.yaml --config ./config.yaml
./bin/elastalert --config ./config.yaml --verbose
python -m elastalert.elastalert --config ./config.yaml --verbose [ --rule example_frequency.yaml ]
补充
filter:
- query:
query_string:
query: "foo: bar AND baz: abc*"
summary_table_fields:
- my_data.username
- my_data.event_type
smtp_host: smtp.qq.com
smtp_port: 25
smtp_auth_file: /root/elastalert/rule_templates/smtp_auth_file.yaml
from_addr: "xxxx@qq.com"
alert:
- "email"
email:
- "xxxx@qq.com"
alert_subject: "Error {} @{}"
alert_subject_args:
- name
- "@timestamp"
alert_text_type: alert_text_only
alert_text: |
### Error frequency exceeds
> Name: {}
> Message: {}
> Host: {} ({})
alert_text_args:
- name
- message
- hostname
- host
alert_text: "
域 名: {}\n
调用方式: {}\n
请求链接: {}\n
状 态 码: {}\n
后端服务器: {}\n
数 量: {}
"
alert_text_type: alert_text_only
alert_text_args:
- host
- method
- request
- status
- upstream
- num_hits
alert:
- "elastalert_modules.dingtalk_alert.DingTalkAlerter"
dingtalk_webhook: "XXXXXX"
dingtalk_msgtype: "text"
|