1. Prometheus组件总览

Prometheus可以监控服务是否正常吗 prometheus数据库监控_prometheus

如上图所示,Prometheus监控系统主要由三部分组成:监控数据采集(上图左)、监控数据存储处理(上图中)、监控数据显示上报(上图右)
  • exporters
    该工具用于将Prometheus不能识别的指标数据转换为Prometheus能识别的指标数据,它可以将各种应用程序、服务、操作系统等的指标数据暴露为HTTP接口,供Prometheus Server采集。
  • pushgateway
    该工具用于将短期任务的指标数据推送到Prometheus Server,它允许应用程序主动将监控指标数据推送到pushgateway,而不用等待Prometheus Server的定期采集。
  • node_exporter
    该工具用于收集Linux/Unix系统指标数据,它可以收集CPU、内存、磁盘、网络等系统指标数据,并将其暴露给Prometheus进行监控;它可以通过HTTP或HTTPS协议暴露指标数据,Prometheus可以通过配置文件或者命令行参数来指定node_exporter的地址和端口。
  • opengauss_exporter
    该工具用于收集openGauss数据库指标数据的exporter,它可以用于收集openGauss数据库的连接数、查询数、事务数、表空间使用情况等指标数据,并将其暴露给Prometheus进行监控;opengauss_exporter可以通过JDBC协议连接到openGauss数据库,并通过HTTP或HTTPS协议暴露指标数据,Prometheus可以通过配置文件或命令行参数来指定opengauss_exporter的地址和端口。
  • Prometheus Server
    负责从上述各种数据源采集指标数据,并将其存储到自带的时间序列数据库(TSDB)中;它可以用内置的查询语言PromQL来查询分析这些数据,以生成有用的警报和图表。
  • altermanager
    该组件用于处理和发送警报,它可以与Prometheus Server集成,接收来自Prometheus Server的警报,并根据用户自定义的规则进行处理和发送;它可以将警报发送到各种目标,包括电子邮件、Slack、PagerDuty等。
  • grafana
    该工具能对Prometheus的数据进行分析,它可以Prometheus的指标数据转换为各种图表、仪表盘等形式,方便用户进行数据分析和监控。

2. 部署顺序及环境

  • 部署顺序

为叙述清晰,本次部署顺序为先部署各组件,再部署Prometheus(实际可按任意顺序部署)。

  • 部署环境
    (1)Linux: openEuler 20.03, arm64架构,三台
    (2)openGauss: 一主二备,端口:15400;监控库:tpcc;主节点:20.20.20.139;备一:20.20.20.141;备二: 20.20.20.143; 版本:6.0.0

2. pushgateway部署

2.1 下载解压

  • 下载地址
https://github.com/prometheus/pushgateway/releases/download
  • 选择合适版本
    根据本次部署环境,选择pushgateway-1.4.3.linux-arm64.tar.gz
wget https://github.com/prometheus/pushgateway/releases/download/v1.4.3/pushgateway-1.4.3.linux-arm64.tar.gz
  tar -zxvf pushgateway-1.4.3.linux-arm64.tar.gz

注意:github下载较慢,若无法下载,请根据自身部署环境选择合适的镜像版本下载

2.2 准备自定义python脚本

进入解压目录,创建监控脚本prome.py,使用python脚本主动将监控指标数据推送到pushgateway,脚本检测项:CPU、系统内存、磁盘、tpmc、new_order、fd、Inode、IO等,可参考:

资源监控脚本示例

2.3 启动

  1. 启动监控脚本
nohup python3 master_prome_collection.py > master_prome_collection.log 2>&1 &
  1. 启动pushgateway
nohup ./pushgateway > pushgateway.log 2>&1 &
  1. 查看master_prome_collection.log和pushgateway.log,正常推送且无报错即可


    根据以上步骤,给三台主机都部署pushgateway。

3. node_exporter部署

3.1 下载解压

  • 下载地址
