基于Promethues与Grafana的Greenplum分布式数据库监控的实现

一、前言

Greenplum是面向数据仓库应用的分布式关系型MPP数据库,基于PostgreSQL开发,跟PostgreSQL的兼容性非常好,大部分PostgreSQL客户端工具及PostgreSQL应用都能运行在Greenplum平台上。GPCC是Greenplum数据库官方商业版的数据库监控软件,对于只能用得起开源的用户来说,只能考虑其他的监控方案了。本文里介绍一种基于Promethues与Grafana的Greenplum分布式数据库监控的实现方案

二、Promethues与Grafana简介

2.1、Prometheus简介

Prometheus是由SoundCloud开发的开源监控报警系统和时序列数据库(TSDB),使用Go语言开发。Prometheus目前在开源社区相当活跃。Prometheus性能也足够支撑上万台规模的集群。其架构图如下:

  • Prometheus Server, 负责从 Exporter 拉取和存储监控数据,并提供一套灵活的查询语言(PromQL)供用户使用。
  • Exporter, 负责收集目标对象(host, container…)的性能数据,并通过 HTTP 接口供 Prometheus Server 获取。
  • 可视化组件,监控数据的可视化展现对于监控方案至关重要。以前 Prometheus 自己开发了一套工具,不过后来废弃了,因为开源社区出现了更为优秀的产品 Grafana。Grafana 能够与 Prometheus 无缝集成,提供完美的数据展示能力。
  • Alertmanager,用户可以定义基于监控数据的告警规则,规则会触发告警。一旦 Alermanager 收到告警,会通过预定义的方式发出告警通知。支持的方式包括 Email、PagerDuty、Webhook 等.

2.2、Grafana简介

Grafana是一个跨平台的开源的度量分析和可视化工具,可以通过将采集的数据查询然后可视化的展示,并及时通知。它主要有以下六大特点:

  • 1、展示方式:快速灵活的客户端图表,面板插件有许多不同方式的可视化指标和日志,官方库中具有丰富的仪表盘插件,比如热图、折线图、图表等多种展示方式;
  • 2、数据源:Graphite,InfluxDB,OpenTSDB,Prometheus,Elasticsearch,CloudWatch和KairosDB等;
  • 3、通知提醒:以可视方式定义最重要指标的警报规则,Grafana将不断计算并发送通知,在数据达到阈值时通过Slack、PagerDuty等获得通知;
  • 4、混合展示:在同一图表中混合使用不同的数据源,可以基于每个查询指定数据源,甚至自定义数据源;
  • 5、注释:使用来自不同数据源的丰富事件注释图表,将鼠标悬停在事件上会显示完整的事件元数据和标记;
  • 6、过滤器:Ad-hoc过滤器允许动态创建新的键/值过滤器,这些过滤器会自动应用于使用该数据源的所有查询。

三、Greenplum监控的实现

Greenplum的监控可类似于PostgreSQL来实现,但又存在差异,不同点在于:

  • 要实现一个Greenplum的Exporter指标采集器;
  • 使用Grafana绘制一个可视化状态图;
  • 基于Prometheus配置报警规则(本文此部分略);

3.1、Greenplum的Exporter指标采集器

这里类比PostgreSQL数据库的Exporter实现方法,实现了一个Greenplum的Exporter,项目地址为:

https://github.com/tangyibo/greenplum_exporter

在greenplum_expoter里主要扩展了实现了客户连接信息、账号连接信息、Segment存储信息、集群节点同步状态、数据库锁监控等相关指标,具体指标如下:

No.指标名称类型标签组指标描述数据源获取方法
1greenplum_cluster_stateGauge

version;

master(master主机名);

standby(standby主机名)

gp 可达状态 ?:1→ 可用;0→ 不可用

SELECT count(*) from gp_dist_random('gp_id');

select version();

SELECT hostname from p_segment_configuration

where content=-1 and role='p';

2greenplum_cluster_uptimeGauge-启动持续的时间select extract(epoch from now() - pg_postmaster_start_time());
3greenplum_cluster_syncGauge-Master同步Standby状态? 1→ 正常;0→ 异常

SELECT count(*) from pg_stat_replication

where state='streaming'

4greenplum_cluster_max_connectionsGauge-最大连接个数

show max_connections;

show superuser_reserved_connections;

5greenplum_cluster_total_connectionsGauge-当前连接个数

select count(*) total, count(*) filter(where current_query='') idle,

count(*) filter(where current_query<>'') active, count(*) filter(where current_query<>'' and not waiting) running, count(*) filter(where current_query<>'' and waiting) waiting from pg_stat_activity where procpid <> pg_backend_pid();

