1. Nginx日志分析系统
1.1 项目需求
Nginx是一款非常优秀的Web服务器, 往往Nginx服务会作为项目的访问入口, 那么, Nginx的性能保障就变得非常重要了, 如果Nginx的运行出现了问题就会对项目有较大的影响, 所以, 我们需要对Nginx的运行有监控措施, 实时掌握Nginx的运行情况, 那就需要手机Nginx的运行指标和分析Nginx 的运行日志了
1.2 业务流程
2. 部署安装Nginx
tar -xvf nginx-1.11.6.tar.gz
yum -y install pcre-devel zlib-devel
yum -y install gcc
yum -y install gcc-c++
cd nginx-1.11.6
./configure
make install
# 启动
cd /usr/local/nginx/sbin/
./nginx
# 通过浏览器访问页面并且查看日志
# 访问地址: http://ip即可
tail -f /usr/local/nginx/logs/access.log
3. Beats简介
官网: https://www.elastic.co/cn/products/beats
4. Filebeat
4.1 架构
收集到指定目录下的log日志文件, 通过Harvester(收割器)不断采集日志文件内容, spooler(卷轴)把收割来的数据传输到下游的一些应用中, 可以配置成ES, Logstash等
4.2 部署与运行
下载https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-6.5.4-linux-x86_64.tar.gz
mkdir /mystudy/beats
上传压缩包到上边的目录中
tar -xvf filebeat-6.5.4-linux-x86_64.tar.gz
cd filebeat-6.5.4-linux-x86_64
# 创建如下配置文件 myfilebeat.yml 这个配置文件是一个测试配置文件, 输入方式是控制台输入, 并不是文件输入
filebeat.inputs:
- type: stdin
enabled: true
setup.template.settings:
index.number_of_shards: 3
output.console:
pretty: true
enable: true
# 启动filebeat -e代表标准启动 -c代表指定配置文件
./filebeat -e -c myfilebeat.yml
# 输入hello运行结果如下
hello
4.3 读取日志文件
设置读取日志文件的配置myfilebeat-log.yml
filebeat.inputs:
- type: log # 定义输入类型为log日志
enabled: true
paths:
- /mystudy/beats/logs/*.log # 配置日志文件路径
setup.template.settings:
index.number_of_shards: 3 # 设置在es中存储的分片数
output.console: # 输出方式控制台
pretty: true # 美化输出
enable: true
启动filebeat
./filebeat -e -c myfilebeat-log.yml
在/mystudy/beats/logs下创建a.log, 并输入如下内容
hello
world
观察filbeat输出
4.4 自定义filebeat字段
修改myfilebeat-log.yml
filebeat.inputs:
- type: log # 定义输入类型为log日志
enabled: true
paths:
- /mystudy/beats/logs/*.log # 配置日志文件路径
tags: ["web","test"] # 添加自定义tag, 便于后续的处理
fields: # 添加自定义字段
from: test-web # 如果不设置下面的那个参数的话from字段会在fields(fields字段与message等字段同级)字段内部, 如果设置了下面的参数from就会与message等字段同一级别下
fields_under_root: true # true为添加到跟节点, false为添加到子节点
setup.template.settings:
index.number_of_shards: 3 # 设置在es中存储的分片数
output.console: # 输出方式控制台
pretty: true # 美化输出
enable: true
启动filebeat
./filebeat -e -c myfilebeat-log.yml
在/mystudy/beats/logs下创建a.log, 并输入如下内容
123
未设置fields_under_root: true时, from字段在fields内部
设置了fields_under_root: true时, from字段根节点下
4.5 输出到Elasticsearch
filebeat.inputs:
- type: log # 定义输入类型为log日志
enabled: true
paths:
- /mystudy/beats/logs/*.log