https://prometheus.io/download/node_exporter/releases/download
  • 选择合适版本
    根据本次部署环境,选择node_exporter-1.7.0.linux-arm64.tar.gz
wget https://github.com/prometheus/node_exporter/releases/download/v1.7.0/node_exporter-1.7.0.linux-arm64.tar.gz
tar -zxvf node_exporter-1.7.0.linux-arm64.tar.gz

3.2 启动

  1. 进入解压目录,启动node_exporter
nohup ./node_exporter > node_exporter.log 2>&1 &
  1. 查看node_exporter.log,无报错即可

    根据以上步骤,给三台主机都部署node_exporter。

3.3 保存json文件

需要将node_exporter对应的json文件保存到本地,后面部署grafana时会用到,文件可到grafana官网获取:

https://grafana.com/grafana/dashboards/

也可到以下地址获取:

https://gitee.com/wang_zhengyuan/python-and-doc/blob/master/doc/json/node-exporter-for-prometheus-dashboard-cn-v20201010_rev24.json

4. opengauss_exporter部署

4.1 数据库准备

  1. 安装openGauss数据库,本次部署为一主二备
  2. 配置md5加密
gs_guc reload -N all -I all -c "password_encryption_type=1"
gs_guc set -N all -I all -h "host all all 20.20.20.139/32 md5"
gs_om -t restart
  1. 创建并登录监控库,创建监控管理员
create database tpcc;
gsql gsql -d tpcc -p 15400 -U origin_user -W db_password -r
create user opengauss_exporter with monadmin password '******';
grant usage on schema dbe_perf to opengauss_exporter;
grant select on pg_stat_replication to opengauss_exporter;

当前版本若要统计复杂语句还需要sysadmin角色

alter role opengauss_exporter sysadmin;

以上步骤只需在主节点执行。

4.2 编译环境准备

opengauss_exporter的编译依赖go 1.15及以上版本(运行不需要)

yum install -y golang
go version

4.3 下载及编译

  • 下载地址
https://gitee.com/opengauss/openGauss-prometheus-exporter
  • 编译
git clone https://gitee.com/opengauss/openGauss-prometheus-exporter.git
cd openGauss-prometheus-exporter
make build

编译后生成bin/opengauss_exporter

4.4 启动

  1. 注册环境变量
export DATA_SOURCE_NAME="host=20.20.20.139 user=opengauss_exporter password=****** port=15400 dbname=tpcc sslmode=disable"
或
export DATA_SOURCE_NAME="postgresql://opengauss_exporter:******@20.20.20.139:15400/tpcc"
  1. 启动opengauss_exporter
nohup ./bin/opengauss_exporter --config="default_all.yml" >> opengauss_exporter.log 2>&1 &
  1. 查看日志,无报错即可(若有报错,可查看本文结尾的FAQ)

    将opengauss_exporter编译好之后,传到另外两台主机上,依以上步骤启动。

4.4 保存json文件

进入dashboard目录,将以下两个json文件保存到本地,后面部署grafana时会用到(OS.json与node_exporter对应的json文件功能重复,不必保存)

detail.json    overview.json

5. prometheus部署

5.1 下载解压

  • 下载地址
https://github.com/prometheus/prometheus/releases/download/
  • 选择合适版本
    根据本次部署环境,这里选择prometheus-2.49.0-rc.2.linux-arm64.tar.gz
wget https://github.com/prometheus/prometheus/releases/download/v2.49.0-rc.2/prometheus-2.49.0-rc.2.linux-arm64.tar.gz
tar -zxf prometheus-2.49.0-rc.2.linux-arm64.tar.gz

5.2 修改配置文件

