监控利器之Prometheus服务监控(三)-上篇
接上篇监控利器之Prometheus组件安装(二),本篇主要讲解通过prometheus监控常用的服务。
1. 监控mysql数据库
1.1 下载监控插件
wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.12.1/mysqld_exporter-0.12.1.linux-amd64.tar.gz
1.2 解压
cd /home/monitor
tar -xf mysqld_exporter-0.12.1.linux-amd64.tar.gz
mv mysqld_exporter-0.12.1.linux-amd64 mysqld_exporter
chown -R root:root /home/monitor/mysqld_exporter
1.3 配置mysql的监控账号
cd /home/monitor/node_exporters
vi .my.cnf
[client]
user=root
password=123456
host=localhost
port=3306
#sock="/tmp/mysql_3306.sock" #如果数据库需要sock文件就配置上,否则不需要此配置
1.4 启动node_exporters(默认监听9104端口)
cd /usr/local/prometheus/mysql_exporters
nohup ./mysqld_exporter --config.my-cnf="/usr/local/prometheus/node_exporters/.my.cnf" &
1.5 修改promethus配置文件,重载服务并验证数据
1)修改promethus配置文件
- job_name: mysql_monitor
static_configs:
- targets: ['mysql_exporters的ip地址:9104']
配置中的targets可以写多个被监控端的api接口地址,建议单独写更清晰些
2)重载服务并验证数据
curl -XPOST http://192.168.16.115:9090/-/reload
curl http://192.168.16.115:9104/metrics #验证接口数据
2. 监控zk集群
2.1 下载监控插件
git项目地址:https://github.com/danielqsj/kafka_exporter
cd /usr/local/src
wget https://github.com/danielqsj/kafka_exporter/releases/download/v1.2.0/kafka_exporter-1.2.0.linux-amd64.tar.gz
2.2 解压
tar -xf kafka_exporter-1.2.0.linux-amd64.tar.gz
mv kafka_exporter-1.2.0.linux-amd64 kafka_exporter
chown -R root:root kafka_exporter
2.3 启动并验证
cd /usr/local/src/kafka_exporter
nohup ./kafka_exporter --web.listen-address="192.168.16.18:4222" --kafka.server=192.168.16.16:9092 >> ./nohup.out 2>&1 &
curl http://192.168.16.16:4222/metrics #查看返回值即可
2.4 修改promethus配置文件,以file_sd_config的方式添加zk服务
1)创建zk的file_sd_config配置文件
cd /home/monitor/prometheus/conf.d/
cat zk_node.json
[
{
"labels": {
"desc": "zk_16_16",
"group": "zookeeper",
"host_ip": "192.168.16.16",
"hostname": "wg-16-16"
},
"targets": [
"192.168.16.16:4221"
]
},
{
"labels": {
"desc": "zk_16_17",
"group": "zookeeper",
"host_ip": "192.168.16.17",
"hostname": "wg-16-17"
},
"targets": [
"192.168.16.17:4221"
]
},
{
"labels": {
"desc": "zk_16_18",
"group": "zookeeper",
"host_ip": "192.168.16.18",
"hostname": "wg-16-18"
},
"targets": [
"192.168.16.18:4221"
]
}
]
2)在promethus的配置文件中添加以下配置
cat prometheus.yml
...
...
job_name: 'zk_node'
scrape_interval: 2m
scrape_timeout: 120s
static_configs:
file_sd_configs:
files:
/home/monitor/prometheus/conf.d/zk_node.json
honor_labels: true
...
...
2.5 重载promethus服务,web端验证
curl -XPOST http://192.168.16.115:9090/-/reload #重载promethus服务
web端验证
3. 监控kafka集群
3.1 下载监控插件
git项目地址:https://github.com/danielqsj/kafka_exporter
cd /usr/local/src
wget https://github.com/danielqsj/kafka_exporter/releases/download/v1.2.0/kafka_exporter-1.2.0.linux-amd64.tar.gz
3.2 解压
tar -xf kafka_exporter-1.2.0.linux-amd64.tar.gz
mv kafka_exporter-1.2.0.linux-amd64 kafka_exporter
chown -R root:root kafka_exporter
3.3 启动并验证
cd /usr/local/src/kafka_exporter
nohup ./kafka_exporter --web.listen-address="192.168.16.18:4222" --kafka.server=192.168.16.16:9092 >> ./nohup.out 2>&1 &
curl http://192.168.16.16:4222/metrics #查看返回值即可
3.4 通过promethus服务端以file_sd_config的方式添加kafka服务
1)创建kafka的file_sd_config配置文件
cd /home/monitor/prometheus/conf.d/
cat kafka_node.json
[
{
"labels": {
"desc": "kafka_16_16",
"group": "kafka",
"host_ip": "192.168.16.16",
"hostname": "wg-16-16"
},
"targets": [
"192.168.16.16:4222"
]
},
{
"labels": {
"desc": "kafka_16_17",
"group": "kafka",
"host_ip": "192.168.16.17",
"hostname": "wg-16-17"
},
"targets": [
"192.168.16.17:4222"
]
},
{
"labels": {
"desc": "kafka_16_18",
"group": "kafka",
"host_ip": "192.168.16.18",
"hostname": "wg-16-18"
},
"targets": [
"192.168.16.18:4222"
]
}
]
2)在promethus的配置文件中添加以下配置
在promethus的配置文件中添加以下配置
cat prometheus.yml
...
...
job_name: 'kafka_node'
scrape_interval: 2m
scrape_timeout: 120s
static_configs:
file_sd_configs:
files:
/home/monitor/prometheus/conf.d/kafka_node.json
honor_labels: true
...
...
3.5 重载promethus服务,web端验证
curl -XPOST http://192.168.16.115:9090/-/reload #重载promethus服务
web端验证
4. 监控nginx
4.1 简介
用Prometheus进行nginx的监控可以自动的对相关server_name和upstream进行监控,你也可以自定义Prometheus的数据标签,实现对不同机房和不同项目的nginx进行监控。
监控Nginx主要用到以下两个模块:
nginx-module-vts:Nginx virtual host traffic status module,Nginx的监控模块,能够提供JSON格式的数据产出。
nginx-vts-exporter:Simple server that scrapes Nginx vts stats and exports them via HTTP for Prometheus consumption。主要用于收集Nginx的监控数据,并给Prometheus提供监控接口,默认端口号9913。
Prometheus:监控Nginx-vts-exporter提供的Nginx数据,并存储在时序数据库中,可以使用PromQL对时序数据进行查询和聚合。
4.2 安装nginx-module-vts模块
git项目地址:https://github.com/vozlt/nginx-module-vts
1)首先查看nginx安装了那些模块
/usr/local/nginx/sbin/nginx -V
2)下载nginx-module-vts模块,并添加模块到nginx
cd /root
git clone git://github.com/vozlt/nginx-module-vts.git
cd /root/nginx-1.14.1
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_flv_module --with-http_gzip_static_module --with-stream --add-module=/root/nginx-module-vts
make #编译,不要make install,不然会覆盖
3)替换nginx二进制文件
mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
cp objs/nginx /usr/local/nginx/sbin/
4)修改nginx.conf配置,验证安装是否成功
http {
...
vhost_traffic_status_filter_by_host on;
...
server {
location /status {
vhost_traffic_status_display;
vhost_traffic_status_display_format html;
}
}
案例配置:由于我的80端口占用,nginx的端口为8080
配置解析:
1> 打开vhost过滤
vhost_traffic_status_filter_by_host on;
开启此功能,在Nginx配置有多个server_name的情况下,会根据不同的server_name进行流量的统计,否则默认会把流量全部计算到第一个server_name上。
2> 在不想统计流量的server区域禁用vhost_traffic_status,配置示例
server {
...
vhost_traffic_status off;
...
}
假如nginx没有规范配置server_name或者无需进行监控的server上,那么建议在此vhost上禁用统计监控功能。否则会出现"127.0.0.1",hostname等的域名监控信息。
5)验证
http://192.168.16.115:8080/status
4.3 安装nginx-vts-exporter
1)下载监控插件
cd /usr/local/src
wget https://github.com/hnlq715/nginx-vts-exporter/releases/download/v0.10.3/nginx-vts-exporter-0.10.3.linux-amd64.tar.gz
2)解压
tar -xf nginx-vts-exporter-0.10.3.linux-amd64.tar.gz
mv nginx-vts-exporter-0.10.3.linux-amd64 nginx-vts-exporter
chown -R root:root nginx-vts-exporter
3)启动并验证
nohup ./nginx-vts-exporter -nginx.scrape_uri=http://192.168.16.115:8080/status/format/json -telemetry.address="192.168.16.115:9913" ./nohup.out 2>&1 &
curl http://192.168.16.115:9913/metrics 查看返回值即可
4.4 通过promethus服务端以file_sd_config的方式添加nginx服务
1)创建nginx_node的file_sd_config配置文件
cd /home/monitor/prometheus/conf.d/
cat nginx_node.json
[
{
"labels": {
"desc": "nginx_16_115",
"group": "nginx",
"host_ip": "192.168.16.115",
"hostname": "wg-16-115"
},
"targets": [
"192.168.16.115:9913"
]
}
]
2)在promethus的配置文件中添加以下配置
cat prometheus.yml
...
...
job_name: 'nginx_node'
scrape_interval: 2m
scrape_timeout: 120s
static_configs:
file_sd_configs:
files:
/home/monitor/prometheus/conf.d/nginx_node.json
honor_labels: true
...
...
4.5 重载promethus服务,web端验证
curl -XPOST http://192.168.16.115:9090/-/reload #重载promethus服务
web端验证
4.6 参考链接
https://blog.csdn.net/qq_25934401/article/details/82968632