Zabbix 6服务器中配置MSSQL by ODBC的方法

时间:2023-04-25

这里介绍一下如何在Zabbix 6下面,使用默认自带的模板MSSQL by ODBC来监控SQL Server数据库。官方关于Template DB MSSQL By ODBC的介绍如下链接所示:

https://www.zabbix.com/integrations/mssql 

这个项目对应的github地址为:

https://git.zabbix.com/projects/ZBX/repos/zabbix/browse/templates/db/mssql_odbc?at=release/6.2 

安装ODBC

当前安装Zabbix 6.2的服务器为RHEL 8,具体版本为:

# more /etc/redhat-release 
Red Hat Enterprise Linux release 8.6 (Ootpa) 

在Zabbix Server或Zabbix Proxy Server上安装ODBC驱动,一般来说,安装Zabbix Server时已经安装了unixODBC等相关包

# yum list installed | grep unixODBC
unixODBC.x86_64                                       2.3.7-1.el8                               @local-rhel-8-for-x86_64-appstream-rpms
unixODBC-devel.x86_64                                 2.3.7-1.el8                               @local-rhel-8-for-x86_64-appstream-rpms 

如果没有安装的的话,使用下面命令安装。

#yum –y install unixODBC unixODBC-devel 

安装Microsoft ODBC 18

这里我们不打算安装freetds这个驱动,我们安装Microsoft ODBC 18驱动。具体可以参考下面链接:

https://learn.microsoft.com/en-us/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server?view=sql-server-ver15
https://learn.microsoft.com/zh-cn/sql/connect/odbc/linux-mac/known-issues-in-this-version-of-the-driver?view=sql-server-ver15#connectivity 

我们去下面链接下载对应的安装包:

https://packages.microsoft.com/rhel/8/prod/ 

本地安装:

#yum localinstall msodbcsql18-18.1.2.1-1.x86_64.rpm 

配置ODBC数据源

检查/etc/odbcinst.ini,你会看到已经配置了ODBC Driver信息

[ODBC Driver 18 for SQL Server]
Description=Microsoft ODBC Driver 18 for SQL Server
Driver=/opt/microsoft/msodbcsql18/lib64/libmsodbcsql-18.1.so.2.1
UsageCount=1 

具体的配置信息如下:

# cat /etc/odbcinst.ini
[PostgreSQL]
Description=ODBC for PostgreSQL
Driver=/usr/lib/psqlodbcw.so
Setup=/usr/lib/libodbcpsqlS.so
Driver64=/usr/lib64/psqlodbcw.so
Setup64=/usr/lib64/libodbcpsqlS.so
FileUsage=1
[MySQL]
Description=ODBC for MySQL
Driver=/usr/lib/libmyodbc5.so
Setup=/usr/lib/libodbcmyS.so
Driver64=/usr/lib64/libmyodbc5.so
Setup64=/usr/lib64/libodbcmyS.so
FileUsage=1
[FreeTDS]
Description=Free Sybase & MS SQL Driver
Driver=/usr/lib/libtdsodbc.so
Setup=/usr/lib/libtdsS.so
Driver64=/usr/lib64/libtdsodbc.so
Setup64=/usr/lib64/libtdsS.so
Port=1433
[MariaDB]
Description=ODBC for MariaDB
Driver=/usr/lib/libmaodbc.so
Driver64=/usr/lib64/libmaodbc.so
FileUsage=1
[Oracle]
Description=ODBC  for Oracle
Driver=/usr/lib/oracle/19.17/client64/lib/libsqora.so.19.1
[ODBC Driver 18 for SQL Server]
Description=Microsoft ODBC Driver 18 for SQL Server
Driver=/opt/microsoft/msodbcsql18/lib64/libmsodbcsql-18.1.so.2.1
UsageCount=1 

在/etc/odbc.ini中配置数据源,一般是你要监控的SQL Server数据库服务器。我们的一个例子如下所示:

[TEST]   ##指定的被监控的数据库名称,最好使用数据库服务器的机器名称
Driver = ODBC Driver 18 for SQL Server
Server = 192.168.7.115   ##服务器的IP地址
Port = 1433					  
Database = master 

注意,建议你加上TrustServerCertificate选项,具体配置如下所示,如果不加这个选项,你很有可能遇到错误2。

样例1:

[TEST]
Driver = ODBC Driver 18 for SQL Server
Server = 192.168.7.115
Port = 1433
Database = master
TrustServerCertificate = Yes 

样例2:

[TEST2]
Driver = ODBC Driver 18 for SQL Server
Server = 192.168.7.116,14033
Port = 14033
Database = master
TrustServerCertificate = Yes 

然后测试验证是否可以连接数据库,用具体的数据库账号密码替换username, password

# isql -v TEST username password
+---------------------------------------+
| Connected!                            |
|                                       |
| sql-statement                         |
| help [tablename]                      |
| quit                                  |
|                                       |
+---------------------------------------+
SQL> 

配置zabbix监控

在主机的配置中添加模板“MSSQL by ODBC”,然后选择“宏“,点击”继承以及主机宏“,然后分别给下面三个宏输入相关值

