ElastAlert--企业微信报警配置方法

       ElastAlert完美契合ElasticSearch,基于轮休机制实现报警功能,报警匹配规则支持any、spike、frequency、flatline、change等方式,报警方式支持email、jira等方式,功能比较强大,能够满足我们的报警需求。以下是ElastAlert的安装和配置说明。

yum -y install gcc openssl-devel zlib zlib-devel
安装依赖
下载最新版python安装包
    https://www.python.org/ftp/python/2.7.13/Python-2.7.13.tgz
解压python 安装包,进入Python-2.7.13目录,执行:
    ./configure 
    make all            
    make install 
    make clean 
    make distclean 
查看版本信息:
    /usr/local/bin/python2.7 -V 
建立软连接,使系统默认的 python指向 python2.7
    mv /usr/bin/python /usr/bin/python2.6.6 
    ln -s /usr/local/bin/python2.7 /usr/bin/python 
重新检验Python 版本
    python -V 
指定 yum 的Python版本
    vi /usr/bin/yum  ,将文件头部的#!/usr/bin/python改成 
    #!/usr/bin/python2.6.6
升级python至2.7
在线安装pip:
1.下载setuptools安装包:
wget https://pypi.python.org/packages/source/s/setuptools/setuptools-3.5.1.zip
2.解压压缩包:unzip setuptools-3.5.1.zip
3.进入unzip setuptools-3.5.1目录,执行python setup.py install,如果这一步出现缺失zlib相关模块,请先yum 安装zlib, zlib-devel,然后重新编译安装python
4.执行usr/local/bin/easy_install pip安装pip
5.执行/usr/local/bin/pip -V检查pip版本
离线安装pip:
1.下载pip安装包:https://pypi.python.org/pypi/pip
2.解压,进入pip-9.0.1目录下,执行python setup.py install
升级PIP
在线安装:
    pip install -i https://pypi.tuna.tsinghua.edu.cn/simple elastalert
离线安装:
    a.在公网环境下载elastalert及其依赖包:
       pip install --download /home/elastalert-dependency -i https://pypi.tuna.tsinghua.edu.cn/simple elastalert
    上传elastalert及其依赖包elastalert-dependency.tar.gz,解压到/home/,离线安装elastalert及其依赖包:
       pip install --no-index --find-links=/home/elastalert-dependency elastalert
安装ElastAlert
rules_folder: /etc/elastalert/rules
run_every:
    minutes: 1
buffer_time:
    minutes: 5
es_host: 192.168.21.107
es_port: 9200
writeback_index: elastalert_status
alert_time_limit:
    days: 2
全局配置文件

 全局配置文件主要参数:   

参数名称                                              说明
es_host                                             es地址
es_port                                             es端口
buffer_time                  查询缓冲时间,ElastAlert会查询buffer_time之前的时间点到当前时间点的数据
run_every                    报警检查(检索es)时间间隔,默认一分钟
rules_folder                 报警规则存放位置,rules_folder下面可以分多级目录存放报警规则配置文件
notify_email                 ElastAlert本身出现异常时,通知email地址
smtp_host                    邮件服务器地址,每个报警规则配置文件中可以覆盖该项配置,elastalert会默认取全局中的配置
smtp_port                    邮件服务器端口,每个报警规则配置文件中可以覆盖该项配置,elastalert会默认取全局中的配置
smtp_auth_file               smtp认证文件位置,每个报警规则配置文件中可以覆盖该项配置,elastalert会默认取全局中的配置
from_addr                    发件人地址,每个报警规则配置文件中可以覆盖该项配置,elastalert会默认取全局中的配置
writeback_index              elastalert状态信息存放索引名称,默认elastalert_status
timestamp_field              设置 buffer_time 时针对哪个字段,默认是@timestamp
es_host: 192.168.21.107
es_port: 9200
index: metricbeat-*
name: CPU_USER_RULE
type: any
filter:
- range:
    system.cpu.user.pct:
      from: 0.8
alert:
- "elastalert_modules.wechat_qiye_alert.WeChatAlerter"
alert_text: "test-主机名:[{0}],cpu用户使用率:[{1}],超过预设阈值:[0.8]"
alert_text_args:
- beat.hostname
- system.cpu.user.pct
alert_text_type: alert_text_only
corp_id: "xxxxx"
secret: "xxxxxxxxxxxxxxxxxxxxxxxxx"
agent_id: "xxxxxx"
party_id: "xxxx"
user_id:  "@all"
tag_id: "xxxxx"
报警配置文件

报警配置文件主要参数:

