一、Elastalert简介

Elastalert是Yelp公司基于python开发的ELK日志告警插件,Elastalert通过查询Elasticsearch中的记录与定于的告警规则进行对比,判断是否满足告警条件。发生匹配时,将为该告警触发一个或多个告警动作。告警规则由Elastalert的rules定义,每个规则定义一个查询。

 

二、部署elastalert(10.8.240.74)

系统:centos7.5

elastalert:0.2.4

python:3.6.8


1 安装Python3和其他所需软件

yum install -y wget gcc openssl-devel epel-release git python3 python3-devel unzip telnet lrzsz

 

2 下载elastalert

1. 服务器下载
git clone https://github.com/Yelp/elastalert.git

会得到一个elastalert-master的文件夹。

 

 

 

如果服务器是内网,没有外网权限,可用有外网的服务器将elastalert 用git clone下载,然后传到内网服务器,然后:

--外网服务器下载

pip3 download -r requirements.txt -d /root/offlinePackages/ --trusted-host mirrors.cloud.aliyuncs.com


--将/root/offlinePackages/ 传到内网服务器,执行

cd  elastalert-master 
pip3 install --no-index --find-links=/root/2022-10-25-16-22/offlinePackages/ -r requirements.txt

 

3 安装依赖组件:

1. 进入elastalert-master

cd elastalert-master
pip3 install -r requirements.txt -i http://172.16.143.83:8081/repository/pipy-public/simple --trusted-host 172.16.143.83

 

这里有个需要注意的是:因为我下载的时候走代理,访问的是官网,而服务器用的是python3.6,所以下载的版本可能会超过python3.6的限制,所以如果发现类似如下报错:

zipp requires Python '>=3.7' but the running Python is 3.6.8

说明版本太新,需要回到requirements.txt文件中,将zipp的版本加上限制,不要让它下载太新的版本。

cat <<EOF>> requirements.txt

zipp==3.6.0

EOF

 

具体版本可以参考:
Successfully installed

PyJWT-2.4.0

PyStaticConfiguration-0.11.1

PyYAML-6.0

apscheduler-3.9.1

attrs-22.1.0

aws-requests-auth-0.4.3

backports.zoneinfo-0.2.1

blist-1.3.6

boto3-1.23.10

botocore-1.26.10

certifi-2022.9.24

cffi-1.15.1

charset-normalizer-2.0.12

configparser-5.2.0

croniter-1.3.7

dataclasses-0.8

defusedxml-0.7.1

docopt-0.6.2

elastic-transport-8.4.0

elasticsearch-8.4.3

envparse-0.2.0

exotel-0.1.5

idna-3.4

importlib-metadata-4.8.3

importlib-resources-5.4.0

jira-1.0.14

jmespath-0.10.0

jsonschema-4.0.0

mock-4.0.3

oauthlib-3.2.2

pbr-5.11.0

prison-0.2.1

py-zabbix-1.1.3

pycparser-2.21

pyrsistent-0.18.0

pysocks-1.7.1

python-dateutil-2.6.1

pytz-2022.5

pytz-deprecation-shim-0.1.0.post0

requests-2.27.1

requests-oauthlib-1.3.1

requests-toolbelt-0.10.1

s3transfer-0.5.2

six-1.16.0

stomp.py-8.0.1

texttable-1.6.4

twilio-6.0.0

typing-extensions-4.1.1

tzdata-2022.5

tzlocal-4.2

urllib3-1.26.12

zipp-3.6.0


最后安装依赖成功

 

 

 


2. 检查有没有setuptools和elasticsearch
pip3 list |grep setuptools
pip3 list |grep elasticsearch

3. 更换elasticsearch版本(可选)
pip3 list --format=columns |grep elasticsearch
pip3 uninstall elasticsearch
pip3 install "elasticsearch==7.0.0" -i http://172.16.143.83:8081/repository/pipy-public/simple --trusted-host 172.16.143.83

 

4 安装elastalert

1. 配置代理,不然请求不了需要安装的地址
[root@soFlu-laijx-test2 elastalert]# cat <<EOF >> setup.cfg
[easy_install]
index_url = http://172.16.143.83:8081/repository/pipy-public/simple
EOF

2. 安装
python3 setup.py install

 

3. 安装成功

 

安装成功后会有这个lib文件夹

 

 

 

 


5 修改配置

[root@iZzj5l9imjpzb7Z ~]# egrep -v '#|^$' config.yaml
rules_folder: rules
run_every:
minutes: 1
buffer_time:
minutes: 15
es_host: 10.8.240.73
es_port: 9200
writeback_index: elastalert_status
writeback_alias: elastalert_alerts
alert_time_limit:
days: 2

 

6 测试创建index,验证elastalert安装是否正常

1. elastalert-create-index --config config.yaml

2. 返回如下说明安装正常

 

 

 

3. 查看elasticsearch的index,能看到刚刚新建的:
curl -XGET http:// 10.8.240.74:9200/_cat/indices |grep elastalert |sort -n

 

 

 

 

 

 

三、部署elasticsearch(具体过程略)(10.8.240.73)

 

修改系统文件:

vim /etc/sysctl.conf
fs.file-max=655360
vm.max_map_count = 655360
sysctl -p

 

下载
Useradd es
Su - es
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.8.2.tar.gz
tar -zxvf elasticsearch-6.8.2.tar.gz

 

