利用Prometheus与Grafana对Mysql服务器的性能监控详解

一、概述

Prometheus是一个开源的服务监控系统,它通过HTTP协议从远程的机器收集数据并存储在本地的时序数据库上。它提供了一个简单的网页界面、一个功能强大的查询语言以及HTTP接口等等。Prometheus通过安装在远程机器上的exporter来收集监控数据,这里用到了以下两个exporter:

  1. node_exporter – 用于机器系统数据
  2. mysqld_exporter – 用于Mysql服务器数据

Grafana是一个开源的功能丰富的数据可视化平台,通常用于时序数据的可视化。它内置了以下数据源的支持:

并可以通过插件扩展支持的数据源。

二、架构图

下面是本次部署的架构图

1.  安装Grafana

Grafana是一个可视化面板(Dashboard),有着非常漂亮的图表和布局展示,功能齐全的度量仪表盘和图形编辑器,支持Graphite、zabbix、InfluxDB、Prometheus等数据源。

1.1 下载并安装

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

选择最新的版本进行安装,按照网站的提示运行脚本即可(监控服务器需可访问外网,如无法访问外网可与我沟通如何离线快速部署)。

运行如下脚本

wget https://dl.grafana.com/oss/release/grafana-6.3.3-1.x86_64.rpm
sudo yum localinstall grafana-6.3.3-1.x86_64.rpm

1.2  启动grafana

安装完成后,grafana服务默认已安装,配置文件为/etc/grafana/grafana.ini,如需修改路径及端口,可在该文件中修改

启动grafana

 /etc/init.d/grafana-server  start

1.3  登录grafana

 访问页面http://服务器IP:3000 ,默认账号、密码admin/admin  首次登录将提示修改密码,建议修改

2.  安装Prometheus

2.1   Prometheus 主程序安装

 Prometheus 主程序,主要是负责存储、抓取、聚合、查询方面

可登录官网进行下载,官网下载地址:https://prometheus.io/download/

根据操作系统类别选择文件进行下载,本次部署在linux上

/**  下载*/
wget https://github.com/prometheus/prometheus/releases/download/v2.12.0/prometheus-2.12.0.linux-amd64.tar.gz 

/**  解压*/
tar -zxvf prometheus-2.12.0.linux-amd64.tar.gz

2.2  启动prometheus主程序

生产环境可参考如下方式启动

/** 生产环境启动*/
nohup ./prometheus --config.file=prometheus.yml --web.enable-lifecycle --storage.tsdb.retention.time=60d   &

/**   
 --web.enable-lifecycle  加上此参数可以远程热加载配置文件,无需重启prometheus,调用指令是curl -X POST http://ip:9090/-/reload
-- storage.tsdb.retention.time 数据默认保存时间为15天,启动时加上此参数可以控制数据保存时间

*/

其他的参数及配置可以在prometheus.yml中调整及配置

3.  在需监控的机器上部署exporter

 3.1   监控linux主机

下载监控linux主机的node_exporter,依旧从官网下载

/**  下载  */
wget https://github.com/prometheus/node_exporter/releases/download/v0.18.1/node_exporter-0.18.1.linux-amd64.tar.gz

/**  解压  */
tar  -zxvf node_exporter-0.18.1.linux-amd64.tar.gz

可以按照默认方式启动

/** 启动 node_exporter*/
cd  node_exporter-0.18.1.linux-amd64
nohup ./node_exporter  &
/**
默认端口9100
*/

3.2  监控MySQL

3.2.1 下载

 下载监控MySQL的mysqld_exporter,依旧从官网下载

/**  下载  */
wget  https://github.com/prometheus/mysqld_exporter/releases/download/v0.12.1/mysqld_exporter-0.12.1.linux-amd64.tar.gz

/**  解压  */
tar -zxvf  mysqld_exporter-0.12.1.linux-amd64.tar.gz

3.2.2  监控账号及修改文件配置

在MySQL里配置MySQL监控账号

/**  创建账号  */
mysql> CREATE USER 'mysql_monitor'@'localhost' identified by 'mysql_monitor'; 
/** 授权 */
mysql> GRANT REPLICATION CLIENT, PROCESS ON *.* TO 'mysql_monitor'@'localhost'; 
mysql> GRANT SELECT ON performance_schema.* TO 'mysql_monitor'@'localhost'; 

/** 
注意,不用版本对权限要求不一致,启动时注意查看日志,如权限不足则继续授权或创建对应的账号 
*/

特别要注意:127.0.0.1要能连接

