首页 > 数据库 > SQL Server > 正文

3. SQL Server数据库状态监控

2024-08-31 00:55:26
字体:
来源:转载
供稿:网友
3. SQL Server数据库状态监控 - 可用空间

数据库用来存放数据,那么肯定需要存储空间,所以对磁盘空间的监视自然就很有必要了。

. 磁盘可用空间

1. 操作系统命令或脚本、接口或工具

(1) DOS命令: fsutil volume diskfree

C:/windows/system32>fsutil volume diskfree C:

Total # of free bytes : 9789493248

Total # of bytes : 64424505344

Total # of avail free bytes : 9789493248

这里用到了fsutil,一个文件系统管理工具(file system utility),应该还有其他一些命令或者脚本也是可以的。

(2) WMI/WMIC: wmic logicaldisk

WMI是个Windows系统的管理接口,在WMIC出现之前,如果要利用WMI管理系统,必须使用一些专门的WMI应用,例如SMS,或者使用WMI的脚本编程API,或者使用象CIM Studio之类的工具。如果不熟悉C++之类的编程语言或VBScript之类的脚本语言,或者不掌握WMI名称空间的基本知识,要用WMI管理系统是很困难的。WMIC改变了这种情况,它为WMI名称空间提供了一个强大的、友好的命令行接口。

C:/windows/system32>wmic logicaldisk get caption,freespace,size

Caption FreeSpace Size

C: 9789071360 64424505344

D: 189013438464 255331397632

这里通过wmic的get命令获取了logicaldisk 的几个参数列。

(3) 性能监视器

LogicalDisk: %Free Space

LogicalDisk: Free Megabytes

总大小 = LogicalDisk: Free Megabytes/ LogicalDisk: %Free Space

性能监视器虽然用于现场诊断还是挺方便的,但实现自动化监控,并不太好用。

2. SQL 语句

(1) 扩展存储过程xp_cmdshell (还是在调用操作系统命令)

DECLARE @Drive TINYINT,      @SQL VARCHAR(100)DECLARE @Drives TABLE(Drive CHAR(1),Info VARCHAR(80))SET @Drive = 97WHILE @Drive <= 122BEGIN    SET @SQL = 'EXEC XP_CMDSHELL ''fsutil volume diskfree ' + CHAR(@Drive) + ':'''    INSERT @Drives    (    Info    )    EXEC(@SQL)    UPDATE @Drives       SET Drive = CHAR(@Drive)     WHERE Drive IS NULL    SET @Drive = @Drive + 1ENDSELECT Drive,SUM(CASE WHEN Info LIKE 'Total # of bytes%' THEN CAST(REPLACE(SUBSTRING(Info, 32, 48), CHAR(13), '') AS BIGINT) ELSE CAST(0 AS BIGINT) END)/1024.0/1024/1024 AS TotalMBytes,SUM(CASE WHEN Info LIKE 'Total # of free bytes%' THEN CAST(REPLACE(SUBSTRING(Info, 32, 48), CHAR(13), '') AS BIGINT) ELSE CAST(0 AS BIGINT) END)/1024.0/1024/1024 AS FreeMBytes,SUM(CASE WHEN Info LIKE 'Total # of avail free bytes%' THEN CAST(REPLACE(SUBSTRING(Info, 32, 48), CHAR(13), '') AS BIGINT) ELSE CAST(0 AS BIGINT) END)/1024.0/1024/1024 AS AvailFreeMBytesFROM(SELECT Drive,       Info  FROM @Drives WHERE Info LIKE 'Total # of %') AS dGROUP BY DriveORDER BY Drive

xp_cmdshell可以执行操作系统命令行,这段脚本用fsutil volume diskfree命令对26个字母的盘符遍历了一遍,不是很好,改用wmic会方便些,如下:

EXEC xp_cmdshell 'wmic logicaldisk get caption,freespace,size';

(2) 扩展存储过程xp_fixeddrives

