這裡介紹一下如何在Zabbix 6下麵,使用預設自帶的模板MSSQL by ODBC來監控SQL Server資料庫。官方關於Template DB MSSQL By ODBC的介紹如下鏈接所示: https://www.zabbix.com/integrations/mssql 這個項目對應的gi ...
這裡介紹一下如何在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
掃描上面二維碼關註我
如果你真心覺得文章寫得不錯,而且對你有所幫助,那就不妨幫忙“推薦"一下,您的“推薦”和”打賞“將是我最大的寫作動力!
本文版權歸作者所有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接.