Centos7 - Prometheus + Grafana 监控平台搭建

Prometheus 是一套开源的系统监控报警框架。Prometheus 所有采集的监控数据均以指标(metric)的形式保存在内置的时间序列数据库当中(TSDB):属于同一指标名称,同一标签集合的、有时间戳标记的数据流。除了存储的时间序列,Prometheus 还可以根据查询请求产生临时的、衍生的时间序列作为返回结果

Exporter 是Prometheus的一类数据采集组件的总称。它负责从目标处搜集数据,并将其转化为Prometheus支持的格式。与传统的数据采集组件不同的是,它并不向中央服务器发送数据,而是等待中央服务器主动前来抓取

Grafana 是一个跨平台的开源的度量分析和可视化工具,可以通过将采集的数据查询然后可视化的展示,并及时通知

本次实验环境: 操作系统 :CentOS Linux release 7.6.1810 MySQL :5.6.43

一.下载Prometheus

下载地址:https://prometheus.io/download/

prometheus mysql告警指标 prometheus监控告警_linux使用

选择合适的版本,右键点击链接并复制链接地址,到linux系统内 用wget 命令下载并解压,这里我下的是2.8.1版本

wget https://github.com/prometheus/prometheus/releases/download/v2.8.1/prometheus-2.8.1.linux-amd64.tar.gz

tar zxvf  prometheus-2.8.1.linux-amd64.tar.gz

mv  prometheus-2.8.1.linux-amd64  /opt/prometheus

 

二.下载并运行 mysqld_exporter,node_exporter

exporter 是需要安装在需要被监控的服务器上的,本次演示为了方便,我就把所有软件都安装在同一个服务器上了

1.下载解压 这两个node_exporter 需要运行在需要被监控的服务器上 在上面 Prometheus 的下载页面,也提供了很多exporter的下载,其中就包括了mysqld_exporter和node_exporter

prometheus mysql告警指标 prometheus监控告警_linux使用_02

#下载解压mysqld_exporter
wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.11.0/mysqld_exporter-0.11.0.linux-amd64.tar.gz

tar zxvf  mysqld_exporter-0.11.0.linux-amd64.tar.gz

mv  mysqld_exporter-0.11.0.linux-amd64 /opt/mysqld_exporter

#下载解压node_exporter
wget https://github.com/prometheus/node_exporter/releases/download/v0.17.0/node_exporter-0.17.0.linux-amd64.tar.gz

tar zxvf  node_exporter-0.17.0.linux-amd64.tar.gz

mv  node_exporter-0.17.0.linux-amd64 /opt/node_exporter

 

2.运行

进入mysqld_exporter安装目录并运行node_exporter

cd /opt/node_exporter

nohup ./node_exporter &

 

运行mysqld_exporter需要连接到MySQL,需要授权,在本案例中,被授权的账号为mysql_monitor,密码为123123

#先用root 账号登录mysql 
mysql -u root -p 

#输入密码登录成功执行授权sql语句
grant replication client, process on *.* to mysql_monitor@"localhost" identified by "123123";

grant select on performance_schema.* to mysql_monitor@"localhost";

 

授权后进入mysqld_exporter安装目录创建.my.cnf配置文件,并运行mysqld_exporter

cd /opt/mysqld_exporter
vim .my.cnf

 

.my.cnf文件中写入以下内容

[client]
user=mysql_monitor
password=123123

 

保存后,运行 mysqld_exporter

nohup ./mysqld_exporter --config.my-cnf=.my.cnf &

 

mysqld_exporter占用9104端口, node_exporter 占用9100端口

三. 配置prometheus 并运行

进入prometheus安装路径并修改配置文件

cd /opt/prometheus

vim prometheus.yml

 

配置文件修改后内容如下

# my global config
global:
  scrape_interval:     15s # 设置刮擦间隔为每15秒。默认值是每1分钟。
  evaluation_interval: 15s # 每15秒评估一次规则。默认值是每1分钟。
  # scrape_timeout is set to the global default (10s).

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

#加载规则一次,然后根据全局'evaluation_interval'定期对它们求值
rule_files:
  # - "first_rules.yml"
  # - "second_rules.yml"

# 一种刮板配置,包含一个要刮的端点:
# Here it's Prometheus itself.
scrape_configs:
  #将作业名称作为标签' job=<job_name> '添加到从该配置中提取的任何timeseries中。- job_name: 'prometheus'

    #metrics_path默认为'/metrics'
    # scheme默认为“http”

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

  - job_name: 'mysql'
    static_configs:
    - targets: ['localhost:9104']
      labels:
        instance: 'db1'
      
  - job_name: 'node'
    static_configs:
    - targets: ['localhost:9100']
      labels:
        instance: 'nd1'

 

 

每个job_name标签标示一个监控的job targets 标签标示受监控的应用的 ip和端口号

注意:这个配置文件要特别注意格式缩进,严格按照他原来的格式来修改,不然会导致prometheus运行不了。

运行prometheus

nohup ./prometheus --config.file=./prometheus.yml &

 

运行后可以通过 cat nohup.out 查看运行日志

