adminbd

Записи по mssql,oracle,windows и linux

Have a Question?

If you have any question you can ask below or enter what you are looking for!

Как сделать хранимую процедуру для мониторинга заданий в MSSQL через prtg.

Хранимая процедура

USE [msdb]
GO

/****** Object:  StoredProcedure [dbo].[alertjob_test]    Script Date: 04/25/2018 08:59:24 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].[alertjob_test]
zakupki NVARCHAR(max)
AS
BEGIN
set nocount ON
DECLARE @job_id VARCHAR(50)
SET @job_id = (SELECT job_id FROM msdb.dbo.sysjobs WHERE [name] = zakupki)
IF (not object_id('tempdb..#x') is null) drop table #x 
SELECT  TOP 2 s.instance_id INTO #x FROM msdb.dbo.sysjobhistory s WHERE s.job_id = @job_id AND s.step_name LIKE '(Job outcome)'  ORDER BY s.instance_id DESC
IF EXISTS (SELECT * FROM msdb.dbo.sysjobs WHERE [name] = zakupki)
BEGIN
	IF (not object_id('tempdb..#z') is null) drop table #z 
	SELECT ja.job_id, j.name as job_name, ja.start_execution_date, isNull(last_executed_step_id,0) + 1 as current_executed_step_id,	js.step_name INTO #z 
		FROM msdb.dbo.sysjobactivity ja left join msdb.dbo.sysjobhistory jh on ja.job_history_id = jh.instance_id inner join msdb.dbo.sysjobs j on ja.job_id = j.job_id inner join msdb.dbo.sysjobsteps js on ja.job_id = js.job_id and isNull(ja.last_executed_step_id,0) + 1 = js.step_id
			WHERE ja.session_id = (select top 1 session_id from msdb.dbo.syssessions order by agent_start_date desc) and start_execution_date is not null and stop_execution_date is NULL
	IF EXISTS ( select job_name from #z where job_name = zakupki )
		BEGIN
    		SELECT 1
		END
	ELSE
		BEGIN
			IF EXISTS (SELECT s.run_status FROM msdb.dbo.sysjobhistory s WHERE s.job_id = @job_id AND s.instance_id > (SELECT MIN(instance_id) FROM #x) AND s.run_status = 0) 
				BEGIN
					SELECT 2
				END
			IF ((SELECT  TOP 1 s.run_status FROM msdb.dbo.sysjobhistory s WHERE s.job_id = @job_id AND s.step_name LIKE '(Job outcome)' ORDER BY s.instance_id DESC) = 3)
				BEGIN
					SELECT 2
				END
			ELSE
				BEGIN
					SELECT 0
				END
		END
	DROP TABLE #x
	DROP TABLE #z
END
ELSE 
	BEGIN
		SELECT 2
	END
END


--DROP PROCEDURE dbo.alertjob_test

--EXEC msdb.dbo.alertjob_test zakupki = N'test' 



				

GO


Настройка в prtg:

Как сделать хранимую процедуру для мониторинга заданий в MSSQL через prtg.

Как сделать хранимую процедуру для мониторинга заданий в MSSQL через prtg.

Leave a Reply

Your email address will not be published. Required fields are marked *

You can use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>