解压后,根据环境情况修改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"]  # prometheus部署的ip及端口

  - job_name: 'pushgateway' # 任务目标名,可以理解成分组,每个分组包含具体的target组员,以下三个targets分别配置三台主机上的pushgateway。
    static_configs:
    - targets: ['20.20.20.139:9091'] # pushgateway部署的ip及端口,端口建议配成默认的9091
      labels:
        instance: pushgateway_pri  # 监控项标签,会在grafana显示,建议配成有意义的字符串(比如这个就代表主节点上部署的pushgateway)

    - targets: ['20.20.20.141:9091']
      labels:
        instance: pushgateway_st1

    - targets: ['20.20.20.143:9091']
      labels:
        instance: pushgateway_st2

  - job_name: 'node_exporter' # 任务目标名,可以理解成分组,每个分组包含具体的target组员,以下三个targets分别配置三台主机上的node_exporter。
    static_configs:
    - targets: ['20.20.20.139:9100'] # node_exporter部署的ip及端口,端口建议配成默认的9100
      labels:
        instance: node_exporter_pri  # 监控项标签,会在grafana显示,建议配成有意义的字符串(比如这个就代表主节点上部署的node_exporter)

    - targets: ['20.20.20.141:9100']
      labels:
        instance: node_exporter_st1

    - targets: ['20.20.20.143:9100']
      labels:
        instance: node_exporter_st2


  - job_name: 'opengauss_exporter' # 任务目标名,可以理解成分组,每个分组包含具体的target组员,以下三个targets分别配置三台主机上的opengauss_exporter。
    static_configs:
    - targets: ['20.20.20.139:9187'] # opengauss_exporter部署的ip及端口,端口建议配成默认的9187
      labels:
        instance: opengauss_exporter_pri  # 监控项标签,会在grafana显示,建议配成有意义的字符串(比如这个就代表主节点上部署的opengauss_exporter)

    - targets: ['20.20.20.141:9187']
      labels:
        instance: opengauss_exporter_st1

    - targets: ['20.20.20.143:9187']
      labels:
        instance: opengauss_exporter_st2

5.3 启动

  1. 进入解压目录,启动prometheus
nohup ./prometheus --config.file=prometheus.yml > prometheus.log 2>&1 &
  1. 查看日志,无报错即可
  2. 查看各监控项状态
    浏览器访问prometheus部署的ip:port/targets,可查看各监控项的状态

    若图中state字段显示为UP,则说明各监控项状态正常,若显示为DOWN,则说明对应的监控组件没有正常启动,需进一步排查。

6. grafana部署

6.1 yum安装

yum install -y https://dl.grafana.com/enterprise/release/grafana-enterprise-10.2.3-1.aarch64.rpm

6.2 启动

  1. 启动/停止/重启grafana
systemctl start/stop/restart grafana-server.service
  1. 查看grafana状态,无报错即可
systemctl status grafana-server.service

Prometheus可以监控服务是否正常吗 prometheus数据库监控_prometheus_02

  1. 访问grafana
    浏览器访问granana部署的ip:port,默认端口为3000,初始用户和密码均为admin,登录后需修改密码

6.3 添加prometheus数据源

登录grafana后,根据以下顺序将前面部署的prometheus添加到grafana(注意:grafana不同版本之间界面有所不同,本次演示以前面部署的grafana-enterprise-10.2.3-1版本为例)

Prometheus可以监控服务是否正常吗 prometheus数据库监控_数据_03


Prometheus可以监控服务是否正常吗 prometheus数据库监控_数据_04


Prometheus可以监控服务是否正常吗 prometheus数据库监控_数据_05


Prometheus可以监控服务是否正常吗 prometheus数据库监控_prometheus_06


Prometheus可以监控服务是否正常吗 prometheus数据库监控_数据库_07

配置数据源的name和prometheus的url:

Prometheus可以监控服务是否正常吗 prometheus数据库监控_prometheus_08

HTTP method 设为GET,测试连接并保存数据源,出现成功提示即可:

Prometheus可以监控服务是否正常吗 prometheus数据库监控_数据库_09

6.4 导入json文件

