各中间件exporter对接到prometheus的方式

监控原理:使用prometheus监控各类中间件,则需要部署对应中间件的exporter,再将exporter暴露的指标地址告知prometheus即可
首先我们需要知道如何将暴露的地址告知prometheus,主要有如下集中方式:

  1. 修改prometheus配置文件的方式
  2. 修改静态文件的方式
  3. 使用httpd方式
  4. 使用注册中心的方式

这里只提到修改prometheus配置文件的方式和静态文件的方式(其他方式可以阅读prometheus的官网)

修改prometheus配置文件如下内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
scrape_configs:
- job_name: 'node_exporter'
scrape_interval: 8s # 采集时间间隔
file_sd_configs:
- files:
- node_exporter.json # 指定静态配置文件
refresh_interval: 10s # 刷新配置文件

- job_name: 'redis_exporter'
scrape_interval: 8s # 采集时间间隔
file_sd_configs:
- files:
- redis_exporter.json # 指定静态配置文件
refresh_interval: 10s # 刷新配置文件

- job_name: 'mysql_exporter'
scrape_interval: 8s # 采集时间间隔
file_sd_configs:
- files:
- mysql_exporter.json # 指定静态配置文件
refresh_interval: 10s # 刷新配置文件

- job_name: 'zookeeper_exporter'
scrape_interval: 8s # 采集时间间隔
file_sd_configs:
- files:
- zookeeper_exporter.json # 指定静态配置文件
refresh_interval: 10s # 刷新配置文件

# ....以此类推,得出其他中间件exporter的配置

# 其中特殊exporter的配置如下
- job_name: 'blackbox-exporter'
metrics_path: /probe
params:
module: [icmp] # 网络探测类型
static_configs:
# 探测目标节点
- targets: ['172.18.40.xxx','172.18.40.xx1','172.18.40.xx3','172.18.40.xx4','172.18.40.xx5']
labels:
group: '172.26.42.123' # 自定义标签
relabel_configs:
- source_labels: [__address__]
target_label: __param_target
- source_labels: [__param_target]
target_label: instance
- target_label: __address__
replacement: 172.26.42.123:9115 # blackbox-exporter 真实部署的服务器地址 ip:port

而这些静态配置文件的格式如下:这里以node_exporter.json为例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[
{
"targets": [
"172.18.40.xxx:9100"
],
"labels": {
"acc": "acc1/acc2"
}
},
{
"targets": [
"172.18.40.xx1:9100"
],
"labels": {
"acc": "acc2/acc3"
}
}
]

以上将部署的exporter服务告知prometheus的配置方式,配置好后,重启prometheus即可。

部署各类中间件的exporter

Mysql-exporter

  • 部署启动 首先填写mysql相关的配置,保存在 my.cnf文件中
    1
    2
    3
    4
    5
    [client]
    host=127.0.0.1
    port=3306
    user=exporter
    password=123456
    然后,启动mysql-exporter,并通过config.my-cnf指定上面的配置文件
    1
    2
    3
    4
    5
    # 默认参数启动
    ./mysqld_exporter --config.my-cnf=/usr/local/prometheus/mysqld_exporter-0.12.1.linux-amd64/my.cnf

    # 详细的参数
    ./mysqld_exporter --web.listen-address=0.0.0.0:9104 --config.my-cnf=/usr/local/prometheus/mysqld_exporter-0.12.1.linux-amd64/my.cnf --collect.slave_status --collect.slave_hosts --log.level=error --collect.info_schema.processlist --collect.info_schema.innodb_metrics --collect.info_schema.innodb_tablespaces --collect.info_schema.innodb_cmp --collect.info_schema.innodb_cmpmem

redis-exporter

部署启动

1
./redis_exporter  -redis.addr 127.0.0.1:6379  -redis.password 123456 -web.listen-address 0.0.0.0:9121

nginx-prometheus-exporter

前提条件

