经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库/运维 » MS SQL Server » 查看文章
SQL Server Alwayson架构下 服务器 各虚拟IP漂移监控告警的功能实现
来源:cnblogs  作者:东山絮柳仔  时间:2019/6/28 8:44:57  对本文有异议

1.需求概括

我们知道,在SQL Server Alwayson 架构中,有多种虚拟IP,例如 WindowsCluster IP,ListenIP,角色高可用性IP(类似于侦听IP)。在某些条件下,例如系统故障,会触发虚拟IP的漂移,如何高效率、低延迟、更好地监控IP漂移情况,是我们DB的一个重要工作。

下面是我们的一个通过SQL Server 存储过程来实现的方案。

2.基本原理 

 周期性收集当前Server 上相应的IP地址,并与上个周期收集的结果比较判断,看那些IP发生了漂移变化。其主要流程图如下;

 

 

3.代码实现

表 DBA_ServerIPDataBase_OverCheck,主要存储 当前 (本收集周期) Server的信息(主要是IP信息、ServerName信息等),其创建脚本如下;

  1. /****** Object: Table [dbo].[DBA_ServerIPDataBase_OverCheck] Script Date: 2019/6/27 16:01:27 ******/
  2. SET ANSI_NULLS ON
  3. GO
  4.  
  5. SET QUOTED_IDENTIFIER ON
  6. GO
  7.  
  8. SET ANSI_PADDING ON
  9. GO
  10.  
  11. CREATE TABLE [dbo].[DBA_ServerIPDataBase_OverCheck](
  12. [LocalServerIP] [varchar](20) NULL,
  13. [ClusterName] [varchar](50) NULL,
  14. [ServerIP] [varchar](20) NULL,
  15. [ServerName] [varchar](100) NULL,
  16. [ServerFullName] [varchar](100) NULL,
  17. [ServerIPType] [varchar](20) NULL,
  18. [DataBaseName] [varchar](300) NULL,
  19. [DisabledFlag] [varchar](1) NULL,
  20. [CreateTime] [datetime] NULL,
  21. [CreateBy] [varchar](50) NULL,
  22. [ModifyTime] [datetime] NULL,
  23. [ModifyBy] [varchar](50) NULL
  24. ) ON [PRIMARY]
  25.  
  26. GO
  27.  
  28. SET ANSI_PADDING OFF
  29. GO
  30.  
  31. EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'Windows集群名称' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'DBA_ServerIPDataBase_OverCheck', @level2type=N'COLUMN',@level2name=N'ClusterName'
  32. GO
  33.  
  34. EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'IP地址' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'DBA_ServerIPDataBase_OverCheck', @level2type=N'COLUMN',@level2name=N'ServerIP'
  35. GO
  36.  
  37. EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'计算机对象名称' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'DBA_ServerIPDataBase_OverCheck', @level2type=N'COLUMN',@level2name=N'ServerName'
  38. GO
  39.  
  40. EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'计算机对象全称' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'DBA_ServerIPDataBase_OverCheck', @level2type=N'COLUMN',@level2name=N'ServerFullName'
  41. GO
  42.  
  43. EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'计算机对象全称' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'DBA_ServerIPDataBase_OverCheck', @level2type=N'COLUMN',@level2name=N'ServerIPType'
  44. GO
  45.  
  46. EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'0实时有效,1第一次失效,2第二次失效,3第三次失效,4第四次失效,5第五次失效,彻底删除' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'DBA_ServerIPDataBase_OverCheck', @level2type=N'COLUMN',@level2name=N'DisabledFlag'
  47. GO
  48.  
  49. EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'创建时间' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'DBA_ServerIPDataBase_OverCheck', @level2type=N'COLUMN',@level2name=N'CreateTime'
  50. GO
  51.  
  52. EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'创建人' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'DBA_ServerIPDataBase_OverCheck', @level2type=N'COLUMN',@level2name=N'CreateBy'
  53. GO

 

