Created a SQL script that actually simply reports the amount of databases backed up with a parameter (number - hours since last backup), how many databases haven't been backed up and how many databases exist on the system.
Simply create the .SQL file for PRTG sensor and execute on the MASTER database.
Set the parameter value in the sensor to e.g. 26 hours (every night backups, plus 2 hours to finish).
Works pretty well with several SQL servers and gives us the necessary information. On top of that we have some file-sensor on the most important DBs running.
The amount of backed up databases vs. all databases vs. not recently backed up depends all on your SQL configuration. We use e.g. LiteSpeed as well and this still works pretty good. Just find out how many databases should be backed up and fix the number in the sensor with minimum and maximum limits so alerts get send out (upper and lower limit should be the same and mirror the amount of DBs expected to be backed up).
Set "RecentlyBackupUpCount" as default channel for better visibility.
Hope this example script helps some of you! I left all my notes in the script as well...
USE master
DECLARE @MaxHours int
SET @MaxHours = @prtg
SELECT
-- how many databases do we have on this server?
(
SELECT COUNT(Name) FROM sys.sysdatabases WHERE name NOT LIKE 'tempdb'
) AS TotalAmountOfDatabases,
(
-- how many databases where backed up within the last n hours?
SELECT COUNT(BackedUpDBs.DatabaseName) FROM (
SELECT sdb.Name AS DatabaseName
,COALESCE(CONVERT(VARCHAR(12), MAX(bus.backup_finish_date), 101),'-') AS LastBackUpDate
,MAX(bus.backup_finish_date) AS LastBackupDateTime
,(MAX(DATEDIFF(hh, GetDate(), bus.backup_finish_date))*-1) AS HoursAgo
FROM sys.sysdatabases sdb
LEFT OUTER JOIN msdb.dbo.backupset bus ON bus.database_name = sdb.name
WHERE 1=1 -- 1=1 as dummy placeholder
AND sdb.Name NOT LIKE 'tempdb' --we ignore tempdb
AND bus.type = 'D' -- only obey DATABASE backups https://technet.microsoft.com/en-us/library/ms186299(v=sql.110).aspx
AND bus.is_damaged = 0
--AND bus.recovery_model = 'FULL' -- only obey FULL backups https://technet.microsoft.com/en-us/library/ms186299(v=sql.110).aspx
--no checking recovery model, this can be set in the settings per DB
AND bus.backup_finish_date IS NOT NULL -- since this can be null, we only check for those which aren't
GROUP BY sdb.Name
) AS BackedUpDBs
WHERE HoursAgo <= @MaxHours
) AS RecentlyBackupUpCount,
(
-- how many databases where NOT backed up within the last n hours?
SELECT COUNT(NotBackedUpDBs.DatabaseName) FROM (
--SELECT NotBackedUpDBs.DatabaseName, * FROM (
SELECT sdb.Name AS DatabaseName
,COALESCE(CONVERT(VARCHAR(12), MAX(bus.backup_finish_date), 101),'-') AS LastBackUpDate
,MAX(bus.backup_finish_date) AS LastBackupDateTime
,(MAX(DATEDIFF(hh, GetDate(), bus.backup_finish_date))*-1) AS HoursAgo
FROM sys.sysdatabases sdb
LEFT OUTER JOIN msdb.dbo.backupset bus ON bus.database_name = sdb.name
WHERE 1=1 -- 1=1 as dummy placeholder
AND sdb.Name NOT LIKE 'tempdb' --we ignore tempdb
AND bus.type = 'D' -- only obey DATABASE backups https://technet.microsoft.com/en-us/library/ms186299(v=sql.110).aspx
--AND bus.recovery_model = 'FULL' -- only obey FULL backups https://technet.microsoft.com/en-us/library/ms186299(v=sql.110).aspx
--no checking recovery model, this can be set in the settings per DB
GROUP BY sdb.Name
) AS NotBackedUpDBs
WHERE HoursAgo > @MaxHours
) AS NOTRecentlyBackupUpCount
Regards
Florian Rossmark
Add comment