zoukankan      html  css  js  c++  java
  • 采用prometheus 监控mysql

    1. prometheus 是什么

    开源的系统监控和报警工具,监控项目的流量、内存量、负载量等实时数据。

    它通过直接或短时jobs中介收集监控数据,在本地存储所有收集到的数据,并且通过定义好的rules产生新的时间序列数据,或发送警报。通过其它api可以将采集到的数据可视化。

    2. 怎么实现监控

    简单的说,主要就是以下几个步骤:

    1. 配置待监控的各个服务器,在每个服务器本地收集并存储数据。如果采用第三方系统收集数据metrics,且数据不是prometheus时序对,则需要定义exporter将那些metrics export为prometheus时序对。现在有很多已经定义好的官方或第三方的exporters有些软件抓取的数据直接就是prometheus格式的。
    2. 找一台服务器部署prometheus服务。然后修改配置文件,设定监控对象的ip地址和端口等。启动prometheus,之后prometheus就会用轮询的方式去各个服务器pull数据。
    3. 分析数据。prometheus提供了强大的查询库,可以定制收集到的数据。prometheus提供了browser的结果呈现,也可以配置使用第三方的数据可视化平台。

    部署监控mysql

    以一个例子来说明部署流程。

    安装和运行prometheus

    有很多种安装方法,这里我使用预编译的二进制文件。到这里下载。之后解压,terminal中输入./prometheus,回车启动prometheus服务。

    监控prometheus自己

    打开解压后的prometheus目录,发现其中有个prometheus.yml文件。prometheus.yml是设置监控对象等的配置文件。打开prometheus.yml,默认的prometheus.yml的初始配置如下:

    global:
      scrape_interval:     15s # By default, scrape targets every 15 seconds.
    
      # Attach these labels to any time series or alerts when communicating with
      # external systems (federation, remote storage, Alertmanager).
      external_labels:
        monitor: 'codelab-monitor'
    
    # 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'
    
        # Override the global default and scrape targets from this job every 5 seconds.
        scrape_interval: 5s
    
        static_configs:
          - targets: ['localhost:9090']
    

    这里设定监控目标为localhost:9090,即prometheus自己。浏览器打开localhost:9090,就能访问prometheus提供的可视化界面。localhost:9090/metrics提供了所有的监控数据信息。其中有一条prometheus_target_interval_length_seconds,表示真实的数据获取间隔,在prometheus首页输入它并回车,就可以看到一系列的数据,它们有不同quantile,从0.01至0.99不等。quantitle表示有多少比例的数据在这个值以内。如果只关注0.99的数据,可以输入prometheus_target_interval_length_seconds{quantile="0.99"}查询。查询还支持函数,比如count(prometheus_target_interval_length_seconds)可 以查询数量。
    如果想要查询结果直接包含数量那个数据,创建一个prometheus.rules文件,在文件中定义这条规则,然后在prometheus.yml中配置rules文件。

    //prometheus.rules
    test:prometheus_target_interval_length_seconds:count = count(prometheus_target_interval_length_seconds)
    
    //prometheus.yml
    # my global config
    global:
      scrape_interval:     15s # By default, scrape targets every 15 seconds.
      evaluation_interval: 15s # By default, scrape targets every 15 seconds.
      # scrape_timeout is set to the global default (10s).
    
      # Attach these labels to any time series or alerts when communicating with
      # external systems (federation, remote storage, Alertmanager).
      external_labels:
          monitor: 'codelab-monitor'
    
    # Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
    rule_files:
      - "prometheus.rules"
      # - "second.rules"
    
    # 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'
    
        # Override the global default and scrape targets from this job every 5 seconds.
        scrape_interval: 5s
    
        # metrics_path defaults to '/metrics'
        # scheme defaults to 'http'.
    
        static_configs:
          - targets: ['localhost:9090']
    

    之后,就可以直接输入test:prometheus_target_interval_length_seconds:count查询数据了。这里rule比较简单,如果有一些常用的但比较复杂的数据,都可以用rule的方法来定义获取。

    监控mysql

    修改prometheus.yml,在文件最后添加:

      - job_name: 'mysql'
    
        # Override the global default and scrape targets from this job every 5 seconds.
        scrape_interval: 5s
    
        # metrics_path defaults to '/metrics'
        # scheme defaults to 'http'.
    
        static_configs:
          - targets: ['localhost:9104']
            labels:
              instance: db1
    

    重启prometheus服务:

    $ ./prometheus -config.file=prometheus.yml
    

    再打开localhost:9090,查看Status -> Targets页面下,就可以看到配置的两个target:一个是prometheus本身,StateUP,另一个是mysql,State DOWN,因为我们还没有配置监控mysql的服务。

    安装并运行mysql exporter

    在在这里下载并解压mysql exporter,或者直接使用docker:

    $ docker pull prom/mysqld-exporter
    

    mysqld_exporter需要连接到mysql,需要mysql的权限,需要先为他创建用户并赋予所需的权限:

    CREATE USER 'mysqlexporter'@'localhost' IDENTIFIED BY 'msyqlexporter';
    GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'exporter'@'localhost'
      WITH MAX_USER_CONNECTIONS 3;
    

    然后在docker中运行exporter,其中DATA_SOURCE_NAME是环境变量,用于连接数据库。

    $ docker run -d           
    -p 9104:9104         
    -e DATA_SOURCE_NAME="mysqlexporter:mysqlexporter@(localhost:3306)/data_store" prom/mysqld-exporter
    

    此时再刷下localhost:9090/targets,就可以看到mysql的state转为UP,即已经成功的监测了mysql。

    总结

    核心的几个点:

    1. 数据收集
      1. 通过中介网关支持短时间序列数据收集
      2. 通过http pull的形式采集时间序列
      3. 可以通过自定义的rules产生新的时间数据系列(即定义一个rule,这个rule可能以已有的监控数据为输入,计算之后得到加工后的监控数据。加入该监控规则后,再监控时就能直接拿到这个加工后的数据了),例如官网的这个例子
      4. 监控目标:服务发现/静态配置。基于服务收集数据,而不是基于服务器收集数据。
    2. 数据存储
      1. 不依赖分布式存储,单个服务器节点工作
      2. 多维度数据模型(键值对确定的时间序列模型),解决了分布式存储的问题。就是说你的项目是分布在多个容器(例如每个服务器有一个容器)中,要获得整个项目的数据,需要监控这所有的容器。可以利用cAdvisor等从每个容器中拿数据,这样得到的数据是分散的,然后采用多维度数据模型配合查询语法就可以查到整个项目的流量数据。
    3. 数据查询
      1. 灵活的查询语言来利用上述维度数据模型
    4. 数据展示
      1. 各种展示面板:expression browser(无需配置)、Grafana等

    局限和适用

    局限:

    1. 单机缺点。因为它是以单个服务器节点工作为基础的,因此每个节点要存储监控数据,那么每个节点的监控数据量就会受限于存储空间。
    2. 内存占用量大(可以配置改善)。因为集成了leveldb(高效插入数据的数据库),在ssd盘下io占用高。

    适用于监控所有时间序列的项目。

  • 相关阅读:
    快速学习javaSE基础4---面向对象的编程
    eclipse 小技巧,设置输入任何字母都有快捷提示.
    快速学习javaSE基础3-java必须了解的基本语法2(熟记)
    快速学习javaSE基础3-java必须了解的基本语法1(熟记)
    数据可视化之导入数据
    pandas读取Excel文件
    pandas进阶
    给定地球上两点的经纬度,求两点之间沿地球表面最短的弧线距离
    06异常
    05设计模式
  • 原文地址:https://www.cnblogs.com/hf-cherish/p/6016374.html
Copyright © 2011-2022 走看看