{$MSSQL.DSN}
{$MSSQL.PASSWORD}
{$MSSQL.USER} 

注意事项

1:你必须事先在被监控的SQL Server数据库上面创建相关账号并授权。如下所示:

USE [master]
GO
CREATE LOGIN [zbx_monitor] WITH PASSWORD=N'*******', DEFAULT_DATABASE=[master], CHECK_EXPIRATION=OFF, CHECK_POLICY=OFF
GO
USE [msdb]
GO
CREATE USER [zbx_monitor] FOR LOGIN [zbx_monitor]
GO
USE [master]
GO
GRANT VIEW SERVER STATE TO [zbx_monitor];
GRANT VIEW ANY DEFINITION TO  [zbx_monitor];
USE [msdb]
GO
GRANT SELECT ON msdb.dbo.sysjobs TO zbx_monitor;
GRANT SELECT ON msdb.dbo.sysjobservers TO zbx_monitor;
GRANT SELECT ON msdb.dbo.sysjobactivity TO zbx_monitor;
GRANT EXECUTE ON msdb.dbo.agent_datetime TO zbx_monitor; 

2:你输入了对应的账号密码后,最好点击右边的T,然后选择密文模式(抑或先选择密文模式),这样不会显示账号密码的明文了。这样比较安全。

选择密文保存后,你再次打开查看,此时密码以密文保存。无法查看明文密码了。

配置完成后,你要检查相关的监控选项,自动发现规则下的数据库监控是否正常,有没有一些问题出现。如果配置OK,Zabbix就会自动采集数据了。

配置可能遇到的错误:

错误1:

Cannot connect to ODBC DSN: [SQL_ERROR]:[HYT00][0][[unixODBC][Microsoft][ODBC Driver 18 for SQL Server]Login timeout expired]|[08001][258][[unixODBC][Microsoft][ODBC Driver 18 for SQL Server]TCP Provider: Error code 0x102]|[08001][258][[unixODBC][Microsoft][ODBC Driver 18 for SQL Server]A network-related or instance-specific error has occurred while establishin]

分析: 遇到这个问题是因为防火墙屏蔽了1433端口(也有可能是其他端口,默认是1433,根据实际情况确认)导致,找系统管理员或网络管理人员开放1433端口即可解决。

可以使用下面命令验证端口是否开放。

#telnet 192.168.xxx.xxx 1433 

如果端口是正常的情况下,依然报下面错误

Cannot connect to ODBC DSN: [SQL_ERROR]:[HYT00][0][[unixODBC][Microsoft][ODBC Driver 18 for SQL Server]Login timeout expired]|[08001][258][[unixODBC][Microsoft][ODBC Driver 18 for SQL Server]TCP Provider: Error code 0x102]|[08001][258][[unixODBC][Microsoft][ODBC Driver 18 for SQL Server]A network-related or instance-specific error has occurred while establishin]

那么需要进一步排查,例如,我遇到的案例,是因为odbc配置问题导致。

验证SQL是否能连接

# isql -v TEST2 xxxxx xxxx
[S1T00][unixODBC][Microsoft][ODBC Driver 18 for SQL Server]Login timeout expired
[08001][unixODBC][Microsoft][ODBC Driver 18 for SQL Server]TCP Provider: Error code 0x102
[08001][unixODBC][Microsoft][ODBC Driver 18 for SQL Server]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online.
[ISQL]ERROR: Could not SQLConnect 

/etc/odbc.ini中odbc配置如下所示:

[TEST2]
Driver = ODBC Driver 18 for SQL Server
Server = 192.168.7.116
Port = 14033
Database = master
TrustServerCertificate = Yes 

改为下面配置就正常了,似乎参数Port不生效。

[TEST2]
Driver = ODBC Driver 18 for SQL Server
Server = 192.168.7.116,14033
Port = 14033
Database = master
TrustServerCertificate = Yes 

错误2:

Cannot connect to ODBC DSN: [SQL_ERROR]:[08001][-1][[unixODBC][Microsoft][ODBC Driver 18 for SQL Server]SSL Provider: [error:1416F086:SSL routines:tls_process_server_certificate:c]|[08001][-1][[unixODBC][Microsoft][ODBC Driver 18 for SQL Server]Client unable to establish connection]

遇到这个错误是因为加密使用的是数据库上的证书。这个证书需要通过证书机构的验证,除非设置了信任证书的属性(SQL_COPT_SS_TRUST_SERVER_CERTIFICATE, SQL_TRUST_SERVER_CERTIFICATE_YES)或者,链接字符串包含TrustServerCertificate=yes。只要其中任意一项是 true,并且数据库上没有证书的话,那么数据库服务器就能用自己生成并签名的证书来加密链接。

修改/etc/odbc.ini配置文件,增加红色部分(TrustServerCertificate = Yes)即可解决问题。

# cat /etc/odbc.ini
[TEST]
Driver = ODBC Driver 18 for SQL Server
Server = 192.168.63.115
Port = 1433
Database = master
TDS_Version = 8.0
TrustServerCertificate = Yes 
    收藏