Red Gate forums :: View topic - Custom Alert: Need query for Job Executing
Return to www.red-gate.com RSS Feed Available

Search  | Usergroups |  Profile |  Messages |  Log in  Register 
Go to product documentation
SQL Monitor 3
SQL Monitor 3 forum

Custom Alert: Need query for Job Executing

Search in SQL Monitor 3 forum
Post new topic   Reply to topic
Jump to:  
Author Message
PDinCA



Joined: 25 Jul 2005
Posts: 509
Location: Costa Mesa, CA, USA

PostPosted: Mon Sep 17, 2012 8:50 pm    Post subject: Custom Alert: Need query for Job Executing Reply with quote

The "Job Duration Unusual" Alert is fine when the job actually stops, but there are times when a Job runs interminably.

I need a Custom Metric that tells me when an active Job has been running for more than my (threshold of pain) max "exceptional duration".

For now, I need that threshold to be in hours. If I can make it data-driven by Jobname, that would be ideal! If the query can poll the same target server as the Job is executing on and retrieve a configuration value from one of my existing configuration tables, I can then set up my "Job Still Running" Alert:

When Job <name> has been running for more than <configured hours for Job <name>> Alert me with High Importance.

Can this be done, and if so, could Red Gate share a sample query for "retrieve currently executing Jobs" that I can filter?

This could then be added to the list on the http://sqlmonitormetrics.red-gate.com site for others to use.

In hopes, with thanks, Stephen
Back to top
View user's profile Send private message
Brian Donahue



Joined: 23 Aug 2004
Posts: 6641

PostPosted: Wed Sep 19, 2012 1:55 pm    Post subject: Reply with quote

Hi Stephen,

You can try plugging this query into a custom metric against the MSDB database. It will report the number of seconds a job named MyJob has been running. You should then be able to set an alert when the number of seconds has been exceeded. I hope this helps!
Code:
/* Get the JobID for MyJob */
DECLARE @JobId UNIQUEIDENTIFIER
SELECT @JobId= job_id FROM msdb..sysjobs WHERE name='MyJob'
/* Define a temp table to hold xp_sqlagent_enum_jobs
This xp will tell us which jobs are running */
DECLARE @ExecutionStatus table
 (
   JobID uniqueidentifier primary key, -- Job ID
   LastRunDate int, LastRunTime int, -- Last run date and time
   NextRunDate int, NextRunTime int, -- Next run date and time
   NextRunScheduleID int, -- an internal schedule id
   RequestedToRun int, RequestSource int, RequestSourceID varchar(128),
   running int,  -- 0 or 1, 1 means the job is executing
   CurrentStep int, -- which step is running
   CurrentRetryAttempt int, -- retry attempt
   JobState int ----0 = Not idle or suspended, 1 = Executing, 2 = Waiting For Thread, 3 = Between Retries, 4 = Idle, 5 = Suspended, [6 = WaitingForStepToFinish], 7 = PerformingCompletionActions
 )
INSERT @ExecutionStatus
        EXEC xp_sqlagent_enum_jobs 1, '', @JobId
        /* Get the number of seconds MyJob has been running, if it is running */
      SELECT DATEDIFF(ss,a.start_execution_date,GETDATE()) AS RunningSeconds FROM msdb..sysjobactivity a INNER JOIN @ExecutionStatus s ON a.job_id=s.JobId WHERE s.running=1
Back to top
View user's profile Send private message
PDinCA



Joined: 25 Jul 2005
Posts: 509
Location: Costa Mesa, CA, USA

PostPosted: Wed Sep 19, 2012 5:48 pm    Post subject: Reply with quote

Helps IMMENSELY.

Many thanks, Brian.

UPDATE
Sadly, the join criterion on just job_id gets me the list of every execution, so I revised the query, using my own replacement for DATEDIFF() for the hours calc as I want to be precise, not just have it fire after 3hrs 1min if it starts at, say, 12:59... (Hate DATEDIFF!)

Using SELECT TOP 1 ... ORDER BY a.start_execution_date DESC gets me the currently executing Job details.
Code:
/* Get the JobID for MyJob */
DECLARE @JobId uniqueidentifier