配置文件修改

cd mysqld_exporter-0.12.0.linux-amd64

vim .my.cnf 
/**  添加如下配置 */
[client]
host=192.168.11.13 
port=3306
user=mysql_monitor
password=mysql_monitor

3.2.3 启动监控脚本

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

3.3  监控redis

 3.3.1 下载redis_exporter

官网上没有redis_exporter, 可以从github上获取,另外redis插件无需放在redis机器上也可以

/**  下载  */
wget https://github.com/oliver006/redis_exporter/releases/download/v0.30.0/redis_exporter-v0.30.0.linux-amd64.tar.gz
/**  解压  */
tar -zxvf  redis_exporter-v0.30.0.linux-amd64.tar.gz

3.3.2  启动redis_exporter

/**  redis无密码 */
nohup  ./redis_exporter -redis.addr=192.168.56.118:6379 -web.listen-address 0.0.0.0:9121  &

/**  redis有密码  */
nohup  ./redis_exporter -redis.addr=192.168.56.118:6479 -redis.password 123456   -web.listen-address 0.0.0.0:9122 & 

/**   
 -web.listen-address  可以自定义监控端口
*/

复制代码

4.  配置prometheus配置文件

4.1  添加各监控项

配置文件可以有多种配置方式,可以根据不同的分类和习惯配置。可参考如下方式配置

# 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: 'OS'

    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.

    static_configs:
    - targets: ['192.168.56.114:9100']
      labels:
          instance: '192.168.56.114'
          
    - targets: ['192.168.56.116:9100']
      labels:
          instance: '192.168.56.116'

    - targets: ['192.168.56.117:9100']
      labels:
          instance: '192.168.56.117'
##  上述job单独做主机监控,每台主机的instance不同

  - job_name: 'mysql'

    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.

    static_configs:
    - targets: ['192.168.56.116:9104']
      labels:
          instance: '192.168.56.116'
    
    - targets: ['192.168.56.117:9104']
      labels:
          instance: '192.168.56.117'

  ## 以上是监控mysql的,instance和主机的instance的相同

  - job_name: 'redis'

    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.

    static_configs:
    - targets: ['192.168.56.118:9121','192.168.56.118:9122']
      labels:
          instance: '192.168.56.118'

    - targets: ['192.168.56.118:9100']
      labels:
          instance: '192.168.56.118'    
#   可以类似上述这种,redis的主机及各redis监控项组合在一起,instance使用相同的

more 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: ['192.168.11.236:9090']

  - job_name: '192.168.11.236'
    scrape_interval: 10s
    static_configs:
      - targets: ['192.168.11.236:9100']

  - job_name: '192.168.11.11'
    scrape_interval: 10s
    static_configs:
      - targets: ['192.168.11.11:9100']

  - job_name: '192.168.11.13'
    scrape_interval: 10s
    static_configs:
      - targets: ['192.168.11.13:9100']

  - job_name: '192.168.8.30'
    scrape_interval: 10s
    static_configs:
      - targets: ['192.168.8.30:9100']

  - job_name: 'mysql83'
    scrape_interval: 10s
    static_configs:
      - targets: ['192.168.8.30:9104']


  - job_name: 'mysql131'  
    scrape_interval: 10s
    static_configs:
      - targets: ['192.168.11.13:9104']
   
  - job_name: 'mysql136'
    scrape_interval: 10s
    static_configs:
      - targets: ['192.168.11.13:9104']

4.2  启动或热加载prometheus

/**  启动  */
nohup ./prometheus --config.file=prometheus.yml --web.enable-lifecycle --storage.tsdb.retention.time=60d   &

/**   
-- storage.tsdb.retention.time 数据默认保存时间为15天,启动时加上此参数可以控制数据保存时间
*/

/**  热加载  */
curl -X POST http://ip:9090/-/reload 


/**   
热加载的前提是启动时加了--web.enable-lifecycle 
*/

5.  配置各监控仪表盘

5.1  下载各监控仪表盘

以上模板grafana官方网站均有,可以根据自己的需要下载对应的模板,对应地址为https://grafana.com/grafana/dashboards

找到对应的仪表盘模板后进入下载

 

5.2  配置数据源

本次使用的均为prometheus数据源,因此配置一个prometheus的数据源

如果之前在grafana上没有配置过数据源 登录后会提示创建

 选择prometheus

 

配置prometheus地址

最终save & Test即可

5.3 导入仪表盘

将5.1中下载的模板导入

导入

修改名称及数据源