6greenplum_cluster_idle_connectionsGauge-idle连接数同上
7greenplum_cluster_active_connectionsGauge-active query同上
8greenplum_cluster_running_connectionsGauge-query executing同上
9greenplum_cluster_waiting_connectionsGauge-query waiting execute同上
10greenplum_node_segment_statusGauge

hostname; address; dbid; content;

preferred_role; port; replication_port

segment的状态status: 1(U)→ up; 0(D)→ downselect * from gp_segment_configuration;
11greenplum_node_segment_roleGauge

hostname; address; dbid; content;

preferred_role; port; replication_port

segment的role角色: 1(P)→ primary; 2(M)→ mirror同上
12greenplum_node_segment_modeGauge

hostname; address; dbid; content;

preferred_role; port; replication_port

segment的mode:1(S)→ Synced; 2(R)→ Resyncing; 3(C)→ Change Tracking; 4(N)→ Not Syncing同上
13greenplum_node_segment_disk_free_mb_sizeGaugehostnamesegment主机磁盘空间剩余大小(MB)SELECT dfhostname as segment_hostname,sum(dfspace)/count(dfspace)/(1024*1024) as segment_disk_free_gb from gp_toolkit.gp_disk_free GROUP BY dfhostname
14greenplum_cluster_total_connections_per_clientGaugeclient每个客户端的total连接数select usename, count() total, count() filter(where current_query='') idle, count(*) filter(where current_query<>'') active from pg_stat_activity group by 1;
15greenplum_cluster_idle_connections_per_clientGaugeclient每个客户端的idle连接数同上
16greenplum_cluster_active_connections_per_clientGaugeclient每个客户端的active连接数同上
17greenplum_cluster_total_online_user_countGauge-在线账号数同上
18greenplum_cluster_total_client_countGauge-当前所有连接的客户端个数同上
19greenplum_cluster_total_connections_per_userGaugeusename每个账号的total连接数select client_addr, count() total, count() filter(where current_query='') idle, count(*) filter(where current_query<>'') active from pg_stat_activity group by 1;
20greenplum_cluster_idle_connections_per_userGaugeusename每个账号的idle连接数同上
21greenplum_cluster_active_connections_per_userGaugeusename每个账号的active连接数同上
22greenplum_cluster_config_last_load_time_secondsGauge-系统配置加载时间SELECT pg_conf_load_time()
23greenplum_node_database_name_mb_sizeGaugedbname每个数据库占用的存储空间大小SELECT dfhostname as segment_hostname,sum(dfspace)/count(dfspace)/(1024*1024) as segment_disk_free_gb from gp_toolkit.gp_disk_free GROUP BY dfhostname
24greenplum_node_database_table_total_countGaugedbname每个数据库内表的总数量SELECT count(*) as total from information_schema.tables where table_schema not in ('gp_toolkit','information_schema','pg_catalog');
25greenplum_exporter_total_scrapedCounter---
26greenplum_exporter_total_errorCounter---
27greenplum_exporter_scrape_duration_secondGauge---
28greenplum_server_users_name_listGauge-用户总数SELECT usename from pg_catalog.pg_user;
29greenplum_server_users_total_countGauge-用户明细同上
30greenplum_server_locks_table_detailGauge

pid;datname;usename;

locktype;mode;

application_name;state;

lock_satus;query

锁信息SELECT * from pg_locks
31greenplum_server_database_hit_cache_percent_rateGauge-缓存命中率select sum(blks_hit)/(sum(blks_read)+sum(blks_hit))*100 from pg_stat_database;
32greenplum_server_database_transition_commit_percent_rateGauge-事务提交

3.2、使用Grafana绘制一个可视化状态图

根据以上监测指标,即可使用Grafana配置图像了,具体内容请见:

https://github.com/tangyibo/greenplum_exporter/blob/master/grafana/greenplum_dashboard.json

四、Greenplum监控安装

该章节里讲述在CentOS7操作系统环境下的安装过程,所在的主机IP配置为:172.17.185.94。

4.1、安装Promethues

1、下载解压

wget https://github.com.cnpmjs.org/prometheus/prometheus/releases/download/v2.19.2/prometheus-2.19.2.linux-amd64.tar.gz
tar zxvf prometheus-2.19.2.linux-amd64.tar.gz
mv prometheus-2.19.2.linux-amd64 /usr/local/prometheus

2、创建用户

groupadd prometheus
useradd -g prometheus -m -d /var/lib/prometheus -s /sbin/nologin prometheus
chown prometheus.prometheus -R /usr/local/prometheus

