Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Failed Jobs report Expand / Collapse
Author
Message
Posted Tuesday, January 29, 2013 3:55 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: 2 days ago @ 11:36 PM
Points: 74, Visits: 481
Hi Guys,

I need a script that will send me a complete list of failed jobs by looping through all the linked servers. Currently, I have a report sent by some third party person through SSRS, which looks quite nice, and it checks for a specific kind of job for e.g: (DBA_weeklyindex_maint). I am actually interested to get this working for my environment. I don't have the code, and lack the knowledge of SSRS. I would really appreciate if you guys could help me out with this script to get job details only for a specific job through SSRS. Is it also possible if you guys could also provide an alternate script that I can use to get the same report, but for all the failed jobs looping through all linked servers, and getting it e-mailed it in a formatted manner, like an "HTML" sort of report.

--Pls. note I've attached the screenshot for the report through SSRS which we get on a weekly basis. It actually gets details from all the servers in prod, and checks for a specific maintenance jobs, and then sends an e-mailed report.

Pls. help me out guys!!!

Regards,
Faisal


  Post Attachments 
maintenance report.png (13 views, 96.98 KB)
Post #1412875
Posted Tuesday, January 29, 2013 4:42 AM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Yesterday @ 12:57 PM
Points: 343, Visits: 1,089
You do not need linked servers. Use powershell or sqlcmd to connect directly to each sql instance, query them and store results. For list of servers use textfile or central management server. There are at least several other ways to do that as well.

_____________________________________________________
XDetails Addin - for SQL Developers and DBA
blog.sqlxdetails.com - Transaction log myths - debunked!
Post #1412917
Posted Tuesday, January 29, 2013 6:53 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: 2 days ago @ 11:36 PM
Points: 74, Visits: 481
Hi Vedran,

Thanks. for those piece of advice. Could you document one way to do this along with the code & procedure to run it successfully. I would really appreciate!!!

Regards,
Faisal
Post #1413018
Posted Tuesday, January 29, 2013 7:00 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 4:32 PM
Points: 2,063, Visits: 3,787
This isn't "pretty" code but it works and will email you the results (I have it running hourly). I just set up a simple SQL Agent job on each of the servers I want to be notified for:
USE [MyDatabase]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[utl_dba_CheckSQLJobsForErrors]
@notifyEmailGroup VARCHAR(300) ='dba_alerts@company.com'
AS

DECLARE @sqlSelect NVARCHAR(500)

CREATE TABLE #SQLAGENTERRORS(
[id] INT IDENTITY(1,1) NOT NULL,
[SQL AGENT JOB NAME] [sysname] NOT NULL,
[step_id] [int] NOT NULL,
[step_name] [sysname] NOT NULL,
[sql_message_id] [int] NOT NULL,
[sql_severity] [int] NOT NULL,
[message] [nvarchar](1024) NULL,
[run_status] [int] NOT NULL,
[run_date] [int] NOT NULL,
[run_time] [int] NOT NULL,
[run_duration] [int] NOT NULL,
[retries_attempted] [int] NOT NULL,
[server] [sysname] NOT NULL
)