参数名称                                                说明
es_host                                          必填参数,es地址
es_port                                          必填参数,es端口
index                         必填参数,指定警报获取数据的ES index,例如,我们想针对metricbeat搜集的数据进行过滤和报警,我们可以指定index为metricbeat-*
name                          必填参数,警报规则名称,不同的规则名字不能相同
type                          必填参数,报警规则类型,可选项目有:any、frequency、spike、flatline、blacklist、whitelist、change、new_term、cardinality
alert                         必填参数,报警方式,我们一般选择email
email                         必填参数,收件人(组),一个或者多个
filter                        可选参数,从elasticsearch获取报警数据的过滤条件
alert_subject                 可选参数,报警主题
alert_text_type               可选参数,报警内容数据项,可以选择exclude_fields、alert_text_only或者不设置
alert_text                    可选参数,警报内容,可以通过{0}…{n}的方式引用alert_text_args指定的参数
alert_text_args               可选参数,设置报警内容参数,可以被alert_text以{0}…{n}的方式引用
smtp_host                     可选参数,邮件服务器地址,不设置的话elastalert会默认取全局中的配置
smtp_port                     可选参数,邮件服务器端口,不设置的话elastalert会默认取全局中的配置
smtp_auth_file                可选参数,smtp认证文件位置,不设置的话elastalert会默认取全局中的配置
from_addr                     可选参数,发件人地址,不设置的话elastalert会默认取全局中的配置
1.wget https://github.com/anjia0532/elastalert-wechat-plugin/archive/master.zip
2.解压master.zip,得到elastalert-wechat-plugin-master目录,进入elastalert-wechat-plugin-master
3.在/home目录下面建立scripts目录,将elastalert-wechat-plugin-master目录下面的elastalert_modules子目录copy到/home/scripts目录下面
4.在/usr/local/lib/python2.7/site-packages目录下面新建elastalert_modules.pth文件,并添加如下内容:
/home/scripts
插件配置
创建报警索引:
elastalert-create-index --host <eshost> --port <esport> --config <elastalert_config> --index <index_name>

单个运行rule:
python -m elastalert.elastalert --config <elastalert_config> --rule <rule_config>
*为了调试的方便,我们可以在rule运行的时候加上--debug或者--verbose参数,两个参数都会打印规则的匹配和警报发出的情况,区别在于--debug不会真正发出警报,而--verbose在打印丰富的调试信息的同时会发出真正的警报。

运行所有rule:
python -m elastalert.elastalert --config <elastalert_config>
*该命令会读取elastalert_config配置中rules_folder目录下面的所有yaml文件,并加载所有rules。
运行
python -m elastalert.elastalert是前端运行的方式,终端退出的话报警规则就停止了,建议使用supervisord方式来让ElastAlert在后台长期运行。
yum安装supervisor后,其配置文件默认位置:/etc/supervisord.conf

[supervisord]
http_port=/var/tmp/supervisor.sock ; (default is to run a UNIX domain socket server)
;http_port=127.0.0.1:9001  ; (alternately, ip_address:port specifies AF_INET)
;sockchmod=0700              ; AF_UNIX socketmode (AF_INET ignore, default 0700)
;sockchown=nobody.nogroup     ; AF_UNIX socket uid.gid owner (AF_INET ignores)
;umask=022                   ; (process file creation umask;default 022)
logfile=/var/log/supervisor/supervisord.log ; (main log file;default $CWD/supervisord.log)
logfile_maxbytes=50MB       ; (max main logfile bytes b4 rotation;default 50MB)
logfile_backups=10          ; (num of main logfile rotation backups;default 10)
loglevel=info               ; (logging level;default info; others: debug,warn)
pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
nodaemon=false              ; (start in foreground if true;default false)
minfds=1024                 ; (min. avail startup file descriptors;default 1024)
minprocs=200                ; (min. avail process descriptors;default 200)
;nocleanup=true              ; (don't clean up tempfiles at start;default false)
;http_username=user          ; (default is no username (open system))
;http_password=123           ; (default is no password (open system))
;childlogdir=/tmp            ; ('AUTO' child log dir, default $TEMP)
;user=chrism                 ; (default is current user, required if root)
;directory=/tmp              ; (default is not to cd during start)
;environment=KEY=value       ; (key value pairs to add to environment)
[supervisorctl]
serverurl=unix:///var/tmp/supervisor.sock ; use a unix:// URL  for a unix socket
;serverurl=http://127.0.0.1:9001 ; use an http:// url to specify an inet socket
;username=chris              ; should be same as http_username if set
;password=123                ; should be same as http_password if set
;prompt=mysupervisor         ; cmd line prompt (default "supervisor")
; The below sample program section shows all possible program subsection values,
; create one or more 'real' program: sections to be able to control them under
; supervisor.
;在这里设置ElastAlert 运行命令
[program:elastalert]
command=python -m elastalert.elastalert --config /home/softwares/elastalert-master/config.yaml            ; the program (relative uses PATH, can take args)
;priority=999                ; the relative start priority (default 999)
;autostart=true              ; start at supervisord start (default: true)
;autorestart=true            ; retstart at unexpected quit (default: true)
;startsecs=10                ; number of secs prog must stay running (def. 10)
;startretries=3              ; max # of serial start failures (default 3)
;exitcodes=0,2               ; 'expected' exit codes for process (default 0,2)
;stopsignal=QUIT             ; signal used to kill process (default TERM)
;stopwaitsecs=10             ; max num secs to wait before SIGKILL (default 10)
;user=chrism                 ; setuid to this UNIX account to run the program
;log_stdout=true             ; if true, log program stdout (default true)
;log_stderr=true             ; if true, log program stderr (def false)
logfile=/var/log/elastalert.log    ; child log path, use NONE for none; default AUTO
logfile_maxbytes=1MB        ; max # logfile bytes b4 rotation (default 50MB)
logfile_backups=10          ; # of logfile backups (default 10)



supervisord启动
后台运行

企业微信号需要自己单独配置,如有问题或者建议,请留言。参考地址如下:

http://elastalert.readthedocs.io/en/latest/elastalert.html#configuration

http://elastalert.readthedocs.io/en/latest/ruletypes.html

http://elastalert.readthedocs.io/en/latest/

https://github.com/anjia0532/elastalert-wechat-plugin

转载于:https://www.cnblogs.com/mliu/p/8677353.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值