SnapMgrSQLJobMonitorDataSource (DataSourceModuleType)

Element properties:

TypeDataSourceModuleType
IsolationAny
AccessibilityPublic
RunAsDefault
OutputTypeSystem.PropertyBagData

Member Modules:

ID Module Type TypeId RunAs 
Scheduler DataSource System.Scheduler Default
SnapMgrSQLJobMonitorDataSourceProbeAction ProbeAction Microsoft.Windows.PowerShellPropertyBagProbe Default

Source Code:

<DataSourceModuleType Accessibility="Public" ID="SnapMgrSQLJobMonitorDataSource">
<Configuration/>
<ModuleImplementation>
<Composite>
<MemberModules>
<DataSource ID="Scheduler" TypeID="System!System.Scheduler">
<Scheduler>
<SimpleReccuringSchedule>
<Interval>360</Interval>
<SyncTime/>
</SimpleReccuringSchedule>
<ExcludeDates/>
</Scheduler>
</DataSource>
<ProbeAction TypeID="Windows!Microsoft.Windows.PowerShellPropertyBagProbe" ID="SnapMgrSQLJobMonitorDataSourceProbeAction">
<ScriptName>SnapMgrSQLJobMonitorDataSourceProbeAction.ps1</ScriptName>
<ScriptBody><Script>


$server = $env:computername
$oAPI = new-object -comObject "MOM.ScriptAPI"



function get-dbdata {
[CmdletBinding()]
param (
[string]$connectionString,
[string]$query,
[switch]$isSQLServer
)
#Credit for Function goes to Don Jones
if ($isSQLServer) {
Write-Verbose 'in SQL Server mode'
$connection = New-Object -TypeName System.Data.SqlClient.SqlConnection
} else {
Write-Verbose 'in OleDB mode'
$connection = New-Object -TypeName System.Data.OleDb.OleDbConnection
}
$connection.ConnectionString = $connectionString
$command = $connection.CreateCommand()
$command.CommandText = $query
if ($isSQLServer) {
$adapter = New-Object -TypeName System.Data.SqlClient.SqlDataAdapter $command
} else {
$adapter = New-Object -TypeName System.Data.OleDb.OleDbDataAdapter $command
}
$SQLdataset = New-Object -TypeName 'System.Data.DataSet'
$adapter.Fill($SQLdataset)|out-null
$SQLdataset.Tables[0]
}

$query = @'
SELECT
[sJOB].[name] AS [JobName]
, CASE [sJOB].[enabled]
WHEN 1 THEN '1'
WHEN 0 THEN '0'
END AS [IsEnabled]
, CASE
WHEN [sSCH].[schedule_uid] IS NULL THEN '0'
ELSE '1'
END AS [IsScheduled]
, [sSVR].[name] AS [OriginatingServerName]
, CASE WHEN [sJOBH].[run_date] IS NULL OR [sJOBH].[run_time] IS NULL THEN NULL
ELSE CAST(
CAST([sJOBH].[run_date] AS CHAR(8))
+ ' '
+ STUFF(
STUFF(RIGHT('000000' + CAST([sJOBH].[run_time] AS VARCHAR(6)), 6)
, 3, 0, ':')
, 6, 0, ':')
AS DATETIME)
END AS [LastRunDateTime]
, CASE [sJOBH].[run_status]
WHEN 0 THEN 'Failed'
WHEN 1 THEN 'Succeeded'
WHEN 2 THEN 'Retry'
WHEN 3 THEN 'Canceled'
WHEN 4 THEN 'Running' -- In Progress
END AS [LastRunStatus]
, STUFF(
STUFF(RIGHT('000000' + CAST([sJOBH].[run_duration] AS VARCHAR(6)), 6)
, 3, 0, ':')
, 6, 0, ':')
AS [LastRunDuration]
, [sJOBH].[message] AS [LastRunStatusMessage]
, CASE [freq_type]
WHEN 1 THEN 'One Time'
WHEN 4 THEN 'Daily'
WHEN 8 THEN 'Weekly'
WHEN 16 THEN 'Monthly'
WHEN 32 THEN 'Monthly - Relative to Frequency Interval'
WHEN 64 THEN 'Start automatically when SQL Server Agent starts'
WHEN 128 THEN 'Start whenever the CPUs become idle'
END [Occurrence]

FROM
[msdb].[dbo].[sysjobs] AS [sJOB]
LEFT JOIN [msdb].[sys].[servers] AS [sSVR]
ON [sJOB].[originating_server_id] = [sSVR].[server_id]
LEFT JOIN [msdb].[dbo].[sysjobsteps] AS [sJSTP]
ON [sJOB].[job_id] = [sJSTP].[job_id]
AND [sJOB].[start_step_id] = [sJSTP].[step_id]
LEFT JOIN [msdb].[dbo].[sysjobschedules] AS [sJOBSCH]
ON [sJOB].[job_id] = [sJOBSCH].[job_id]
LEFT JOIN [msdb].[dbo].[sysschedules] AS [sSCH]
ON [sJOBSCH].[schedule_id] = [sSCH].[schedule_id]


