主机环境:
[root@test ~]# uname -a
Linux test 3.10.0-693.el7.x86_64 #1 SMP Thu Jul 6 19:56:57 EDT 2017 x86_64 x86_64 x86_64 GNU/Linux
[root@test ~]# cat /etc/redhat-release
Red Hat Enterprise Linux Server release 7.4 (Maipo)
[root@test ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.139.132 netmask 255.255.255.0 broadcast 192.168.139.255
ether 00:0c:29:ca:b7:70 txqueuelen 1000 (Ethernet)
RX packets 356 bytes 41989 (41.0 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 224 bytes 41057 (40.0 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
-bash-4.2$ psql --version //查看PostgreSQL版本
psql (PostgreSQL) 9.2.23
Zabbix版本:3.4.9
主机防火墙和selinux关闭
Zabbix yum安装参考:https://www.tracymc.cn/archives/614,编译安装可参考https://www.tracymc.cn/archives/218.
PostgreSQL安装参考:https://www.tracymc.cn/archives/510
具体步骤:
1.安装libzbxpgsql
[root@test pgsql]# rpm -ivh https://download.postgresql.org/pub/repos/yum/9.2/redhat/rhel-7.3-x86_64/pgdg-redhat92-9.2-2.noarch.rpm //对应好系统以及PostgreSQL版本
获取https://download.postgresql.org/pub/repos/yum/9.2/redhat/rhel-7.3-x86_64/pgdg-redhat92-9.2-2.noarch.rpm
警告:/var/tmp/rpm-tmp.69Md0a: 头V4 DSA/SHA1 Signature, 密钥 ID 442df0f8: NOKEY
准备中... ################################# [100%]
正在升级/安装...
1:pgdg-redhat92-9.2-2 ################################# [100%]
[root@test pgsql]# yum install -y postgresql-libs
[root@test pgsql]# yum -y install libconfig
[root@test home]# wget http://cdn.cavaliercoder.com/libzbxpgsql/yum/zabbix32/rhel/7/x86_64/libzbxpgsql-1.1.0-1.el7.x86_64.rpm ////对应好系统版本
[root@test home]# rpm -ivh libzbxpgsql-1.1.0-1.el7.x86_64.rpm
准备中... ################################# [100%]
正在升级/安装...
1:libzbxpgsql-1.1.0-1 ################################# [100%]
[root@test home]# find / -name libzbxpgsql.so
/usr/lib64/zabbix/modules/libzbxpgsql.so
[root@test mozilla]# cd /usr/lib64/zabbix/modules/
[root@test modules]# ll
总用量 60
-rwxr-xr-x 1 root root 60856 2月 20 2017 libzbxpgsql.so
2.修改zabbix_agentd.conf配置文件
[root@test zabbix]# vi /etc/zabbix/zabbix_agentd.conf //yum安装zabbix默认路径为/etc/zabbix
LoadModulePath=/usr/lib64/zabbix/modules/
LoadModule=libzbxpgsql.so
主要修改如上两行,LoadModulePath和LoadModule就是第一步安装完libzbxpgsql-1.1.0-1.el7.x86_64.rpm以后find libzbxpgsql.so找到的路径
3.测试是否配置成功
[root@test zabbix]# zabbix_agentd -t pg.modver
zabbix_agentd [4953]: starting agent module libzbxpgsql 1.1.0
zabbix_agentd [4953]: using module configuration file: /etc/zabbix/libzbxpgsql.conf
pg.modver [s|libzbxpgsql 1.1.0, compiled for Zabbix 3.2.3] //说明安装成功
[root@test zabbix]# systemctl restart zabbix-agent //重启agent
4.创建数据库监控用户
[root@test zabbix]# su - postgres //psql命令是在postgres用户下,就像sqlplus是在oracle用户下
[root@test modules]# vi /var/lib/pgsql/data/pg_hba.conf //修改配置文件,否则数据库用户设置密码是会报"psql: 致命错误: 用户 "monitor" Ident 认证失败"
将IPv4下的ident修改为md5,如下:
# IPv4 local connections:
host all all 127.0.0.1/32 md5
[root@test zabbix]# systemctl restart postgresql //重启postgresql
-bash-4.2$ createdb monitordb //创建监控数据库
-bash-4.2$ psql
psql (9.2.23)
输入 "help" 来获取帮助信息.
postgres=# CREATE USER monitor WITH PASSWORD 'monitor'; //创建数据库监控用户
CREATE ROLE
postgres=# GRANT CONNECT ON DATABASE monitordb TO monitor; //授予连接权限
GRANT
验证刚刚创建的用户是否能登录:
-bash-4.2$ psql -h localhost -U monitor -p 5432 monitordb -W
用户 monitor 的口令:
psql (9.2.23)
输入 "help" 来获取帮助信息.
monitordb=> \l
资料库列表
名称 | 拥有者 | 字元编码 | 校对规则 | Ctype | 存取权限
-----------+----------+----------+-------------+-------------+-----------------------
monitordb | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | =Tc/postgres +
| | | | | postgres=CTc/postgres+
| | | | | monitor=c/postgres
postgres | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 |
template0 | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
(4 行记录)
PostgreSQL常见命令:https://www.tracymc.cn/archives/510
设置客户端连接PostgreSQL不需要密码请参考:https://www.tracymc.cn/archives/838
5.导入监控模板
模板地址:https://github.com/cavaliercoder/libzbxpgsql/blob/master/templates/Template_PostgreSQL_Server_3.0.xml
导入下载的模板,如下:
导入成功后可以在配置-模板查看已导入的模板,如下:
6.关联模板并设置宏变量
添加模板:
配置-主机点击相关主机进去配置模板,如下:
设置宏变量,如下:
{$PG_CONN} => host=localhost port=5432 user=monitor connect_timeout=10
{$PG_DB} => monitordb //就是步骤4新建的用户和库
注意:步骤4新建的用户的时候设置了密码,密码不能配置在宏变量这里,需要在postgresql数据库那台主机的zabbix(yum安装zabbix的话家目录默认为/var/lib/zabbix)用户家目录新建一个.pgpass文件,注意这个文件一定要在zabbix用户的家目录下,不然会报错获取不到数据,内容如下:
[root@test zabbix]# ll /var/lib/zabbix/.pgpass
-rw------- 1 zabbix zabbix 41 6月 8 11:43 /var/lib/zabbix/.pgpass
[root@test zabbix]# cat /var/lib/zabbix/.pgpass
localhost:5432:monitordb:monitor:monitor //格式为hostname:port:database:username:password
[root@test zabbix]# chmod 600 /var/lib/zabbix/.pgpass
相关参考文档:https://www.postgresql.org/docs/current/static/libpq-pgpass.html http://cavaliercoder.com/libzbxpgsql/documentation/remote-monitoring-agent/
设置客户端连接PostgreSQL不需要密码请参考:https://www.tracymc.cn/archives/838
7.zabbix web验证是否有数据
关联模板并设置宏变量后,稍等一会,查看web页面是否有数据.
部分截图如下:
hello,
zabbix_agentd -t pg_modver
zabbix_agentd [29384]: using module configuration file: /etc/zabbix/libzbxpgsql.conf
starting agent module libzbxpgsql 1.1.0pg_modver [m|ZBX_NOTSUPPORTED] [Unsupported item key.]
这个问题怎么处理呢?
libzbxpgsql 看下这个有没有装对