修改配置
[es@iZzj5l9imjpzb7Z ~]$ cat elasticsearch-6.8.2/config/elasticsearch.yml |egrep -v '#|^$'
cluster.name: my-application
node.name: node-1
path.data: /home/es/elasticsearch-6.8.2/data
path.logs: /home/es/elasticsearch-6.8.2/logs
network.host: 0.0.0.0
http.port: 9200

 

启动
./elasticsearch-6.8.2/bin/elasticsearch –d


四、部署kibana(具体过程略)(10.8.240.73)

 

启动:
/root/kibana-6.8.23-linux-x86_64/bin/kibana >> /root/kibana-6.8.23-linux-x86_64/kibana.log &

 


五、部署nginx(具体过程略)(10.8.240.73)

systemctl start nginx

 

 


六、部署logstash(具体过程略)(10.8.240.73)

下载,解压安装包

修改配置文件

启动
/usr/local/logstash-6.8.23/bin/logstash -f /usr/local/logstash-6.8.23/config/mynginx.conf >> /usr/local/logstash-6.8.23/logs/mynginx.log 2>&1 &

 

 


七、配置告警

前提条件:

Logstash能够采集到nginx日志,并传输到elasticsearch。
通过kibana管理—索引模式—创建索引后,在Discover中检索验证日志是否在elasticsearch。

1. 配置rules/system.yaml
(这里的rules,必须和config.yaml文件中rules_folder: rules字段对应)
mkdir rules/
cat rules/system.yaml (配置一分钟内有10次检索到包含ERROR的内容则告警)

# ---------------- es配置 ----------------------
es_host: 10.8.240.73
es_port: 9200 # es的端口
#es_username: elastic # es的用户名
#es_password: 111qqq # es的密码

# ----------------

# --------------- 报警基础信息配置 ------------------
name: Nginx_404 # 报警邮件的标题
type: frequency # 类型:频率
index: mynginx*,10.8.240.73-mynginx* # 监控的索引,多个使用逗号隔开
num_events: 10 # 时间内触发的次数
timeframe:
minutes: 1 # 时间,和上边的参数关联,1分钟内有1次会报警

# ---------------------


# ---------------- 报警过滤条件 -------------------
# For more info: http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl.html
filter:
- query:
query_string:
query: "message: *404*"
alert_text: "nginx日志异常测试" # 会在报警内容中显示
smtp_host: EXCHANGE.*****.com # smtp的地址
smtp_port: 25 # 端口
smtp_auth_file: /root/elastalert-master/smtp_auth_file.yaml # 用户密码的文件
email_reply_to: 111@*****.com # 发送邮件的邮箱
from_addr: 111@*****.com
alert:
- "email" # 报警类型
email: # 收件人地址
- "laijx@*****.com"

# -------------- 重复告警 ---------------
realert:
minutes: 1 # 1分钟内相同告警不会重复发送

#exponential_realert:
# minutes: 10 #指数型增长最大值


# 指数级扩大 realert 时间,中间如果有报警,
# 则按照5->10->20->40->60不断增大报警时间直到最大时间,
# 如果之后报警减少,则会慢慢恢复原始realert时间
# --------------------------------------------


# ------------ 聚合相同告警 ------------------
#aggregation_key: name
#聚合报警的内容,只展示 name 与 message
#summary_table_fields:
# - name
# - _index
# - message
# --------------------------

# ------------------------告警内容格式化
# 注意这些格式化的信息,都是从es中拿过来的


alert_subject: "Error {0}"
alert_subject_args:
- name # 该name 为本代码上面定义的name
- host
alert_text_type: alert_text_only
alert_text: |
Nginx 404 一分钟超过10次!

> datetime: {0}
> Host: {1}
> index: {3}

> Message: {2}
> log_file: {4}
> num_hits: {5}
> num_matches: {6}

alert_text_args:
- "@timestamp"
- host
- message
- _index
- path
- num_hits
- num_matches

# alert_text: "system日志异常测试" # 会在报警内容中显示,非格式化内容
# ----------------------
View Code

 

 

2. 配置smtp_auth_file.yaml邮件密码文件

cat smtp_auth_file.yaml

user: "111@*****.com"
password: '*****.'


3. 启动
Cat <<EOF>> /etc/hosts
172.16.102.90 EXCHANGE.*****.com
EOF

> nohup.out && nohup python3 -m elastalert.elastalert --verbose --config config.yaml &


4. 观察日志
tail nohup.out -f

 

 

 

5. 触发告警
手动请求http://10.8.240.73/ERROR.htm,模拟ERROR。


 

 

 

 

 

 

 


6. 观察邮件

 

 


 

7. 修改规则自动加载
修改一些告警阈值会自动重载:
num_events: 2 # 时间内触发的次数
timeframe:
minutes: 1 # 时间,和上边的参数关联,1分钟内有1次会报警

 

 

 


但是如果修改了告警规则的name,需要重启,不然会报错:

 

 先到这吧,后续应该还要再熟悉更多规则的配置和告警。

 

 参考链接:

https://github.com/Yelp/elastalert

https://www.jianshu.com/p/e6281cf72e95/

https://www.cnblogs.com/gered/p/15400623.html

 posted on 2022-10-31 10:17  Laijx  阅读(840)  评论(0编辑  收藏  举报