MySQL-Prometheus

1. 部署prometheus

mkdir -p /ups/app/monitor/
# 解压
tar -xf prometheus-2.17.0.linux-amd64.tar.gz -C /ups/app/monitor/
# 重命名目录
cd /ups/app/monitor/
mv prometheus-2.17.0.linux-amd64 prometheus
# 创建目录
mkdir -p {bin,logs,rules,config,data}
# 创建用户
groupadd -g 2000 prometheus
useradd -u 2000 -g prometheus -c "Prometheus Server" prometheus
# 修改目录属主
chown -R prometheus.prometheus /ups/app/monitor/prometheus
# 重构目录结构
cd /ups/app/monitor/prometheus
mv prometheus promtool tsdb bin/
mv prometheus.yml config/

# 配置服务启动项
cat > /usr/lib/systemd/system/prometheus.service <<-EOF
[Unit]
Description=https://prometheus.io
After=network.target
After=postgresql-12.service mysql3308.service mysql.service

[Service]
User=prometheus
Group=prometheus

Type=simple

Restart=on-failure
# WorkingDirectory=/ups/app/monitor/prometheus/
# RuntimeDirectory=prometheus
# RuntimeDirectoryMode=0750
ExecStart=/ups/app/monitor/prometheus/bin/prometheus --config.file=/ups/app/monitor/prometheus/config/prometheus.yml --storage.tsdb.retention=30d

# Sets open_files_limit
LimitNOFILE=10000
TimeoutStopSec=20

[Install]
WantedBy=multi-user.target
EOF

# 检查并重新加载配置文件
./bin/promtool check config config/prometheus.yml
# 启动服务
./bin/prometheus --config.file=config/prometheus.yml
或
# 加载服务
systemctl daemon-reload

systemctl enable prometheus.service
systemctl start  prometheus.service
systemctl stop   prometheus.service
systemctl status prometheus.service

# 打开Web界面,默认端口9090
http://192.168.10.181:9090

 

clipboard

2. 部署mysqld_exporter

# 创建用户
groupadd -g 2000 prometheus
useradd -u 2000 -g prometheus -c "Prometheus Server" prometheus

# 解压文件
mkdir -p /ups/app/monitor/
tar -xf mysqld_exporter-0.12.1.linux-amd64.tar.gz -C /ups/app/monitor/

# 重命名目录
cd /ups/app/monitor/
mv mysqld_exporter-0.12.1.linux-amd64 mysqld_exporter

# 修改目录属主
chown -R prometheus.prometheus /ups/app/monitor/mysqld_exporter

# 待监控MySQL上创建用户
CREATE USER 'exporter'@'localhost' IDENTIFIED BY 'exporter';
GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'exporter'@'localhost';
CREATE USER 'exporter'@'192.168.10.%' IDENTIFIED BY 'exporter';
GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'exporter'@'192.168.10.%';
flush privileges;

# 配置mysql客户端账号密码文件
cat > /ups/app/monitor/mysqld_exporter/.my.cnf <<EOF
[client]
user=exporter
password=exporter
port=3308
socket=/ups/app/mysql/mysql3308/logs/mysql3308.sock
host=localhost
EOF

chmod 400 /ups/app/monitor/mysqld_exporter/.my.cnf
chown prometheus:prometheus /ups/app/monitor/mysqld_exporter/.my.cnf


# 配置服务文件
cat > /usr/lib/systemd/system/mysql_exporter.service <<-EOF
[Unit]
Description=https://prometheus.io
After=network.target
After=postgresql-12.service mysql3308.service mysql.service

[Service]
User=prometheus
Group=prometheus

Restart=on-failure
# ExecStart=/ups/app/monitor/mysqld_exporter/mysqld_exporter --config.my-cnf=/ups/app/monitor/mysqld_exporter/.my.cnf

ExecStart=/ups/app/monitor/mysqld_exporter/mysqld_exporter \
            -config.my-cnf=/ups/app/monitor/mysqld_exporter/.my.cnf \
            -collect.info_schema.innodb_tablespaces \
            -collect.info_schema.innodb_metrics  \
            -collect.perf_schema.tableiowaits \
            -collect.perf_schema.indexiowaits \
            -collect.perf_schema.tablelocks \
            -collect.engine_innodb_status \
            -collect.perf_schema.file_events \
            -collect.binlog_size \
            -collect.info_schema.clientstats \
            -collect.perf_schema.eventswaits

[Install]
WantedBy=multi-user.target
EOF


# 启动服务
systemctl daemon-reload
systemctl restart mysql_exporter.service
systemctl status mysql_exporter.service

