Zabbix template for Microsoft SQL Server介绍git
这里介绍Zabbix下监控Microsoft SQL Server数据库很是好用的一个模板,模板名为“Zabbix template for Microsoft SQL Server”,此模板的下载地址为:github
Zabbix share的地址:sql
https://share.zabbix.com/databases/microsoft-sql-server/template-for-microsoft-sql-servershell
GitHub的地址:数据库
https://github.com/MantasTumenas/Zabbix-template-for-Microsoft-SQL-Serversegmentfault
下面的实验、测试均为Zabbix 5.x,其它Zabbix版本没有通过测试验证。另外,建议使用GitHub下Microsoft SQL Server目录下的模板。感受这个模板遇到的问题比较少,若是你使用Zabbix share下的模板,问题多到烦死你,除非你有能力Fix掉这些问题。服务器
解压GitHub下的模板文件(Zabbix-template-for-Microsoft-SQL-Server-master.zip),你就会发现下面分三个(Zabbix share的只有两个目录)目录,分别以下命名:app
Microsoft SQL Server #分支版本,这里部署的是这个模板。
Without SQL instance discovery #适用于单实例SQL Server监控
With SQL instance discovery #适用于多实例SQL Server监控
Zabbix share下模板(Zabbix Template for Microsoft SQL Server.zip)的目录:ide
Without SQL instance discovery #适用于单实例SQL Server监控
With SQL instance discovery #适用于多实例SQL Server监控
Microsoft SQL Server下还有下面个目录,具体以下所示:测试
Documentation #下面是Zabbix template for Microsoft SQL Server的文档资料,绝对是我见过的Zabbix模板里面最详细的资料
Scripts #下面是Powe12rShell监控脚本
Template #下面是Template模板
User parameters #下面有一个文件userparams.conf,里面定义了User parameters参数的一些样例
Zabbix Value Mapping #下面有SQL Agent Job status.xml和SQL Database status.xml这两个文件。里面定义了一些映射值。
这个模板包含这些功能和特征,以下所示:
Features
• MS SQL performance counters.
• MS SQL instance Low Level Discovery.
• MS SQL database Low Level Discovery.
• MS SQL agent job Low Level Discovery.
• MS SQL database backup monitoring.
• MS SQL database mirroring monitoring.
• MS SQL Always On monitoring.
• MS SQL Log Shipping monitoring.
支持的版本,详细信息请见下面介绍:
Supported versions
Tested on Microsoft SQL Server 2012, 2014 and 2016. It may work with earlier versions, but some items (with missing performance counters) may be unsupported. For the extensive overview on the performance counters difference between MS SQL 2008 and MS SQL 2012 you can read here (https://blog.dbi-services.com/sql-server-2012-new-perfmon-counters/).
Tested on Zabbix 3.4.0. It may work with earlier versions, but some items (for example service.info[service,<param>]) may be unsupported. The template was started on Zabbix 2.4.0 but after each new Zabbix version, objects were modified or new things were added.
注意:这里测试的环境为Zabbix 5.x, 因此这个模板也是支持Zabbix 5.x的,请知晓!
部署过程
Without SQL instance discovery模板部署
官方文档的部署步骤:
1. Import templates via Configuration >> Templates:
• “Template Microsoft SQL Server DE Tier 3.xml”
• “Template Microsoft SQL Server DE Tier 2.xml”
• “Template Microsoft SQL Server DE Tier 1.xml”
• “Template Microsoft SQL Server SA Tier 3.xml”
2. Import value mappings via Administration >> General >> Value mapping:
• “SQL Agent Job status.xml”
• “SQL Database status.xml”
3. Copy catalog MSSQL with PowerShell scripts (*.ps1) to a location a Zabbix Agent can access (by default “C:\...\Zabbix\bin\”).
4. Copy 3 *.conf files from catalog “User parameters” to a location a Zabbix Agent can access (by default “C:\...\Zabbix\”).
5. Update “zabbix_agentd.win.conf”:
• add line “Include= C:\Program Files\Zabbix\mssql.agent.userparams.conf”.
• add line “Include= C:\Program Files\Zabbix\mssql.backup.userparams.conf”.
• add line “Include= C:\Program Files\Zabbix\mssql.basic.userparams.conf”.
6. Grant rights for Zabbix Agent service account. It needs read rights on tables:
• msdb.dbo.sysjobhistory
• msdb.dbo.sysjobs
• master.sys.databases
• msdb.dbo.backupset
• msdb.dbo.log_shipping_monitor_secondary.
7. By default, Zabbix Agent service account is NT AUTHORITY\SYSTEM which is already in SQL Server. If you need to monitor mirrored databases or databases in Always On, you will have to give Zabbix Agent’s service account (NT AUTHORITY\SYSTEM by default) sysadmin rights. More about it here.
8. Restart Zabbix Agent.
9. Depending on your SQL server edition and monitoring requirements select and add templates to a host.
10. Modify macros in templates according to your needs. Default values are below:
Macros |
Macros meaning |
Value |
Meaning |
Trigger |
{$SYSDBFTIME1} |
Sys db full backup time value 1 |
25 |
25 hours |
Information |
{$SYSDBFTIME2} |
Sys db full backup time value 2 |
50 |
50 hours |
Low |
{$SYSDBFTIME3} |
Sys db full backup time value 3 |
75 |
75 hours |
Medium |
{$UDBDTIME1} |
User db diff backup time value 1 |
48 |
2 days |
Information |
{$UDBDTIME2} |
User db diff backup time value 2 |
72 |
3 days |
Low |
{$UDBDTIME3} |
User db diff backup time value 3 |
96 |
4 days |
Medium |
{$UDBFTIME1} |
User db full backup time value 1 |
168 |
7 days |
Information |
{$UDBFTIME2} |
User db full backup time value 2 |
192 |
8 days |
Low |
{$UDBFTIME3} |
User db full backup time value 3 |
216 |
9 days |
Medium |
{$UDBLTIME1} |
User db log backup time value 1 |
30 |
30 minutes |
Information |
{$UDBLTIME2} |
User db log backup time value 2 |
60 |
60 minutes |
Low |
{$UDBLTIME3} |
User db log backup time value 3 |
90 |
90 minutes |
Medium |
{$EVENTLOGTIME} |
Event log recovery time value |
28h |
28 hours |
Medium |
{$DAYS} |
Maintenance job time value |
7 |
7 days |
None |
11. “Template Microsoft SQL Server SA Tier 3.xml” lets you discover SQL agent jobs. Discovery rules consist of:
• “SQL Server Agent Discovery” – discover SQL Agent service.
• “SQL Server Agent Jobs P1 Discovery” – discover SQL Agent jobs.
• “SQL Server Agent Jobs P2 Discovery” – discover SQL Agent jobs.
• “SQL Server Agent Jobs P3 Discovery” – discover SQL Agent jobs.
12. Difference between “SQL Server Agent Jobs P1 / P2 / P3 Discovery” are triggers. They can be configured differently. For example:
• “SQL Server Agent Jobs P1 Discovery” – alerts after trigger failed. Good for monitoring jobs, which need immediate attention. Like failed job “CHECKDB”.
• “SQL Server Agent Jobs P2 Discovery” – alerts after trigger failed two times. Good for monitoring jobs, which need attention, but not immediate. For example, job “DB LOG BACKUP” failed 1st time, but it will run again in 30 minutes. If 2nd time it fails again, then alert is raised.
• “SQL Server Agent Jobs P3 Discovery” – alerts after trigger failed but with additional conditions. Good for monitoring jobs, which do not need immediate attention. Like failed job “IndexOptimize”. Alert will be raised only during Monday – Friday, during 08:00 – 16:00. If you want to change day and hour parameters, you can do it directly in triggers.
• In mssql.agent.userparams.conf I placed 2 additional user parameters. In case you need to create your own custom items for monitoring P(riority)4 and P(priority)5 jobs.
13. Every discovery rule “SQL Server Agent Jobs P1 / P2 / P3 Discovery” has its filters there you can enter the job name, you want to associate with a selected rule:
If you leave a filter empty, all agent jobs will be discovered. To avoid that, I entered a simple place holder for every rule – ENTER_JOB_NAME.
下面结合我的的操做用中文简单描述一下:
1:在“配置”-> "模板“下导入下面四个模板:
• “Template Microsoft SQL Server DE Tier 3.xml”
• “Template Microsoft SQL Server DE Tier 2.xml”
• “Template Microsoft SQL Server DE Tier 1.xml”
• “Template Microsoft SQL Server SA Tier 3.xml”
注意,从Zabbix share上下载的模板,只有下面两个模板:
“Template SQL Server Instance 0 DE.xml”
“Template SQL Server Instance 0 SA.xml”
另外,默认状况下,这些模板位于Templates下面,我的喜欢将其分配到Templates/Databases组下面,方便往后的使用和管理! 步骤1只须要作一次就行了。这个是针对Zabbix Server而言。
2:在“管理”(Administration)->“通常”(General)-> "值映射"(Value mapping)下面导入值映射
“SQL Agent Job status.xml”
“SQL Database status.xml”
注意:步骤2也是只需作一次便可。
3:将Scirpt目录下的MSSQL目录(里面有一些PowerShell脚本)拷贝到Zabbix Agent能访问的路径(默认状况下,将其拷贝到“C:\...\Zabbix\bin\”下面),这里将其拷贝到C:\zabbix\bin\win64下面。固然你能够根据实际状况进行调整设定。也能够按照官方文档设定。
4:将User parameters目录下的3个配置文件拷贝到Zabbix Agent能访问的路径下(默认状况下为“C:\...\Zabbix\”),这里我将其拷贝到C:\zabbix\conf目录下面。
因为第三步,我将这些PowerShell脚本放在C:\zabbix\bin\win64\MSSQL,因此,这三个参数文件(mssql.agent.userparams.conf、mssql.backup.userparams.conf、mssql.basic.userparams.conf)不少配置信息必须修改。这个根据实际状况调整,以下例子所示:
例子(修改前)
# User parameter to get agent name. Tier 3 template.
UserParameter=tier3.agent.mssql.discovery,powershell.exe -NoProfile -ExecutionPolicy Bypass -File "C:\Program Files\zabbix\bin\MSSQL\DiscoveryDatabaseAgent\Discovery.mssql.instanceagentname.ps1"
# User parameter to get job name. Priority 5. Tier 3 template.
UserParameter=tier3.jobsp5.mssql.discovery,powershell.exe -NoProfile -ExecutionPolicy Bypass -File "C:\Program Files\zabbix\bin\MSSQL\DiscoveryDatabaseAgent\Discovery.mssql.jobname.ps1"
例子(修改后)
# User parameter to get agent name. Tier 3 template.
UserParameter=tier3.agent.mssql.discovery,powershell.exe -NoProfile -ExecutionPolicy Bypass -File "C:\zabbix\bin\win64\MSSQL\DiscoveryDatabaseAgent\Discovery.mssql.instanceagentname.ps1"
# User parameter to get job name. Priority 5. Tier 3 template.
UserParameter=tier3.jobsp5.mssql.discovery,powershell.exe -NoProfile -ExecutionPolicy Bypass -File "C:\zabbix\bin\win64\MSSQL\DiscoveryDatabaseAgent\Discovery.mssql.jobname.ps1"
5:更新zabbix_agentd.conf下的配置
• add line “Include= C:\Program Files\Zabbix\mssql.agent.userparams.conf”.
• add line “Include= C:\Program Files\Zabbix\mssql.backup.userparams.conf”.
• add line “Include= C:\Program Files\Zabbix\mssql.basic.userparams.conf”.
我的的设置以下,这个确定根据具体实际状况进行调整。
Include=C:\zabbix\conf\mssql.agent.userparams.conf
Include=C:\zabbix\conf\mssql.backup.userparams.conf
Include=C:\zabbix\conf\mssql.basic.userparams.conf
6:受权给Zabbix Agent服务器帐号权限,它须要下面一些表的查询查询
• msdb.dbo.sysjobhistory
• msdb.dbo.sysjobs
• master.sys.databases
• msdb.dbo.backupset
• msdb.dbo.log_shipping_monitor_secondary.
7:默认状况下,Zabbix Agent的服务帐号为NT AUTHORITY\SYSTEM,它是SQL Server下一个已经存在的帐号,若是你须要监控数据镜像或Always On下面的一些数据库,你须要授予Zabbix Agent的服务帐号sysadmin角色权限。更多参考相关资料。
8:重启Zabbix Agent服务。
9:在Zabbix Server上给相关须要监控的主机添加对应的模板。
以下所示,勾选下面四个模板。
此时,你就会在主机的配置里面看到关于SQL Server监控的一些应用集(Applications)选项(截图只是部分)
Zabbix share的模板配置略有区别,它有详细的配置文档,有兴趣的能够本身测试验证一下。下面是以前测试整理的简单步骤。
1:在“配置”-> "模板“下导入下面两个模板:
Template SQL Server Instance 0 DE.xml
Template SQL Server Instance 0 SA.xml
2:在“管理”(Administration)->“通常”(General)-> "值映射"(Value mapping)下面导入值映射
“SQL Agent Job status.xml”
“SQL Database status.xml”
3:将Discovery.mssql.server.ps1文件copy到Zabbix Agent能访问的地方,我的将其放置在C:\zabbix\bin\win64下面
4:编辑Discovery.mssql.server.ps1文件,在文件的第14行,找到下面脚本,用服务器名替换“InsertSQLInstanceName”
[Parameter(Mandatory = $false, Position = 2)]$SQLInstanceName="EnterInstanceName"
参考博客https://segmentfault.com/a/1190000019203337,也能够修改Discovery.mssql.server.ps1脚本,添加下面一段代码(红色部分),之后直接copy这个文件便可,不用作任何修改。这样省事方便不少。
Param(
[Parameter(Mandatory = $true, Position = 0)] [string]$select,
[Parameter(Mandatory = $false, Position = 1)][string]$2,
[Parameter(Mandatory = $false, Position = 2)]$SQLInstanceName="EnterInstanceName"
)
if ($SQLInstanceName -eq "EnterInstanceName")
{
$SQLInstanceName = $(hostname.exe)
}
5:修改zabbix_agentd.conf中的参数UserParameter, 若是你将文件Discovery.mssql.server.ps1放在C:\Program Files\zabbix\bin下面,那么就能够用userparams.conf中的值。
UserParameter=databases.mssql.discovery,powershell.exe -NoProfile -ExecutionPolicy Bypass -File "C:\Program Files\zabbix\bin\Discovery.mssql.server.ps1" JSONDBNAME
UserParameter=jobs.mssql.discovery,powershell.exe -NoProfile -ExecutionPolicy Bypass -File "C:\Program Files\zabbix\bin\Discovery.mssql.server.ps1" JSONJOBNAME
UserParameter=data.mssql.discovery[*],powershell.exe -NoProfile -ExecutionPolicy Bypass -File "C:\Program Files\zabbix\bin\Discovery.mssql.server.ps1" $1 "$2"
我的作了一些变跟。由于将文件Discovery.mssql.server.ps1放在C:\zabbix\bin\win64下面
UserParameter=databases.mssql.discovery,powershell.exe -NoProfile -ExecutionPolicy Bypass -File "C:\zabbix\bin\win64\Discovery.mssql.server.ps1" JSONDBNAME
UserParameter=jobs.mssql.discovery,powershell.exe -NoProfile -ExecutionPolicy Bypass -File "C:\zabbix\bin\win64\Discovery.mssql.server.ps1" JSONJOBNAME
UserParameter=data.mssql.discovery[*],powershell.exe -NoProfile -ExecutionPolicy Bypass -File "C:\zabbix\bin\win64\Discovery.mssql.server.ps1" $1 "$2"
6:给运行Zabbix Agent 服务的帐号授予数据库的相关权限,它须要访问msdb.dbo.sysjobhistory和msdb.dbo.sysjobs,默认状况,运行Zabbix Agent 服务的帐号为NT AUTHORITY\SYSTEM已经在数据库中。
固然你能够建立一个帐号,而后在Discovery.mssql.server.ps1中设置,取消$uid和$pwd的设置,填上建立的的帐号密码。
# Desenvolvido por Diego Cavalcante - 06/12/2017
# Monitoramento Windows SQLServer
# Versco: 1.1.0
# Criaeco = Versco 1.0.0 29/08/2017 (Script Bisico).
# Update = Versco 1.1.0 02/01/2018 (Obrigado @bernardolankheet, JOBSTATUS Retornava N = 5 Nunca Executado).
# Update = by Oleg D. and Mantas T. Translated to EN, added SQL Insance name.
# Parameters. Change Line 14 $SQLInstanceName="InstanceName" to correct instance name
Param(
[Parameter(Mandatory = $true, Position = 0)] [string]$select,
[Parameter(Mandatory = $false, Position = 1)][string]$2,
[Parameter(Mandatory = $false, Position = 2)]$SQLInstanceName="xxxx" #具体的实例名
)
#Login SQLInstanceName
#$uid = "Login" #具体的登陆名和密码
#$pwd = "Password"
7:重启Zabbix Agent服务
8:给相关服务器(host)添加模板。
9:若是须要的话,更新宏
10:默认状况下,须要添加两个模板,除非你数据库是SQL Server Express edition,那么你只须要添加模板“Template SQL Server Instance 0 DE Baseline”
11:最好将这两个模板分类到Templates/Databases群组下面,方便往后的使用和管理!
With SQL instance discovery 的模板建立也很是简单,跟上面的差别不是太大。按照官方文档的操做步骤,逐步操做便可。
使用总结
1:例如,YourSQlDba数据库的恢复模式为简单模式,只作了完整备份。那么监控就会触发告警,告诉你这个YourSQlDba数据库的没有作差别备份和事务日志备份。以下截图
若是你不想它触发告警,你能够在监控项(Item)里面找到“SQL Server Databases Discovery: SQL Instance MSSQLSERVER Database YourSQLDba: Diff Backup Status”,禁用这些监控项(Item)便可。
2:若是数据库实例上有脱机的数据库(offline),那么你必须禁用这个数据库的相关监控项(Item),不然,你会在Zabbix Agent的日志中发现大量相似这样的日志
...............................................................................
19120:20200826:154534.767 active check "perf_counter["\SQLServer:Databases(xxxx)\Log File(s) Used Size (KB)"]" is not supported: Cannot obtain performance information from collector.
19120:20200826:154534.768 active check "perf_counter["\SQLServer:Databases(xxxx)\Log Flush Wait Time"]" is not supported: Cannot obtain performance information from collector.
19120:20200826:154534.769 active check "perf_counter["\SQLServer:Databases(xxxx)\Log Flush Waits/sec"]" is not supported: Cannot obtain performance information from collector.
19120:20200826:154534.769 active check "perf_counter["\SQLServer:Databases(xxxx)\Log Flushes/sec"]" is not supported: Cannot obtain performance information from collector.
19120:20200826:154534.769 active check "perf_counter["\SQLServer:Databases(xxxx)\Log Growths"]" is not supported: Cannot obtain performance information from collector.
19120:20200826:154534.770 active check "perf_counter["\SQLServer:Databases(xxxx)\Log Shrinks"]" is not supported: Cannot obtain performance information from collector.
19120:20200826:154534.770 active check "perf_counter["\SQLServer:Databases(xxxx)\Log Truncations"]" is not supported: Cannot obtain performance information from collector.
..............................................................................
另外,若是不由用这个数据库的相关监控项(Item),那么你会在Zabbix的Queue队列里面看到大量被延迟的监控项(Item)。禁用了脱机数据库的相关Item后,你就会观察到Queue队列延迟的Item不见了。
3:你看到相似下面这样各类告警或信息。下面截图仅仅是部分截图,而后就是理解各类告警和解决问题了。
各种监控指标都有图形。能够查看这些指标的曲线图。
问题小结:
在使用Zabbix template for Microsoft SQL Server模板过程当中,也遇到了一些小问题,下面是这些问题的集合。下面绝大部分问题是Zabbix share下的模板才会遇到的。下面描述问题时尽可能标明是那个分支模板遇到的问题。强烈推荐使用GitHub上的分支版本。可让你绕过不少坑。
问题1:Zabbix Agent日志中出现下面错误。
764:20200715:140830.588 active check "perf_counter["\SQLServer:Databases(DBAInventory)\Active Transactions"]" is not supported: Cannot obtain performance information from collector.
764:20200715:140830.588 active check "perf_counter["\SQLServer:Databases(DBAInventory)\Data File(s) Size (KB)"]" is not supported: Cannot obtain performance information from collector.
764:20200715:140830.589 active check "perf_counter["\SQLServer:Databases(DBAInventory)\Log Bytes Flushed/sec"]" is not supported: Cannot obtain performance information from collector.
764:20200715:140830.589 active check "perf_counter["\SQLServer:Databases(DBAInventory)\Log File(s) Size (KB)"]" is not supported: Cannot obtain performance information from collector.
764:20200715:140830.590 active check "perf_counter["\SQLServer:Databases(DBAInventory)\Log File(s) Used Size (KB)"]" is not supported: Cannot obtain performance information from collector.
764:20200715:140830.590 active check "perf_counter["\SQLServer:Databases(DBAInventory)\Log Flush Wait Time"]" is not supported: Cannot obtain performance information from collector.
764:20200715:140830.590 active check "perf_counter["\SQLServer:Databases(DBAInventory)\Log Flush Waits/sec"]" is not supported: Cannot obtain performance information from collector.
764:20200715:140830.591 active check "perf_counter["\SQLServer:Databases(DBAInventory)\Log Flushes/sec"]" is not supported: Cannot obtain performance information from collector.
764:20200715:140830.591 active check "perf_counter["\SQLServer:Databases(DBAInventory)\Log Growths"]" is not supported: Cannot obtain performance information from collector.
764:20200715:140830.592 active check "perf_counter["\SQLServer:Databases(DBAInventory)\Log Shrinks"]" is not supported: Cannot obtain performance information from collector.
764:20200715:140830.592 active check "perf_counter["\SQLServer:Databases(DBAInventory)\Log Truncations"]" is not supported: Cannot obtain performance information from collector.
764:20200715:140830.592 active check "perf_counter["\SQLServer:Databases(DBAInventory)\Percent Log Used"]" is not supported: Cannot obtain performance information from collector.
检查分析发现,DBAInventory数据库被设置为脱机状态,这台服务器应用了模板"Template SQL Server Instance 0 DE Baseline",那么就会生成一些监控项(Items)和一些触发器(Triggers),这些Items和Tiggers的状态是“不支持的”(Not supported),因此在主机设置里面,经过过滤搜索数据库DBAInventory的监控项和触发器,以下所示,而后将其停用(Disable)后,zabbix_agentd.log中就不会出现这个错误信息了。
问题2:遇到 Timeout while executing a shell script.错误。
1364:20200709:085346.828 active check "jobs.mssql.discovery" is not supported: Timeout while executing a shell script.
1364:20200709:085842.183 Failed to execute command "powershell.exe -NoProfile -ExecutionPolicy Bypass -File "C:\zabbix\bin\win64\Discovery.mssql.server.ps1" JSONDBNAME": Timeout while executing a shell script.
1364:20200709:085842.183 active check "databases.mssql.discovery" is not supported: Timeout while executing a shell script.
修改zabbix_agentd.conf配置文件中的参数Timeout, 例如将Timeout调整为30
### Option: Timeout
# Spend no more than Timeout seconds on processing.
#
# Mandatory: no
# Range: 1-30
# Default:
# Timeout=3
Timeout=30
此时你就会发现zabbix_agentd.log不会出现这个错误了。
整理的文档,原本有十几个小问题,所有列在此处,不只感受很是混乱,并且占用了大量的篇幅,后面想一想,这里就简单列举一两个问题,后面有空,打算将这些问题以单篇展开述说。
参考资料:
https://share.zabbix.com/databases/microsoft-sql-server/template-for-microsoft-sql-server
https://github.com/MantasTumenas/Zabbix-template-for-Microsoft-SQL-Server