ELK-日志服务【filebeat-安装使用】

目录

【1】安装Filebeat

【2】配置-测试

【3】配置使用Filebeat 

【4】filebeat-收集系统文件日志

【5】配置filebeat,将/var/log/all.log日志采集到es集群中

【6】定制索引名称

【7】收集多个web节点的日志,输出到相同的索引中

【8】filebeat-收集nginx日志

【9】修改nginx的日志格式

【10】图形化展示

【11】filebeat-收集nginx的访问日志+错误日志

【12】filebeat收集nginx多虚拟主机日志

【13】收集tomcat日志

【14】filebeat-收集tomcat错误日志


【1】安装Filebeat

[root@filebeat ~]# rpm -ivh filebeat-7.4.0-x86_64.rpm 
warning: filebeat-7.4.0-x86_64.rpm: Header V4 RSA/SHA512 Signature, key ID d88e42b4: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...1:filebeat-7.4.0-1                 ################################# [100%]

【2】配置-测试

  • 配置filebeat从终端读入,从终端输出
[root@filebeat ~]# vim /etc/filebeat/test.yml
filebeat.inputs:
- type: stdinenabled: true
output.console:pretty: trueenable: true## 测试
[root@filebeat ~]# filebeat -e -c test.yml

【3】配置使用Filebeat 

[root@filebeat ~]# cp /etc/filebeat/filebeat.yml /etc/filebeat/filebeat.yml_bak
[root@filebeat ~]# vim /etc/filebeat/filebeat.yml
filebeat.inputs:
- type: logenabled: truepaths:- /var/log/messagesoutput.elasticsearch:hosts: ["10.0.0.21:9200","10.0.0.22:9200","10.0.0.23:9200"]enable: true## 测试
[root@filebeat ~]# systemctl restart filebeat.service

  • 使用kibana读取filebeat索引中的数据

 

 

 

 

  • 日志中写入新的数据,刷新验证是否能被命中
[root@filebeat ~]# echo "test" >> /var/log/messages

【4】filebeat-收集系统文件日志

系统日志包含messages、secure、cron、dmesg、ssh、boot等

如果挨个配置会变得很麻烦,我们可以将这些日志进行统一几种管理,使用rsyslog将本地所有类型的日志都写入到/var/log/all.log文件中,然后使用filebeat对该文件进行收集

[root@filebeat ~]# yum -y install rsyslog
....
$ModLoad imudp
$UDPServerRun 514
....
*.* /var/log/all.log
....## 重启测试
[root@filebeat ~]# systemctl restart rsyslog.service    
[root@filebeat ~]# logger "rsyslog test from all"
[root@filebeat ~]# grep "all" /var/log/all.log 
Jul 11 05:25:47 filebeat root: rsyslog test from all

【5】配置filebeat,将/var/log/all.log日志采集到es集群中

  • 先删除es中的索引和kibana中匹配的索引,重启后生成新的索引

[root@filebeat ~]# vim /etc/filebeat/filebeat.yml
filebeat.inputs:
- type: logenabled: truepaths:- /var/log/all.loginclude_lines: ['^ERR', '^WARN', 'sshd']output.elasticsearch:hosts: ["10.0.0.21:9200","10.0.0.22:9200","10.0.0.23:9200"]enable: true[root@filebeat ~]# systemctl restart filebeat.service

【6】定制索引名称

[root@filebeat ~]# vim /etc/filebeat/filebeat.ymlfilebeat.inputs:
- type: logenabled: truepaths:- /var/log/all.loginclude_lines: ['^ERR', '^WARN', 'sshd']output.elasticsearch:hosts: ["10.0.0.21:9200","10.0.0.22:9200","10.0.0.23:9200"]enable: trueindex: "system-%{[agent.version]}-%{+yyyy.MM.dd}"
setup.ilm.enabled: false             # 索引的生命周期。默认开启,开启后索引名称只能是filebeat
setup.template.name: "system"        # 定义模板名称
setup.template.pattern: "system-*"   # 定义模板匹配索引的名称## 索引分片,方式一
setup.template.settings:index.number_of_shards: 3index.number_of_replicas: 1## 索引分片,方式二
1、修改system模板,添加分片和副本数量
2、删除模板关联的索引
3、重启filebeat
4、产生新的日志验证

  • 我们需要删除syste模板和索引,因为模板默认分片就是1,要不然分片永远不会生效

## 重启
[root@filebeat ~]# systemctl restart filebeat.service## 产生新的日志,验证

 

 

 第二种方式

【7】收集多个web节点的日志,输出到相同的索引中

  • web-01配置filebeat
