Prometheus+Grafana监控主机和MySQL数据库

1.环境#

主机:vhost4, vhost5, vhost6
系统:CentOS7
vhost4: MySQL.slave, Prometheus, NodeExporter, MysqlExporter
vhost5: MySQL.master, NodeExporter, MysqlExporter
vhost6: MySQL.slave, NodeExporter, MysqlExporter
Prometheus: v2.24.1
Grafana: v7.4.0
NodeExporter: v1.1.0
MysqlExporter: v0.14.0

2.Prometheus#

2.1 下载压缩包并解压#

1
# ls -al /opt/prometheus/prometheus/prometheus-2.24.1.linux-amd64
1
2
3
4
5
6
7
8
9
10
11
总用量 165812
drwxr-xr-x 5 3434 3434 144 5月 10 16:35 .
drwxr-xr-x 3 root root 84 5月 5 13:51 ..
drwxr-xr-x 2 3434 3434 38 1月 20 2021 console_libraries
drwxr-xr-x 2 3434 3434 173 1月 20 2021 consoles
drwxr-xr-x 13 root root 4096 5月 10 17:00 data
-rw-r--r-- 1 3434 3434 11357 1月 20 2021 LICENSE
-rw-r--r-- 1 3434 3434 3420 1月 20 2021 NOTICE
-rwxr-xr-x 1 3434 3434 89894679 1月 20 2021 prometheus
-rw-r--r-- 1 root root 1324 5月 10 16:35 prometheus.yml
-rwxr-xr-x 1 3434 3434 79870425 1月 20 2021 promtool

2.2 配置#

1
# cat /opt/prometheus/prometheus/prometheus-2.24.1.linux-amd64/prometheus.yml 
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
# my global config
global:
scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
# scrape_timeout is set to the global default (10s).

# Alertmanager configuration
alerting:
alertmanagers:
- static_configs:
- targets:
# - alertmanager:9093

# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
# - "first_rules.yml"
# - "second_rules.yml"

# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
- job_name: 'prometheus'

# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.

static_configs:
- targets: ['localhost:9090']

- job_name: 'node'
static_configs:
- targets: ['vhost4:9100', 'vhost5:9100', 'vhost6:9100']

- job_name: 'mysqld'
static_configs:
- targets: ['vhost4:9104','vhost5:9104','vhost6:9104']

2.3 服务化#

1
# cat /usr/lib/systemd/system/prometheus.service 
1
2
3
4
5
6
7
8
9
10
[Unit]
Description=Prometheus

[Service]
WorkingDirectory=/opt/prometheus/prometheus/prometheus-2.24.1.linux-amd64
ExecStart=/opt/prometheus/prometheus/prometheus-2.24.1.linux-amd64/prometheus --config.file=/opt/prometheus/prometheus/prometheus-2.24.1.linux-amd64/prometheus.yml
User=root

[Install]
WantedBy=multi-user.target

3.Grafana#

3.1 下载安装包并安装#

1
# ls -al /opt/prometheus/grafana/
1
2
3
4
总用量 49732
drwxr-xr-x 2 root root 40 2月 8 2021 .
drwxr-xr-x 6 root root 83 5月 10 16:33 ..
-rw-r--r-- 1 root root 50921720 2月 8 2021 grafana-7.4.0-1.x86_64.rpm
1
# rpm -ivh /opt/prometheus/grafana/grafana-7.4.0-1.x86_64.rpm

3.2 配置#

默认

3.3 服务化#

1
2
# systemctl start grafana-server.service
# systemctl enable grafana-server.service

4.NodeExporter#

4.1 下载压缩包并解压#

1
# ls -al node_exporter/node_exporter-1.1.0.linux-amd64
1
2
3
4
5
6
总用量 18720
drwxr-xr-x 2 root root 56 2月 6 2021 .
drwxr-xr-x 3 root root 88 2月 8 2021 ..
-rw-r--r-- 1 root root 11357 2月 6 2021 LICENSE
-rwxr-xr-x 1 root root 19151814 2月 6 2021 node_exporter
-rw-r--r-- 1 root root 463 2月 6 2021 NOTICE

4.2 配置#

4.3 服务化#

1
# cat /usr/lib/systemd/system/node-exporter.service
1
2
3
4
5
6
7
8
9
[Unit]
Description=NodeExporter

[Service]
ExecStart=/opt/prometheus/node_exporter/node_exporter-1.1.0.linux-amd64/node_exporter
User=root

[Install]
WantedBy=multi-user.target

5.MysqlExporter#

5.1 下载压缩包并解压#

1
# ls -al /opt/prometheus/mysqld_exporter/mysqld_exporter-0.14.0.linux-amd64/
1
2
3
4
5
6
7
总用量 14828
drwxr-xr-x 2 root root 72 5月 10 16:37 .
drwxr-xr-x 3 root root 48 5月 10 16:34 ..
-rw-r--r-- 1 root root 11357 5月 10 16:34 LICENSE
-rw-r--r-- 1 root root 35 5月 10 16:37 my.cnf
-rwxr-xr-x 1 root root 15163162 5月 10 16:34 mysqld_exporter
-rw-r--r-- 1 root root 65 5月 10 16:34 NOTICE

5.2 配置#

1
# cat /opt/prometheus/mysqld_exporter/mysqld_exporter-0.14.0.linux-amd64/my.cnf 
1
2
3
[client]
user=数据库账号
password=数据库密码

按需还可以指定 host 和 port

5.3 服务化#

1
# cat /usr/lib/systemd/system/mysqld-exporter.service 
1
2
3
4
5
6
7
8
9
[Unit]
Description=MysqldExporter

[Service]
ExecStart=/opt/prometheus/mysqld_exporter/mysqld_exporter-0.14.0.linux-amd64/mysqld_exporter --config.my-cnf=/opt/prometheus/mysqld_exporter/mysqld_exporter-0.14.0.linux-amd64/my.cnf
User=root

[Install]
WantedBy=multi-user.target

6.资料#