import即可

5.4 配置完成后即可查看各监控情况

主机监控如下

MySQL

 

Redis

 其他如果需要其他监控项也可以自定义添加

三、Alertmanager(email报警)

Alertmanager简介及机制
Alertmanager处理由类似Prometheus服务器等客户端发来的警报,之后需要删除重复、分组,并将它们通过路由发送到正确的接收器,比如电子邮件、Slack等。Alertmanager还支持沉默和警报抑制的机制。

分组
分组是指当出现问题时,Alertmanager会收到一个单一的通知,而当系统宕机时,很有可能成百上千的警报会同时生成,这种机制在较大的中断中特别有用。

例如,当数十或数百个服务的实例在运行,网络发生故障时,有可能服务实例的一半不可达数据库。在告警规则中配置为每一个服务实例都发送警报的话,那么结果是数百警报被发送至Alertmanager。

但是作为用户只想看到单一的报警页面,同时仍然能够清楚的看到哪些实例受到影响,因此,人们通过配置Alertmanager将警报分组打包,并发送一个相对看起来紧凑的通知。

分组警报、警报时间,以及接收警报的receiver是在配置文件中通过路由树配置的。

抑制
抑制是指当警报发出后,停止重复发送由此警报引发其他错误的警报的机制。

例如,当警报被触发,通知整个集群不可达,可以配置Alertmanager忽略由该警报触发而产生的所有其他警报,这可以防止通知数百或数千与此问题不相关的其他警报。

抑制机制可以通过Alertmanager的配置文件来配置。

沉默
沉默是一种简单的特定时间静音提醒的机制。一种沉默是通过匹配器来配置,就像路由树一样。传入的警报会匹配RE,如果匹配,将不会为此警报发送通知。

沉默机制可以通过Alertmanager的Web页面进行配置。

3.1 Alertmanager安装

下载地址:https://github.com/prometheus/alertmanager/releases/download/v0.15.3/alertmanager-0.15.3.linux-amd64.tar.gz

tar -zxvf alertmanager-0.15.3.linux-amd64.tar.gz

cd alertmanager-0.15.3.linux-amd64

执行命令 mv simple.yml alertmanager.yml,并修改 alertmanager.yml 配置:

启动 ./alertmanager-config.file= alertmanager.yml #默认配置项为alertmanager.yml

 注:alertmanager.yml配置文件,默认是不存在的,需要新建。

3.2 alertmanager.yml的配置

# 全局配置项
global: 
  resolve_timeout: 5m #处理超时时间,默认为5min
  smtp_smarthost: 'smtp.sina.com:25' # 邮箱smtp服务器代理
  smtp_from: '******@sina.com' # 发送邮箱名称
  smtp_auth_username: '******@sina.com' # 邮箱名称
  smtp_auth_password: '******' # 邮箱密码或授权码
  wechat_api_url: 'https://qyapi.weixin.qq.com/cgi-bin/' # 企业微信地址


# 定义模板信心
templates:
  - 'template/*.tmpl'

# 定义路由树信息
route:
  group_by: ['alertname'] # 报警分组依据
  group_wait: 10s # 最初即第一次等待多久时间发送一组警报的通知
  group_interval: 10s # 在发送新警报前的等待时间
  repeat_interval: 1m # 发送重复警报的周期 对于email配置中,此项不可以设置过低,否则将会由于邮件发送太多频繁,被smtp服务器拒绝
  receiver: 'email' # 发送警报的接收者的名称,以下receivers name的名称

# 定义警报接收者信息
receivers:
  - name: 'email' # 警报
    email_configs: # 邮箱配置
    - to: '******@163.com'  # 接收警报的email配置
      html: '{{ template "test.html" . }}' # 设定邮箱的内容模板
      headers: { Subject: "[WARN] 报警邮件"} # 接收邮件的标题
    webhook_configs: # webhook配置
    - url: 'http://127.0.0.1:5001'
    send_resolved: true
    wechat_configs: # 企业微信报警配置
    - send_resolved: true
      to_party: '1' # 接收组的id
      agent_id: '1000002' # (企业微信-->自定应用-->AgentId)
      corp_id: '******' # 企业信息(我的企业-->CorpId[在底部])
      api_secret: '******' # 企业微信(企业微信-->自定应用-->Secret)
      message: '{{ template "test_wechat.html" . }}' # 发送消息模板的设定
