ELK安装alert插件并设置elastalert最后告警

ELK最基本的搭建,不然这个无法进行操作,这是网址:
https://blog.csdn.net/fenghumen/article/details/109083538

1 安装python3 环境

yum -y install openssl openssl-devel gcc  gcc-c++
tar zxvf Python-3.6.2.tgz
cd Python-3.6.2
./configure --prefix=/usr/local/python3 --with-openssl
make && make install

2 设置软链接

rm -rf /usr/bin/python
ln -s /usr/local/python3/bin/python3.6 /usr/bin/python
ln -s /usr/local/python3/bin/pip3.6 /usr/bin/pip

3 修复yum命令

vim /usr/bin/yum 将python 修改为python2
vim /usr/libexec/urlgrabber-ext-down 将python 修改为python2

3 安装alert 插件

tar zxvf v0.2.1_elasticalert.tar.gz
 mv elastalert-0.2.1/ /usr/local/elastalert

安装依赖包:

cd /usr/local/elastalert
pip install -r requirements.txt -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com
python setup.py  install

会生成以下命令

-rwxr-xr-x. 1 root root      422 8月  19 03:13 elastalert-create-index
-rwxr-xr-x. 1 root root      396 8月  19 03:13 elastalert
-rwxr-xr-x. 1 root root      416 8月  19 03:13 elastalert-test-rule
-rwxr-xr-x. 1 root root      430 8月  19 03:13 elastalert-rule-from-kibana

创建软链接:

ln -s /usr/local/python3/bin/elastalert* /usr/bin/

4 设置elastalert 索引

elastalert-create-index
[root@bogon ~]# elastalert-create-index 
Enter Elasticsearch host: 192.168.1.4 ## 设置es的主机ip
Enter Elasticsearch port: 9200   ## 设置es监听端口号
Use SSL? t/f: f ## 是否启用ssl,(f表示不启用!!)
其余都直接回车即可!!

5 设置 alert的主配置文件config.yaml

[root@bogon elastalert]# pwd
/usr/local/elastalert
[root@bogon elastalert]# mv config.yaml.example config.yaml
配置详解:
rules_folder: example_rules # 用来放置 告警规则的
run_every:
  minutes: 1   #设置告警执行的频率(一分钟运行一次!!)
buffer_time:
  minutes: 15  # 设置请求里时间字段的范围(举个例子:15:30-15.45分区间的log信息。)
es_host: 192.168.1.4   # es 的主机信息
es_port: 9200   # es的端口信息
writeback_index: elastalert_status  # 创建的index 名称
alert_time_limit:
  days: 2 # 失败重试的时间限制

6 设置告警规则

[root@bogon example_rules]# pwd
/usr/local/elastalert/example_rules
cp example_frequency.yaml nginx_frequency.yaml
配置详解:
es_host: 192.168.1.4 # es主机信息
es_port: 9200  # es监听的端口号
name: nginx frequency rule  # 设置告警规则的名称
type: frequency # 设置告警规则的类型(频率)
index: nginx_log*  # 设置监听的index 名称
num_events: 5  # 设置在限定的时间内,触发的次数
timeframe:
  hours: 1   # 设置限定时间
filter:
  - regexp:
      message: ".*"   #表示message 字段下,只要有内容,并且在1小时内触发了5次就告警!!
alert:
- "email"   # 设置邮件告警
email:
- "[email protected]"
- "[email protected]"
- "[email protected]"  # 设置接收告警的邮箱地址
smtp_host: smtp.qq.com  # 设置smtp的地址
smtp_port: 25   #设置smtp监听端口号
smtp_auth_file: /usr/local/elastalert/email_auth.yaml  # 设置smtp 验证信息
from_addr: [email protected]   # 设置发送邮件的邮箱地址
[root@bogon elastalert]# cat  /usr/local/elastalert/email_auth.yaml
user: "[email protected]"
password: "jponzubigyxxbiaj" 

7 验证邮件是否能正常发送

yum -y install mailx
vim /etc/mail.rc
set from=[email protected]
set smtp=smtp.qq.com
set smtp-auth-user=[email protected]
set smtp-auth-password=jponzubigyxxbiaj
set smtp-auth=login

发送测试邮件

echo "test" |mail -s "xx" [email protected]

8 运行alert 服务

elastalert --config /usr/local/elastalert/config.yaml  --rule /usr/local/elastalert/example_rules/nginx_frequency.yaml  --verbose

9 nginx 日志里状态码包含222的则触发告警

filter:
- term:
    status: "222"

猜你喜欢

转载自blog.csdn.net/fenghumen/article/details/109104909