低百分比的磁盘空间警报

时间:2015-03-23 16:26:37

标签: sql sql-server sql-server-2012

我有一个查询,如果它的命中率低于25 GB,则会为服务器驱动器提供空间。但我需要百分比基础,如果它低于10%,那么它应该提醒我们。我在讨论购买监控工具时需要这个。

以下是查询:

Create Procedure dbo.DiskSpaceAlert_Surya

As
Begin

create table #DriveSpaceLeft (Drive varchar(10),
                              [MB Free] bigint)
insert #DriveSpaceLeft (Drive, [MB Free])
   EXEC master.dbo.xp_fixeddrives;

create table DrivesWithIssue (Drive varchar(10),
                              [MB Free] bigint )

insert into DrivesWithIssue 
  select Drive, [MB Free] from #DriveSpaceLeft
  where [MB Free] < 25000

drop table #DriveSpaceLeft

declare @cnt int  
select @cnt=COUNT(1) from DrivesWithIssue
if (@cnt > 0)
begin

    declare @strsubject varchar(100)
    select @strsubject='Check drive space on ' + @@SERVERNAME

    declare @tableHTML  nvarchar(max);
    set @tableHTML =
        N'<H1>Drives with less that 25 GB Free  - ' + @@SERVERNAME + '</H1>' +
        N'<table border="1">' +
        N'<tr><th>Drive</th>' +
        N'<th>MB Free</th></tr>' +
        CAST ( ( SELECT td = [Drive], '',
                        td = [MB Free]
                  FROM DrivesWithIssue
                  FOR XML PATH('tr'), TYPE 
        ) AS NVARCHAR(MAX) ) +
        N'</table>' ;

    EXEC msdb.dbo.sp_send_dbmail
    --@from_address='test@test.com',
    @recipients='',
    @subject = @strsubject,
    @body = @tableHTML,
    @body_format = 'HTML' ,
    @profile_name='Databasemail'
end

drop table DrivesWithIssue
End

结果是

enter image description here

我需要另一列作为免费百分比。

如果您有任何其他脚本可以满足此要求,请随时分享。

2 个答案:

答案 0 :(得分:1)

我找到了解决问题的方法......

SP以获取驱动器空间为&lt; 10

&#13;
&#13;
USE [MYDB]
GO

/****** Object:  StoredProcedure [dbo].[DiskSpaceAlert_Surya]    Script Date: 3/24/2015 11:49:50 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


Create Procedure [dbo].[DiskSpaceAlert_Surya]

As
Begin
declare @cnt int 

create table #DriveSpaceLeft (Drive varchar(10), [total_space] bigint,
                              [MB_Free] bigint, [percentage_free] decimal(12,2))
insert #DriveSpaceLeft (Drive, [total_space],[MB_Free],[percentage_free])
     SELECT DISTINCT 
	--S.TOTAL_BYTES,s.available_bytes,s.logical_volume_name,*,
			s.volume_mount_point [Drive],
			CAST(s.TOTAL_BYTES / 1048576 as decimal(12,2)) [Total Space MBs],
			CAST(s.available_bytes / 1048576 as decimal(12,2)) [AvailableMBs],
			(CAST(s.available_bytes / 1048576 as decimal(12,2)) / 
			CAST(s.TOTAL_BYTES / 1048576 as decimal(12,2)) * 100) [Percentage]
		FROM 
			sys.master_files f
			CROSS APPLY sys.dm_os_volume_stats(f.database_id, f.[file_id]) s;
	 SELECT @cnt=COUNT(*) FROM #DriveSpaceLeft  WHERE percentage_free < 50
 
if (@cnt > 0)
begin
   	declare @strsubject varchar(100)
	select @strsubject='Check drive space on ' + @@SERVERNAME

	declare @tableHTML  nvarchar(max);
	set @tableHTML =
	    N'<H1>Drives with less than 10% Free  - ' + @@SERVERNAME + '</H1>' +
		N'<table border="1">' +
		N'<tr><th>Drive</th>' +
		N'<th>Total space</th>'+
        N'<th>MB Free</th>'+
	    N'<th>Percentage (%) Free</th></tr>' +
	    CAST ( ( SELECT td = [Drive], '',
                		td = [total_space], '',
						td = [MB_Free], '      ',
						td = [percentage_free]
						FROM #DriveSpaceLeft WHERE percentage_free < 50
				  FOR XML PATH('tr'), TYPE 
		) AS NVARCHAR(MAX) ) +
		N'</table>' ;

	EXEC msdb.dbo.sp_send_dbmail
	@recipients='con-balususr@mail.com;',
	@subject = @strsubject,
	@body = @tableHTML,
	@body_format = 'HTML' ,
	@profile_name='Databasemail'
	
end
DROP TABLE #DriveSpaceLeft
End

GO
&#13;
&#13;
&#13;

结果是(我测试了50%以下)

enter image description here

感谢所有宝贵的时间......

答案 1 :(得分:0)

SET NOCOUNT ON;

IF Object_ID('tempdb..#drives', 'U') IS NOT NULL
  BEGIN
    DROP TABLE #drives;
  END
;

CREATE TABLE #drives (
   drive              nvarchar(512)
 , drive_name         nvarchar(512)
 , available_space_mb decimal(18,4)
 , total_space_mb     decimal(18,4)
 , percentage_free As Cast((available_space_mb / total_space_mb) As decimal(5,2))
);

INSERT INTO #drives (drive, available_space_mb)
  EXEC master.dbo.xp_fixeddrives
;

; WITH drive_details AS (
  SELECT DISTINCT
         Left(s.volume_mount_point, 1) As drive
       , s.logical_volume_name As drive_name
       , s.total_bytes     / 1024.0 / 1024.0 As total_space_mb
       , s.available_bytes / 1024.0 / 1024.0 As available_space_mb
  FROM   sys.master_files As f
   CROSS
    APPLY sys.dm_os_volume_stats(f.database_id, f.file_id) As s
)
UPDATE drives
SET    drive_name         = drive_details.drive_name
     , total_space_mb     = drive_details.total_space_mb
     , available_space_mb = drive_details.available_space_mb
FROM   #drives As drives
 INNER
  JOIN drive_details
    ON drive_details.drive = drives.drive
;

SELECT drive
     , drive_name
     , available_space_mb
     , total_space_mb
     , percentage_free
FROM   #drives
ORDER
    BY drive
;