# 一个inhibition规则是在与另一组匹配器匹配的警报存在的条件下,使匹配一组匹配器的警报失效的规则。两个警报必须具有一组相同的标签。 
inhibit_rules: 
  - source_match: 
     severity: 'critical' 
    target_match: 
     severity: 'warning' 
    equal: ['alertname', 'dev', 'instance']

注:

1)repeat_interval配置项,对于email来说,此项不可以设置过低,否则将会由于邮件发送太多频繁,被smtp服务器拒绝

2)企业微信注册地址:https://work.weixin.qq.com

上述配置的email、webhook和wechat三种报警方式。目前Alertmanager所有的报警方式有以下几个方面:

email_config
hipchat_config
pagerduty_config
pushover_config
slack_config
opsgenie_config
victorops_config

3.3.tmpl模板的配置

1)test.tmpl

{{ define "test.html" }}
<table border="1">
        <tr>
                <td>报警项</td>
                <td>实例</td>
                <td>报警阀值</td>
                <td>开始时间</td>
        </tr>
        {{ range $i, $alert := .Alerts }}
                <tr>
                        <td>{{ index $alert.Labels "alertname" }}</td>
                        <td>{{ index $alert.Labels "instance" }}</td>
                        <td>{{ index $alert.Annotations "value" }}</td>
                        <td>{{ $alert.StartsAt }}</td>
                </tr>
        {{ end }}
</table>
{{ end }}

注:上述Labels项,表示prometheus里面的可选label项。annotation项表示报警规则中定义的annotation项的内容。

2)test_wechat.tmpl

{{ define "cdn_live_wechat.html" }}
  {{ range $i, $alert := .Alerts.Firing }}
    [报警项]:{{ index $alert.Labels "alertname" }}
    [实例]:{{ index $alert.Labels "instance" }}
    [报警阀值]:{{ index $alert.Annotations "value" }}
    [开始时间]:{{ $alert.StartsAt }}
  {{ end }}
{{ end }}

注:此处range遍历项与email模板中略有不同,只遍历当前没有处理的报警(Firing)。此项如果不设置,则在Alert中已经Resolved的报警项,也会被发送到企业微信。

3.4、在Prometheus模块定义告警规则

alertmanager_rules.yml样例配置文件(与prometheus同目录下)

groups:
 - name: test-rules
   rules:
   - alert: InstanceDown # 告警名称
     expr: up == 0 # 告警的判定条件,参考Prometheus高级查询来设定
     for: 2m # 满足告警条件持续时间多久后,才会发送告警
     labels: #标签项
      team: node
     annotations: # 解析项,详细解释告警信息
      summary: "{{$labels.instance}}: has been down"
      description: "{{$labels.instance}}: job {{$labels.job}} has been down "
      value: {{$value}}

groups:

- name: test-rule
  rules:
  - alert: "内存报警"
    expr: 100 - ((node_memory_MemAvailable * 100) / node_memory_MemTotal) > 10
    for: 1s
    labels:
       severity: warning
    annotations:
       summary: "服务名:{{$labels.alertname}}"
       description: "业务500报警: {{ $value }}"
       value: "{{ $value }}"


- name: test-rule2
  rules:
  - alert: "内存报警"
    expr: 100 - ((node_memory_MemAvailable * 100) / node_memory_MemTotal) > 40
    for: 1s
    labels:
       severity: test
    annotations:
       summary: "服务名:{{$labels.alertname}}"
       description: "业务500报警: {{ $value }}"
       value: "{{ $value }}"

3.5、告警信息生命周期的3中状态

1)inactive:表示当前报警信息即不是firing状态也不是pending状态

2)pending:表示在设置的阈值时间范围内被激活的

3)firing:表示超过设置的阈值时间被激活的

3.6、结果展示

启动prometheus和alertmanager,满足报警条件后,就可以收到报警邮件了。

1. 浏览器界面化告警展示

在浏览器输入alertmanager的配置地址,即可查看所监控到的报警信息

http://localdns:9093/#/alerts

如图所示:

2 . 邮箱告警展示

   1)原始邮箱告警展示

 2)模板邮箱告警展示

 3、企业微信告警展示

四. Prometheus与其他模块对接

  由于Prometheus灵活的接口配置和数据获取方式,可以很灵活的与其他模块进行对接,用于实时监控多个模块。

包括以下常用模块:

1.node_exporter

用户监控节点虚拟机的指标信息。

下载地址:https://github.com/prometheus/node_exporter/releases/download/v0.17.0-rc.0/node_exporter-0.17.0-rc.0.linux-386.tar.gz

2.jmx_exporter

