«

centos7部署Prometheus+Grafana

LiHaiYang 发布于 阅读:1230 服务器监控告警


[TOC]

Prometheus

安装

wget "https://install.jishuliu.cn/prometheus/prometheus-2.32.1.linux-amd64.tar.gz"
tar xf prometheus-2.32.1.linux-amd64.tar.gz -C /usr/local/
cd /usr/local/
mv prometheus-2.32.1.linux-amd64/ prometheus
# 全局配置
global:
  scrape_interval:     15s # 设置抓取间隔,默认为1分钟
  evaluation_interval: 15s #估算规则的默认周期,每15秒计算一次规则。默认1分钟
  # scrape_timeout  #默认抓取超时,默认为10s

# Alertmanager相关配置
alerting:
  alertmanagers:
  - static_configs:
    - targets:
      # - alertmanager:9093

# 规则文件列表,使用'evaluation_interval' 参数去抓取
rule_files:
  # - "first_rules.yml"
  # - "second_rules.yml"

#  抓取配置列表
scrape_configs:
  - job_name: 'prometheus'
    static_configs:
    - targets: ['localhost:9090']
useradd  -s /sbin/nologin -M prometheus 
mkdir  /usr/local/prometheus/data
chown -R prometheus:prometheus /usr/local/prometheus/
chown -R prometheus:prometheus /usr/local/prometheus/data
vim /etc/systemd/system/prometheus.service

[Unit]
Description=Prometheus
Documentation=https://prometheus.io/
After=network.target
[Service]
Type=simple
User=prometheus
ExecStart=/usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/prometheus.yml --storage.tsdb.path=/usr/local/prometheus/data
Restart=on-failure
[Install]
WantedBy=multi-user.target
systemctl start prometheus
systemctl status prometheus
systemctl enable prometheus

image.png

添加Node Exporter

node exporter收集服务器指标

# 个人服务器地址
https://install.jishuliu.cn/prometheus/node_exporter-1.3.1.linux-amd64.tar.gz
# GitHub下载地址为
https://github.com/prometheus/node_exporter/releases/download/v1.3.1/node_exporter-1.3.1.linux-amd64.tar.gz
wget "https://install.jishuliu.cn/prometheus/node_exporter-1.3.1.linux-amd64.tar.gz"
tar xf node_exporter-1.3.1.linux-amd64.tar.gz -C /usr/local/
mv node_exporter-1.3.1.linux-amd64/ node_exporter
vim /usr/lib/systemd/system/node_exporter.service

[Unit]
Description=node_exporter
After=syslog.target
After=network.target

[Service]
Type=simple
ExecStart=/usr/local/node_exporter/node_exporter
Restart=always
RestartSec=10
StartLimitInterval=100

[Install]
WantedBy=multi-user.target
systemctl daemon-reload  # 重新载入配置文件
systemctl start node_exporter  # 启动
systemctl enable node_exporter  # 设置开机自启
systemctl status node_exporter  # 查看运行状态
● node_exporter.service - node_exporter
   Loaded: loaded (/usr/lib/systemd/system/node_exporter.service; enabled; vendor preset: disabled)
   Active: active (running) since Tue 2022-01-04 17:57:00 CST; 59s ago
 Main PID: 30176 (node_exporter)
   CGroup: /system.slice/node_exporter.service
           └─30176 /usr/local/node_exporter/node_exporter

Jan 04 17:57:00 iZ2zeg8k6x4e3osz5lt9wuZ node_exporter[30176]: ts=2022-01-04T09:57:00.343Z caller=node_exporter.go:115 level=info collector=thermal_zone
Jan 04 17:57:00 iZ2zeg8k6x4e3osz5lt9wuZ node_exporter[30176]: ts=2022-01-04T09:57:00.343Z caller=node_exporter.go:115 level=info collector=time
Jan 04 17:57:00 iZ2zeg8k6x4e3osz5lt9wuZ node_exporter[30176]: ts=2022-01-04T09:57:00.343Z caller=node_exporter.go:115 level=info collector=timex
Jan 04 17:57:00 iZ2zeg8k6x4e3osz5lt9wuZ node_exporter[30176]: ts=2022-01-04T09:57:00.343Z caller=node_exporter.go:115 level=info collector=udp_queues
Jan 04 17:57:00 iZ2zeg8k6x4e3osz5lt9wuZ node_exporter[30176]: ts=2022-01-04T09:57:00.343Z caller=node_exporter.go:115 level=info collector=uname
Jan 04 17:57:00 iZ2zeg8k6x4e3osz5lt9wuZ node_exporter[30176]: ts=2022-01-04T09:57:00.343Z caller=node_exporter.go:115 level=info collector=vmstat
Jan 04 17:57:00 iZ2zeg8k6x4e3osz5lt9wuZ node_exporter[30176]: ts=2022-01-04T09:57:00.343Z caller=node_exporter.go:115 level=info collector=xfs
Jan 04 17:57:00 iZ2zeg8k6x4e3osz5lt9wuZ node_exporter[30176]: ts=2022-01-04T09:57:00.343Z caller=node_exporter.go:115 level=info collector=zfs
Jan 04 17:57:00 iZ2zeg8k6x4e3osz5lt9wuZ node_exporter[30176]: ts=2022-01-04T09:57:00.343Z caller=node_exporter.go:199 level=info msg="Listening on" address=:9100
Jan 04 17:57:00 iZ2zeg8k6x4e3osz5lt9wuZ node_exporter[30176]: ts=2022-01-04T09:57:00.343Z caller=tls_config.go:195 level=info msg="TLS is disabled." http2=false