使用nginx-prometheus-exporter监控nginx时,该nginx必须要开启stub_status功能

开启 NGINX stub_status 功能

  1. 开源 Nginx 提供一个简单页面用于展示状态数据,该页面由 tub_status 模块提供。执行以下命令检查 Nginx 是否已经开启了该模块:
    1
    nginx -V 2>&1 | grep -o with-http_stub_status_module
    • 如果在终端中输出 with-http_stub_status_module ,则说明 Nginx 已启用 tub_status 模块。
    • 如果未输出任何结果,则可以使用 -with-http_stub_status_module 参数从源码重新配置编译一个 Nginx。示例如下:
      1
      2
      3
      4
      5
      ./configure \
      … \
      --with-http_stub_status_module
      make
      sudo make install
  2. 确认 stub_status 模块启用之后,修改 Nginx 的配置文件指定 status 页面的 URL。示例如下:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    server {
    location /nginx_status {
    stub_status;

    access_log off;
    allow 127.0.0.1;
    deny all;
    }
    }
  3. 检查并重新加载 nginx 的配置使其生效。
    1
    2
    nginx -t
    nginx -s reload
  4. 完成上述步之后,可以通过配置的 URL 查看 Nginx 的指标:
    1
    2
    3
    4
    Active connections: 45
    server accepts handled requests
    1056958 1156958 4491319
    Reading: 0 Writing: 25 Waiting : 7

部署启动

1
nginx-prometheus-exporter -nginx.scrape-uri=http://<nginx>:8080/stub_status

zookeeper-exporter

启动

1
./zookeeper_exporter -zookeeper 127.0.0.1:2181 -bind-addr :9143

elasticsearch-exporter

1
2
3
4
5
6
7
8
## 参数说明:
--es.uri     默认http://localhost:9200,连接到的Elasticsearch节点的地址(主机和端口)。 这可以是本地节点(例如localhost:9200),也可以是远程Elasticsearch服务器的地址
--es.all 默认flase,如果为true,则查询群集中所有节点的统计信息,而不仅仅是查询我们连接到的节点。
--es.cluster_settings 默认flase,如果为true,请在统计信息中查询集群设置
--es.indices 默认flase,如果为true,则查询统计信息以获取集群中的所有索引。
--es.indices_settings 默认flase,如果为true,则查询集群中所有索引的设置统计信息。
--es.shards 默认flase,如果为true,则查询集群中所有索引的统计信息,包括分片级统计信息(意味着es.indices = true)。
--es.snapshots 默认flase,如果为true,则查询集群快照的统计信息。

部署启动

1
2
3
4
5
# es集群1:10.xxx.xxx.10:9200
./elasticsearch_exporter --es.all --es.indices --es.cluster_settings --es.indices_settings --es.shards --es.snapshots --es.timeout=10s --web.listen-address=":9114" --web.telemetry-path="/metrics" --es.uri http://10.xxx.xxx.10:9200

# 如果es集群开启了x-pack验证则可以使用如下
./elasticsearch_exporter --es.all --es.indices --es.cluster_settings --es.indices_settings --es.shards --es.snapshots --es.uri http://用户名:密码@111.xxx.xxx.6:9200

kafka-exporter

部署启动

1
./kafka_exporter --web.listen-address=9308 --kafka.server=kafka:9092 [--kafka.server=another-server ...]

Grafana Dashboard ID: 7589, name: Kafka Exporter Overview.

postgresql-exporter

部署启动

1
2
3
4
5
6
7
./postgres_exporter <flags>

# 举例
# 设置环境变量
DATA_SOURCE_NAME="postgresql://postgres:password@localhost:5432/postgres?sslmode=disable"
# 启动,指定端口
./postgres_exporter --web.listen-address=9187

