文章目录
- 一、 nginx-prometheus-exporter
- 1 nginx 配置
- 1.1 Nginx 模块支持
- 1.2 Nginx 配置文件配置
- 2 部署 nginx-prometheus-exporter
- 2.1 二进制方式部署
- 2.1.1 解压部署
- 2.1.2 配置 systemd
- 2.1.3 添加 prometheus 的配置
- 2.1.4 Dashborad
- 2.2 docker-compose 方式部署
- 3 可配置的指标
- 3.1 通用指标
- 二、mysql-exporter
- 1 部署
- 2 配置
- 2.1 方式一的配置
- 2.2 方式二的配置
- 3 监控参数
- 三、 reids-exporter
- 1 部署
- 1.1 下载二进制包
- 2 配置监控集群、主从模式
- 2.1 配置 systemd
- 2.2 配置到 Prometheus
- 3 配置监控哨兵模式
- 3.1 配置 systemd
- 3.2 配置 prometheus
- 4 Dashboard
- 四、rabbitmq-exporter
- 五、postgresql-exporter
- 自动发现数据库
一、 nginx-prometheus-exporter
1 nginx 配置
1.1 Nginx 模块支持
nginx 安装的时候需要有 nginx 的状态模块: stub_status
可通过如下命令检查
nginx -V 2>&1 | grep -o with-http_stub_status_module
1.2 Nginx 配置文件配置
添加如下配置到自己 nginx 的配置文件中
server {listen 9010;location /metrics {stub_status on;access_log off;error_log off;allow 127.0.0.1;deny all;}
}
2 部署 nginx-prometheus-exporter
2.1 二进制方式部署
下载不同版本: https://github.com/nginxinc/nginx-prometheus-exporter/releases
下载 Linux 的 X86 版本
curl -o nginx-prometheus-exporter_0.11.0_linux_amd64.tar.gz -L https://github.com/nginxinc/nginx-prometheus-exporter/releases/download/v0.11.0/nginx-prometheus-exporter_0.11.0_linux_amd64.tar.gz
2.1.1 解压部署
tar -xf nginx-prometheus-exporter_0.11.0_linux_amd64.tar.gz nginx-prometheus-exporter
mv nginx-prometheus-exporter /usr/local/bin/
2.1.2 配置 systemd
nginx-exporter.service
[Unit]
Description=NGINX Prometheus Exporter
Requires=nginx_exporter.socket[Service]
User=nginx_exporter
ExecStart=/usr/local/bin/nginx-prometheus-exporter -nginx.scrape-uri="http://127.0.0.1:9010/metrics"[Install]
WantedBy=multi-user.target
2.1.3 添加 prometheus 的配置
- job_name: "nginx"scrape_interval: 5sstatic_configs:- targets:- "<nginx-prometheus-exporter 的IP>:9113"labels:group: 'nginxs'
2.1.4 Dashborad
https://github.com/nginxinc/nginx-prometheus-exporter/blob/main/grafana/dashboard.json
2.2 docker-compose 方式部署
version: "3.9"
services:nginx-exporter:image: bitnami/nginx-exporter:0.11.0environment:# nginx 服务暴露个监控的 URLSCRAPE_URI: http://127.0.0.1:9010/metricsNGINX_RETRIES: 2NGINX_RETRY_INTERVAL: 2srestart: alwaysnetwork_mode: "host"expose:- "9113"
3 可配置的指标
3.1 通用指标
Name | Type | Description | Labels |
---|---|---|---|
nginxexporter_build_info | Gauge | 显示导出程序内部版本信息 | gitCommit, version |
nginx_up | Gauge | 显示上一次获取到的nginx的状态:1表示获取成功,0表示获取失败 | [] |
二、mysql-exporter
1 部署
docker pull prom/mysqld-exporter:v0.14.0
2 配置
监控方式有两种:
- 方式一:是把
mysqld-exporter
和需要被监控的 mysql 部署在同一台服务器上,这样的话,每个需要被监控的 mysql 都需要同时部署一个mysqld-exporter
。 - 方式二: 是把
mysqld-exporter
部署在一个任意的服务器上,通过对 Prometheus进行相关配置,让这一个mysqld-exporter
去每个需要被监控的 mysql 服务上获取对应的监控信息。这样只需要部署一个mysqld-exproter
2.1 方式一的配置
在需要监控的mysql中,添加如下用户和权限。
如果将 mysqld-exporter
部署在 mysql 服务器上,使用如下内容创建用户和权限。
CREATE USER 'exporter'@'localhost' IDENTIFIED BY 'XXXXXXXX';
GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'exporter'@'localhost';
将如下内容添加到 prometheus.yml
中
- job_name: mysqlstatic_configs:- targets:# mysqld-exporter的IP和端口- "168.2.5.95:9104"- "168.2.5.97:9104"
2.2 方式二的配置
如果将 mysqld-exporter
部署非 mysql 服务器上,使用如下内容创建用户和权限。
CREATE USER 'exporter'@'mysqld-exporter部署所在服务器的IP' IDENTIFIED BY 'XXXXXXXX';
GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'exporter'@'mysqld-exporter部署所在服务器的IP';
在部署包的根目录下创建 .my.cnf
文件,参考如下内容填充文件
这个文件路径可以放在其他地方,启动的时候使用 --config.my-cnf=<path>
指定即可
.my.cnf
[client]
host = mysql1
user = exporter
password = XXXXXXXX# 有几个添加几个
[client.mysql2]
host = mysql2
user = exporter
password = XXXXXXXX
[client.mysql3]
host = mysql3
user = exporter
password = XXXXXXXX
将如下内容添加到 prometheus.yml
中
- job_name: mysqlmetrics_path: /probeparams:auth_module:# 这里需要对应 .my.cnf 中的配置- client- client.mysql2- client.mysql3static_configs:- targets:# 被监控的 mysql 服务器IP和端口- "168.2.5.95:3306"- "168.2.5.97:3306"relabel_configs:- source_labels: [__address__]target_label: __param_target- source_labels: [__param_target]target_label: instance- target_label: __address__# mysqld-exporter的IP和端口replacement: localhost:9104
3 监控参数
监控参数是配置在启动命令后面的参数。
collect.auto_increment.columns 5.1 Collect auto_increment columns and max values from information_schema.
collect.binlog_size 5.1 Collect the current size of all registered binlog files
collect.engine_innodb_status 5.1 Collect from SHOW ENGINE INNODB STATUS.
collect.engine_tokudb_status 5.6 Collect from SHOW ENGINE TOKUDB STATUS.
collect.global_status 5.1 Collect from SHOW GLOBAL STATUS (Enabled by default)
collect.global_variables 5.1 Collect from SHOW GLOBAL VARIABLES (Enabled by default)
collect.heartbeat 5.1 Collect from heartbeat.
collect.heartbeat.database 5.1 Database from where to collect heartbeat data. (default: heartbeat)
collect.heartbeat.table 5.1 Table from where to collect heartbeat data. (default: heartbeat)
collect.heartbeat.utc 5.1 Use UTC for timestamps of the current server (pt-heartbeat is called with --utc). (default: false)
collect.info_schema.clientstats 5.5 If running with userstat=1, set to true to collect client statistics.
collect.info_schema.innodb_metrics 5.6 Collect metrics from information_schema.innodb_metrics.
collect.info_schema.innodb_tablespaces 5.7 Collect metrics from information_schema.innodb_sys_tablespaces.
collect.info_schema.innodb_cmp 5.5 Collect InnoDB compressed tables metrics from information_schema.innodb_cmp.
collect.info_schema.innodb_cmpmem 5.5 Collect InnoDB buffer pool compression metrics from information_schema.innodb_cmpmem.
collect.info_schema.processlist 5.1 Collect thread state counts from information_schema.processlist.
collect.info_schema.processlist.min_time 5.1 Minimum time a thread must be in each state to be counted. (default: 0)
collect.info_schema.query_response_time 5.5 Collect query response time distribution if query_response_time_stats is ON.
collect.info_schema.replica_host 5.6 Collect metrics from information_schema.replica_host_status.
collect.info_schema.tables 5.1 Collect metrics from information_schema.tables.
collect.info_schema.tables.databases 5.1 The list of databases to collect table stats for, or ‘*’ for all.
collect.info_schema.tablestats 5.1 If running with userstat=1, set to true to collect table statistics.
collect.info_schema.schemastats 5.1 If running with userstat=1, set to true to collect schema statistics
collect.info_schema.userstats 5.1 If running with userstat=1, set to true to collect user statistics.
collect.mysql.user 5.5 Collect data from mysql.user table
collect.perf_schema.eventsstatements 5.6 Collect metrics from performance_schema.events_statements_summary_by_digest.
collect.perf_schema.eventsstatements.digest_text_limit 5.6 Maximum length of the normalized statement text. (default: 120)
collect.perf_schema.eventsstatements.limit 5.6 Limit the number of events statements digests by response time. (default: 250)
collect.perf_schema.eventsstatements.timelimit 5.6 Limit how old the ‘last_seen’ events statements can be, in seconds. (default: 86400)
collect.perf_schema.eventsstatementssum 5.7 Collect metrics from performance_schema.events_statements_summary_by_digest summed.
collect.perf_schema.eventswaits 5.5 Collect metrics from performance_schema.events_waits_summary_global_by_event_name.
collect.perf_schema.file_events 5.6 Collect metrics from performance_schema.file_summary_by_event_name.
collect.perf_schema.file_instances 5.5 Collect metrics from performance_schema.file_summary_by_instance.
collect.perf_schema.file_instances.remove_prefix 5.5 Remove path prefix in performance_schema.file_summary_by_instance.
collect.perf_schema.indexiowaits 5.6 Collect metrics from performance_schema.table_io_waits_summary_by_index_usage.
collect.perf_schema.memory_events 5.7 Collect metrics from performance_schema.memory_summary_global_by_event_name.
collect.perf_schema.memory_events.remove_prefix 5.7 Remove instrument prefix in performance_schema.memory_summary_global_by_event_name.
collect.perf_schema.tableiowaits 5.6 Collect metrics from performance_schema.table_io_waits_summary_by_table.
collect.perf_schema.tablelocks 5.6 Collect metrics from performance_schema.table_lock_waits_summary_by_table.
collect.perf_schema.replication_group_members 5.7 Collect metrics from performance_schema.replication_group_members.
collect.perf_schema.replication_group_member_stats 5.7 Collect metrics from performance_schema.replication_group_member_stats.
collect.perf_schema.replication_applier_status_by_worker 5.7 Collect metrics from performance_schema.replication_applier_status_by_worker.
collect.slave_status 5.1 Collect from SHOW SLAVE STATUS (Enabled by default)
collect.slave_hosts 5.1 Collect from SHOW SLAVE HOSTS
collect.sys.user_summary 5.7 Collect metrics from sys.x$user_summary (disabled by default).
三、 reids-exporter
1 部署
github 地址 https://github.com/oliver006/redis_exporter
1.1 下载二进制包
不同版本
https://github.com/oliver006/redis_exporter/releases
Liunx X86_64
curl -o redis-exporter.tar.gz -L https://github.com/oliver006/redis_exporter/releases/download/v1.52.0/redis_exporter-v1.52.0.linux-amd64.tar.gztar -xf redis-exporter.tar.gz redis_exporter-v1.52.0.linux-amd64/redis_exportermv redis_exporter-v1.52.0.linux-amd64/redis_exporter /usr/local/bin/
2 配置监控集群、主从模式
说明:
一般生产环境都是为 redis 配置密码,如果是高版本 5.x.x 以上,主从、哨兵、集群模式下都支持配置密码。
但是, 哨兵模式下配置了认证密码(不是哨兵连接主从redis的密码),要求 spring-boot-starter-data-redsi
的版本更新到 2.3.4,否则无法连接到设置了密码认证的 哨兵。
所以下面就两种情况下如何配置进行说明。
2.1 配置 systemd
[Unit]
Description=The Redis exporter 监控程序
After=network-online.target
Wants=network-online.target[Service]
# 集群模式
ExecStart=/usr/local/bin/redis_exporter -is-cluster -redis.addr node1-redis-host:6379 -redis.password=密码# 主从模式, 去掉 -is-cluster
#ExecStart=/usr/local/bin/redis_exporter -redis.addr master-redis-host:6379 -redis.password=密码KillSignal=SIGQUIT
Restart=always
RestartPreventExitStatus=1 6 SIGABRT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true
LimitNOFILE=1048576
LimitNPROC=1048576[Install]
WantedBy=multi-user.target
-is-cluster
如果监控的是集群模式的 redis 需要加上, redis 主从模式不需要。-redis.addr
可以配置被监控的redis中的任意一台。-redis.password=密码
如果有密码认证,加上,密码部分不要用任何单引号或双引号(会被判断为密码的一部分)。redsi-user
新版本 redis 支持。
可选
-include-system-metrics
会多一个指标redis_total_system_memory_bytes
占用系统的总内存
2.2 配置到 Prometheus
scrape_configs:## 配置获取多个 redis 服务监控指标- job_name: 'redis_exporter_targets'static_configs:- targets:# 集群模式- redis://node1-redis-host:7001- redis://node2-redis-host:7001- redis://node3-redis-host:7001- redis://node4-redis-host:7001- redis://node5-redis-host:7001- redis://node6-redis-host:7001# - targets:# 主从模式# - redis://master-redis-host:6379# - redis://slave1-redis-host:6379# - redis://slave2-redis-host:6379metrics_path: /metricsrelabel_configs:- source_labels: [__address__]target_label: __param_target- source_labels: [__param_target]target_label: instance- target_label: __address__replacement: <<REDIS-EXPORTER-HOSTNAME>>:9121
3 配置监控哨兵模式
3.1 配置 systemd
[Unit]
Description=The Redis Sentinel Exporter 监控程序
After=network-online.target
Wants=network-online.target[Service]
ExecStart=/usr/local/bin/redis_exporter -redis.addr sentinel1-redis-host:27001 -web.listen-address=0.0.0.0:9122KillSignal=SIGQUITRestart=alwaysRestartPreventExitStatus=1 6 SIGABRTTimeoutStopSec=5
KillMode=process
PrivateTmp=true
LimitNOFILE=1048576
LimitNPROC=1048576[Install]
WantedBy=multi-user.target
-web.listen-address
自定义监听地址:端口
3.2 配置 prometheus
scrape_configs:## 配置获取多个 redis 服务监控指标- job_name: 'sentinel_redis_exporter'static_configs:- targets:# 集群模式- redis://sentinel1-redis-host:27001- redis://sentinel2-redis-host:27001- redis://sentinel3-redis-host:27001metrics_path: /metricsrelabel_configs:- source_labels: [__address__]target_label: __param_target- source_labels: [__param_target]target_label: instance- target_label: __address__replacement: <<REDIS-SENTINEL-EXPORTER-HOSTNAME>>:9122
4 Dashboard
如下连接中有多个可供选择
https://github.com/oliver006/redis_exporter/tree/master/contrib
我选择的是 https://github.com/oliver006/redis_exporter/blob/master/contrib/grafana_prometheus_redis_dashboard.json
效果图
dashborad 源码
{"__inputs": [{"name": "DS_PROMETHEUS","label": "Prometheus","description": "","type": "datasource","pluginId": "prometheus","pluginName": "Prometheus"}],"__requires": [{"type": "grafana","id": "grafana","name": "Grafana","version": "3.1.1"},{"type": "panel","id": "graph","name": "Graph","version": ""},{"type": "datasource","id": "prometheus","name": "Prometheus","version": "1.0.0"},{"type": "panel","id": "singlestat","name": "Singlestat","version": ""}],"annotations": {"list": [{"builtIn": 1,"datasource": "-- Grafana --","enable": true,"hide": true,"iconColor": "rgba(0, 211, 255, 1)","name": "Annotations & Alerts","type": "dashboard"}]},"description": "Redis Dashboard for Prometheus Redis Exporter 1.x","editable": true,"gnetId": 763,"graphTooltip": 1,"id": null,"iteration": 1583850456553,"links": [],"panels": [{"cacheTimeout": null,"colorBackground": false,"colorValue": false,"colors": ["rgba(245, 54, 54, 0.9)","rgba(237, 129, 40, 0.89)","rgba(50, 172, 45, 0.97)"],"datasource": "${DS_PROMETHEUS}","decimals": 0,"editable": true,"error": false,"format": "s","gauge": {"maxValue": 100,"minValue": 0,"show": false,"thresholdLabels": false,"thresholdMarkers": true},"gridPos": {"h": 7,"w": 3,"x": 0,"y": 0},"id": 9,"interval": null,"isNew": true,"links": [],"mappingType": 1,"mappingTypes": [{"name": "value to text","value": 1},{"name": "range to text","value": 2}],"maxDataPoints": 100,"nullPointMode": "connected","nullText": null,"options": {},"postfix": "","postfixFontSize": "50%","prefix": "","prefixFontSize": "50%","rangeMaps": [{"from": "null","text": "N/A","to": "null"}],"sparkline": {"fillColor": "rgba(31, 118, 189, 0.18)","full": false,"lineColor": "rgb(31, 120, 193)","show": true},"tableColumn": "","targets": [{"expr": "max(max_over_time(redis_uptime_in_seconds{instance=~\"$instance\"}[$__interval]))","format": "time_series","interval": "","intervalFactor": 2,"legendFormat": "","metric": "","refId": "A","step": 1800}],"thresholds": "","title": "Max Uptime","type": "singlestat","valueFontSize": "70%","valueMaps": [{"op": "=","text": "N/A","value": "null"}],"valueName": "current"},{"cacheTimeout": null,"colorBackground": false,"colorValue": false,"colors": [