LEFT JOIN (
SELECT
[job_id]
, [run_date]
, [run_time]
, [run_status]
, [run_duration]
, [message]
, ROW_NUMBER() OVER (
PARTITION BY [job_id]
ORDER BY [run_date] DESC, [run_time] DESC
) AS RowNumber
FROM [msdb].[dbo].[sysjobhistory]
WHERE [step_id] = 0
) AS [sJOBH]
ON [sJOB].[job_id] = [sJOBH].[job_id]
AND [sJOBH].[RowNumber] = 1

Where Sjstp.command like '%SmsqlJobLauncher.exe%'

'@


$D ="."+ [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain().name
$instances= gwmi -ComputerName $server -Class win32_service -Filter "Name='MSSQLSERVER' or name like '%MSSQL$%'"|%{
if($_.name -like "*`$*"){($_.name -split '\$')[1]}
else{$_.name}
}
$serverfromsnapmgr = (Get-ItemProperty -Path 'HKLM:\software\Network Appliance\SnapManager For SQL Server\Client' -Name MountSQLServer).MountSQLServer

if(($instances|measure).count -ne 0){
$ALL_JOINED_JOBS = @()

foreach($instance in $instances){
if($instance -eq 'MSSQLSERVER'){
$conn_string = $serverfromsnapmgr}
else{
$conn_string = $server +'\' + $instance
}
$sqldata1 = get-dbdata -connectionString "server=$($conn_string);Integrated Security=SSPI" -isSQLServer -query $query

$ALL_JOINED_JOBS +=$sqldata1
}
}

$ALL_JOINED_JOBS1 =$ALL_JOINED_JOBS |%{
new-object psobject -Property @{
JobName = $_.JobName
OriginatingServerName = $_.OriginatingServerName
LastRunDateTime=&amp;{
if($_.LastRunDateTime.GetType().name -eq 'dbnull'){
0}
Else{$_.LastRunDateTime}
}
DaysSinceLastRun = (New-TimeSpan (&amp;{
if($_.LastRunDateTime.GetType().name -eq 'dbnull'){
0}
Else{$_.LastRunDateTime}
}) (get-date)
).days
LastRunStatus = $_.LastRunStatus
isScheduled = [bool][int]$_.isscheduled
isEnabled = [bool][int]$_.isenabled
Occurrence = $_.Occurrence
LastRunDuration = $_.LastRunDuration
LastRunStatusMessage = $_.LastRunStatusMessage
LastRunDurationSeconds = (&amp;{$duration = $_.lastrunduration
$d_split = $duration -split ':'
([int]$d_split[0]*60*60)+([int]$d_split[1]*60)+[int]$d_split[2]
})
}
}


if(($ALL_JOINED_JOBS1|measure).count -eq 0){
exit}
Else{
Function Create-DeviceDiscoveryData {
param($JOBData)


$PropertyBag = $oAPI.CreatePropertyBag()
$PropertyBag.AddValue('Name', [string]$JOBData.JobName)
$PropertyBag.AddValue('Job_Return_Status',[string]$JOBData.LastRunStatus)
$PropertyBag.AddValue('LastRunDurationSeconds',[int]$JOBData.LastRunDurationSeconds)
$PropertyBag.AddValue('LastRunDuration',[string]$JOBData.LastRunDuration)
$PropertyBag.AddValue('LastRunDateTime',[datetime]$JOBData.LastRunDateTime)
$PropertyBag.AddValue('DaysSinceLastRun',[int]$JOBData.DaysSinceLastRun)
$PropertyBag.AddValue('LastRunStatusMessage',[string]$JOBData.LastRunStatusMessage)
$PropertyBag.AddValue('isScheduled',[bool]$JOBData.isScheduled)
$PropertyBag.AddValue('isEnabled',[bool]$JOBData.isEnabled)
$PropertyBag.AddValue('Occurrence',[string]$JOBData.Occurrence)

$PropertyBag

}

foreach($JOBData in $ALL_JOINED_JOBS1){
Create-DeviceDiscoveryData -JOBData $JOBData
}




}


</Script></ScriptBody>
<TimeoutSeconds>300</TimeoutSeconds>
</ProbeAction>
</MemberModules>
<Composition>
<Node ID="SnapMgrSQLJobMonitorDataSourceProbeAction">
<Node ID="Scheduler"/>
</Node>
</Composition>
</Composite>
</ModuleImplementation>
<OutputType>System!System.PropertyBagData</OutputType>
</DataSourceModuleType>