[root@filebeat ~]# vim /etc/rsyslog.conf
.....
$ModLoad imudp
$UDPServerRun 514
.....
*.* /var/log/all.log
.....[root@filebeat ~]# vim /etc/filebeat/filebeat.yml
filebeat.inputs:
- type: logenabled: truepaths:- /var/log/all.loginclude_lines: ['^ERR', '^WARN', 'sshd']output.elasticsearch:hosts: ["10.0.0.21:9200","10.0.0.22:9200","10.0.0.23:9200"]enable: trueindex: "system-%{[agent.version]}-%{+yyyy.MM.dd}"setup.ilm.enabled: false
setup.template.name: "system"
setup.template.pattern: "system-*"
setup.template.settings:index.number_of_shards: 3index.number_of_replicas: 1[root@filebeat ~]# systemctl restart rsyslog.service
[root@filebeat ~]# systemctl restart filebeat.service
  • web-02配置filebeat
[root@filebeat ~]# vim /etc/rsyslog.conf
.....
$ModLoad imudp
$UDPServerRun 514
.....
*.* /var/log/all.log
.....[root@filebeat-02 ~]# vim /etc/filebeat/filebeat.yml
filebeat.inputs:
- type: logenabled: truepaths:- /var/log/all.loginclude_lines: ['^ERR', '^WARN', 'sshd']output.elasticsearch:hosts: ["10.0.0.21:9200","10.0.0.22:9200","10.0.0.23:9200"]enable: trueindex: "system-%{[agent.version]}-%{+yyyy.MM.dd}"setup.ilm.enabled: false
setup.template.name: "system"
setup.template.pattern: "system-*"
setup.template.settings:index.number_of_shards: 3index.number_of_replicas: 1[root@filebeat-02 ~]# systemctl restart rsyslog.service
[root@filebeat-02 ~]# systemctl restart filebeat.service

 

【8】filebeat-收集nginx日志

  • 获取用户细信息:来源IP、地域、网站PV、UV、状态码、访问时间等

lb-server

10.0.0.27

web-01

10.0.0.25

web-02

10.0.0.26

  • lb-server