SET @sqlSelect = '
SELECT DISTINCT
a.[Name][SQL AGENT JOB NAME],[step_id],[step_name],0[sql_message_id]'+
',0[sql_severity],0[message],0[run_status],[run_date],[run_time],0[run_duration] '+
', 0 [retries_attempted],[server]
FROM [msdb].[dbo].[sysjobs] as a
JOIN [msdb].[dbo].[sysjobhistory] as b on
a.job_id=b.job_id '+
'WHERE
b.Sql_Severity <> 0
AND run_date = REPLACE(CONVERT(VARCHAR(10),GETDATE(),121),''-'','''') '+
'AND [run_time] >= '''+REPLACE(CONVERT(VARCHAR(20),DATEADD(minute, -20,GETDATE()),08),':','')+''''+
' ORDER BY [run_date],a.[Name],b.[step_id] '


-- WRITE TEMP TABLE TO FORMAT DATA
INSERT INTO #SQLAGENTERRORS
EXECUTE MASTER..SP_EXECUTESQL @sqlSelect

IF(SELECT Count(1) from #SQLAGENTERRORS) > 0
BEGIN
DECLARE @calldate DATETIME
,@sqlStatement NVARCHAR(350)
,@subject VARCHAR(128)
,@body VARCHAR(4000)

SET @body='
'

SET @subject = 'Job Step Failures Detected ' + CONVERT(Varchar(25),GETDATE(),120)

IF(SELECT Count(1) from #SQLAGENTERRORS) > 0
BEGIN
DECLARE @Run_Date VARCHAR(12)
,@Run_Time VARCHAR(12)
,@SQLAGENTJOBNAME VARCHAR(200)
,@step_id VARCHAR(12)
,@step_name VARCHAR(200)
,@sql_message_id VARCHAR(12)
,@sql_severity VARCHAR(12)
,@message VARCHAR(500)
,@run_status VARCHAR(12)
,@run_duration VARCHAR(12)
,@retries_attempted VARCHAR(12)
,@server VARCHAR(25)
,@id INT
,@maxID INT

SELECT @id = 1, @maxID = MAX(ID) from #SQLAGENTERRORS
SELECT * from #SQLAGENTERRORS
WHILE @id <= @maxID
BEGIN
SELECT
@Run_Date = RTRIM(SUBSTRING(CONVERT(VARCHAR(8),[run_date]),1,4)+ '-'+ SUBSTRING(CONVERT(VARCHAR(8),[run_date]),5,2)+'-'+SUBSTRING(CONVERT(VARCHAR(8),[run_date]),7,2))
,@Run_Time = CASE WHEN LEN([run_time]) = 5 THEN
RTRIM('0'+ SUBSTRING(CONVERT(VARCHAR(10),[run_time]),1,1)+':'+SUBSTRING(CONVERT(VARCHAR(10),[run_time]),2,2)+':'+SUBSTRING(CONVERT(VARCHAR(10),[run_time]),4,2))
ELSE
RTRIM(SUBSTRING(CONVERT(VARCHAR(10),[run_time]),1,2)+':'+SUBSTRING(CONVERT(VARCHAR(10),[run_time]),3,2)+':'+SUBSTRING(CONVERT(VARCHAR(10),[run_time]),5,2))
END
,@SQLAGENTJOBNAME = RTRIM([SQL AGENT JOB NAME])
,@step_id=RTRIM([step_id])
,@step_name=RTRIM([step_name])
,@sql_message_id=RTRIM([sql_message_id])
,@sql_severity=RTRIM([sql_severity])
,@message=RTRIM([message])
,@run_status=RTRIM([run_status])
,@run_duration=RTRIM([run_duration])
,@retries_attempted=RTRIM([retries_attempted])
,@server=RTRIM([server])
FROM #SQLAGENTERRORS
WHERE id=@id

SELECT @body= @body +'ERROR #'+RTRIM(CONVERT(VARCHAR(10),@id)) +' - '+ @SQLAGENTJOBNAME +' on ' + @server +'

RUN DATE: ' + @Run_Date + ' Run Time: ' +@Run_Time +'
=======================================
Step: ' + @step_id+' - '+@step_name+'


'

SET @id=@id+1

END
EXEC msdb..sp_send_dbmail @recipients = @notifyEmailGroup
,@subject = @subject
,@body=@body


END
END

DROP TABLE #SQLAGENTERRORS



______________________________________________________________________________
"Never argue with an idiot; They'll drag you down to their level and beat you with experience"
Post #1413026
Posted Tuesday, January 29, 2013 7:22 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: 2 days ago @ 11:36 PM
Points: 74, Visits: 481
Thanks. for that code Jessie.

I really need the body part formatted that can give me the details what I am looking for the way it's there in the image attached. I would appreciate if someone comes up or modify the code that you've provided so that I can get a more formatted version of that stuff. I also appreciate if someone comes up with an SSRS solution, documenting the step & providing the code for the same. I think there are ways in which we can format how the results will look when the report is mailed, same as the one in the attachment.

Thanks. once again ""MydoggyJessie" for your help....really appreciate.

Regards,
Faisal
Post #1413038
Posted Tuesday, January 29, 2013 7:44 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 4:32 PM
Points: 2,063, Visits: 3,787
Create a simple SSRS report (RDL file) and call the stored-procedure running this code, format the report to your liking. Reporting Services is pretty straightforward...

______________________________________________________________________________
"Never argue with an idiot; They'll drag you down to their level and beat you with experience"
Post #1413056
Posted Wednesday, January 30, 2013 9:22 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: 2 days ago @ 11:36 PM
Points: 74, Visits: 481
Hey Jessie,

The code is great but it seems that it doesn't do what I wanted. I don't want to set it up across all the instances, instead I would rather set it up across an instance & then get the result by looping through all of my linked servers. This will send me a consolidated report of job failures noticed across all the instances.

I would appreciate if someone could really help me out over here.

Regards,
Faisal
Post #1413683
Posted Wednesday, January 30, 2013 12:36 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 4:32 PM
Points: 2,063, Visits: 3,787
It should be quite simple to modify the code to execute against your linked servers...
Example:
INSERT INTO #SQLAGENTERRORS
EXECUTE [LINKSERVER].MASTER..SP_EXECUTESQL @sqlSelect



______________________________________________________________________________
"Never argue with an idiot; They'll drag you down to their level and beat you with experience"
Post #1413743
Posted Wednesday, January 30, 2013 11:04 PM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: 2 days ago @ 11:36 PM
Points: 74, Visits: 481
Hey Jessie,

That's what I don't want to do providing the linked server name each time for the execute statement. I would like to store all my linked server info in some table or retrieve it from sys.sysservers...something in that way, then either using a cursor loop through all those servers present in that table. That's a much quicker way of doing it I feel.

Thanks. anyways.

Regards,
Faisal
Post #1413848
Posted Thursday, January 31, 2013 7:18 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: 2 days ago @ 11:36 PM
Points: 74, Visits: 481
Hi Jessie,

It seems that your code is not working correctly. After creating the stored proc, when I execute it just completes it's execution within no time...something in the code doesn't seem to do what it has to do, and to add a little more twist e-mail msg can't be sent to my e-mail address...just fyi I have my database mail settings correctly configured, so there is no doubt that there is something to look in the mail part. Could you pls. check your code, most probably the @sqlSelect part.

Regards,
Faisal
Post #1414128
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse