如何在 Zabbix 中使用 PostgreSQL by ODBC 监控範本

PostgreSQL 监控涉及收集和分析各种性能指标,如查询执行时间、磁盘使用率、连线状态等。这不仅有助于预防故障,还能提高系统效能,优化资料库架构,以应对日益複杂的业务需求,在 Zabbix 中使用 PostgreSQL by ODBC 监控範本,我们可以容易的监控以下项目:

收集预写式日誌(Write-Ahead Log,WAL)的指标从 pg_stat_replication 收集指标按查询执行时间收集所有指标从每个数据库的 pg_locks 收集所有指标将 pg_stat_database 中的所有指标作为所有数据库的总和进行收集从 pg_stat_activity 收集所有指标从 pg_stat_bgwriter 收集所有指标

因为笔者没有 PostgreSQL 的环境,所以将使用 Docker 来快速进行演示。

PostgreSQL
使用 Docker 来进行安装

docker run -p 5432:5432 `  -e POSTGRES_USER=your_account `  -e POSTGRES_PASSWORD=your_password `  -e POSTGRES_DB=postgres `  --name db `  -d postgres:15

验证是否安装成功

docker exec db psql -V
psql (PostgreSQL) 15.4 (Debian 15.4-2.pgdg120+1)

pgAdmin
接下来我们需要安装 pgAdmin 图形化工具来方便管理 PostgreSQL

docker run -p 8080:80 `  -e 'PGADMIN_DEFAULT_EMAIL=user@domain.com' `  -e 'PGADMIN_DEFAULT_PASSWORD=your_password' `  -d dpage/pgadmin4

浏览 http://localhost:8080 就可以使用了

点选 Add New Server

填入 PostgreSQL 连线资讯

演示的环境準备就绪了

监控帐号建立
新增一个用于监控的 PostgreSQL 帐号并授予 pg_monitor 角色,使其可读取与执行各种监视视图和函数。

透过下列指令新增 zbx_monitor 并授予 pg_monitor 角色

CREATE USER zbx_monitor WITH PASSWORD 'your_password' INHERIT;GRANT pg_monitor TO zbx_monitor;

安装 PostgreSQL ODBC Driver
透过以下指令进行安装

sudo apt-get install odbc-postgresql

配置 ODBC 驱动程式管理员

sudo vi /etc/odbcinst.ini
[postgresql]Description = General ODBC for PostgreSQLDriver      = /usr/lib/x86_64-linux-gnu/odbc/psqlodbcw.soSetup       = /usr/lib/x86_64-linux-gnu/odbc/libodbcpsqlS.soFileUsage   = 1# Since 1.6 if the driver manager was built with thread support you may add another entry to each driver entry.# This entry alters the default thread serialization level.Threading   = 2

配置 ODBC 连接讯息

sudo vi /etc/odbc.ini
[your_database_name]Description = PostgreSQL database 1Driver  = postgresql#CommLog = /tmp/sql.logUsername = zbx_monitorPassword = your_password# Name of Server. IP or DNSServername = your_postgresql_ip# Database nameDatabase = postgres# Postmaster listening portPort = 5432# Database is read only# Whether the datasource will allow updates.ReadOnly = No# PostgreSQL backend protocol# Note that when using SSL connections this setting is ignored.# 7.4+: Use the 7.4(V3) protocol. This is only compatible with 7.4 and higher backends.Protocol = 7.4+# Includes the OID in SQLColumnsShowOidColumn = No# Fakes a unique index on OIDFakeOidIndex  = No# Row Versioning# Allows applications to detect whether data has been modified by other users# while you are attempting to update a row.# It also speeds the update process since every single column does not need to be specified in the where clause to update a row.RowVersioning = No# Show SystemTables# The driver will treat system tables as regular tables in SQLTables. This is good for Access so you can see system tables.ShowSystemTables = No# If true, the driver automatically uses declare cursor/fetch to handle SELECT statements and keeps 100 rows in a cache.Fetch = Yes# Bools as Char# Bools are mapped to SQL_CHAR, otherwise to SQL_BIT.BoolsAsChar = Yes# SSL modeSSLmode = disable# Send to backend on connectionConnSettings =

进行 ODBC 连接测试

isql -v your_database_name zbx_monitor your_password

登入成功,执行 select version() 看看。

+---------------------------------------+| Connected!                            ||                                       || sql-statement                         || help [tablename]                      || quit                                  ||                                       |+---------------------------------------+SQL> select version();+----------------------------------------------------------------------------------------------------------------------+| version                                                                                                              |+----------------------------------------------------------------------------------------------------------------------+| PostgreSQL 15.4 (Debian 15.4-2.pgdg120+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 12.2.0-14) 12.2.0, 64-bit  |+----------------------------------------------------------------------------------------------------------------------+SQLRowCount returns 11 rows fetchedSQL> quit

确认可以透过 ODBC 登入想要监控的资料库伺服器,接下来就可以回到 Zabbix 进行设定。

Zabbix
由于 Zabbix 预设没有 PostgreSQL by ODBC 监控範本,请参考官网连结下载后进行汇入。

确认没问题后,点选 Import。

依照自己的分类与命名习惯建立 Host 主机即可,然后在 Templates 选择 Templates/Databases,勾选 PostgreSQL by ODBC 。

接下来切换到 Macros 页面,添加以下 Macro。

{$PG.CONNSTRING}:Servername=your_postgresql_ip;Port=5432;Driver=/usr/lib/x86_64-linux-gnu/odbc/psqlodbcw.so;SSLmode=disable{$PG.PASSWORD}:your_password

验证一下 Last Data 是否有抓到了

然后可以到 Granafa 开心的画图了

监控 PostgreSQL 是确保资料库系统稳定运作的关键一环。透过持续监控,我们能够即时捕捉系统性能异常、识别潜在问题并迅速回应,确保系统运作在最佳状态。

今天的教学就到这边,谢谢大家。

参考文件

https://www.zabbix.com/integrations/postgresqlhttps://www.pgadmin.org/docs/pgadmin4/latest/container_deployment.htmlhttps://www.zabbix.com/documentation/6.0/en/manual/config/items/itemtypes/odbc_checks/unixodbc_postgresql

关于作者: 网站小编

码农网专注IT技术教程资源分享平台,学习资源下载网站,58码农网包含计算机技术、网站程序源码下载、编程技术论坛、互联网资源下载等产品服务,提供原创、优质、完整内容的专业码农交流分享平台。

热门文章