下载地址:https://repo1.maven.org/maven2/io/prometheus/jmx/jmx_prometheus_javaagent/0.3.1/jmx_prometheus_javaagent-0.3.1.jar

3.elasticsearch_exporter

下载地址:https://github.com/justwatchcom/elasticsearch_exporter/releases/download/v1.0.4rc1/elasticsearch_exporter-1.0.4rc1.linux-386.tar.gz

4.redis_exporter

下载地址:https://github.com/oliver006/redis_exporter/releases/download/v0.22.0/redis_exporter-v0.22.0.linux-386.tar.gz

5.mysqld_exporter

下载地址;https://github.com/prometheus/mysqld_exporter/releases/download/v0.11.0/mysqld_exporter-0.11.0.linux-386.tar.gz

6.postgres_exporter

下载地址:https://github.com/wrouesnel/postgres_exporter/releases/download/v0.4.7/postgres_exporter_v0.4.7_linux-amd64.tar.gz

7.mongodb_exporter

下载地址:https://github.com/dcu/mongodb_exporter/releases/download/v1.0.0/mongodb_exporter-linux-amd64

8.statsd_exporter

下载地址:https://github.com/prometheus/statsd_exporter/releases/download/v0.8.0/statsd_exporter-0.8.0.linux-amd64.tar.gz

9.mesos_exporter

下载地址:https://github.com/mesos/mesos_exporter/releases/download/v1.1.1/mesos_exporter-1.1.1.linux-arm64.tar.gz

10.apache_exporter

下载地址:https://github.com/Lusitaniae/apache_exporter/releases/download/v0.5.0/apache_exporter-0.5.0.linux-amd64.tar.gz

11.hadoop_exporter

下载地址:https://github.com/wyukawa/hadoop_exporter

12.logstash_exporter

下载地址:https://github.com/BonnierNews/logstash_exporter/archive/v0.1.2.tar.gz

13.sql_exporter

下载地址:https://github.com/justwatchcom/sql_exporter/releases/download/v0.2.0/sql_exporter-0.2.0.linux-amd64.tar.gz

14.oracle_exporter

下载地址:https://github.com/iamseth/oracledb_exporter/releases/download/0.0.8/oracledb_exporter.linux-amd64

15.zookeeper_exporter

下载地址1:https://github.com/carlpett/zookeeper_exporter/releases/download/v1.0.1/zookeeper_exporter-1.0.1.linux-amd64.tar.gz

下载地址2:https://github.com/carlpett/zookeeper_exporter/releases/download/v1.0.2/zookeeper_exporter

16.influxdb_exporter

下载地址:https://github.com/prometheus/influxdb_exporter/releases/download/v0.1.0/influxdb_exporter-0.1.0.linux-amd64.tar.gz

17.zabbix_exporter

下载地址:https://github.com/MyBook/zabbix-exporter/archive/1.0.2.tar.gz

18.opentsdb_exporter

下载地址:https://github.com/cloudflare/opentsdb_exporter/archive/0.0.3.tar.gz

19.grafana_exporter

下载地址:https://github.com/frodenas/grafana_exporter/releases/download/v0.1.0/grafana_exporter-0.1.0.linux-amd64.tar.gz

20.json_exporter

下载地址:https://github.com/sciffer/json_exporter

 
 more /usr/local/mysqld_exporter/.my.cnf
[client]
host=192.168.11.131
port=3306
user=mysql_monitor
password=mysql_monitor


/**  创建账号  */
select host,user,authentication_string,plugin,grant_priv,super_priv from mysql.user;

 CREATE USER 'mysql_monitor'@'%' identified by 'mysql_monitor';
 
 GRANT REPLICATION CLIENT, PROCESS ON *.* TO 'mysql_monitor'@'%';
 
  GRANT SELECT ON performance_schema.* TO 'mysql_monitor'@'%';
 
  flush privileges;
 
  firewall-cmd --zone=public --add-port=9100/tcp --permanent

    firewall-cmd --zone=public --add-port=9104/tcp --permanent
    
firewall-cmd --reload

firewall-cmd --list-ports

 nohup  /usr/local/node_exporter/node_exporter  &
 
  nohup  /usr/local/mysqld_exporter/mysqld_exporter --config.my-cnf=/usr/local/mysqld_exporter/.my.cnf &
 
 
 IP:9090/targets
  IP:3000/targets
 

发布了216 篇原创文章 · 获赞 89 · 访问量 45万+

猜你喜欢

转载自blog.csdn.net/eagle89/article/details/105050570