根据以下步骤,将前面保存的node-exporter-for-prometheus-dashboard-cn-v20201010_rev24.json、detail.json、overview.json导入grafana

Prometheus可以监控服务是否正常吗 prometheus数据库监控_json_10


Prometheus可以监控服务是否正常吗 prometheus数据库监控_数据_11


Prometheus可以监控服务是否正常吗 prometheus数据库监控_数据_12


Prometheus可以监控服务是否正常吗 prometheus数据库监控_json_13

6.5 效果展示

  • node_exporter监控界面

7. FAQ

  1. Panel plugin not found:
  • 原因:缺少grafana面板插件,导致部分仪表盘不能显示
  • 方法:

(1)查看grafana状态,查看grafana的配置文件

Prometheus可以监控服务是否正常吗 prometheus数据库监控_hg_14

(2)从配置文件中查看grafana的插件路径

Prometheus可以监控服务是否正常吗 prometheus数据库监控_json_15

(3)下载插件,解压后放到grafana的插件路径下

wget https://grafana.com/api/plugins/grafana-singlestat-panel/versions/latest/download -O grafana-singlestat-panel.zip
wget https://grafana.com/api/plugins/dalvany-image-panel/versions/latest/download -O dalvany-image-panel.zip
unzip grafana-singlestat-panel.zip
unzip dalvany-image-panel.zip
mv grafana-singlestat-panel /var/lib/grafana/plugins/
mv dalvany-image-panel /var/lib/grafana/plugins/

(4)重启grafana

systemctl restart grafana-server.service

(5)解决Only queries that return single series/table is supported问题

Prometheus可以监控服务是否正常吗 prometheus数据库监控_数据_16


按如下顺序配置表盘(表盘可以随便选择,有助于展示效果即可)

Prometheus可以监控服务是否正常吗 prometheus数据库监控_prometheus_17


Prometheus可以监控服务是否正常吗 prometheus数据库监控_json_18

效果:

Prometheus可以监控服务是否正常吗 prometheus数据库监控_prometheus_19

  1. Datasource prometheus was not fount 或面板空白
  • 原因:Query options未定义
  • 方法:两种情形都可按如下方式操作(Width of panel 可自定义,美观即可)
  • 效果:

  1. 数据库运行状态/运行时间/版本显示异常
  • 原因:Fields没有选定数据库节点
  • 方法:按如下方式操作
  • 效果:
  1. 面板显示No data或opengauss_exporter的日志有报错
  • 原因:启动opengauss_exporter的配置文件default_all.yml里面部分sql语法错误或因数据库版本迭代导致这些sql查询的视图或函数已废弃
  • 方法:使用监控管理员登录被监控的库,逐一执行default_all.yml里的查询sql,将报错的sql修正,再重启opengauss_exporter即可,举例如下:
-- sql格式错误
 select CURRENT_CATALOG AS datname,schemaname schema_name,relname table_name,indexrelname index_name,pg_table_size(indexrelid) as index_size
         from pg_stat_user_indexes
         where schemaname not in('pg_catalog', 'information_schema','snapshot')
         order by 4 desc limit 100;
         
-- 修正后
select CURRENT_CATALOG AS datname,schemaname as schema_name,relname as table_name,indexrelname as index_name,pg_table_size(indexrelid) as index_size
        from pg_stat_user_indexes
        where schemaname not in('pg_catalog', 'information_schema','snapshot')
        order by 4 desc limit 100;

注意:有的视图(如tb_slow_queries)已经废弃,与其有关的部分资源可能会监控不到,用户可以根据自己的需要修改sql语句从别的表或视图中查询对应监控项,若所有sql都修正,则opengauss_exporter无报错,若有部分sql无法修正,则opengauss_exporter每次执行这些sql时都会有报错,这并不会影响其他查询结果,可不解决。

  1. Datasource Redis was not found与Datasource Elasticsearch was not found


    该问题需要部署Redis和Elasticsearch,且提供的监控能力非必要,所以可不用解决。