SQL Server自动化运维系列——监控跑批Job运行状态(Power Shell)

需求描述

在我们的生产环境中,大多数情况下,我们需要有自己的运维系统,包括检测我们自己的健康状态。如果发生异常并需要事先警告,通知的形式通常通过电子邮件通知。

[En]

In our production environment, in most cases, we need to have our own operation and maintenance system, including the detection of our own health status. If an exception occurs and requires advance warning, the form of notification is generally notified by e-mail.

在上一篇文章中已经分析了SQL SERVER中关于邮件的基础配置,本篇将利用此功能对多台Server的跑批Job进行监控。

本篇实现

1、每天检查服务器中的SQL Server跑批Job的运行状态,如果跑批失败,则发邮件告诉管理员失败的明细

2、解决多台服务器同时检查

监控脚本

首先我们来解决第二个问题,关于多台服务器的问题:

wuxuelei-pc

配置文件名字:computername.xml,这样就解决很多服务器的问题,只需要在配置文件中增加就可以,因为我在本地测试,所以就配置了我的本地电脑

脚本如下:

$server = "(local)"
$uid = "sa"
$db="master"
$pwd="password"
$mailprfname = "TestMail"
$recipients = "787449667@qq.com"
$subject = "老大,快去看看这些服务器的Job跑失败了!"
$computernamexml = "F:\PowerShell\发送邮件\computername.xml"

function GetServerName($xmlpath)
{
    $xml = [xml] (Get-Content $xmlpath)
    $return = New-Object Collections.Generic.List[string]
    for($i = 0;$i -lt $xml.computernames.ChildNodes.Count;$i++)
    {
        if ( $xml.computernames.ChildNodes.Count -eq 1)
        {
            $cp = [string]$xml.computernames.computername
        }
        else
        {
            $cp = [string]$xml.computernames.computername[$i]
        }
        $return.Add($cp.Trim())
    }
    $return
}

function GetAlterCounter($xmlpath)
{
    $xml = [xml] (Get-Content $xmlpath)
    $return = New-Object Collections.Generic.List[string]
    $list = $xml.counters.Counter
}

function CreateAlter($message)
{
    $SqlConnection = New-Object System.Data.SqlClient.SqlConnection
    $CnnString ="Server = $server; Database = $db;User Id = $uid; Password = $pwd"
    $SqlConnection.ConnectionString = $CnnString
    $CC = $SqlConnection.CreateCommand();
    if (-not ($SqlConnection.State -like "Open")) { $SqlConnection.Open() }

    $cc.CommandText=
            " EXEC msdb..sp_send_dbmail
             @profile_name  = '$mailprfname'
            ,@recipients = '$recipients'
            ,@body = '$message'
            ,@subject = '$subject'
"
    $cc.ExecuteNonQuery()|out-null
    $SqlConnection.Close();
}

$report = ""
$item = New-Object Collections.Generic.List[string]
$names = GetServerName($computernamexml)
foreach($cp in $names)
{
$srv=New-Object "Microsoft.SqlServer.Management.Smo.Server" "(local)"
$item=$srv.jobserver.jobs | where-object {$_.lastrunoutcome -eq "Failed" -and $_.isenabled -eq $TRUE} |  select OriginatingServer,name,Description,lastrunoutcome,lastrundate,JobSteps
#Write-Host $item.JobSteps.name
$report += " 服务器:"+$item.OriginatingServer+"  Job名称:"+$item.name+"  Job描述:"+$item.Description +"  Job最后运行状态:"+$item.lastrunoutcome  +"  Job最后运行时间:"+$item.lastrundate +"  Job失败的步骤名称:"+$item.JobSteps.name  + "`n"
}
#生产警告
CreateAlter $report

通过上述脚本,生成跑批任务,就可以定时监控多台服务器的Job了。

当然,建议放在所有Job的运行完成之后,进行检测。

上述代码中,有两个技术点:

1、需要自己配置SQL Server邮件代理,具体方法参照我上一篇:点击此

2、需要自己配置跑批计划,方法自己网上搜,很简单。

本篇所监控的Job状态,利用的是上一篇我们创建的Job,跑批肯定失败的。

SQL Server自动化运维系列——监控跑批Job运行状态(Power Shell)

效果图如下

点击来看一下邮件明细内容:

SQL Server自动化运维系列——监控跑批Job运行状态(Power Shell)

给出的失败信息还是挺详细的。

另外,因为我只建立了一个失败的Job,所以邮件中只是发送一个条。

其实关于此监控,还有一些状态是可以监控的:

1、比如:可以指定服务器上的部分Job进行监控

2、监控Job的状态:失败或者正常等

以上内容,可以自己根据需要灵活配置。

结语

本篇就列举了一下利用PowerShell实现自动化运维和检测。算作抛砖引玉了吧,自己另有需求可以自己灵活实现。

另外关于Job,一般除了SQL Server的Agent会存在,还有一部分是操作系统的计划任务也需要检测,我们后面的文章解决此问题。

关于SQL Server自动化运维和检测的内容很广泛,其中很多都是从日常的经验中出发,一步步的从手动到自动的过程。

后面的文章,我们将会更深入关于SQL Server的自动化优化运维进行分析。有兴趣的童鞋,可以提前关注。

文章的最后,给出该系列其它的一些监控方式,皆为原创

SQL Server需要监控哪些计数器

SQL Server自动化运维系列——监控性能指标脚本(Power Shell)

SQL Server自动化运维系列——监控磁盘剩余空间及SQL Server错误日志(Power Shell)

SQL Server自动化运维系列——关于邮件通知那点事(.Net开发人员的福利)

如果您看了本篇博客,觉得对您有所收获,请不要吝啬您的” 推荐“。

Original: https://www.cnblogs.com/zhijianliutang/p/4428018.html
Author: 指尖流淌
Title: SQL Server自动化运维系列——监控跑批Job运行状态(Power Shell)

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/34023/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

最近整理资源【免费获取】:   👉 程序员最新必读书单  | 👏 互联网各方向面试题下载 | ✌️计算机核心资源汇总