使用Metricbeat和Filebeat监控Nginx性能指标和日志
文章目录
- 使用Metricbeat和Filebeat监控Nginx性能指标和日志
- 前言
- 安装和配置配置Metricbeat
- 下载Merticbeat
- 安装Metricbeat
- Metricbeat目录
- 配置连接Elasticsearch和Kibana
- 启用Nginx stub_status模块
- 启用Nginx模块
- 设置Metricbeat assets
- 以服务方式运行Metricbeat
- 安装和配置Filebeat
- 下载Filebeat
- 安装Filebeat
- Filebeat目录
- 配置连接Elasticsearch和Kibana
- 启用Nginx模块
- 指定要采集的Nginx日志文件路径(可选)
- 设置Filebeat assets
- 以服务方式运行Filebeat
- 在Kibana中可视化数据
- 查看Filebeat日志数据
- 查看Metricbeat指标数据
- 定制Kibana Dashboard
- 参考文档
使用Metricbeat和Filebeat监控Nginx性能指标和日志
前言
本文描述了通过Metricbeat和Filebeat来分别监控Nginx的性能指标和日志,并将数据存储到Elasticsearch中,再通过Kibana进行可视化数据分析。
前置条件:
CentOS7 x86_64
Elastic Stack:
- 已以rpm方式安装Elastichsearch 7.4.0
- 已以rpm方式安装Kibana 7.4.0
如果还没有安装Elasticsearch和Kibana,可以参考:
- 在CentOS 7上以rpm方式安装Elasticsearch 7.4.0
- 在CentOS7上以rpm方式安装Kibana 7.4.0
目标机器:
- 已以rpm方式安装Nginx
安装和配置配置Metricbeat
参见:
- https://www.elastic.co/guide/en/beats/metricbeat/current/metricbeat-installation-configuration.html
下载Merticbeat
Metribeat 7.4.0:
- https://www.elastic.co/cn/downloads/past-releases/metricbeat-7-4-0
Metricbeat 最新版:
- https://www.elastic.co/cn/downloads/beats/metricbeat
下载Metricbeat 7.4.0 rpm和sha512:
# download rpm package
wget https://artifacts.elastic.co/downloads/beats/metricbeat/metricbeat-7.4.0-x86_64.rpm# download sha512
wget https://artifacts.elastic.co/downloads/beats/metricbeat/metricbeat-7.4.0-x86_64.rpm.sha512
校验sha512:
sha512sum -c metricbeat-7.4.0-x86_64.rpm.sha512
安装Metricbeat
安装Metricbeat:
rpm -ivh metricbeat-7.4.0-x86_64.rpm
Metricbeat目录
Metricbeat目录:
参见:
- https://www.elastic.co/guide/en/beats/metricbeat/current/directory-layout.html
配置目录为/etc/metricbeat
:
metricbeat.yml
为Metricbeat主配置文件。modules.d/*.yml
为Meticbeat module配置文件
程序目录为/usr/share/metricbeat
:
bin
目录为可执行文件目录。
数据目录为/var/lib/metricbeat
,日志目录为/var/log/metricbeat
。
配置连接Elasticsearch和Kibana
如果Metricbeat和Elasticsearch和Kibana安装在同一台机器上,则不需要修改配置。
编辑·/etc/metricbeat/metricbeat.yml
,设置Elasticsearch和Kibana的连接。
修改前:
output.elasticsearch:# Array of hosts to connect to.hosts: ["localhost:9200"]# Optional protocol and basic auth credentials.#protocol: "https"#username: "elastic"#password: "changeme"setup.kibana:# Kibana Host# Scheme and port can be left out and will be set to the default (http and 5601)# In case you specify and additional path, the scheme is required: http://localhost:5601/path# IPv6 addresses should always be defined as: https://[2001:db8::1]:5601#host: "localhost:5601"# Kibana Space ID# ID of the Kibana Space into which the dashboards should be loaded. By default,# the Default Space will be used.#space.id:
启用Nginx stub_status模块
Metricbeat nginx模块监控Nginx时,需要启用Nginx的stub_status模块(默认不开启)。
检查Nginx是否包含了stub_status
模块:
nginx -V 2>&1 | grep --color -- --with-http_stub_status_module
在已有的Nginx配置文件(比如/etc/nginx/nginx.conf
)的server
中添加:
location /nginx_status {stub_status;allow 127.0.0.1; #only allow requests from localhostdeny all; #deny all other hosts }
测试Nginx配置:
nginx -t
重新加载Nginx配置:
nginx -s reload
测试stub_status:
curl http://127.0.0.1/nginx_status
参见:
- https://www.nginx.com/blog/monitoring-nginx/
- https://www.tecmint.com/enable-nginx-status-page/
启用Nginx模块
参见:
- https://www.elastic.co/guide/en/beats/metricbeat/current/metricbeat-module-nginx.html
查看Metricbeat module:
metricbeat modules list
可以看到Metricbeat默认只启用了系统级别的指标数据采集(system
)。
启用Metricbeat nginx module:
metricbeat modules enable nginx
再次运行metricbeat modules list
,可以看到Metricbeat nginx module已经被启用。
Metricbeat nginx module的配置文件为/etc/metricbeat/modules.d/nginx.yml
,修改该配置文件。
修改前:
- module: nginx#metricsets:# - stubstatusperiod: 10s# Nginx hostshosts: ["http://127.0.0.1"]# Path to server status. Default server-status#server_status_path: "server-status"#username: "user"#password: "secret"
修改后:
- module: nginxmetricsets: ["stubstatus"]enabled: trueperiod: 10s# Nginx hostshosts: ["http://127.0.0.1"]# Path to server status. Default server-statusserver_status_path: "nginx_status"
注意:server_status_path
需要和启用Nginx stub_status模块时设置的location保持一致。
测试Metricbeat配置:
metricbeat test config -e
设置Metricbeat assets
设置Metricbeat的Elasticsearch Index template和加载Kibana Dashboard:
# -e 表示把日志输出到控制台
metricbeat setup -e
以服务方式运行Metricbeat
以服务方式运行Metricbeat:
systemctl enable metricbeat
systemctl start metricbeat
查看Metricbeat服务日志:
systemctl status metricbeat -l
可以看到,Metricbeat的启动命令如下:
/usr/share/metricbeat/bin/metricbeat -e -c /etc/metricbeat/metricbeat.yml -path.home /usr/share/metricbeat -path.config /etc/metricbeat -path.data /var/lib/metricbeat -path.logs /var/log/metricbeat
查看Metricbeat服务详细日志:
journalctl -xefu metricbeat
安装和配置Filebeat
参见:
- https://www.elastic.co/guide/en/beats/filebeat/7.9/filebeat-installation-configuration.html
下载Filebeat
Filebeat 7.4.0:
- https://www.elastic.co/cn/downloads/past-releases/filebeat-7-4-0
Filebeat 最新版:
- https://www.elastic.co/cn/downloads/beats/filebeat
下载Filebeat 7.4.0 rpm和sha512:
# download rpm package
wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-7.4.0-x86_64.rpm# download sha512
wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-7.4.0-x86_64.rpm.sha512
校验sha512:
sha512sum -c filebeat-7.4.0-x86_64.rpm.sha512
安装Filebeat
安装Filebeat:
rpm -ivh filebeat-7.4.0-x86_64.rpm
Filebeat目录
Filebeat目录:
参见:
- https://www.elastic.co/guide/en/beats/filebeat/7.9/directory-layout.html
配置目录为/etc/filebeat
:
filebeat.yml
为Filebeat主配置文件。modules.d/*.yml
为Filebeat module配置文件
程序目录为/usr/share/filebeat
:
bin
目录为可执行文件目录。
数据目录为/var/lib/filebeat
,日志目录为/var/log/filebeat
。
配置连接Elasticsearch和Kibana
如果Filebeat和Elasticsearch和Kibana安装在同一台机器上,则不需要修改配置。
编辑·/etc/filebeat/filebeat.yml
,设置Elasticsearch和Kibana的连接。
修改前:
output.elasticsearch:# Array of hosts to connect to.hosts: ["localhost:9200"]# Optional protocol and basic auth credentials.#protocol: "https"#username: "elastic"#password: "changeme"setup.kibana:# Kibana Host# Scheme and port can be left out and will be set to the default (http and 5601)# In case you specify and additional path, the scheme is required: http://localhost:5601/path# IPv6 addresses should always be defined as: https://[2001:db8::1]:5601#host: "localhost:5601"# Kibana Space ID# ID of the Kibana Space into which the dashboards should be loaded. By default,# the Default Space will be used.#space.id:
启用Nginx模块
参见:
- https://www.elastic.co/guide/en/beats/filebeat/current/filebeat-module-nginx.html
查看Filebeat module:
filebeat modules list
启用Filebeat nginx module:
filebeat modules enable nginx
再次运行filebeat modules list
,可以看到Filebeat nginx module已经被启用。
Filebeat nginx module的配置文件为/etc/filebeat/modules.d/nginx.yml
,可以修改该配置文件的access.var.paths
和error.var.paths
来指定要收集的Nginx access logs和error logs。
在CentOS7上,Nginx日志的默认路径为:
- access log:
/var/log/nginx/access.log
- error log:
/var/log/nginx/error.log
指定要采集的Nginx日志文件路径(可选)
编辑/etc/filebeat/modules.d/nginx.yml
,指定要采集的Nginx的日志文件路径,支持glob模糊匹配。
示例:
修改前:
- module: nginx# Access logsaccess:enabled: true# Set custom paths for the log files. If left empty,# Filebeat will choose the paths depending on your OS.#var.paths:# Error logserror:enabled: true# Set custom paths for the log files. If left empty,# Filebeat will choose the paths depending on your OS.#var.paths:
修改后:
- module: nginx# Access logsaccess:enabled: true# Set custom paths for the log files. If left empty,# Filebeat will choose the paths depending on your OS.var.paths: ["/var/log/nginx/*access.log"]# Error logserror:enabled: true# Set custom paths for the log files. If left empty,# Filebeat will choose the paths depending on your OS.var.paths: ["/var/log/nginx/*error.log"]
测试修改后的模块配置文件是否正确:
filebeat test config -e
设置Filebeat assets
设置Filebeat的Elasticsearch Index template和加载Kibana Dashboard:
# -e 表示把日志输出到控制台
filebeat setup -e
以服务方式运行Filebeat
以服务方式运行Filebeat:
systemctl enable filebeat
systemctl start filebeat
查看Filebeat服务日志:
systemctl status filebeat -l
可以看到,Filebeat的启动命令如下:
/usr/share/filebeat -path.config /etc/filebeat -path.data /var/lib/filebeat -path.logs /var/log/filebeat
查看Filebeat服务详细日志:
journalctl -xefu filebeat
在Kibana中可视化数据
查看Filebeat日志数据
在Kibana中
打开Discover菜单,选中
filebeat-*
,可以看到Filebeat采集并经过提取字段的Nginx日志数据。打开Logs菜单,就可以看到Filebeat采集的详细的Nginx日志数据了。
打开Dashboard菜单,输入
nginx
搜索,打开"[Filebeat Nginx] Access and error logs ECS",就可以看到Nginx日志数据的Dashboard了。
查看Metricbeat指标数据
在Kibana中
打开Discover菜单,选中
metricbeat-*
,可以看到Metricbeat采集并经过提取字段的指标数据。打开Dashboard菜单
- 输入
metricbet nginx
搜索,打开"[Metricbeat Nginx] Overview ECS",就可以看到Nginx指标数据的Dashboard了。 - 输入
metricbeat system
搜索,打开"[Metricbeat System] Overview ECS",就可以看到系统指标数据的Dashboard了,并可切换到Host Overview和Containers overview查看指定的主机和进程的指标数据。
- 输入
定制Kibana Dashboard
下一步还需要定制Kibana Dashboard来根据需要展示数据,并进行深入地可视化分析。
参见:
- https://www.elastic.co/guide/en/kibana/7.9/dashboard.html
参考文档
https://www.elastic.co/cn/blog/how-to-monitor-nginx-web-servers-with-the-elastic-stack
https://logz.io/blog/nginx-web-server-monitoring/
使用Metricbeat和Filebeat监控Nginx性能指标和日志相关推荐
- filebeat获取nginx的access日志配置
filebeat获取nginx的access日志配置产生nginx日志的服务器即生产者服务器配置: 拿omp.chinasoft.com举例: 1.nginx.conf主配置文件添加日志格式log_f ...
- 部署filebeat收集nginx日志
搭建filebeat自动发现日志 在上一篇博客我们部署了logstash去读取日志,但是logstash需要消耗的资源较大.在每台客户端安装logstash不现实. Filebeat是一个轻量级的日志 ...
- ELK结合Beats工具的搭建使用(Metricbeat、Filebeat、Topbeat)
ELK之间的合作机制: L(Logstash)作为信息收集者,主要是用来对日志的搜集.分析.过滤,支持大量的数据获取方式,一般工作方式为c/s架构,client端安装在需要收集日志的主机上,serve ...
- Zabbix监控Nginx性能状态
Nginx在生产环境中的应用越来越广泛,所以需要对nginx的性能状态做一些监控,从而发现故障隐患,Ngnx的监控指标可分为:基本活动指标,错误指标,性能指标 监控Nginx思路: 1)首先,要想监控 ...
- ELK集群部署(八)之监控nginx日志
监控nginx日志 103上安装nginx yum install -y nginx启动nginx systemctl start nginx访问 http://192.168.56.103/ 配置n ...
- 手把手教你使用zabbix监控nginx
zabbix监控nginx,多亏了容哥(杨容)的帮忙,为了感谢容哥的帮助,写了这篇文章. 环境介绍: 服务器系统版本:CentOSrelease 6.6 (Final) 内核版本:Linux hk_n ...
- 写了个Python脚本监控nginx进程
写了个Python脚本监控nginx进程 « Xiaoxia[PG] 写了个Python脚本监控nginx进程 接上一文用iptables让SSH服务对陌生人说不.还是有点担心这个学期内,nginx可 ...
- 利用ngxtop实时监控nginx的访问情况
利用ngxtop实时监控nginx的访问情况 关于对nginx web server的实时访问的实时监控问题,我很久之前就想实现的,现在虽有nginx自带的status扩展,但那是全局的,无法细分到v ...
- zabbix监控nginx,PHP-FPM,ELK报警
首先nginx编译安装此模块--with-http_stub_status_module 参考教程如下 Zabbix监控Nginx http://www.ttlsa.com/zabbix/zabbix ...
- 配置 Zabbix 监控 Nginx(Apache、php-fpm)
2019独角兽企业重金招聘Python工程师标准>>> Zabbix 监控 Nginx 使用 zabbix 监控 nginx,实际上是通过 nginx 自带 status 模块来获取 ...
最新文章
- [置顶] 自己动手实现OpenGL-OpenGL原来如此简单(三)
- silverlight控件打印预览
- TCP/IP学习笔记(一)分层模型概述
- Java日志性能那些事
- Cloud for Customer custom BO创建时间随着行项目数量增加而增加的关系
- python中@staticmethod_Python中的@staticmethod和@classmethod的区别
- 在mysql命令行下执行sql文件
- 链表去重 保留第一个元素
- eclipse目录改名,子目录及JAVA文件同步更改
- 电力电气自动计算excel表格大全【共46份】
- js判断数组的六种方法
- Unity中实现3D拾取功能及其原理
- 以太网卡 及 以太网帧
- 批发进销存管理软件,商品分类管理,对商品分类批量价格管理,商品分类导入导出的操作
- 对数组名取地址 a[ ],a
- 【Qt学习】04 信号-槽 子窗口向主窗口传递参数
- 带着红领巾,站在巨人肩膀上登鼻上脸,文化或浮躁?
- 《LoadRunner 12七天速成宝典》—第2章2.4节让代码动起来
- 完结 - 参考文献及附录
- 1071 小赌怡情 (15 分) (C语言)