Prometheus 是一套开源的系统监控报警框架。Prometheus 所有采集的监控数据均以指标(metric)的形式保存在内置的时间序列数据库当中(TSDB):属于同一指标名称,同一标签集合的、有时间戳标记的数据流。除了存储的时间序列,Prometheus 还可以根据查询请求产生临时的、衍生的时间序列作为返回结果
Exporter 是Prometheus的一类数据采集组件的总称。它负责从目标处搜集数据,并将其转化为Prometheus支持的格式。与传统的数据采集组件不同的是,它并不向中央服务器发送数据,而是等待中央服务器主动前来抓取
Grafana 是一个跨平台的开源的度量分析和可视化工具,可以通过将采集的数据查询然后可视化的展示,并及时通知
本次实验环境:
- 操作系统 :CentOS Linux release 7.9.2009 (Core)
- MySQL :5.7.21
主机 | 安装软件 |
---|---|
192.168.199.106 | prometheus,grafana,node_exporter |
192.168.199.101 | mysqld_exporter,mysql |
安装prometheus
下载地址:
https://prometheus.io/download/
tar -zxvf prometheus-2.25.0.linux-amd64.tar.gz
进入prometheus安装路径并修改配置文件,注意:这个配置文件要特别注意格式缩进,严格按照他原来的格式来修改
vim prometheus.yml
# 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: 'mysql'
static_configs:
- targets: ['192.168.199.101:9104']
labels:
instance: 'db1'
- job_name: 'node'
static_configs:
- targets: ['localhost:9100']
labels:
instance: 'nd1'
运行:
nohup ./prometheus --config.file=./prometheus.yml &
exporter 是需要安装在需要被监控的服务器上的
安装node_exporter
tar -zxvf node_exporter-1.1.1.linux-amd64.tar.gz
运行
nohup ./node_exporter &
安装mysqld_exporter
tar -zxvf mysqld_exporter-0.12.1.linux-amd64.tar.gz
连接到需要被监控的mysql主机,添加授权账号
mysql -u root -p
grant replication client, process on *.* to mysql_monitor@"192.168.199.101" identified by "123456";
grant select on performance_schema.* to mysql_monitor@"192.168.199.101" identified by "123456";
flush privileges;
进入mysqld_exporter安装目录创建.my.cnf配置文件
[client]
user=mysql_monitor
password=123123
port=3306
host=127.0.0.1
运行
nohup ./mysqld_exporter --config.my-cnf=.my.cnf &
mysqld_exporter占用9104端口, node_exporter 占用9100端口
浏览器访问 服务器的9090端口可以访问prometheus的页面, 然后我们进入status 目录下的Targets页面,我们在配置文件配置的三个监控的job状态都是up的

安装grafana
wget https://dl.grafana.com/oss/release/grafana-7.4.2-1.x86_64.rpm
sudo yum install grafana-7.4.2-1.x86_64.rpm
systemctl start grafana-server
从浏览器访问服务的3000端口,可以访问grafana的页面,初始账号和密码都是admin
,登录成功并修改了密码之后,我们添加一个data source ,并且在选择data source type 时选择 Prometheus,配置好 data sources 后,我们需要去下载dashboard 的json文件并导入,
dashboard 的json文件下载地址:https://grafana.com/grafana/dashboards
本次我们下载 “mysql overview” 和 “1 Node Exporter 0.16 0.17 for Prometheus 监控展示看板”
下载了 mysql overview 和 1 Node Exporter 0.16 0.17 for Prometheus 监控展示看板 的json文件之后,我们需要导入到grafana,
参考: