Tárolt eljárás, amely ütemezhető rendszeres ellenőrzésre:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
USE [master] GO CREATE PROC dbo.usp_AlertOnDBSpace (@minFreeDrvPctAllowed DECIMAL(5, 1) = 30.0) AS BEGIN SET NOCOUNT ON DECLARE @localDrivesSpaceInfo TABLE ( idx INT IDENTITY ,DriveLetter CHAR(1) ,FreeSpaceGB FLOAT ,TotSizeGB FLOAT ,pctFree AS round(100.0 * FreeSpaceGB / TotSizeGB, 1) ); DECLARE @infoFromPS TABLE (data VARCHAR(200)); DECLARE @i INT = 0 DECLARE @tblen INT DECLARE @currDrv CHAR(1) DECLARE @currDrvPct DECIMAL(5, 1) DECLARE @freeGB FLOAT DECLARE @totGB FLOAT DECLARE @warnMsg VARCHAR(128) INSERT INTO @infoFromPS EXEC xp_cmdshell 'Powershell.exe "Get-WMIObject Win32_LogicalDisk -filter "DriveType=3"| Format-Table DeviceID,FreeSpace,Size"'; DELETE FROM @infoFromPS WHERE data IS NULL OR data LIKE '%DeviceID%' OR data LIKE '%----%' OR len(rtrim(ltrim(data))) < 3; INSERT INTO @localDrivesSpaceInfo SELECT LEFT(data, 1) AS driveLetter ,ltrim(rtrim(substring(data, 3, len(data) - 2 - charIndex(' ', REVERSE(data), 1)))) AS FreeSpaceGB ,right(data, charIndex(' ', REVERSE(data), 1) - 1) AS TotSizeGB FROM @infoFromPS SELECT @tblen = count(*) FROM @localDrivesSpaceInfo SET @i = @i + 1 WHILE @i <= @tblen BEGIN SELECT @currDrv = DriveLetter ,@currDrvPct = pctFree ,@freeGB = ROUND(FreeSpaceGB / POWER(1024, 3), 1) ,@totGB = ROUND(TotSizeGB / POWER(1024, 3), 1) FROM @localDrivesSpaceInfo WHERE @i = idx; IF ( @currDrv IN ( SELECT DISTINCT left(upper(rtrim(ltrim(t.physical_name))), 1) AS DB_Drive FROM sys.master_files t ) ) AND (@currDrvPct < @minFreeDrvPctAllowed) BEGIN SET @warnMsg = CONCAT ( 'Warning! - Drive "' ,@currDrv ,'" has ' ,rtrim(ltrim(Str(@currDrvPct))) ,'% of disk space left!' ,' (' ,rtrim(ltrim(Str(@freeGB))) ,' GB free out of ' ,rtrim(ltrim(Str(@totGB))) ,' GB ) ' ) SELECT @warnMsg AS msg -- log it in viewer and in srv error logs EXEC xp_logevent 60000 ,@warnMsg ,warning END SET @i = @i + 1 END END GO |
Az eredeti szkript:
https://www.mssqltips.com/sqlservertip/4670/simple-free-drive-space-alert-for-sql-server-databases/