--exec xp_fixeddrivesIF object_id('tempdb..#drivefreespace') IS NOT NULL    DROP TABLE #drivefreespaceCREATE TABLE #drivefreespace(Drive CHAR(1), FreeMb bigint)INSERT #drivefreespace EXEC ('exec xp_fixeddrives')SELECT * FROM #drivefreespace

Drive

FreeMb

C

9316

D

180013

总算不依赖操作系统命令了,不过,这个存储过程只能返回磁盘可用空间,没有磁盘总空间。

(3) DMV/DMF: sys.dm_os_volume_stats

SELECT DISTINCT       @@SERVERNAME as [server]      ,volume_mount_point as drive      ,cast(available_bytes/ 1024.0 / 1024.0 / 1024.0 AS INT) as free_gb      ,cast(total_bytes / 1024.0 / 1024.0 / 1024.0 AS INT) as total_gbFROM sys.master_files AS fCROSS APPLY sys.dm_os_volume_stats(f.database_id, f.file_id)ORDER BY @@SERVERNAME, volume_mount_point

server

drive

free_gb

total_gb

C:/

9

59

D:/

175

237

从SQL Server 2008 R2 SP1开始,有了这个很好用的DMF: sys.dm_os_volume_stats,弥补了之前xp_fixeddrives没有磁盘总空间的不足。

不过,看它的参数就可以知道,没被任何数据库使用的磁盘,是查看不了的,所以xp_fixeddrives还有存在的必要。

. 数据库可用空间

1. 文件可用空间查看

(1) 文件已用空间,当前大小(已分配空间),最大值,如下:

select @@SERVERNAME as server_name      ,DB_NAME() as database_name      ,case when data_space_id = 0 then 'LOG'            else FILEGROUP_NAME(data_space_id)             end as file_group      ,name as logical_name      ,physical_name      ,type_desc      ,FILEPROPERTY(name,'SpaceUsed')/128.0 as used_size_Mb      ,size/128.0 as allocated_size_mb       ,case when max_size = -1 then max_size             else max_size/128.0             end as max_size_Mb      ,growth      ,is_percent_growth from sys.database_fileswhere state_desc = 'ONLINE'

(2) 再算上磁盘的空闲空间,改动如下:

select @@SERVERNAME as server_name      ,DB_NAME() as database_name      ,case when data_space_id = 0 then 'LOG'            else FILEGROUP_NAME(data_space_id)             end as file_group      ,name as logical_name      ,physical_name      ,type_desc      ,FILEPROPERTY(name,'SpaceUsed')/128.0 as used_size_mb      ,size/128.0 as allocated_size_mb      ,case when max_size = -1 then max_size                 else max_size/128.0                 end as max_size_mb      ,vs.available_bytes/1024.0/1024 as disk_free_mb      ,growth      ,CAST(is_percent_growth as int) as is_percent_growthfrom sys.database_files dfcross apply sys.dm_os_volume_stats(DB_ID(),df.file_id) vswhere state_desc = 'ONLINE'

如果是SQL Server 2008 SP1以前的版本,可用xp_fixeddrives生成磁盘空闲空间表,再进行关联。

(3) 结合文件是否自增长,文件最大值,磁盘空间,算出文件可用空间比率,改动如下:

select @@SERVERNAME as server_name      ,DB_NAME() as database_name      ,case when data_space_id = 0 then 'LOG'            else FILEGROUP_NAME(data_space_id)             end as file_group      ,name as logical_name      ,physical_name      ,type_desc      ,FILEPROPERTY(name,'SpaceUsed')/128.0 as used_size_mb      ,size/128.0 as allocated_size_mb      ,case when max_size = -1 then max_size                 else max_size/128.0                 end as max_size_mb      ,vs.available_bytes/1024.0/1024 as disk_free_mb      ,case when growth = 0 then  (size - FILEPROPERTY(name,'SpaceUsed'))*1.0/size            when growth > 0 and max_size = -1 then ((size/128.0 + vs.available_bytes/1024.0/1024) - FILEPROPERTY(name,'SpaceUsed')/128.0)/(size/128.0 + vs.available_bytes/1024.0/1024)            when growth
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表