本文章基于elk版本6.7.0监控错误日志
安装ELK和xpack安装破解略过,可以参考另一篇博客
https://blog.csdn.net/qq_43076479/article/details/106349348
配置Nginx代理
server
{
listen 8080;#监听端口
server_name localhost;#域名
index index.html index.htm index.php;
root /usr/local/webserver/nginx/html;#站点目录
location ~ .*\.(php|php5)?$
{
#fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|ico)$
{
expires 30d;
# access_log off;
}
location / {
proxy_pass <钉钉机器人Webhook地址>;
}
location ~ .*\.(js|css)?$
{
expires 15d;
# access_log off;
}
access_log off;
}
启动nginx,查看监听端口
钉钉创建机器人步骤略过
配置x-pack报警规则
登录kibana平台
在左侧菜单栏,单击Dev Tools(开发工具)
在Console中执行如下命令创建一个报警文档
以下示例以创建log_error_watch
为例,每隔15s
查询sdtomcatlog_2020.09
索引中是否出现ERROR日志,如果出现0
次以上则触发报警,报警规则1分钟以内没有处理则继续报警,配置需要按需修改
PUT _xpack/watcher/watch/log_error_watch
{
"trigger": {
"schedule": {
"interval": "15s"
}
},
"input": {
"search": {
"request": {
"indices": ["sdtomcatlog_2020.09"],
"body": {
"query": {
"match": {
"level": "ERROR"
}
}
}
}
}
},
"condition": {
"compare": {
"ctx.payload.hits.total": {
"gt": 0
}
}
},
"actions" : {
"test_issue" : {
"throttle_period": "1m",
"webhook" : {
"method" : "POST",
"url" : "http://10.4.7.102:8080",
"body" : "{\"msgtype\": \"text\", \"text\": { \"content\": \"快汇通项目error 日志出现了,请在1分钟内使用elastic用户在kibana平台的Dev Tools操作栏执行确认操作:PUT _xpack/watcher/watch/log_error_watch/_ack/test_issue\"}}"
}
}
}
}
告警示意图如下,内容可自定义
告警出现后我们需要在kibana平台的左侧菜单栏,单击Dev Tools(开发工具),执行报警内容的确认操作,
PUT _xpack/watcher/watch/log_error_watch/_ack/test_issue
处理完并把错误日志删除:
POST /sdtomcatlog_2020.09/_delete_by_query
{
"query": { "match": { "level": "ERROR" } }
}
删除索引的所有错误日志
查看报警规则状态:
GET _xpack/watcher/watch/log_error_watch
只有在这个状态下才会继续监控错误日志,前提是记得把前边的错误日志都删除才会自动恢复此状态。
body可自定义报警内容,例如:
"body" : "{\"msgtype\": \"text\", \"text\": { \"content\": \" 日志告警\n 日志条数: {{ctx.payload.hits.total}}\n 机器名称: {{ ctx.payload.hits.hits.0._source.host.name }}\n 日志路径: {{ ctx.payload.hits.hits.0._source.source }}\n 实例ID:{{ ctx.payload.hits.hits.0._source.meta.cloud.instance_id }}\n 报错日志:\n{{#ctx.payload.hits.hits}}{{_source.message}}\n {{/ctx.payload.hits.hits}}\"}}"