디스크 남은 공간 확인하기 > db

본문 바로가기
사이트 내 전체검색

db

디스크 남은 공간 확인하기

페이지 정보

작성자 서방님 댓글 1건 조회 161회 작성일 06-12-13 20:46

본문

현재 하드디스크의 사용 가능한 여유 공간을 체크하여 지정한 사이즈보다 적은 경우 오류 메세지를 발생시키는 저장 프로시져의 예입니다.

1. 아래의 스트립트를 수행하여 sp_ABCheckFreeSpac 저장 프로시져를 만들어 사용하면 됩니다.

USE MASTER
GO

IF OBJECT_ID('sp_ABCheckFreeSpace') IS NOT NULL
DROP PROCEDURE sp_ABCheckFreeSpace
GO

CREATE PROCEDURE sp_ABCheckFreeSpace (@Threshhold int = 300)

/* Name: sp_ABCheckFreeSpace

Purpose: Raise an error if the free space on a drive that contains the root-level
directory name of MSSQL7 has less than the threshhold of free space.

Returns: Count of errors raised (0 if no problems).

Sample Usage:
sp_ABCheckFreeSpace 200

History:
03/20/2001 VRI Created.
*/

AS

SET NOCOUNT ON

DECLARE @FreeSpace int,
@FreeSpaceChar varchar(15),
@Drive char(1),
@FileExist int,
@Mssql varchar(20),
@ErrorCount int

SELECT @ErrorCount = 0

CREATE TABLE #Drives (Drive char(1), FreeSpace int)
CREATE TABLE #FileExists (FileExists int, FileIsDir int, ParentDirExists int)

INSERT #Drives
EXEC master..xp_fixeddrives

WHILE EXISTS (SELECT * FROM #Drives)
BEGIN

SELECT @Drive = Drive,
@FreeSpace = FreeSpace
FROM #Drives

DELETE #Drives
WHERE Drive = @Drive

SELECT @Mssql = @Drive + ':MSSQL7',
@FreeSpaceChar = convert(varchar(10), @FreeSpace)

DELETE #FileExists
INSERT #FileExists
EXEC master..xp_fileexist @Mssql

IF @FreeSpace < @Threshhold AND EXISTS (SELECT * FROM #FileExists WHERE FileIsDir = 1)
BEGIN
RAISERROR('Drive %s on server %s has only %s MB left.', 19, 1, @Drive, @@Servername,
@FreeSpaceChar) WITH LOG
SELECT @ErrorCount = @ErrorCount + 1
END

END
RETURN @ErrorCount
GO

GRANT EXEC ON sp_ABCheckFreeSpace TO public
GO


2. 사용하는 방법은 저장 프로시져 이름 뒤에 어느 정도의 공간이 남아야 하는지 목표로 하는 사이즈를 MB 단위로 지정하면 됩니다. 다음의 예는 500MB 보다 적은 여유 공간일 경우 오류를 발생시키는 것입니다.

sp_ABCheckFreeSpace 500

다음과 같은 오류가 발생합니다.

서버: 메시지 50000, 수준 19, 상태 1, 프로시저 sp_ABCheckFreeSpace, 줄 56
Drive C on server FUTURE has only 460 MB left.

※ 본 스크립트는 http://www.swynk.com 에서 가져온 것입니다.

댓글목록

서방님님의 댓글

서방님 작성일

<P style="MARGIN-LEFT: 5px"><B>[참고] 수행이 제대로 안되는 경우 참고하세요.</B></P>
<P style="MARGIN: 0px 5px; LINE-HEIGHT: 24px">스크립트를 그대로 사용하시면 제대로 작동하지 않을 수 있습니다.<BR>다음과 같이 빨간색으로 되어 있는 두 곳을 적절하게 수정해 보시기 바랍니다.<BR><BR>******<BR><BR>USE MASTER<BR>GO <BR>IF OBJECT_ID('sp_ABCheckFreeSpace') IS NOT NULL<BR>DROP PROCEDURE sp_ABCheckFreeSpace <BR>GO<BR><BR>CREATE PROCEDURE sp_ABCheckFreeSpace (@Threshhold int = 300)<BR><BR>/* Name: sp_ABCheckFreeSpace<BR><BR>Purpose: Raise an error if the free space on a drive that contains the root-level<BR>directory name of MSSQL7 has less than the threshhold of free space.<BR><BR>Returns: Count of errors raised (0 if no problems).<BR><BR>Sample Usage:<BR>sp_ABCheckFreeSpace 200<BR><BR>History:<BR>03/20/2001 VRI Created.<BR>*/<BR><BR>AS<BR><BR>SET NOCOUNT ON<BR><BR>DECLARE @FreeSpace int,<BR>@FreeSpaceChar varchar(15), <BR>@Drive char(1),<BR>@FileExist int,<BR><FONT color=red>@Mssql varchar(20),</FONT> <BR><FONT color=green>-- MS SQL이 성치된 경로가 20 바이트를 넘는다면 varchar(100) 정도로 늘려주세요.</FONT><BR>@ErrorCount int<BR><BR>SELECT @ErrorCount = 0<BR><BR>CREATE TABLE #Drives (Drive char(1), FreeSpace int)<BR>CREATE TABLE #FileExists (FileExists int, FileIsDir int, ParentDirExists int)<BR><BR>INSERT #Drives <BR>EXEC master..xp_fixeddrives<BR><BR>WHILE EXISTS (SELECT * FROM #Drives)<BR>BEGIN<BR><BR>SELECT @Drive = Drive, <BR>@FreeSpace = FreeSpace<BR>FROM #Drives<BR><BR>DELETE #Drives<BR>WHERE Drive = @Drive<BR><BR><FONT color=red>SELECT @Mssql = @Drive + ':MSSQL7',</FONT><BR><FONT color=green>-- MS SQL 서버가 설치된 경로를 적어 주세요. <BR>-- 저의 경우는 :MSSQL7 대신에 :Program FilesMicrosoft SQL Server를 적어 주었습니다.</FONT><BR>@FreeSpaceChar = convert(varchar(10), @FreeSpace)<BR><BR>DELETE #FileExists<BR>INSERT #FileExists <BR>EXEC master..xp_fileexist @Mssql<BR><BR>IF @FreeSpace &lt; @Threshhold AND EXISTS (SELECT * FROM #FileExists WHERE FileIsDir = 1)<BR>BEGIN<BR>RAISERROR('Drive %s on server %s has only %s MB left.', 19, 1, @Drive, @@Servername, <BR>@FreeSpaceChar) WITH LOG<BR>SELECT @ErrorCount = @ErrorCount + 1<BR>END <BR><BR>END<BR>RETURN @ErrorCount<BR>GO<BR><BR>GRANT EXEC ON sp_ABCheckFreeSpace TO public<BR>GO<BR><BR>*****<BR><BR>즐거운 날 되세요~</P>

Total 464건 26 페이지
게시물 검색

회원로그인

접속자집계

오늘
31
어제
302
최대
1,347
전체
155,092
Latest Crypto Fear & Greed Index

그누보드5
Copyright © 서방님.kr All rights reserved.