2.filebeat

1.filebeat介绍
2.filebeat的使用
3.filebeat收集系统日志实践
4.filebeat收集nginx日志实践
5.filebeat收集apache日志实践
6.filebeat收集tomcat日志实践

1.filebeat介绍

1.1) filebeat是什么

filebeat是轻量级的数据采集器 可以实现数据的转发数据的采集功能!

1.2) filebeat组件

filebeat的组件有 输入(input) 收割(harvester),两个组件相互协同把数据收集并发送出去

    1. 输入input: 负责输入的输入。
    1. 收割机 harvester: 负责逐行读取单个文件的内容,并将输入发送到指定终端

1.3) filebeat工作流程

输入方式选择log,它的工作原理如下
会监视指定路径的一个或者多个日志文件,如果有新的内容更新会用收割机harvester程序,把更新的内容发送到指定的终端。例如:elasticsearch logstach redis kafka

2.filebeat的使用

2.1 安装filebeat

清华镜像站下载filebeat

rpm -ivh https://mirrors.tuna.tsinghua.edu.cn/elasticstack/7.x/yum/7.8.1/filebeat-7.8.1-x86_64.rpm

2.2 配置filebeat

2.2.1 配置filebeat终端输入,终端输出
[root@rongbiz002 ~]# cat /etc/filebeat/filebeat.yml
filebeat.inputs:
- type: stdin
  enabled: true
output.console:
  pretty: true
  enable: true
2.2.2 配置filebeat文件读取,终端输出]
[root@rongbiz002 ~]# cat /etc/filebeat/filebeat.yml
filebeat.inputs:
- type: log
  enabled: true
  paths:
    - /var/log/message
output.console:
  pretty: true
  enable: true
2.2.3 配置filebeat文件读取,输出Elasticsearch
[root@rongbiz002 ~]# cat /etc/filebeat/filebeat.yml
filebeat.inputs:
- type: log
  enabled: true
  paths:
    - /www/wwwlogs/rongbiz.com-access_log

output.elasticsearch:
  hosts: ["xxxx:9200","xxxx:9201","xxxx:9202"]
  index: "rongbiz.com-access-%{+yyyy.MM}"

2.2.4 自定义index名称,输出Elasticsearch

默认filebeat输入到elasticsearch的索引名称为 filebest-*,有时需要自定义索引名称,需要以下操作

  • 修改filebeat配置文件
  • 删除elasticsearch存储的索引,重新生成
  • 删除kibana的索引模式
[root@rongbiz002 ~]# cat /etc/filebeat/filebeat.yml
filebeat.inputs:
- type: log
  enabled: true
  paths:
    - /www/wwwlogs/rongbiz.com-access_log

output.elasticsearch:
  hosts: ["xxxx:9200","xxxx:9201","xxxx:9202"]
  index: "rongbiz.com-access-%{+yyyy.MM}"       #自定义索引名称

setup.ilm.enabled: false                        #索引生命周期ilm功能默认开启,开启后索引名称只能为filebeat-*  所以需要关闭
setup.template.name: rongbiz.com                #定义模板名称
setup.template.pattern: rongbiz.com-*           #定义模板关联的索引名称

3.filebeat收集系统日志实践

3.1 配置rsyslog

3.1.1 安装rsyslog
yum install rsyslog -y
3.1.2 配置rsyslog
[root@rongbiz-43 ~]# vim /etc/rsyslog.conf
# Provides UDP syslog reception
$ModLoad imudp
$UDPServerRun 514

#*.* $IP:514             #将本地日志发送远端主机
*.* /var/log/sys43.log   #将本地所有日志保存至本地/var/log/sys.log
3.1.3 重启rsyslog
[root@rongbiz-43 ~]# systemctl restart rsyslog
[root@rongbiz-43 ~]# logger "rsyslog test from yangtao"
[root@rongbiz-43 ~]# tail  -n 10 /var/log/sys43.log 
Dec 16 10:49:44 rongbiz-43 tailscaled: monitor: RTM_DELROUTE: src=, dst=ff00::/8, gw=, outif=42197, table=255
..............................................
Dec 16 10:50:14 rongbiz-43 root: rsyslog test from yangtao

3.2 配置filebeat

[root@rongbiz-43 ~]# cat /etc/filebeat/filebeat.yml
filebeat.inputs:
- type: log
  enabled: true
  paths: /var/log/sys43.log
  include_lines: ['^WARN','^ERR','sshd']        #收集包含^WARN ^ERR sshd相关信息日志
  exclude_lines: ['DEBUG']                      #排除包涵DEBUG相关信息日志

