关于 elk 集群搭建请查看: http://blog.51cto.com/linyingyong/2084284
先上一个我最后弄出来的图形:
环境 :
nginx 服务器 安装 filebeat
配置:
1.nginx 配置
nginx 配置日志格式为 json, 修改 nginx 配置文件:
vim /usr/local/nginx/conf/nginx.conf
在 http 标签下添加:
- log_format logstash_json '{"@timestamp":"$time_local",'
- '"@fields": { ''"remote_addr":"$remote_addr",'
- '"remote_user": "$remote_user", ''"body_bytes_sent":"$body_bytes_sent",'
- '"request_time": "$request_time", ''"status":"$status",'
- '"request": "$request", ''"request_method":"$request_method",'
- '"http_referrer": "$http_referer", ''"body_bytes_sent":"$body_bytes_sent",'
- '"http_x_forwarded_for": "$http_x_forwarded_for", ''"http_user_agent":"$http_user_agent"} }';
在需要收集日志 server 添加以下 access_log logs/www.access.log logstash_json; # logstash_json 需要和上面定义名字相同
nginx 从新加载, 加载, 就能看到新生成的日志就是 json 格式
- /usr/local/nginx/sbin/nginx -t
- /usr/local/nginx/sbin/nginx -s reload
生成后日志截图
2. 安装配置 filebeat
- wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-6.2.2-x86_64.rpm
- rpm -ivh filebeat-6.2.2-x86_64.rpm
编辑 filebeat 配置文件
vim /etc/filebeat/filebeat.yml
注释原来的:
- #- type: log
- # paths:
- # - /var/log/*.log
在注释下面添加自己的定义的 log
- - input_type: log
- paths:
- - /usr/local/nginx/logs/xxx.access.log
- json.keys_under_root: true # 收集 json 格式
- json.overwrite_keys: true # 收集 json 格式
- fields: # 当我们需要收集多个 log 并且分开命名需要
- log_source: zhiliao-www
- #-------------------------- Elasticsearch output --------------------------
- # 配置输出
- setup.template.name: "zhiliao-nginx-01"
- setup.template.pattern: "zhiliao-nginx-01-*"
- output.elasticsearch:
- hosts: ["192.168.10.63:9200"]
- index: "zhiliao-nginx-01-%{ yyyy.MM.dd}"
启动 filebeat: /etc/init.d/filebeat start
3. kinaba 添加索引
输入 zhiliao-nginx * 自动匹配到了目前的索引, 点击 next step 添加
4. 自定义 kinaba 可视化显示
网站状态码占比举例
选择 pie 饼状图
选择刚才添加的 nginx 索引
选择 Split Slies
Aggregation 选择 Terms
Field 字段选择 json 传递过来后的字段, 可以在首页查看到状态码是 @fields.status
Order By 选择 count
Order :Descendi 个数 8(这个显示多少自己定义, 状态码显示 8 个够了)
Custom Label: 状态码: (这个是在鼠标放在对应区域显示)
然后点击三角箭头就可以预览
保存自定义的图表
新建一个 Dashboard 加载我们添加的 Visualization
来源: http://www.bubuko.com/infodetail-2522419.html