浏览器访问 服务器的9090端口可以访问prometheus的页面

prometheus mysql告警指标 prometheus监控告警_linux使用_03

然后我们进入status 目录下的Targets页面

prometheus mysql告警指标 prometheus监控告警_json_04

我们可以看到,我们在配置文件配置的三个监控的job状态都是up的

prometheus mysql告警指标 prometheus监控告警_nMOn服务器性能检测工具_05

如果状态不是up,则证明该job的配置有问题或着监控的应用没有运行起来,可以返回去检查一下。

prometheus 对于数据的展现并不直观和美观,所以,我们需要grafana

四.下载安装并运行Grafana

下载地址:https://grafana.com/grafana/download

prometheus mysql告警指标 prometheus监控告警_linux使用_06

可以按照官网的指导下载安装合适的版本,这里我下载的是6.1.3版本

wget https://dl.grafana.com/oss/release/grafana-6.1.3-1.x86_64.rpm 

sudo yum localinstall grafana-6.1.3-1.x86_64.rpm

 

然后运行grafana

systemctl start grafana-server

 

运行后我们从浏览器访问服务的3000端口,可以访问grafana的页面

prometheus mysql告警指标 prometheus监控告警_nMOn服务器性能检测工具_07

初始账号和密码 都是 admin 登录成功并修改了密码之后,我们添加一个data source ,并且在选择data source type 时选择 Prometheus

prometheus mysql告警指标 prometheus监控告警_nMOn服务器性能检测工具_08

在配置好data source 后点击 save&test 按钮,如果提示data source is working 则为成功

prometheus mysql告警指标 prometheus监控告警_mysql_09

配置好 data sources 后,我们需要去下载dashboard 的json文件并导入, 当然,你也可以自己去创建dashboard.

本次我们下载 “mysql overview” 和 “1 Node Exporter 0.16 0.17 for Prometheus 监控展示看板” ,下面演示 “mysql overview” dashboard 的json文件下载过程

dashboard 的json文件下载地址:https://grafana.com/dashboards

prometheus mysql告警指标 prometheus监控告警_adb 性能数据收集_10

你可以搜索你需要的json文件

prometheus mysql告警指标 prometheus监控告警_nMOn服务器性能检测工具_11

我选择了一个下载次数最多的,选择后,我们可以预览这个dashbord 展示的内容,点击 dowload json 下载 json 文件,这里还需要注意一下 Dependencies里的版本,因为有些版本不支持的问题可能会导致导入的dashboard 不显示图标或者图表都是空的。如果下载的dashboard用不了,可以换一个试试。

prometheus mysql告警指标 prometheus监控告警_json_12

“1 Node Exporter 0.16 0.17 for Prometheus 监控展示看板” 的下载方法和上面差不多。

下载了 “”mysql overview“” 和 “1 Node Exporter 0.16 0.17 for Prometheus 监控展示看板” 的json文件之后,我们需要导入到grafana, 我演示下 ‘1 Node Exporter 0.16 0.17 for Prometheus 监控展示看板’ dashboard的json文件导入。

prometheus mysql告警指标 prometheus监控告警_adb 性能数据收集_13

点击 upload json file,并选择下载好的json文件

prometheus mysql告警指标 prometheus监控告警_nMOn服务器性能检测工具_14

修改好 name 和 prometheus node 后 点击import

prometheus mysql告警指标 prometheus监控告警_nMOn服务器性能检测工具_15

然后就备件款的node的信息就很直观且美观的展现出来了。但是这个dashboard的磁盘总空间 那一块 有警告 说找不到 grafana-piechart-panel 插件,接下来我们就进行插件的安装

prometheus mysql告警指标 prometheus监控告警_mysql_16

grafana-piechart-panel插件 是一个饼状图插件,grafana的插件安装很简单

在grafana 安装的服务器环境 执行以下命令进行插件安装

grafana-cli plugins install grafana-piechart-panel

#插件安装后重启grafana
systemctl restart grafana-server

 

然后我们刷新下 grafana的dashboar页面 就可以看到饼状图显示出来了

prometheus mysql告警指标 prometheus监控告警_linux使用_17

 报警需要邮件功能,邮件功能配置在  /etc/grafana/grafana.ini 文件

 

 修改grafana的配置文件

 

grafana的配置文件默认是在  /etc/grafana/grafana.ini  

#################################### SMTP / Emailing ##########################
[smtp]
enabled = true  #是否允许开启
host = smtp.exmail.qq.com:465    #发送服务器地址,可以再邮箱的配置教程中找到:
user = 你的邮箱
# If the password contains # or ; you have to wrap it with trippel quotes. Ex """#password;"""
# 这个密码是你开启smtp服务生成的密码
password = 你的密码
;cert_file =
;key_file =
;skip_verify = false
from_address = 你的邮箱
from_name = Grafana
# EHLO identity in SMTP dialog (defaults to instance_name)
ehlo_identity = dashboard.example.com
[emails]
;welcome_email_on_sign_up = true
#################################### Logging ##########################

重新启动grafana服务,让配置文件生效

service grafana-server restart