ELK+elastalert 相关
一、安装elastalert
1、环境
- CentOS:7.6
- Python:3.6.9
- pip:19.3
- elastalert:0.2.1
- elk:7.7
2、配置Python3.6.9环境
安装依赖包
# yum -y install wget openssl openssl-devel gcc gcc-c++
或者
# yum install -y wget gcc openssl-devel epel-release git
下载包python3.6.9
# wget https://www.python.org/ftp/python/3.6.9/Python-3.6.9.tgz
安装
# mkdir -p /usr/local/python # tar xf Python-3.6.9.tgz # cd Python-3.6.9 # ./configure --prefix=/usr/local/python --with-openssl # make && make install
配置
# mv /usr/bin/python /usr/bin/python_old # ln -s /usr/local/python/bin/python3 /usr/bin/python # ln -s /usr/local/python/bin/pip3 /usr/bin/pip # pip install --upgrade pip # 这一步后面有更新 ,可以先不用执行
注意,所有依赖python2的脚本或者命令,需要更改为python2.7,因为现在默认的python版本为3.6,例如
# sed -i '1s/python/python2.7/g' /usr/bin/yum # sed -i '1s/python/python2.7/g' /usr/libexec/urlgrabber-ext-down
验证
$ python -V Python 3.6.9 $ pip -V pip 19.3 from /usr/local/python/lib/python3.6/site-packages/pip (python 3.6)
安装pip3
# yum install -y python3 python3-devel
############下面这块省略直接安装elastalert 到这里说一下 ,因为后面elastalert运行会遇到个错误 ,装个模块即可解决
# pip install elasticsearch==7.7.0
或者
# pip3 install "elasticsearch>=5.0.0"
错误是这样的
# python -m elastalert.elastalert --verbose --config config.yaml --rule example_rules/my_rule.yaml Traceback (most recent call last): File "/usr/local/python/lib/python3.6/runpy.py", line 183, in _run_module_as_main mod_name, mod_spec, code = _get_module_details(mod_name, _Error) File "/usr/local/python/lib/python3.6/runpy.py", line 109, in _get_module_details import(pkg_name) File "/usr/local/python/lib/python3.6/site-packages/elastalert-0.2.4-py3.6.egg/elastalert/init.py", line 6, in <module> from elasticsearch import RequestsHttpConnection ImportError: cannot import name 'RequestsHttpConnection'
##################
3、安装elastalert
下载包
### 国外的源
# git clone https://github.com/Yelp/elastalert.git
### 上面下载失败的,可以使用国内的镜像资源下载
# git clone https://gitee.com/mirrors/elastalert.git
# cd elastalert
安装
# pip3 install "setuptools>=11.3"
如果下面这步出现报错等信息 那么请先更新pip3 ## # python -m pip install --upgrade pip
# pip3 install -r requirements.txt
下面这步请根据版本选择
# pip3 install "elasticsearch<8,>7"
# python3 setup.py install 这一步容易卡住,报错,要重试 ,等好久
安装成功后可以看到四个命令
# ll /usr/local/python/bin/elastalert* /usr/local/python/bin/elastalert /usr/local/python/bin/elastalert-create-index /usr/local/python/bin/elastalert-rule-from-kibana /usr/local/python/bin/elastalert-test-rule
软连接到/usr/bin下,方便使用
# ln -s /usr/local/python/bin/elastalert* /usr/bin
- elastalert 报警执行的命令,会根据报警规则执行相应操作。
- elastalert-create-index会创建一个索引,ElastAlert会把执行记录存放到这个索引中,默认情况下,索引名叫elastalert_status。其中有4个_type,都有自己的@timestamp字段,所以同样也可以用kibana来查看这个索引的日志记录情况。
- elastalert-rule-from-kibana从Kibana3已保存的仪表盘中读取Filtering设置,帮助生成config.yaml里的配置。不过注意,它只会读取filtering,不包括queries。
- elastalert-test-rule测试自定义配置中的rule设置。
二、使用
官方文档:https://elastalert.readthedocs.io
规则文档:https://elastalert.readthedocs.io/en/latest/ruletypes.html
1、主配置文件设置
1,cd到安装的elastalert目录下
2,
# cp config.yaml.example config.yaml //复制一份config.yaml
config.yaml 有效配置
# egrep -v '^$|^#' config.yaml rules_folder: rules run_every: minutes: 1 buffer_time: minutes: 15 es_host: 192.168.0.60 es_port: 9200 writeback_index: elastalert_status writeback_alias: elastalert_alerts alert_time_limit: days: 2
3,配置rule
# cp example.rules rules //复制一份rules目录
# cd rules
# egrep -v '^$|^#' frequency.yaml name: packetbeat type: frequency index: packetbeat-* num_events: 1 timeframe: hours: 1 filter: - query: query_string: query: "host.name: node1-61" smtp_host: smtp.qq.com smtp_port: 465 smtp_auth_file: /opt/elastalert/smtp_auth_file.yaml //邮箱账号密码的配置 密码要加 ' ' #回复给那个邮箱 email_reply_to: xxx1@qq.com ###从哪个邮箱发送 from_addr: xxx1@qq.com # (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: - "xxx2@qq.com"
邮箱这块目前没有配置明白,先记录一下
2、创建告警索引
执行elastalert-create-index命令在ES创建索引,这不是必须的步骤,但是强烈建议创建。因为对于审计和测试很有用,并且重启ES不影响计数和发送alert。
$ elastalert-create-index Elastic Version: 7.3.2 Reading Elastic 6 index mappings: Reading index mapping 'es_mappings/6/silence.json' Reading index mapping 'es_mappings/6/elastalert_status.json' Reading index mapping 'es_mappings/6/elastalert.json' Reading index mapping 'es_mappings/6/past_elastalert.json' Reading index mapping 'es_mappings/6/elastalert_error.json' New index elastalert_status created Done!
看到这个输出,就说明创建成功了,也可以请求一下看看:
$ curl 127.0.0.1:9200/_cat/indices?v health status index uuid pri rep docs.count docs.deleted store.size pri.store.size green open elastalert_status_status lh8LL4iCQeSn0afyzxBX7w 1 1 0 0 460b 230b green open elastalert_status i7B7IfCuSb2Sex8U5KoTZg 1 1 0 0 460b 230b green open elastalert_status_past et2aF44VR4WQnxB8T7zD4Q 1 1 0 0 460b 230b green open elastalert_status_silence lhXHEsuUQeGZaW3cRLp5pQ 1 1 0 0 460b 230b green open elastalert_status_error zykwk4KtSyyOY7ckxQTrkA 1 1 0 0 460b 230b
注意里边在配置邮件通知的时候,还需要引用外部的一个文件,这个文件里用于存放对应邮箱的用户名密码,或者是邮箱邮箱的授权码
$ vim /opt/elastalert/smtp_auth_file.yaml user: "test01@163.com" password: "xxxxxxx"
4、规则测试
测试 规则。
# elastalert-test-rule --config config.yaml ./rules/frequency.yaml INFO:elastalert:Note: In debug mode, alerts will be logged to console but NOT actually sent. To send them but remain verbose, use --verbose instead. Didn't get any results. INFO:elastalert:Note: In debug mode, alerts will be logged to console but NOT actually sent. To send them but remain verbose, use --verbose instead. 1 rules loaded ......... elastalert_status - {'rule_name': 'Nginx_err', 'endtime': datetime.datetime(2020, 1, 11, 7, 30, 59, 793352, tzinfo=tzutc()), 'starttime': datetime.datetime(2020, 1, 10, 7, 30, 59, 793352, tzinfo=tzutc()), 'matches': 0, 'hits': 0, '@timestamp': datetime.datetime(2020, 1, 11, 7, 31, 0, 76042, tzinfo=tzutc()), 'time_taken': 0.24003815650939941}
如果没有报错,则说明可用。
5、启动
启动方式有两种
(1)指定规则文件路径
# python -m elastalert.elastalert --verbose --config config.yaml --rule rules/frequency.yaml
# python -m elastalert.elastalert --verbose --config /opt/elastalert/config.yaml --rule /opt/elastalert/rules/frequency.yaml
(2)在全局路径config.yaml下,配置规则存放在加载规则rules目录下
# python -m elastalert.elastalert --verbose
6、验证
服务启动之后,日志能够很清晰看到整个过程,此时可以在刚刚的索引原点请求几个不存在的接口,造一些404状态,过一会儿应该可以看到日志中的说明,有告警发出,邮箱应该也能收到了。
三、优化
1、启动方式
上边的启动命令只是在前台启动,并不给力,可以使用nohup启动,或者是通过supervisord管理,会更加方便。 supervisord如何安装就不说了.
创建配置文件:
$cat /etc/supervisord.d/elastalert1.ini [program:elastalert1] directory=/data/elastalert1/ command=python -m elastalert.elastalert --verbose --config /data/elastalert1/config.yaml process_name=elastalert1 autorestart=true startsecs=15 stopsignal=INT stopasgroup=true killasgroup=true redirect_stderr=true stdout_logfile=/data/log/elastalert1.log stdout_logfile_maxbytes=5MB
然后启动即可
# supervisorctl update
# supervisorctl start elastalert1
分类:
ELK
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 一文读懂知识蒸馏
· 终于写完轮子一部分:tcp代理 了,记录一下