如何查询警报系统启用邮件配置文件属性

时间:2014-04-07 19:26:55

标签: sql-server sql-server-2008-r2 sql-agent

要从SQL Server代理发送自动作业失败通知,您必须配置数据库邮件,然后转至SQL Agent properties > Alert System > Mail Session > Enable mail profile以配置要用于发送的mail systemmail profile邮件通知。我们有许多服务器,并且希望设置一个中央跨服务器作业,以确保在各种服务器上检查Enable mail profile选项,否则,计划作业将无提示地失败而不发送电子邮件通知。

是否有支持的方式查询msdb数据库以使用T-SQL(或通过其他方式编程)获取这些设置?

在SQL Server代理UI中显示属性页面时运行SQL事件探查器跟踪显示对msdb.dbo.sp_get_sqlagent_properties的引用,这是一个未记录的过程(我更倾向于使用文档化对象来帮助我们解决未来的问题),以及对master.dbo.xp_instance_regread的几次调用,我认为每个SQL Server实例安装都可以更改reg键。

有没有人知道查询是否配置enable mail profile选项的方法,还检索SQL Agent警报系统配置中指定的mail profile?我们的大多数服务器都是SQL Server 2008 R2,有一些SQL 2012.我更喜欢SQL 2008+支持。

提前致谢!

3 个答案:

答案 0 :(得分:2)

面向未来是一个非常明智的想法:)。如您所述,xp_regread和xp_instance_regread都没有记录。 http://social.msdn.microsoft.com/Forums/sqlserver/en-US/b83dd2c1-afde-4342-835f-c1debd73d9ba/xpregread解释了您的担忧(此外,它为您提供了另一种选择)。

您的跟踪和您的sp_helptext'sp_get_sqlagent_properties'运行是一个良好的开端。接下来要做的是运行sp_helptext'sp_helptext',并记下它对sys.syscomments的引用。 BOL sys.syscomments主题将您重定向到sys.sql_modules,这指向下一步。不幸的是,根据您的需要,运行USE msdb将返回一行(对于'sp_get_sqlagent_properties'); SELECT object_name(object_id)FROM sys.sql_modules WHERE definition LIKE'%sp_get_sqlagent_properties%'。因此,我认为你运气不好 - 似乎没有其他的,公开记录的模块(sproc)。我的假设可能是错的:)。

我推断出客户端(SMO,SSMS等)需要存在xp_reg%调用,例如设置/获取代理属性。更重要的是(为了您的需要),您的sp_helptext运行还显示SSMS(客户端)正在使用注册表存储(即不是SQL存储)。不幸的是,我必须推断(基于图书馆搜索中缺少证据)这些密钥(及其值)也没有记录......

以上似乎让你陷入困境。您可以决定“如果我们要依赖未记录的注册表项,我们也可以依靠未记录的调用来阅读它们”,但我不会建议:)。您也可以在https://connect.microsoft.com/提交功能请求(您的需求很明确),但由于您的需求涉及客户端功能请求,我建议您在等待修复时屏住呼吸:)。

或许现在是时候退一步看看更大的图片了:

  • 该密钥的更改频率,以及此流程对该更改进行轮询的频率是多少?

  • 电子邮件使用邮件原语。发信人:“亲爱的收件人,你收到了我的邮件吗?”收件人:“亲爱的发件人,你给我发了邮件吗?”禁用电子邮件配置文件不是电子邮件失败的唯一原因。

与定期检查密钥相比,不同的方法会更有用吗?

一种方法是定期发送“KeepAlive”电子邮件。如果没有定期收到“KeepAlive”电子邮件,也许该密钥被调整,也许邮局正在度假,或者可能还有其他事情(同样糟糕)。此外,这种方法应该得到完全支持,记录并且面向未来。谁知道在下一版本的SQL Server代理中将如何使用和使用哪些密钥?

没有解决第一个项目符号(也不会通过定期检查密钥来解决),也许您还有其他需求(值得在MS连接上提及)。

答案 1 :(得分:1)

我终于找到了使用PowerShell和Microsoft.SqlServer.Management.Smo.Agent.JobServer执行此操作的方法。

以下是我编写的PowerShell脚本,用于检查是否启用了SQL代理邮件警报,并确保在服务器重新启动时将SQL代理设置为自动启动。这适用于本地或远程SQL实例。

# usage examples
Check-SQLAgentConfiguration -InstanceName 'localhost\sql2014'
Check-SQLAgentConfiguration -InstanceName 'RemoteServerName'


function Check-SQLAgentConfiguration{
    param([string]$InstanceName='localhost')

    [System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | out-null;

    $smosrv = new-object Microsoft.SqlServer.Management.Smo.Server($InstanceName);
    $smosrv.ConnectionContext.ConnectTimeout = 5; #5 seconds timeout.
    $smosrv.ConnectionContext.ApplicationName = 'PowerShell Check-SQLAgentConfiguration';
    "Server: {0}, Instance: {1}, Version: {2}, Product Level: {3}" -f $smosrv.Name, $smosrv.InstanceName, $smosrv.Version, $smosrv.ProductLevel;

    # NOTE: this does not seem to ever fail even if SQL Server is offline.
    if(!$smosrv){"SQL Server Connection failed";  return $null;}   

    $agent = $smosrv.JobServer;

    if(!$agent -or $agent -eq $null){
        throw "Agent Connection failed";  
        return -2;
    }

    $agentConfigErrMsg = "";

    if($agent.AgentMailType -ne "DatabaseMail"){ $agentConfigErrMsg += " AgentMailType: " + $agent.AgentMailType + "; "; }
    if(!$agent.DatabaseMailProfile){$agentConfigErrMsg += " DatabaseMailProfile: " + $agent.DatabaseMailProfile + "; ";}
    if($agent.SqlAgentAutoStart -ne "True"){$agentConfigErrMsg += " SqlAgentAutoStart: " + $agent.SqlAgentAutoStart + " ServiceStartMode: " + $agent.ServiceStartMode + "; "; }

    if($agentConfigErrMsg.length -gt 0){
       $agentConfigErrMsg = "Invalid SQL Agent config! " + $agentConfigErrMsg; 
       throw $agentConfigErrMsg;
       return -1;
    }

    <##
    #for debugging:
    "Valid: "
    "AgentMailType:" + $agent.AgentMailType;
    "DatabaseMailProfile: " + $agent.DatabaseMailProfile;
    "ServiceStartMode: " + $agent.ServiceStartMode;
    "SqlAgentAutoStart: " + $agent.SqlAgentAutoStart;
    #"SqlAgentMailProfile: " + $agent.SqlAgentMailProfile;
    #>

    return 0; 
}

答案 2 :(得分:0)

SQL 2008R2使用类似服务代理的队列进行邮件处理(http://technet.microsoft.com/en-us/library/ms175887%28v=sql.105%29.aspx)。在我们的环境中,我检查相应的队列是否存在并处于活动状态。

SELECT * FROM msdb.sys.service_queues
WHERE name = N'ExternalMailQueue' 
AND is_receive_enabled = 1;

此表格在线列出(http://technet.microsoft.com/en-us/library/ms187795%28v=sql.105%29.aspx)。

测试表明,当我们从新实例开始时,这会产生必要的转换 - &gt;已启用邮件 - &gt;邮件关闭然后又回来了。