添加MySQL exporter

mysql exporter收集mysql指标

官网下载地址:https://prometheus.io/download/#mysqld_exporter
个人源下载地址:https://install.jishuliu.cn/prometheus/mysqld_exporter-0.14.0.linux-amd64.tar.gz
# 连接数据库
mysql -u 用户名 -p 密码
    # 创建一个mysql_monitor的用户并设置密码为123、连接地址为localhost
    grant select,replication client, process on *.* to 'mysql_monitor'@'localhost' identified by '123';
    # 刷新权限信息
    flush privileges;
# 解压压缩包
tar xf mysqld_exporter-0.14.0.linux-amd64.tar.gz
mv mysqld_exporter-0.14.0.linux-amd64/ mysqld_expoeorter
# 进入目录
cd mysqld_exporter
# 新增.my.cnf文件并写入以下内容,user为连接数据库的用户名,password为密码
    [client]
    user=mysql_monitor
    password=123
# 在/usr/lib/systemd/system/目录下新增mysqld_exporter.service文件并写入以下内容
[Unit]
Description=mysqld_exporter
After=syslog.target
After=network.target

[Service]
Type=simple
# /usr/local/mysqld_exporter/mysqld_exporter为解压路径下的mysqld_exporter可执行文件的绝对路径
# --config.my-cnf为存储mysql账号密码的文件的绝对路径
ExecStart=/usr/local/mysqld_exporter/mysqld_exporter --config.my-cnf=/usr/local/mysqld_exporter/.my.cnf
Restart=always
RestartSec=10
StartLimitInterval=100

[Install]
WantedBy=multi-user.target
systemctl daemon-reload
service mysqld_exporter start
systemctl enable mysqld_exporter

Prometheus添加数据源

  - job_name: 'linux-exporter'
    metrics_path: /metrics
    static_configs:
    - targets: ['172.25.34.51:9100'] # node_exporter
    - targets: ['172.25.34.51:9104'] # mysqld_exporter
systemctl restart prometheus

image.png

Grafana

wget "https://install.jishuliu.cn/grafana/grafana-enterprise-8.3.3-1.x86_64.rpm"
sudo yum install grafana-enterprise-8.3.3-1.x86_64.rpm
[database]
# You can configure the database connection by specifying type, host, name, user and password
# as separate properties or as on string using the url properties.

# Either "mysql", "postgres" or "sqlite3", it's your choice
type = mysql
host = 127.0.0.1:3306
name = grafana
user = root
# If the password contains # or ; you have to wrap it with triple quotes. Ex """#password;"""
password = 123456
# 登录mysql命令行
mysql -uroot -p123456
# 创建数据库
create database grafana default character set utf8;
# 删除用户及权限
drop user grafana@'localhost';
# 创建用户
create user grafana@'localhost' IDENTIFIED by '123456';
# 设置权限
GRANT ALL ON grafana.* TO grafana@'localhost' WITH GRANT option;
# 刷新权限
flush PRIVILEGES;
yum install fontconfig freetype* urw-fonts -y
systemctl status grafana-server   # 查看服务状态
systemctl start grafana-server   # 启动服务
systemctl enable grafana-server  # 开机自启动

添加数据源

image.png

image.png

image.png

添加Dashboard

node_exporter

image.png

image.png

image.png

image.png

mysqld_exporter

官网下载地址:https://grafana.com/grafana/dashboards/7362-mysql-overview/
个人下载地址:https://install.jishuliu.cn/grafana/mysql-overview_rev5.json


扫描二维码,在手机上阅读
取消
微信二维码
微信二维码
支付宝二维码