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

2023-03-24 00:00:00 配置 数据源 账号 可视化 同上

一、前言
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存储信息、集群节点同步状态、数据库锁监控等相关指标,具体指标如下:

指标名称 类型 标签组 指标描述 数据源获取方法
1 greenplum_cluster_state Gauge
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';

2 greenplum_cluster_uptime Gauge - 启动持续的时间 select extract(epoch from now() - pg_postmaster_start_time());
3 greenplum_cluster_sync Gauge - Master同步Standby状态? 1→ 正常;0→ 异常
SELECT count(*) from pg_stat_replication

where state='streaming'

4 greenplum_cluster_max_connections Gauge - 大连接个数
show max_connections;

show superuser_reserved_connections;

5 greenplum_cluster_total_connections Gauge - 当前连接个数
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();

6 greenplum_cluster_idle_connections Gauge - idle连接数 同上
7 greenplum_cluster_active_connections Gauge - active query 同上
8 greenplum_cluster_running_connections Gauge - query executing 同上
9 greenplum_cluster_waiting_connections Gauge - query waiting execute 同上
10 greenplum_node_segment_status Gauge
hostname; address; dbid; content;

preferred_role; port; replication_port

segment的状态status: 1(U)→ up; 0(D)→ down select * from gp_segment_configuration;
11 greenplum_node_segment_role Gauge
hostname; address; dbid; content;

preferred_role; port; replication_port

segment的role角色: 1(P)→ primary; 2(M)→ mirror 同上
12 greenplum_node_segment_mode Gauge
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 同上
13 greenplum_node_segment_disk_free_mb_size Gauge hostname segment主机磁盘空间剩余大小(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
14 greenplum_cluster_total_connections_per_client Gauge client 每个客户端的total连接数 select usename, count() total, count() filter(where current_query='') idle, count(*) filter(where current_query<>'') active from pg_stat_activity group by 1;
15 greenplum_cluster_idle_connections_per_client Gauge client 每个客户端的idle连接数 同上
16 greenplum_cluster_active_connections_per_client Gauge client 每个客户端的active连接数 同上
17 greenplum_cluster_total_online_user_count Gauge - 在线账号数 同上
18 greenplum_cluster_total_client_count Gauge - 当前所有连接的客户端个数 同上
19 greenplum_cluster_total_connections_per_user Gauge usename 每个账号的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;
20 greenplum_cluster_idle_connections_per_user Gauge usename 每个账号的idle连接数 同上
21 greenplum_cluster_active_connections_per_user Gauge usename 每个账号的active连接数 同上
22 greenplum_cluster_config_last_load_time_seconds Gauge - 系统配置加载时间 SELECT pg_conf_load_time()
23 greenplum_node_database_name_mb_size Gauge dbname 每个数据库占用的存储空间大小 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
24 greenplum_node_database_table_total_count Gauge dbname 每个数据库内表的总数量 SELECT count(*) as total from information_schema.tables where table_schema not in ('gp_toolkit','information_schema','pg_catalog');
25 greenplum_exporter_total_scraped Counter - - -
26 greenplum_exporter_total_error Counter - - -
27 greenplum_exporter_scrape_duration_second Gauge - - -
28 greenplum_server_users_name_list Gauge - 用户总数 SELECT usename from pg_catalog.pg_user;
29 greenplum_server_users_total_count Gauge - 用户明细 同上
30 greenplum_server_locks_table_detail Gauge
pid;datname;usename;

locktype;mode;

application_name;state;

lock_satus;query

锁信息
————————————————
版权声明:本文为CSDN博主「inrgihc」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/inrgihc/article/details/108686638

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


本文来源:https://blog.csdn.net/inrgihc/article/details/108686638

相关文章