SELECT @JobId = job_id
  FROM msdb..sysjobs
 WHERE name = '_Quarter Hourly Update Aggregation'
/* Define a temp table to hold xp_sqlagent_enum_jobs
This xp will tell us which jobs are running */
DECLARE @ExecutionStatus TABLE
      ( JobID                 uniqueidentifier PRIMARY KEY -- Job ID
      , LastRunDate           int
      , LastRunTime           int -- Last run date and time
      , NextRunDate           int
      , NextRunTime           int -- Next run date and time
      , NextRunScheduleID     int -- an internal schedule id
      , RequestedToRun        int
      , RequestSource         int
      , RequestSourceID       varchar(128)
      , running               int -- 0 or 1, 1 means the job is executing
      , CurrentStep           int -- which step is running
      , CurrentRetryAttempt   int -- retry attempt
      , JobState              int ----0 = Not idle or suspended, 1 = Executing, 2 = Waiting For Thread, 3 = Between Retries, 4 = Idle, 5 = Suspended, [6 = WaitingForStepToFinish], 7 = PerformingCompletionActions
      )
INSERT @ExecutionStatus
  EXEC xp_sqlagent_enum_jobs 1
                           , ''
                           , @JobId
/* Get the number of hours the has been running, if it is running */
SELECT TOP 1
       DBA.dbo.udf_GetElapsedHours(a.start_execution_date, GETDATE()) AS RunningHours
  FROM msdb..sysjobactivity a
       INNER  JOIN @ExecutionStatus s
                ON s.JobId = a.job_id
 WHERE s.running = 1
 ORDER BY a.start_execution_date DESC


Thanks, though, for your original query - wouldn't be anywhere without it. Cool


Last edited by PDinCA on Wed Sep 19, 2012 8:22 pm; edited 2 times in total
Back to top
View user's profile Send private message
PDinCA



Joined: 25 Jul 2005
Posts: 509
Location: Costa Mesa, CA, USA

PostPosted: Wed Sep 19, 2012 7:18 pm    Post subject: Reply with quote

As there's no Uservoice (that I can see), could I register this post as an Enhancement Request?

As it is, I can use the above for ONE Job per Custom Alert, which is fine if that's all one cared about, up to a handful perhaps would be OK, but in environments with many servers and many, many Jobs, I can see this kind of thing being unwieldy in a hurry.

Would RG please consider adding a standard alert "Job Still Running"? It should be reusable by being able to select the job of interest and state the job duration that when exceeded would raise the alert. Alert thresholds could use the standard Low/Medium/High measures for "Duration in excess of the Alert Duration", perhaps.

Thanks in hopes.
Back to top
View user's profile Send private message
Brian Donahue



Joined: 23 Aug 2004
Posts: 6641

PostPosted: Thu Sep 20, 2012 9:09 am    Post subject: Reply with quote

Hi Stephen,

I'm glad I could help. I'm surprised that you can't just get this information from sysjobhistory, but that table only updates after the job finishes. So you have to use an undocumented xp to get the running jobs and then check the job activity table.

Sql Monitor has a UserVoice site here in case you want to add suggestions:

http://sqlmonitor.uservoice.com/forums/91743-suggestions
Back to top
View user's profile Send private message
PDinCA



Joined: 25 Jul 2005
Posts: 509
Location: Costa Mesa, CA, USA

PostPosted: Thu Sep 20, 2012 6:06 pm    Post subject: Reply with quote

Thanks for the Uservoice URL, Brian, I added to the very similar suggestion titled, "specify a time in which jobs should be finished by" at http://sqlmonitor.uservoice.com/forums/91743-suggestions/suggestions/2708577-specify-a-time-in-which-jobs-should-be-finished-by

Would you please post the RSS URL for the SSC Uservoice as I like to keep an eye on suggestions that way (I already have the SQL Prompt, SSC and Search RSS feeds active). Thanks.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic All times are GMT + 1 Hour
Page 1 of 1

 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group