目录
一、Prometheus使用
1、Prometheus介绍
2、安装配置
2.1、客户端安装
2.2、服务端安装
2.3、配置文件配置
2.4、启动prometheus
二、Grafana使用
1、Grafana介绍
1.1、安装配置
1.2、配置文件
1.3、启动grafana
1.4、配置数据源
1.5、导入仪表盘
三、问题记录
1、监控数据断点
环境介绍
192.168.27.132 MySQL主库,node_exporter,mysqld_exporter
192.168.27.129 MySQL从库,node_exporter,mysqld_exporter
192.168.27.151 MySQL记录信息库,prometheus服务端
Prometheus(普罗米修斯)是一套开源的监控&报警&时间序列数据库的组合,起始是由SoundCloud公司开发的。随着发展,越来越多公司和组织接受采用Prometheus,社会也十分活跃,他们便将它独立成开源项目,并且有公司来运作。Google SRE的书内也曾提到跟他们BorgMon监控系统相似的实现是Prometheus。
Prometheus基本原理是通过HTTP协议周期性抓取被监控组件的状态,这样做的好处是任意组件只要提供HTTP接口就可以接入监控系统,不需要任何SDK或者其他的集成过程。这样做非常适合虚拟化环境比如VM或者Docker 。
Prometheus应该是为数不多的适合Docker、Mesos、Kubernetes环境的监控系统之一。
输出被监控组件信息的HTTP接口被叫做exporter 。目前互联网公司常用的组件大部分都有exporter可以直接使用,比如Varnish、Haproxy、Nginx、MySQL、Linux 系统信息 (包括磁盘、内存、CPU、网络等等),具体支持的源看:https://github.com/prometheus。


它的服务过程是这样的Prometheus daemon负责定时去目标上抓取metrics(指标) 数据,每个抓取目标需要暴露一个http服务的接口给它定时抓取。
与其他监控系统相比,Prometheus的主要特点是:
Prometheus和grafana都是基于go环境下,因为需要服务器上有go环境
#yum –y install go
下面介绍如何使用Prometheus + Grafana对MySQL服务器性能进行监控。
我们用到了以下两个exporter:

