07-19-2023, 05:22 PM
I'm trying to setup a process that will check the agent jobs on all servers in an availability group and report back any that might be missing from secondary replicas.
I have CMS setup with all servers from the AG and can run a query for agent jobs across the replicas, but I only want to return jobs that are on some servers but not all of them.
I wrote a query to get this information by using COUNT() but because the query results are grouped by server, this isn't working. I also tried to put the results in a temporary table to see if this would help, but that doesn't seem to have made much of a difference.
There are four servers in the AG and the following query should return the name of any jobs that aren't on all four servers, but because it seems to return the server name too it isn't recognising them as the same Job.
CREATE TABLE #jobs (
JobName NVARCHAR(128))
INSERT INTO #jobs
SELECT name JobName
FROM sysjobs
SELECT *
FROM #jobs
GROUP BY JobName
HAVING COUNT(*) < 4
ORDER BY JobName
DROP TABLE #jobs
The query as it stands would throw out a result set such as below:
+------------+------------------------------------------+
| ServerName | JobName |
+------------+------------------------------------------+
| ServerA | DBA DatabaseBackup - AG_DATABASES - FULL |
| ServerA | OutputFile Cleanup |
| ServerB | DBA DatabaseBackup - AG_DATABASES - FULL |
| ServerB | IndexOptimize - USER_DATABASES |
| ServerC | DBA DatabaseBackup - AG_DATABASES - FULL |
| ServerB | IndexOptimize - USER_DATABASES |
| ServerD | DBA DatabaseBackup - AG_DATABASES - FULL |
| ServerD | Output File Cleanup |
+------------+------------------------------------------+
The job DBA DatabaseBackup - AG_DATABASES - FULL exists for all four servers so I don't want this one returned, but because of the implicit servername column which comes from the CMS connection it doesn't recognise them as the same value.
I have CMS setup with all servers from the AG and can run a query for agent jobs across the replicas, but I only want to return jobs that are on some servers but not all of them.
I wrote a query to get this information by using COUNT() but because the query results are grouped by server, this isn't working. I also tried to put the results in a temporary table to see if this would help, but that doesn't seem to have made much of a difference.
There are four servers in the AG and the following query should return the name of any jobs that aren't on all four servers, but because it seems to return the server name too it isn't recognising them as the same Job.
CREATE TABLE #jobs (
JobName NVARCHAR(128))
INSERT INTO #jobs
SELECT name JobName
FROM sysjobs
SELECT *
FROM #jobs
GROUP BY JobName
HAVING COUNT(*) < 4
ORDER BY JobName
DROP TABLE #jobs
The query as it stands would throw out a result set such as below:
+------------+------------------------------------------+
| ServerName | JobName |
+------------+------------------------------------------+
| ServerA | DBA DatabaseBackup - AG_DATABASES - FULL |
| ServerA | OutputFile Cleanup |
| ServerB | DBA DatabaseBackup - AG_DATABASES - FULL |
| ServerB | IndexOptimize - USER_DATABASES |
| ServerC | DBA DatabaseBackup - AG_DATABASES - FULL |
| ServerB | IndexOptimize - USER_DATABASES |
| ServerD | DBA DatabaseBackup - AG_DATABASES - FULL |
| ServerD | Output File Cleanup |
+------------+------------------------------------------+
The job DBA DatabaseBackup - AG_DATABASES - FULL exists for all four servers so I don't want this one returned, but because of the implicit servername column which comes from the CMS connection it doesn't recognise them as the same value.