output.elasticsearch:
  hosts: ["192.168.1.90:9200","192.168.1.43:9200","192.168.1.39:9200"]
  index: "sys43-%{[agent.version]}-%{+yyy.MM.dd}"

setup.ilm.enabled: false
setup.template.name: "system"
setup.template.pattern: "sys*-*"

3.3 配置kibana

3.3.1 创建索引

3.3.2 配置索引名称

3.3.3 查看kibana索引数据

4.filebeat收集nginx日志实践

4.1 配置nginx的日志以json格式输出

    log_format json '{ "time_local": "$time_local", '
                '"remote_addr":"$remote_addr", '
                '"referer": "$http_referer",'
                '"request": "$request", '
                '"status": $status, '
                '"bytes": $body_bytes_sent, '
                '"http_user_agent": "$http_user_agent",'
                '"x_forwarded":"$http_x_forwarded_for", '
                '"up_addr":"$upstream_addr",'
                '"up_host":"$upstream_http_host",'
                '"upstream_time":"$upstream_response_time",'
                '"site":"$http_host",'
                '"request_time":"$request_time"'
                '}';

4.2 配置filebeat收集日志

root@dwxz:~# cat /etc/filebeat/filebeat.yml
filebeat.inputs:
- type: log
  enabled: true
  paths:
    - /var/log/nginx/openc2p.cn-access.log

  json.keys_under_root: true        #Flase会将json解析的格式存储至message,改为true则不存储至message
  json.overwrite_keys: true         #覆盖默认message字段,使用自定义json格式的key

  processors:
  - drop_event:
      when:
        regexp:
          http_user_agent: Zabbix
  tags: ["c2p_nginx_access"]

- type: log
  enabled: true
  paths:
    - /var/log/nginx/openc2p.cn-error.log

  json.keys_under_root: true
  json.overwrite_keys: true
  tags: ["c2p_nginx_error"]


output.elasticsearch:
  hosts: ["xxxx:9200","xxxx:9201","xxxx:9202"]
  indices:
    - index: "c2p_nginx-access-%{+yyyy.MM}"
      when.contains: 
        tags: "c2p_nginx_access"

    - index: "c2p_nginx-error-%{+yyyy.MM}"
      when.contains:
        tags: "c2p_nginx_error"

setup.ilm.enabled: false
setup.template.name: openc2p.cn
setup.template.pattern: openc2p.cn-*

kibana

5.filebeat收集apache日志实践

5.1 配置apache 输入日志格式

<IfModule log_config_module>
    LogFormat "{ \
                 \"@time_local\": \"%{%Y-%m-%dT%H:%M:%S%z}t\", \
                 \"@version\": \"1\", \
                 \"tags\":[\"apache\"], \
                 \"message\": \"%h %l %u %t \\\"%r\\\" %>s %b\", \
                 \"remote_addr\": \"%a\", \
                 \"duration\": %D, \
                 \"status\": %>s, \
                 \"request\": \"%U%q\", \
                 \"urlpath\": \"%U\", \
                 \"urlquery\": \"%q\", \
                 \"bytes\": %B, \
                 \"method\": \"%m\", \
                 \"site\": \"%{Host}i\", \
                 \"referer\": \"%{Referer}i\", \
                 \"http_user_agent\": \"%{User-agent}i\" \
                 }" apache_json

</IfModule>

5.2 虚拟主机调用日志格式

<VirtualHost *:443>
    ServerAdmin webmaster@example.com
    DocumentRoot "/www/wwwroort/www.rongbiz.com/"
  ..................................................
    ErrorLog "/www/wwwlogs/www.rongbiz.com-error_log"
    CustomLog "/www/wwwlogs/www.rongbiz.com-access_log" combined
    CustomLog "/www/wwwlogs/rongbiz.com-access_log" apache_json

5.3 配置filebeat配置文件

[root@rongbiz002 ~]# cat /etc/filebeat/filebeat.yml
filebeat.inputs:
- type: log
  enabled: true
  paths:
    - /www/wwwlogs/rongbiz.com-access_log
    - /www/wwwlogs/c2p.rongbiz.com-access_log
  json.keys_under_root: true
  json.overwrite_keys: true

  processors:
  - drop_event:
      when:
        regexp:
          http_user_agent: Zabbix

output.elasticsearch:
  hosts: ["xxxx:9200","xxxx:9201","xxxx:9202"]
  index: "rongbiz.com-access-%{+yyyy.MM}"

setup.ilm.enabled: false
setup.template.name: rongbiz.com
setup.template.pattern: rongbiz.com-*

6.filebeat收集tomcat日志实践

posted @ 2022-12-15 14:23  老夫聊发少年狂88  阅读(150)  评论(0编辑  收藏  举报