下载安装exporter ( https://prometheus.io/download/ )
解压后的文件建议统一放在一个文件夹下。同时在以下的进程启动的命令也是建议使用绝对路径,方便以后直接通过进程查看找到启动文件路径。
Ps:安装prometheus的那台服务器也需要安装。
下载完成后,在监控的服务器上进行解压
1、node_exorter
#nohup ./node_exporter –web.listen-address=”:9100″ &
–也可以使用配置service服务的方式启动
–web.listen-address=”:9100″ ps:该参数为指定端口号启动
进行运行就可以监控服务器性能了
2、mysqld_exporter
mysqld_exporter需要连接到Mysql,所以需要Mysql的权限,我们先为它创建用户并赋予所需的权限,密码根据实际情况进行。
mysql> GRANT REPLICATION CLIENT,PROCESS,SELECT ON *.* TO grafana@’127.0.0.1′ identified by ‘grafana’;
mysql> flush privileges;
ps :假如mysql开起了审计功能,还需要以下权限:
init_connect+binlog审计功能的话,需要你记录审计日志信息表的读写权限
mysql> GRANT insert ON monitor.log TO grafana@’127.0.0.1′ identified by ‘grafana’;
还需要在mysql服务器上创建.my.cnf文件用与mysql的连接验证
默认mysqld_exporter是读取/root/.my.cnf
vim .my.cnf
[client]
host=127.0.0.1
port=3306
user=grafana
password=grafana
然后使用#nohup ./mysqld_exporter –config.my-cnf=”/root/.my.cnf” –web.listen-address=”:9104″& 的方式指定验证文件的位置。
–web.listen-address=”:9104″ ps:该参数为指定端口号启动
Ps :这个授权的用户的密码中不能带有 ” ? ” ,否则会报错。
下载安装Prometheus(https://prometheus.io/download/)

然后解压
#tar –zxvf prometheus-2.8.0.linux-amd64.tar.gz
#ln -s /opt/prometheus/prometheus-2.8.0.linux-amd64 /usr/local/ prometheus
#cd /usr/local/Prometheus
然后编辑配置文件加入你要监控的mysql服务器
2.3.1、prometheus.yml
在/usr/local/Prometheus/ 中
[root@localhost prometheus]# cat 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
1`job=<job_name>
1`to any timeseries scraped from this config.
– job_name: ‘prometheus’
scrape_interval: 15s
scrape_timeout: 15s
# metrics_path defaults to ‘/metrics’
# scheme defaults to ‘http’.
static_configs:
– targets:
– localhost:9090
– job_name: ‘grafana’
scrape_interval: 15s
scrape_timeout: 15s
static_configs:
– targets: [‘localhost:3000’]
– job_name: ‘linux’
scrape_interval: 15s
scrape_timeout: 15s
file_sd_configs:
– files:
– /usr/local/prometheus/conf/linux.yml
– job_name: ‘mysql’
scrape_interval: 15s
scrape_timeout: 15s
file_sd_configs:
– files:
– /usr/local/prometheus/conf/mysql.yml
2.3.2、配置linux.yml文件
[root@localhost prometheus]# cat linux.yml
labels:
instance: 192.168.27.132 –备注:这个是targets地址的别名
labels:
instance: 192.168.27.151
labels:
instance: 192.168.27.129
2.3.3、配置mysql.yml文件
[root@localhost prometheus]# cat mysql.yml
labels:
instance: 192.168.27.132
labels:
instance: 192.168.27.129
第一种方式:
# nohup /usr/local/prometheus/prometheus \
–config.file=/usr/local/prometheus/prometheus.yml \
–storage.tsdb.path=/usr/local/prometheus/data/ \
–web.enable-lifecycle –web.listen-address=:9091 &
–web.enable-lifecycle ps:是热加载配置文件,而不需要重启prometheus
–storage.tsdb.retention.time=30d ps:指定tsdb数据库文件存储的时间(30天)
–storage.tsdb.retention.size=10G ps: 指定tsdb数据库文件存储占用的最大空间
–web.listen-address=:9091 ps:指定prometheus启动的端口号
# curl -X POST http://192.168.27.151:9090/-/reload
Alertmanager的配置文件也可以使用此方式热加载配置文件
第二种:
配置启动服务
Vim /etc/systemd/system/prometheus.service
[Unit]
Description=Prometheus
Documentation=https://prometheus.io/
After=network.target
[Service]
Type=simple
User=prometheus
ExecStart=nohup /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 daemon-reload
# systemctl enable prometheus.service
# systemctl start prometheus.service
启动成功后,Prometheus内置了一个web界面,我们可通过http://192.168.27.151:9090进行访问:

在Status->Targets页面下,我们可以看到我们配置的Target。

下载rpm包的方式进行安装
#wget https://dl.grafana.com/oss/release/grafana-6.0.1-1.x86_64.rpm
# yum localinstall grafana-6.0.1-1.x86_64.rpm
grafana包内文件详情
二进制文件: /usr/sbin/grafana-server
启动文件: /etc/init.d/grafana-server
启动环境变量: /etc/sysconfig/grafana-server
配置文件: /etc/grafana/grafana.ini
systemd服务名称: grafana-server.service –需要自己配置
默认配置的日志文件:var/log/grafana/grafana.log
sqlite3数据库文件: /var/lib/grafana/grafana.db
修改配置文件
Grafana用户需要对grafana数据库有all privileges的权限
Mysql> create database grafana;
Mysql> grant all privileges on grafana.* to grafana@’127.0.0.1′ identified by ‘grafana’;
Mysql> grant all privileges on grafana.* to grafana@’localhost’ identified by ‘grafana’;
查找到[server] 修改
domain = 192.168.27.151 –本机IP
root_url = http://192.168.27.151:3000
enable_gzip = true
查找到[database] 修改 –修改默认的数据源为mysql
type = mysql
host = 127.0.0.1:3306
name = grafana
user = grafana
password = grafana
max_idle_conn = 20
查找到[session] 修改 –修改session界面的数据存放位置为mysql
provider = mysql
provider_config = grafana: grafana @tcp(127.0.0.1:3306)/grafana
cookie_name = grafana_sess
查找到[users]修改
allow_org_create = true
查找到[smtp] 修改SMTP发送的邮件服务器
enabled = true
host = smtp.163.com:25
user = sky****@163.com
# If the password contains # or ; you have to wrap it with trippel quotes. Ex “””#password;”””
password = 123456a
skip_verify = true
from_address = sky****@163.com
查找到[alerting]修改
enabled = true
execute_alerts = true
查找到[analytics]修改
reporting_enabled = flase –不启用向grafana发送使用情况的信息
check_for_updates = flase –不启用检查更新功能
查找到viewers_can_edit修改
viewers_can_edit=true
可以使viewer用户可以修改和编辑仪表盘,但是不能进行保存,默认是flase
第一种:直接指定配置文件和home目录启动
#nohup /usr/sbin/grafana-server –homepath=/usr/share/grafana –config=/etc/grafana/grafana.ini &
第二种:
配置启动服务
Vim /etc/systemd/system/ grafana.service
[Unit]
Description=grafana
Documentation=https://prometheus.io/
After=network.target
[Service]
Type=simple
User=grafana
ExecStart=/usr/sbin/grafana-server –homepath=/usr/share/grafana –config=/etc/grafana/grafana.ini
Restart=on-failure
[Install]
WantedBy=multi-user.target
# systemctl daemon-reload
# systemctl enable grafana.service
# systemctl start grafana.service
启动成功后,我们可通过http://192.168.27.151:3000进行访问:


假如mysql和prometheus与grafana在同一台机器上,则地址可以设置为localhost,否则为对应的ip地址
1、点击设置中的Data Source ->Add data source

2、点击添加prometheus数据源。

3、配置完成后点击保存。

4、添加MySQL数据源

然后点击保存
1、点击create 中的import,然后点击Upload.json.flie或者直接黏贴json代码的方式,进行导入。

最后点击导入就可以了。

现在就可以查看你要监控的信息了。

Grafana监控的图表中有时候会出现断点的数据,这部分数据可能是因为采集端压力过高造成的,导致prometheus拿不到客户端的cpu时间片
还有就是scrap采集的时间间隔和推送间隔太过频繁,导致对prometheus的压力过大。
建议使用30s的采集间隔
scrape_interval: 30s
scrape_timeout: 30s
这个是6.0.0版本的grafana的bug,需要升级到6.0.1。升级后在不显示图片的情况消失。
在编写告警名称的时候,需要注意名称的编写,如果名称中有test或者alert这类的敏感词,会被直接当初垃圾邮件,导致发送失败。