SQL Server 定時檢查數據庫服務器磁盤空間

查看磁盤空間的方法有不少,好比 Powershell,  WMIC 等,下面是用T-SQL的方法查看。對於多服務器,能夠寫遊標,也能夠在 Register Server 中創建一個組,好比 Production Group,QA Group 等等, 添加相應的服務器到不一樣的組中,最後在組的節點上應用下面的腳本便可。shell

 

 

create table #a (id int IDENTITY(1,1),DiskName varchar(50))服務器

insert into #a(DiskName)spa

  exec xp_cmdshell  'wmic LOGICALDISK get name'server

create table #b (id int IDENTITY(1,1),freespace varchar(50))ci

insert into #b(freespace)get

  exec xp_cmdshell  'wmic LOGICALDISK get freespace'cmd

create table #c (id int IDENTITY(1,1),size varchar(50))io

insert into #c(size)table

  exec xp_cmdshell  'wmic LOGICALDISK get size'select

 

select server_name=@@servername,DiskName

,convert(bigint,replace(size,char(13),''))/1024/1024/1024 as total_disk_size_gb

,convert(bigint,replace(#b.freespace,char(13),''))/1024/1024/1024 as free_disk_size_gb

,convert(varchar,convert(decimal(4, 2),(convert(decimal(15, 2),convert(decimal(15, 2),replace(#b.freespace,char(13),''))/1024/1024/1024*100)/

convert(decimal(15, 2),convert(decimal(15, 2),replace(size,char(13),''))/1024/1024/1024))))+'%' as free_space_percent

from #a join #b on #a.id=#b.id join #c on #a.id=#c.id

where #a.id >1 and #b.freespace is not null and charindex(char(13),replace(#b.freespace,' ','')) <>1

 

drop table #a,#b,#c

相關文章
相關標籤/搜索