表 DBA_ServerIPDataBase_OverCheckOriginOrigin,主要存储 以前 (前一个收集周期) Server的信息,是用来比较变化的基准表,其表结构 与表DBA_ServerIPDataBase_OverCheck相同,创建脚本如下:

 

  1. /****** Object: Table [dbo].[DBA_ServerIPDataBase_OverCheckOriginOrigin] Script Date: 2019/6/27 16:56:28 ******/
  2. SET ANSI_NULLS ON
  3. GO
  4.  
  5. SET QUOTED_IDENTIFIER ON
  6. GO
  7.  
  8. SET ANSI_PADDING ON
  9. GO
  10.  
  11. CREATE TABLE [dbo].[DBA_ServerIPDataBase_OverCheckOriginOrigin](
  12. [LocalServerIP] [varchar](20) NULL,
  13. [ClusterName] [varchar](50) NULL,
  14. [ServerIP] [varchar](20) NULL,
  15. [ServerName] [varchar](100) NULL,
  16. [ServerFullName] [varchar](100) NULL,
  17. [ServerIPType] [varchar](20) NULL,
  18. [DataBaseName] [varchar](300) NULL,
  19. [DisabledFlag] [varchar](1) NULL,
  20. [CreateTime] [datetime] NULL,
  21. [CreateBy] [varchar](50) NULL,
  22. [ModifyTime] [datetime] NULL,
  23. [ModifyBy] [varchar](50) NULL
  24. ) ON [PRIMARY]
  25.  
  26. GO
  27.  
  28. SET ANSI_PADDING OFF
  29. GO

 

