Monitor in SQLServer Free Space with Available Disk for db Log files

MS Techie 2,706 Reputation points
2021-08-26T10:21:37.757+00:00

For my SQLServer , how to Monitor free space in database log files after considering the available disk size

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
14,128 questions
{count} votes

Accepted answer
  1. AmeliaGu-MSFT 13,991 Reputation points Microsoft Vendor
    2021-08-27T05:55:57.667+00:00

    Hi MSTechie-7364,
    Please refer to the query which might be helpful:

    ;WITH src AS
    (
      SELECT 
             FileID      = f.file_id,
             LogicalName = f.name,
             [Path]      = f.physical_name, 
             FileSizeMB  = f.size/128.0, 
             UsedSpaceMB = CONVERT(bigint, FILEPROPERTY(f.[name], 'SpaceUsed'))/128.0, 
             GrowthMB    = CASE f.is_percent_growth WHEN 1 THEN NULL ELSE f.growth/128.0 END,
             MaxSizeMB   = NULLIF(f.max_size, -1)/128.0,
             DriveSizeMB = vs.total_bytes/1048576.0,
             DriveFreeMB = vs.available_bytes/1048576.0
      FROM sys.database_files AS f
      CROSS APPLY sys.dm_os_volume_stats(DB_ID(), f.file_id) AS vs
    )
    SELECT  FileID, LogicalName, [Path],
      FileSizeMB  = CONVERT(decimal(18,2), FileSizeMB),
      FreeSpaceMB = CONVERT(decimal(18,2), FileSizeMB-UsedSpaceMB),
      [%]         = CONVERT(decimal(5,2), 100.0*(FileSizeMB-UsedSpaceMB)/FileSizeMB),
      GrowthMB    = COALESCE(RTRIM(CONVERT(decimal(18,2), GrowthMB)), '% warning!'),
      MaxSizeMB   = CONVERT(decimal(18,2), MaxSizeMB),
      DriveSizeMB = CONVERT(bigint, DriveSizeMB),
      DriveFreeMB = CONVERT(bigint, DriveFreeMB),
      [%]         = CONVERT(decimal(5,2), 100.0*(DriveFreeMB)/DriveSizeMB)
    FROM src
    

    Best Regards,
    Amelia

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.