Flags

  • help Show context-sensitive help (also try –help-long and –help-man).
  • web.listen-address Address to listen on for web interface and telemetry. Default is :9187.
  • web.telemetry-path Path under which to expose metrics. Default is /metrics.
  • disable-default-metrics Use only metrics supplied from queries.yaml via -extend.query-path.
  • disable-settings-metrics Use the flag if you don’t want to scrape pg_settings.
  • auto-discover-databases Whether to discover the databases on a server dynamically.
  • extend.query-path Path to a YAML file containing custom queries to run. Check out [queries.yaml](https://github.com/prometheus-community/postgres_exporter/blob/master/queries.yaml) for examples of the format.
  • dumpmaps Do not run - print the internal representation of the metric maps. Useful when debugging a custom queries file.
  • constantLabels Labels to set in all metrics. A list of label=value pairs, separated by commas.
  • version Show application version.
  • exclude-databases A list of databases to remove when autoDiscoverDatabases is enabled.
  • include-databases A list of databases to only include when autoDiscoverDatabases is enabled.
  • log.level Set logging level: one of debuginfowarnerror.
  • log.format Set the log format: one of logfmtjson.
  • web.config.file Configuration file to use TLS and/or basic authentication. The format of the file is described in the exporter-toolkit repository.

Environment Variables

The following environment variables configure the exporter:

  • DATA_SOURCE_NAME the default legacy format. Accepts URI form and key=value form arguments. The URI may contain the username and password to connect with.
  • DATA_SOURCE_URI an alternative to DATA_SOURCE_NAME which exclusively accepts the hostname without a username and password component. For example, my_pg_hostname or my_pg_hostname?sslmode=disable.
  • DATA_SOURCE_URI_FILE The same as above but reads the URI from a file.
  • DATA_SOURCE_USER When using DATA_SOURCE_URI, this environment variable is used to specify the username.
  • DATA_SOURCE_USER_FILE The same, but reads the username from a file.
  • DATA_SOURCE_PASS When using DATA_SOURCE_URI, this environment variable is used to specify the password to connect with.
  • DATA_SOURCE_PASS_FILE The same as above but reads the password from a file.
  • PG_EXPORTER_WEB_LISTEN_ADDRESS Address to listen on for web interface and telemetry. Default is :9187.
  • PG_EXPORTER_WEB_TELEMETRY_PATH Path under which to expose metrics. Default is /metrics.
  • PG_EXPORTER_DISABLE_DEFAULT_METRICS Use only metrics supplied from queries.yaml. Value can be true or false. Default is false.
  • PG_EXPORTER_DISABLE_SETTINGS_METRICS Use the flag if you don’t want to scrape pg_settings. Value can be true or false. Default is false.
  • PG_EXPORTER_AUTO_DISCOVER_DATABASES Whether to discover the databases on a server dynamically. Value can be true or false. Default is false.
  • PG_EXPORTER_EXTEND_QUERY_PATH Path to a YAML file containing custom queries to run. Check out [queries.yaml](https://github.com/prometheus-community/postgres_exporter/blob/master/queries.yaml) for examples of the format.
  • PG_EXPORTER_CONSTANT_LABELS Labels to set in all metrics. A list of label=value pairs, separated by commas.
  • PG_EXPORTER_EXCLUDE_DATABASES A comma-separated list of databases to remove when autoDiscoverDatabases is enabled. Default is empty string.
  • PG_EXPORTER_INCLUDE_DATABASES A comma-separated list of databases to only include when autoDiscoverDatabases is enabled. Default is empty string, means allow all.
  • PG_EXPORTER_METRIC_PREFIX A prefix to use for each of the default metrics exported by postgres-exporter. Default is pg

bind-exporter

部署启动

1
2
3
4
5
6
7
./bind_exporter \
--bind.pid-file=/var/run/named/named.pid \
--bind.timeout=20s \
--web.listen-address=0.0.0.0:9153 \
--web.telemetry-path=/metrics \
--bind.stats-url=http://localhost:8053/ \
--bind.stats-groups=server,view,tasks