3、创建Systemd服务

cat > /etc/systemd/system/prometheus.service <<EOF
[Unit]
Description=prometheus
After=network.target
[Service]
Type=simple
User=prometheus
ExecStart=/usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/prometheus.yml --storage.tsdb.path=/var/lib/prometheus/data 
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF

4、启动Prometheus

systemctl daemon-reload
systemctl start prometheus
systemctl status prometheus

5、访问WEB界面

访问如下地址以检测验证成功安装:

 http://172.17.185.94:9090

说明:如果访问不通,记得关闭主机的防火墙哦,命令为:systemctl stop firewalld.service

4.2、安装Greenplum-Expoter

1、下载安装包

wget https://gitee.com/inrgihc/greenplum_exporter/attach_files/622155/download/greenplum_exporter-1.1-rhel7.x86_64.rpm

说明:该Exporter支持Greenplum V5.x及Greenplum V6.x等版本。

2、安装软件包

[root@localhost ~]# rpm -ivh greenplum_exporter-1.1-rhel7.x86_64.rpm 
准备中...                          ################################# [100%]
正在创建信箱文件: 文件已存在
正在升级/安装...
   1:greenplum_exporter-1.1-rhel7     ################################# [100%]

软件默认安装在: /usr/local/greenplum_exporter

3、配置数据库连接

修改/usr/local/greenplum_exporter/etc/greenplum.conf文件中配置的greenplum数据库服务器的地址和gpadmin账号的密码(可见下)。默认如下:

[root@localhost etc]# pwd
/usr/local/greenplum_exporter/etc
[root@localhost etc]# cat greenplum.conf 
GPDB_DATA_SOURCE_URL=postgres://gpadmin:gpadmin@127.0.0.1:5432/postgres?sslmode=disable
[root@localhost etc]# 

4、启动Expoter程序

systemctl start greenplum_exporter
systemctl status greenplum_exporter

5、验证

访问如下地址以检测验证成功安装:

http://172.17.185.94:9297/metrics

说明:如果访问不通,记得关闭主机的防火墙哦

 

也可使用docker直接部署:

docker run -d -p 9297:9297 -e GPDB_DATA_SOURCE_URL=postgres://gpadmin:gpadmin@10.17.20.11:5432/postgres?sslmode=disable inrgihc/greenplum-exporter:latest

其中的环境变量GPDB_DATA_SOURCE_URL为连接greenplum6的连接串,该连接串以postgres://为前缀,具体格式如下:

postgres://gpadmin:password@10.17.20.11:5432/postgres?sslmode=disable
postgres://[数据库连接账号,必须为gpadmin]:[账号密码,即gpadmin的密码]@[数据库的IP地址]:[数据库端口号]/[数据库名称,必须为postgres]?[参数名]=[参数值]&[参数名]=[参数值]

4.3、配置Promethues

1、将greenplum_expter配置到prometheus.yml的采集target列表中

vim /usr/local/prometheus/prometheus.yml

在文件尾部追加如下配置:

  - job_name: 'greenplum'
    static_configs:
    - targets: ['127.0.0.1:9297']

完整配置如下:

2、重启prometheus服务

systemctl restart prometheus

3、检测promethus的web端配置已成功

4.3、安装Grafana可视化工具

1、下载

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

2、安装

yum localinstall -y grafana-7.0.5-1.x86_64.rpm

3、配置

配置文件位于/etc/grafana/grafana.ini,这里暂时保持默认配置即可

4、启动

systemctl start grafana-server

5、访问

访问地址:http://172.17.185.94:3000

用户名:admin

密码:admin

4.4、配置Grafana

当使用admin账号登录grafana后,再进行如下配置操作:

1、添加promethus数据源

2、配置greenplum状态图

https://github.com/tangyibo/greenplum_exporter/blob/master/grafana/greenplum_dashboard.json中配置的内容粘贴到上图红色框框内,或者填写仪表图的Grafana ID  13822, 然后点击load按钮即可加载。

五、Greenplum的Docker安装

5.1、Greenplum5

docker run -i -d -p 5432:5432 psavchenko/greenplum-db-5.9.0

账号:gpadmin

密码:welcome1

5.2、Greenplum6

mkdir -p /usr/local/gpdb/data
docker run -d -p 5432:5432 -v /usr/local/gpdb/data:/data  inrgihc/greenplum:6.11.1

账号:gpadmin

密码:greenplum

 

更多安装方式可参考:https://blog.csdn.net/inrgihc/article/details/108686153

评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值