或

# 启动客户端
./mysqld_exporter --config.my-cnf=/ups/app/monitor/mysqld_exporter/.my.cnf

# 默认端口:9104

clipboard

clipboard

 

3. Prometheus监控MySQL

# 配置prometheus.yml文件
cat >> /ups/app/monitor/prometheus/config/prometheus.yml <<-EOF

  - job_name: 'MySQL'
    static_configs:
    - targets: ['progs:9104','192.168.10.181:9104']

EOF

# 检查并重新加载配置文件
./bin/promtool check config config/prometheus.yml
# 重启服务
systemctl restart prometheus

 

image

 

4. Prometheus监控Linux主机

需要安装node_exporter

# 创建用户
groupadd -g 2000 prometheus
useradd -u 2000 -g prometheus -c "Prometheus Server" prometheus

# 解压文件
mkdir -p /ups/app/monitor/
tar -xf node_exporter-1.0.0-rc.0.linux-amd64.tar.gz -C /ups/app/monitor/

# 重命名目录
cd /ups/app/monitor/
mv node_exporter-1.0.0-rc.0.linux-amd64 node_exporter

# 修改目录属主
chown -R prometheus.prometheus /ups/app/monitor/node_exporter


# 配置服务文件
cat > /usr/lib/systemd/system/node_exporter.service <<-EOF
[Unit]
Description=https://prometheus.io
After=network.target

[Service]
User=prometheus
Group=prometheus
Restart=on-failure
ExecStart=/ups/app/monitor/node_exporter/node_exporter

[Install]
WantedBy=multi-user.target
EOF


# 启动服务
systemctl daemon-reload
systemctl restart node_exporter.service
systemctl status node_exporter.service

或

# 启动客户端
./node_exporter


Prometheus监控主机

# 配置prometheus.yml文件
cat >> /ups/app/monitor/prometheus/config/prometheus.yml <<-EOF

  - job_name: 'Host'
    static_configs:
    - targets: ['progs:9100']
EOF

# 检查并重新加载配置文件
./bin/promtool check config config/prometheus.yml
# 重启服务
systemctl restart prometheus

# 默认端口:9100

 

5. 部署grafana

mkdir -p /ups/app/monitor/
# 解压
tar -xf grafana-6.7.1.linux-amd64.tar.gz -C /ups/app/monitor/

# 重命名目录
cd /ups/app/monitor/
mv grafana-6.7.1 grafana

# 创建用户
groupadd -g 2001 grafana
useradd -u 2001 -g grafana -c "Grafana Server" grafana

# 修改目录属主
chown -R grafana.grafana /ups/app/monitor/grafana

# 配置服务启动项
cat > /usr/lib/systemd/system/grafana.service <<-EOF
[Unit]
Description=Grafana instance
Documentation=http://docs.grafana.org
Wants=network-online.target
After=network-online.target
After=After=postgresql-12.service mysql3308.service mysql.service

[Service]
# EnvironmentFile=/etc/sysconfig/grafana-server
User=grafana
Group=grafana
Type=notify
Restart=on-failure
WorkingDirectory=/ups/app/monitor/grafana
RuntimeDirectory=grafana
RuntimeDirectoryMode=0750

# ExecStart=/ups/app/monitor/grafana/bin/grafana-server                               \
#                             --config=\${CONF_FILE}                                   \
#                             --pidfile=\${PID_FILE_DIR}/grafana-server.pid            \
#                             --packaging=rpm                                         \
#                             cfg:default.paths.logs=\${LOG_DIR}                       \
#                             cfg:default.paths.data=\${DATA_DIR}                      \
#                             cfg:default.paths.plugins=\${PLUGINS_DIR}                \
#                             cfg:default.paths.provisioning=\${PROVISIONING_CFG_DIR}  

ExecStart=/ups/app/monitor/grafana/bin/grafana-server
LimitNOFILE=10000
TimeoutStopSec=20

[Install]
WantedBy=multi-user.target
EOF

# 启动服务
/ups/app/monitor/grafana/bin/grafana-server &
或
# 加载服务
systemctl daemon-reload

systemctl enable  grafana.service
systemctl start   grafana.service
systemctl stop    grafana.service
systemctl restart grafana.service
systemctl status  grafana.service

# 打开Web界面,默认端口3000 (默认账号/密码:admin/admin)
http://192.168.10.181:3000

image

image

5.1 配置grafana

image

 

image

 

image

 

image

-- MySQL监控面板

image

image

posted @ 2020-03-26 19:59  KuBee  阅读(305)  评论(0编辑  收藏  举报