具体的存储过程 USP_DBA_ServerIPDataBase_OverCheck,用来实现 收集、比较、告警等功能,代码实现如下:

 

  1. /****** Object: StoredProcedure [dbo].[USP_DBA_ServerIPDataBase_OverCheck] Script Date: 2019/6/27 15:26:06 ******/
  2. SET ANSI_NULLS ON
  3. GO
  4.  
  5. SET QUOTED_IDENTIFIER ON
  6. GO
  7.  
  8.  
  9. CREATE PROCEDURE [dbo].[USP_DBA_ServerIPDataBase_OverCheck]
  10. AS
  11. BEGIN
  12.  
  13. DECLARE @ipline VARCHAR(200)
  14. DECLARE @ip VARCHAR(20)
  15. DECLARE @pingname VARCHAR(40)
  16. DECLARE @ServerName VARCHAR(100)
  17. DECLARE @ServerFullName VARCHAR(100)
  18. Declare @CurrDateTime nvarchar(20)
  19. Declare @PreDiffDateTime nvarchar(20) =''
  20.  
  21. Set @CurrDateTime=CONVERT(VARCHAR(19),GETDATE(),120)
  22. SET NOCOUNT ON
  23.  
  24. Declare @ServerIP VARCHAR(20)
  25. SET @ServerName=@@SERVERNAME
  26. SET @ServerIP = CAST(CONNECTIONPROPERTY('local_net_address') AS varchar(20))
  27. ----Set @ServerIP ='XXX.XXX.XXX.XXX' --如果不准确的话,请手动定义
  28. DELETE FROM DBA_ServerIPDataBase_OverCheck
  29. WHERE CreateTime< CONVERT(VARCHAR(19),DATEADD( HH,-10,GETDATE()),120)
  30. SET @ip = NULL
  31. IF OBJECT_ID('tempdb..#tempserverip') IS NOT NULL
  32. DROP TABLE #tempserverip
  33. CREATE TABLE #tempserverip ( ipline VARCHAR(200) )
  34. INSERT #tempserverip
  35. EXEC master..xp_cmdshell 'ipconfig /all '
  36. INSERT INTO DBA_ServerIPDataBase_OverCheck
  37. ( LocalServerIP,ServerIP,DisabledFlag,CreateTime,CreateBy
  38. )
  39. SELECT @ServerIP, p.ServerIP,'0'AS DisabledFlag,GETDATE() AS CreateTime ,@@SERVERNAME AS CreateBy
  40. FROM dbo.DBA_ServerIPDataBase_OverCheck i
  41. RIGHT JOIN ( SELECT RTRIM(LTRIM(REPLACE(SUBSTRING(ipline,
  42. CHARINDEX(':',
  43. ipline) + 1, 20),
  44. '(首选)', ''))) AS 'ServerIP'
  45. FROM #tempserverip
  46. WHERE UPPER(ipline) LIKE '%IPv4 地址%'--这里需要注意一下,系统不同这里的匹配值就不同
  47. AND UPPER(ipline) NOT LIKE '%192.168.%'
  48. AND UPPER(ipline) NOT LIKE '%169.254.%'
  49. ) p ON i.ServerIP = p.ServerIP
  50. WHERE i.ServerIP IS NULL --只关注漂来飘往数据
  51.  
  52. --0002 -20180530 针对20180530持续告警问题,发现告警时间超过预期,进行优化。聚焦点再表DBA_ServerIPDataBase_OverCheck中的CreateTime栏位,精准更新 begin
  53. update i set i.CreateTime=getdate()
  54. FROM dbo.DBA_ServerIPDataBase_OverCheck i
  55. RIGHT JOIN ( SELECT RTRIM(LTRIM(REPLACE(SUBSTRING(ipline,
  56. CHARINDEX(':',
  57. ipline) + 1, 20),
  58. '(首选)', ''))) AS 'ServerIP'
  59. FROM #tempserverip
  60. WHERE UPPER(ipline) LIKE '%IPv4 地址%'--这里需要注意一下,系统不同这里的匹配值就不同
  61. AND UPPER(ipline) NOT LIKE '%192.168.%'
  62. AND UPPER(ipline) NOT LIKE '%169.254.%'
  63. ) p ON i.ServerIP = p.ServerIP
  64. ------
  65. DECLARE IP CURSOR
  66. FOR
  67. SELECT ServerIP
  68. FROM dbo.DBA_ServerIPDataBase_OverCheck WHERE DisabledFlag IS NOT NULL AND DisabledFlag=0
  69. OPEN IP
  70. FETCH NEXT FROM IP INTO @ip
  71. WHILE @@FETCH_STATUS = 0
  72. BEGIN
  73. --SET @pingname = 'ping -a ' + @ip
  74. SET @pingname = 'ping -a ' + @ip + ' -n 1 -l 10'
  75.  
  76. TRUNCATE TABLE #tempserverip
  77. INSERT #tempserverip
  78. EXEC master..xp_cmdshell @pingname
  79.  
  80. SELECT @ServerName = REPLACE(RTRIM(LTRIM(SUBSTRING(ipline, 8,
  81. CHARINDEX('[',
  82. ipline) - 8))),
  83. '.XXXXXX.com', '') ,-----加域的电脑,计算机名字可能带有域名,请根据实际情况替换
  84. @ServerFullName = RTRIM(LTRIM(SUBSTRING(ipline, 8,
  85. CHARINDEX('[',
  86. ipline) - 8)))
  87. FROM #tempserverip
  88. WHERE ipline LIKE '%正在 Ping%'
  89.  
  90. UPDATE dbo.DBA_ServerIPDataBase_OverCheck
  91. SET ServerName = @ServerName ,
  92. ServerFullName = @ServerFullName
  93. WHERE ServerIP = @ip
  94.  
  95. FETCH NEXT FROM IP INTO @ip
  96. END
  97. CLOSE IP
  98. DEALLOCATE IP
  99. UPDATE dbo.DBA_ServerIPDataBase_OverCheck
  100. SET ServerIPType = 'Localhost'
  101. WHERE ServerName = @@SERVERNAME
  102.  
  103.  
  104. if OBJECT_ID('sys.availability_group_listener_ip_addresses') IS NOT NULL
  105. begin
  106. update a set a.ServerIPType='ListenIP'
  107. from DBA_ServerIPDataBase_OverCheck a inner join sys.availability_group_listener_ip_addresses b
  108. on a.ServerIP=b.ip_address
  109. end
  110.  
  111. IF OBJECT_ID('sys.dm_hadr_cluster') IS NOT NULL
  112. BEGIN
  113. UPDATE dbo.DBA_ServerIPDataBase_OverCheck SET ClusterName=(SELECT cluster_name FROM sys.dm_hadr_cluster)
  114. UPDATE dbo.DBA_ServerIPDataBase_OverCheck SET ServerIPType='WindowsCluster' WHERE ServerName=(SELECT cluster_name FROM sys.dm_hadr_cluster)
  115. END
  116. IF OBJECT_ID('sys.dm_hadr_cluster') IS NULL
  117. BEGIN
  118. UPDATE dbo.DBA_ServerIPDataBase_OverCheck SET ClusterName='Not Cluster'
  119. END
  120. --0002 -20180530 针对20180530持续告警问题,发现告警时间超过预期,进行优化。聚焦点再表DBA_ServerIPDataBase_OverCheck中的CreateTime栏位,精准更新 begin
  121. UPDATE DBA_ServerIPDataBase_OverCheck SET DataBaseName=STUFF(
  122. (SELECT ',' + name FROM sys.databases
  123. WHERE name not in ('master'
  124. ,'tempdb','model','msdb','ReportServer','ReportServerTempDB','distribution')
  125. for xml path('') ),
  126. 1,1,'')
  127. -----002 end
  128. --the mail alarm
  129. declare @SQL as varchar(200)
  130. declare @Subject as varchar(200)=N'DB SERVER IP 有漂移,请检查确认!'
  131. declare @Body as nvarchar(max)=''
  132.  
  133. select @PreDiffDateTime= CreateTime from DBA_ServerIPDataBase_OverCheckOriginOrigin
  134. order by CreateTime
  135. SELECT TOP 0 A.* into #temp_DBA_ServerIPDataBase_OverCheck_diff FROM DBA_ServerIPDataBase_OverCheck A INNER JOIN DBA_ServerIPDataBase_OverCheckOriginOrigin B
  136. ON A.ServerIP =B.ServerIP
  137. IF EXISTS(SELECT * FROM DBA_ServerIPDataBase_OverCheck A LEFT JOIN DBA_ServerIPDataBase_OverCheckOriginOrigin B ON A.ServerIP =B.ServerIP WHERE B.ServerIP IS NULL)
  138. BEGIN
  139.  
  140. INSERT INTO #temp_DBA_ServerIPDataBase_OverCheck_diff
  141. SELECT A.* FROM DBA_ServerIPDataBase_OverCheck A LEFT JOIN DBA_ServerIPDataBase_OverCheckOriginOrigin B ON A.ServerIP =B.ServerIP WHERE B.ServerIP IS NULL
  142.  
  143. if exists( select * from #temp_DBA_ServerIPDataBase_OverCheck_diff )
  144. begin
  145. set @Body= N'<html>'
  146. + N'<style type="text/css">'
  147. + N' td {border:solid #9ec9ec; border-width:1px 1px 1px 1px; padding:4px 0px;}'
  148. + N' table {border:1px solid #9ec9ec;width:80%;border-width:0px 0px 0px 0px;font-size:14px}'
  149. + N'</style>'
  150. + N'<H1 style="color:#FF0000;font-size:14px"></H1>'
  151. SET @Body=@Body+'<body><font color=#0000CC>Dear All,<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;此List是监控到过去10 MIn Server IP 异常情况,'+@ServerIP +'服务器有新IP创建声明(新增), 请及时Check。具体数据如下:;<br><br><table>'
  152. SET @Body=@Body+'<tr bgcolor=#FFFF00 align="center"><td>ClusterName</td><td>ServerIP</td><td>ServerName</td><td>ServerIPType</td><td>DatabaseName</td><td>DisabledFlag</td><td>当前时间</td><td>差异采样时间</td></tr>'
  153. SELECT @Body=@Body+'<tr><td>'+ClusterName+'</td><td>'+ServerIP+'</td><td>'+ServerName+'</td><td>'+ServerIPType+'</td><td>'+DatabaseName+'</td><td>'+ DisabledFlag+'</td><td>'+@CurrDateTime+'</td><td>'+ @PreDiffDateTime+'</td></tr>'
  154. from #temp_DBA_ServerIPDataBase_OverCheck_diff
  155. SET @Body = @Body +'</table><font color=#0000CC><br><br>DBA<br>Best wishes</body><html>'
  156. end
  157. END
  158.  
  159.  
  160. IF EXISTS(SELECT * FROM DBA_ServerIPDataBase_OverCheck A RIGHT JOIN DBA_ServerIPDataBase_OverCheckOriginOrigin B ON A.ServerIP =B.ServerIP WHERE A.ServerIP IS NULL)
  161. BEGIN
  162.  
  163. DELETE FROM #temp_DBA_ServerIPDataBase_OverCheck_diff
  164. INSERT INTO #temp_DBA_ServerIPDataBase_OverCheck_diff
  165. SELECT A.* FROM DBA_ServerIPDataBase_OverCheck A RIGHT JOIN DBA_ServerIPDataBase_OverCheckOriginOrigin B ON A.ServerIP =B.ServerIP WHERE A.ServerIP IS NULL
  166.  
  167. if exists( select * from #temp_DBA_ServerIPDataBase_OverCheck_diff )
  168. begin
  169. set @Body= N'<html>'
  170. + N'<style type="text/css">'
  171. + N' td {border:solid #9ec9ec; border-width:1px 1px 1px 1px; padding:4px 0px;}'
  172. + N' table {border:1px solid #9ec9ec;width:80%;border-width:0px 0px 0px 0px;font-size:14px}'
  173. + N'</style>'
  174. + N'<H1 style="color:#FF0000;font-size:14px"></H1>'
  175. SET @Body=@Body+'<body><font color=#0000CC>Dear All,<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;此List是监控到过去10 MIn Server IP 异常情况,'+@ServerIP +'服务器有IP漂移(消减), 请及时Check。具体数据如下:;<br><br><table>'
  176. SET @Body=@Body+'<tr bgcolor=#FFFF00 align="center"><td>ClusterName</td><td>ServerIP</td><td>ServerName</td><td>ServerIPType</td><td>DatabaseName</td><td>DisabledFlag</td><td>当前时间</td><td>差异采样时间</td></tr>'
  177. SELECT @Body=@Body+'<tr><td>'+ClusterName+'</td><td>'+ServerIP+'</td><td>'+ServerName+'</td><td>'+ServerIPType+'</td><td>'+DatabaseName+'</td><td>'+ DisabledFlag+'</td><td>'+@CurrDateTime+'</td><td>'+ @PreDiffDateTime+'</td></tr>'
  178. from #temp_DBA_ServerIPDataBase_OverCheck_diff
  179. SET @Body = @Body +'</table><font color=#0000CC><br><br>DBA<br>Best wishes</body><html>'
  180. end
  181.  
  182. END
  183. SET @BODY=REPLACE(@BODY,'''','')
  184. IF REPLACE(@BODY,' ','')<>''
  185. BEGIN
  186.  
  187. Declare @AllEmailToAddress varchar(3000)=''
  188. Declare @AllEmailCcAddress varchar(3000)=''
  189. Select @AllEmailToAddress='hanmeimei;xiaoming;lilei'
  190.  
  191. Select @AllEmailCcAddress='laoban'
  192.  
  193.  
  194. exec msdb..sp_send_dbmail @profile_name = 'AutoMail' -- profile 名称,请检查此参数,根据实际情况进行替换
  195. ,@recipients = @AllEmailToAddress -- 收件人邮箱
  196. ,@copy_recipients=@AllEmailCcAddress
  197. ,@subject = @Subject -- 邮件标题
  198. ,@body = @BODY -- 邮件内容
  199. ,@body_format = 'HTML' -- 邮件格式
  200. ,@file_attachments=''
  201. ,@importance = 'HIGH' -- varchar(10) 告警级别
  202. END
  203.  
  204.  
  205. ------------------新增立即插入-----------
  206.  
  207. insert into [dbo].[DBA_ServerIPDataBase_OverCheckOriginOrigin]
  208. select a.* from DBA_ServerIPDataBase_OverCheck a left join DBA_ServerIPDataBase_OverCheckOriginOrigin b
  209. on a.ServerIP=b.ServerIP
  210. where b.ServerIP is null
  211.  
  212. ---漂移后,指定时间段后直接删除过时数据,暂定八个小时。【即如果有漂移(减少),减少的IP信息,则在指定时间后,删除。】
  213.  
  214. delete b
  215. from DBA_ServerIPDataBase_OverCheck a right join DBA_ServerIPDataBase_OverCheckOriginOrigin b
  216. on a.ServerIP=b.ServerIP
  217. where a.ServerIP is null
  218. and b.CreateTime< CONVERT(VARCHAR(19),DATEADD( HH,-3,GETDATE()),120)

    -----将数据插入到远程Server DB中,远程Server有一个SP过程,用来判断漂移前/后DB是否有变化。远程的SP主要是依据 LocalServerIP ServerIP 对应关系变化情况来判断。这段代码省略,下次再描述
  219. SET NOCOUNT OFF
  220.  
  221. End
  222.  
  223.  
  224. GO

 

 4.功能实现

例如当 服务器有新IP创建声明(新增)时,其发出的告警邮件如下:

 

 

 

本文版权归作者所有,未经作者同意不得转载,谢谢配合!!!

本文版权归作者所有,未经作者同意不得转载,谢谢配合!!!

本文版权归作者所有,未经作者同意不得转载,谢谢配合!!!

原文链接:http://www.cnblogs.com/xuliuzai/p/11099058.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号