[root@lb-server-01 ~]# vim /etc/nginx/conf.d/filebeat-test.conf
upstream file {server 10.0.0.25;server 10.0.0.26;
}
server {listen 80;server_name www.filebeat-test.org;location / {proxy_pass http://file;include proxy_params;}
}[root@lb-server-01 ~]# vim /etc/nginx/proxy_params
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1;
proxy_connect_timeout 60s;      # nginx连接后端的超时时间
proxy_read_timeout 60s;         # 响应头部超时时间
proxy_send_timeout 60s;         # 响应数据主体的超时时间
proxy_buffering on;             # 开启缓冲区
proxy_buffer_size 8k;           # 缓冲区Header大小
proxy_buffers 4 64k;            # 缓冲区数量 * 大小 = 最大接收[root@lb-server-01 ~]# systemctl reload nginx
  • web-01和web-02配置相同
[root@filebeat conf.d]# vim /etc/nginx/conf.d/filebeat-test.conf
server {listen 80;server_name www.filebeat-test.org;root /code/filebeat;location / {index index.html;}
}[root@filebeat conf.d]# mkdir -p /code/filebeat
[root@filebeat conf.d]# echo "filebeat-test-web-01" >> /code/filebeat/index.html
[root@filebeat-02 conf.d]# echo "filebeat-test-web-02" >> /code/filebeat/index.html
[root@filebeat conf.d]# systemctl reload nginx.service

  • web-01、web-02 配置filebeat
[root@filebeat conf.d]# vim /etc/filebeat/nginx-filebeat-access.yml
filebeat.inputs:
- type: logenabled: truepaths:- /var/log/nginx/access.logoutput.elasticsearch:hosts: ["10.0.0.21:9200","10.0.0.22:9200","10.0.0.23:9200"]enable: trueindex: "nginx-access-%{[agent.version]}-%{+yyyy.MM.dd}"setup.ilm.enabled: false
setup.template.name: "nginx"
setup.template.pattern: "nginx-*"[root@filebeat conf.d]# filebeat -e -c /etc/filebeat/nginx-filebeat-access.yml &>/dev/null &
[1] 13738
  • 验证

 

 

  • 我们看到在message字段中,记录的信息非常的多,不适合我们之后的统计,那么怎么做呢

方式一、修改nginx的日志格式 json 方式二、filebeat —> logstash

【9】修改nginx的日志格式

[root@filebeat ~]# vim /etc/nginx/nginx.conf
............
............log_format json '{ "time_local": "$time_local", ''"remote_addr": "$remote_addr", ''"referer": "$http_referer", ''"request": "$request", ''"status": $status, ''"bytes": $body_bytes_sent, ''"agent": "$http_user_agent", ''"x_forwarded": "$http_x_forwarded_for", ''"up_addr": "$upstream_addr", ''"up_host": "$upstream_http_host", ''"upstream_time": "$upstream_response_time", ''"request_time": "$request_time"''}'
...........access_log  /var/log/nginx/access-json.log  json;[root@filebeat ~]# systemctl reload nginx.service
[root@filebeat ~]# tailf /var/log/nginx/access-json.log 
{ "time_local": "11/Jul/2023:08:44:55 -0400", "remote_addr": "10.0.0.27", "referer": "-", "request": "GET / HTTP/1.1", "status": 200, "bytes": 21, "agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36", "x_forwarded": "10.0.0.1", "up_addr": "-", "up_host": "-", "upstream_time": "-", "request_time": "0.000"}access_log/var/log/nginx/access.logmain## 收集日志改为access-json.log
[root@filebeat ~]# vim /etc/filebeat/nginx-filebeat-access.yml 
filebeat.inputs:
- type: logenabled: truepaths:- /var/log/nginx/access-json.logjson_keys_under_root: true    # false表示将json解析的内容存储在message字段,true表示不存储在message字段中json.overwrite_keys: true     # 覆盖message字段,使用自定义json的keyoutput.elasticsearch:hosts: ["10.0.0.21:9200","10.0.0.22:9200","10.0.0.23:9200"]enable: trueindex: "nginx-access-%{[agent.version]}-%{+yyyy.MM.dd}"setup.ilm.enabled: false
setup.template.name: "nginx"
setup.template.pattern: "nginx-*"[root@filebeat ~]# kill 13738
[root@filebeat ~]# filebeat -e -c /etc/filebeat/nginx-filebeat-access.yml &>/dev/null &

 

【10】图形化展示

 

【11】filebeat-收集nginx的访问日志+错误日志

[root@filebeat ~]# vim /etc/filebeat/nginx-filebeat-access-error.yml
filebeat.inputs:
- type: logenabled: truepaths:- /var/log/nginx/access-json.logjson_keys_under_root: truejson.overwrite_keys: truetags: ["nginx-access"]- type: logenabled: truepaths:- /var/log/nginx/error.logtags: ["nginx-error"]output.elasticsearch:hosts: ["10.0.0.21:9200","10.0.0.22:9200","10.0.0.23:9200"]enable: trueindices:- index: "nginx-access-%{[agent.version]}-%{+yyyy.MM.dd}"when.contains:tags: "nginx-access"- index: "nginx-error-%{[agent.version]}-%{+yyyy.MM.dd}"when.contains:tags: "nginx-error"setup.ilm.enabled: false
setup.template.name: "nginx"
setup.template.pattern: "nginx-*"[root@filebeat ~]# filebeat -e -c /etc/filebeat/nginx-filebeat-access-error.yml &>/dev/null &

 

【12】filebeat收集nginx多虚拟主机日志

[root@filebeat filebeat]# vim /etc/nginx/conf.d/filebeat-test-01.conf 
server {listen 80;server_name www.filebeat-test-01.org;root /code/filebeat-01;access_log /var/log/nginx/access-test-01.log json;location / {index index.html;}
}[root@filebeat ~]# vim /etc/nginx/conf.d/filebeat-test-02.conf
server {listen 80;server_name www.filebeat-test-02.org;root /code/filebeat-02;access_log /var/log/nginx/access-test-02.log json;location / {index index.html;}
}[root@filebeat filebeat]# mkdir /code/filebeat-01
[root@filebeat filebeat]# echo "www.filebeat-01-web01" >> /code/filebeat-01/index.html[root@filebeat ~]# mkdir /code/filebeat-02
[root@filebeat ~]# echo "www.filebeat-02-web01" >> /code/filebeat-02/index.html
[root@filebeat ~]# systemctl reload nginx.service
  • 配置filebeat
[root@filebeat ~]# cat /etc/filebeat/filebeat.yml 
filebeat.inputs:
- type: logenabled: truepaths:- /var/log/nginx/access-test-01.logjson_keys_under_root: truejson.overwrite_keys: truetags: ["nginx-web01"]- type: logenabled: truepaths:- /var/log/nginx/access-test-02.logjson_keys_under_root: truejson.overwrite_keys: truetags: ["nginx-web02"]- type: logenabled: truepaths:- /var/log/nginx/error.logtags: ["nginx-error"]output.elasticsearch:hosts: ["10.0.0.21:9200","10.0.0.22:9200","10.0.0.23:9200"]enable: trueindices:- index: "nginx-web01-%{[agent.version]}-%{+yyyy.MM.dd}"when.contains:tags: "nginx-web01"- index: "nginx-web02-%{[agent.version]}-%{+yyyy.MM.dd}"when.contains:tags: "nginx-web02"- index: "nginx-error-%{[agent.version]}-%{+yyyy.MM.dd}"when.contains:tags: "nginx-error"setup.ilm.enabled: false
setup.template.name: "nginx"
setup.template.pattern: "nginx-*"[root@filebeat filebeat]# systemctl restart filebeat.service

 

【13】收集tomcat日志

  • 修改tomcat的日志格式
  <Host name="www.file-tomcat.org"  appBase="webapps"unpackWARs="true" autoDeploy="true"><Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"prefix="file-tomcat_access_log." suffix=".txt"pattern="{&quot;clientip&quot;:&quot;%h&quot;,ClientUser&quot;:&quot;%l&quot;,&quot;authenticated&quot;:&quot;%u&quot;,&quot;AccessTime&quot;:&quot;%t&quot;,&quot;method&quot;:&quot;%r&quot;,&quot;status&quot;:&quot;%s&quot;,&quot;SendBytes&quot;:&quot;%b&quot;,&quot;Query?string&quot;:&quot;%q&quot;,&quot;parner&quot;:&quot;%{Referer}i&quot;,&quot;AgentVersion&quot;:&quot;%{User-Agent}i&quot;}" /></Host>
  • 启动tomcat,验证日志格式
[root@filebeat soft]# systemctl restart tomcat.service
[root@filebeat soft]# tailf /soft/tomcat/logs/file-tomcat_access_log..2023-07-12.txt
{"clientip":"10.0.0.1",ClientUser":"-","authenticated":"-","AccessTime":"[12/Jul/2023:03:26:54 -0400]","method":"GET / HTTP/1.1","status":"200","SendBytes":"11156","Query?string":"","parner":"-","AgentVersion":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36"}
  • 配置filebeat
[root@filebeat filebeat]# vim filebeat.yml
filebeat.inputs:
- type: logenabled: truepaths:- /soft/tomcat/logs/file-tomcat_access_log.*.txtjson_keys_under_root: truejson.overwrite_keys: truetags: ["tomcat-access"]output.elasticsearch:hosts: ["10.0.0.21:9200","10.0.0.22:9200","10.0.0.23:9200"]enable: trueindices:- index: "tomcat-access-%{[agent.version]}-%{+yyyy.MM.dd}"when.contains:tags: "tomcat-access"setup.ilm.enabled: false
setup.template.name: "tomcat"
setup.template.pattern: "tomcat-*"[root@filebeat filebeat]# systemctl restart filebeat.service
  •  验证

【14】filebeat-收集tomcat错误日志

[root@filebeat filebeat]# vim filebeat.yml
filebeat.inputs:
- type: logenabled: truepaths:- /soft/tomcat/logs/file-tomcat_access_log.*.txtjson_keys_under_root: truejson.overwrite_keys: truetags: ["tomcat-access"]- type: logenabled: truepaths:- /soft/tomcat/logs/catalina.outmultiline.pattern: '^\d{2}'multiline.negate: truemultiline.match: aftermultiline.max_lines: 1000tags: ["tomcat-error"]output.elasticsearch:hosts: ["10.0.0.21:9200","10.0.0.22:9200","10.0.0.23:9200"]enable: trueindices:- index: "tomcat-access-%{[agent.version]}-%{+yyyy.MM.dd}"when.contains:tags: "tomcat-access"- index: "tomcat-error-%{[agent.version]}-%{+yyyy.MM.dd}"when.contains:tags: "tomcat-error"setup.ilm.enabled: false
setup.template.name: "tomcat"
setup.template.pattern: "tomcat-*"[root@filebeat filebeat]# systemctl restart filebeat.service
  • 验证

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/21397.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

matlab处理数据

Matlab异常值处理https://blog.csdn.net/weixin_57345774/article/details/126965835?csdn_share_tail%7B%22type%22%3A%22blog%22%2C%22rType%22%3A%22article%22%2C%22rId%22%3A%22126965835%22%2C%22source%22%3A%22qq_53011270%22%7D&fromshareblogdetail 异常值识别和…

【ELK企业级日志分析系统】部署Filebeat+Kafka+Logstash+Elasticsearch+Kibana集群详解(EFLFK)

部署FilebeatKafkaLogstashElasticsearchKibana集群详解 1. Kafka1.1 Kafka概述1.1.1 为什么需要消息队列&#xff08;MQ&#xff09;1.1.2 使用消息队列的好处 1.2 消息队列的两种模式1.3 Kafka定义1.3.1 Kafka简介1.3.2 Kafka的特性1.3.3 Kafka系统架构1.3.4 Partation数据路…

基于预测控制模型的自适应巡航控制仿真与机器人实现(Matlab代码实现)

目录 &#x1f4a5;1 概述 &#x1f4da;2 运行结果 &#x1f389;3 参考文献 &#x1f468;‍&#x1f4bb;4 Matlab代码 &#x1f4a5;1 概述 自适应巡航控制技术为目前由于汽车保有量不断增长而带来的行车安全、驾驶舒适性及交通拥堵等问题提供了一条有效的解决途径&am…

OpenCV中的RGB与YUV转换

1 基本概念 YUV 颜色空间从模拟电视时代开始就被广泛应用于彩色图像的转换与处理。其基于一个 3x3 的矩阵&#xff0c;通过线性变换将 RGB 像素转换为一个亮度&#xff08;Luma&#xff09;分量 Y 以及两个色度&#xff08;Chroma&#xff09;分量 U 和 V。由于模拟电视存在着多…

3分钟阿里云轻量应用服务器和云服务器的区别对比

阿里云服务器ECS和轻量应用服务器有什么区别&#xff1f;云服务器ECS是明星级云服务器&#xff0c;轻量应用服务器可以理解为简化版的云服务器ECS&#xff0c;轻量适用于单机应用&#xff0c;云服务器ECS适用于集群类高可用高容灾应用&#xff0c;阿里云百科来详细说下阿里云轻…

ArcGIS如何制作横版图例

如果你经常制图&#xff0c;肯定使用过插入图例这个功能&#xff0c;默认情况下&#xff0c;插入的图例是竖着的&#xff0c;在某些情况下&#xff0c;如果需要横着的图例是否可以实现呢&#xff0c;答案是肯定的&#xff0c;这里为大家介绍一下ArcGIS如何制作横版图例&#xf…

opencv 05 彩色RGB像素值操作

opencv 05 彩色RGB像素值操作 RGB 模式的彩色图像在读入 OpenCV 内进行处理时&#xff0c;会按照行方向依次读取该 RGB 图像的 B 通道、G 通道、R 通道的像素点&#xff0c;并将像素点以行为单位存储在 ndarray 的列中。例如&#xff0c; 有一幅大小为 R 行C 列的原始 RGB 图像…

毫秒级突破!腾讯技术团队是如何做前端性能优化的?

&#x1f449;腾小云导读 搜狗百科是一个服务于互联网用户的高质量内容平台。文章主要介绍团队在梳理业务时发现百科无线前端项目在研发流程、架构设计、研发效率、页面性能等方面存在诸多问题和痛点。作者团队是如何对这个系统进行升级和改造的&#xff1f;又是如何分析出怎么…

Go语言网络编程:HTTP服务端之底层原理与源码分析——http.HandleFunc()、http.ListenAndServe()

一、启动 http 服务 import ("net/http" ) func main() {http.HandleFunc("/ping", func(w http.ResponseWriter, r *http.Request) {w.Write([]byte("ping...ping..."))})http.ListenAndServe(":8999", nil) }在 Golang只需要几行代…

7.11 学习记录

目录 242.有效的字母异位词 349. 两个数组的交集 202. 快乐数 1. 两数之和 454.四数相加II 383. 赎金信 代码随想录 (programmercarl.com)https://www.programmercarl.com/%E5%93%88%E5%B8%8C%E8%A1%A8%E7%90%86%E8%AE%BA%E5%9F%BA%E7%A1%80.html#%E5%B8%B8%E8%A7%81%E7…

pytorch grid_sample易错点

pytorch grid_sample易错点 易错点是&#xff1a; grid_sample函数中, x对应w, y对应h !! grid_sample函数中, x对应w, y对应h !! grid_sample函数中, x对应w, y对应h !! 函数的作用 output的size和grid的size是一样的&#xff0c;所以output中某一位置(h, w)的值&#xff0c…

【算法基础】进制转换

一、X进制转十进制 (一)Question 1. 问题描述 2. Input 第一行一个整数 x; 第二行一个字符串 S。 3. Output 输出仅包含一个整数,表示答案。 4. Sample Input 16 7B5. Sample Output 123(二)题解 #include <bits/stdc++.h> using