-- Script generated on 6/23/2009 9:22 PM -- By: kr01738 -- Server: 202.60.222.148 BEGIN TRANSACTION DECLARE @JobID BINARY(16) DECLARE @ReturnCode INT SELECT @ReturnCode = 0 IF (SELECT COUNT(*) FROM msdb.dbo.syscategories WHERE name = N'[Uncategorized (Local)]') < 1 EXECUTE msdb.dbo.sp_add_category @name = N'[Uncategorized (Local)]' -- Delete the job with the same name (if it exists) SELECT @JobID = job_id FROM msdb.dbo.sysjobs WHERE (name = N'Sample_Monitoring_Job') IF (@JobID IS NOT NULL) BEGIN -- Check if the job is a multi-server job IF (EXISTS (SELECT * FROM msdb.dbo.sysjobservers WHERE (job_id = @JobID) AND (server_id <> 0))) BEGIN -- There is, so abort the script RAISERROR (N'Unable to import job ''Sample_Monitoring_Job'' since there is already a multi-server job with this name.', 16, 1) GOTO QuitWithRollback END ELSE -- Delete the [local] job EXECUTE msdb.dbo.sp_delete_job @job_name = N'Sample_Monitoring_Job' SELECT @JobID = NULL END BEGIN -- Add the job EXECUTE @ReturnCode = msdb.dbo.sp_add_job @job_id = @JobID OUTPUT , @job_name = N'Sample_Monitoring_Job', @owner_login_name = N'sa', @description = N'No description available.', @category_name = N'[Uncategorized (Local)]', @enabled = 1, @notify_level_email = 0, @notify_level_page = 0, @notify_level_netsend = 0, @notify_level_eventlog = 2, @delete_level= 0 IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback -- Add the job steps EXECUTE @ReturnCode = msdb.dbo.sp_add_jobstep @job_id = @JobID, @step_id = 1, @step_name = N'Send MSg', @command = N'/* Author : Kuldip Rindani Create Date : 01-APR-2009 Purpose : For Dispatching Sample Alert using AlertDispatcher.exe Dependencies : Script uses xp_cmdshell to execute a Exe. therefore, it should be enabled on machine were monitoring is setup. Modification History : */ Declare @Row_Count int Declare @ExePath varchar(255), @varMsg varchar(255), @varCmd varchar(2000) Declare @IPAddress varchar(25), @Port varchar(10) --Path for exe Need to customized as per site setup. Set @ExePath = ''C:\Monitoring\AlertDispatcher.exe'' --Remember below is sample query for monitoring, your could be different than it. --Below query is for checking if any Data is present is table for filter columns. select @Row_Count = Row_Count from OpenQuery( LinkName, '' Select count(*) as Row_Count from Your_Database..Your_Tables with (nolock) where Your_Columns = ''''SOMEVALUE'''' '') Set @varMsg = ''Date : '' + CAST(GETDATE() as varchar(50)) + '', Alert for Your_Database..Your_Tables on Your_Server = '' + CAST(Row_Count as varchar(10)) --Below is sample logic for send Alert when data is present in Your_Database..Your_Tables IF @Row_Count > 0 BEGIN --Loop through each Registered Alert Receiver and dispatch the message Declare C1 cursor For select IPAddress, Port from MonitorIP where sent_flag = 1 Open C1 Fetch Next From C1 InTo @IPAddress, @Port While @@Fetch_Status <>-1 Begin --Create the String for executing AlertDispatcher.exe utlity. set @varCmd = @ExePath + '' '' + @IPAddress + '' '' + @Port + '' "'' + @varMsg + ''"'' exec master..xp_cmdshell @varCmd Fetch Next From C1 InTo @IPAddress, @Port End Close C1 Deallocate C1 END ', @database_name = N'master', @server = N'', @database_user_name = N'', @subsystem = N'TSQL', @cmdexec_success_code = 0, @flags = 6, @retry_attempts = 0, @retry_interval = 1, @output_file_name = N'C:\Sample_Alert_Job_Log.txt', @on_success_step_id = 0, @on_success_action = 1, @on_fail_step_id = 0, @on_fail_action = 2 IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback EXECUTE @ReturnCode = msdb.dbo.sp_update_job @job_id = @JobID, @start_step_id = 1 IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback -- Add the job schedules EXECUTE @ReturnCode = msdb.dbo.sp_add_jobschedule @job_id = @JobID, @name = N'Every 1 Hour - Starting 9.00 AM to 9.00 PM', @enabled = 1, @freq_type = 8, @active_start_date = 20090512, @active_start_time = 90000, @freq_interval = 127, @freq_subday_type = 8, @freq_subday_interval = 1, @freq_relative_interval = 0, @freq_recurrence_factor = 1, @active_end_date = 99991231, @active_end_time = 213059 IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback -- Add the Target Servers EXECUTE @ReturnCode = msdb.dbo.sp_add_jobserver @job_id = @JobID, @server_name = N'(local)' IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback END COMMIT TRANSACTION GOTO EndSave QuitWithRollback: IF (@@TRANCOUNT > 0) ROLLBACK TRANSACTION EndSave: