Check Free Space of Drive

Technical support and scripting issues

Moderators: JRL, Dorian (MJT support)

Post Reply
alanimal
Junior Coder
Posts: 40
Joined: Thu Jun 09, 2005 11:57 pm

Check Free Space of Drive

Post by alanimal » Mon Jun 09, 2008 1:09 am

:shock: Hi Guys,

I have a drive for log files on an SQL server that now and then fill up.

How can I check that the drive as sufficient room, and if not, the script automatically restarts the MSSQLSERVER service? (this clears out the TempDB log files).

Thanks in advance
AL

User avatar
Rain
Automation Wizard
Posts: 550
Joined: Tue Aug 09, 2005 5:02 pm
Contact:

Post by Rain » Mon Jun 09, 2008 3:34 pm

Is this what you looking for?

Code: Select all

VBSTART

Function FreeSpace(drvPath)
  Dim fso, drv, s
  Set fso = CreateObject("Scripting.FileSystemObject")
  Set drv = fso.GetDrive(fso.GetDriveName(drvPath))
  FreeSpace = drv.FreeSpace
End Function

VBEND

let>DIRECTORY=C:\
VBEval>FreeSpace("%DIRECTORY%"),fs
MessageModal>Free Space on Drive %DIRECTORY%: %fs% bytes

User avatar
JRL
Automation Wizard
Posts: 3532
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Mon Jun 09, 2008 4:47 pm

Or, straight out of help for libfunc>

Code: Select all

LibFunc>kernel32,GetDiskFreeSpaceExA,spacefree,c:\,REF:0,REF:0,REF:0
Let>used=spacefree_3/1024
Let>free=spacefree_4/1024
MessageModal> Disk space used: %used%Kb %CRLF% Free space: %free%Kb
Edit:Somehow in a copy and paste from help, a percent sign disappeared.
I put it back.
Last edited by JRL on Tue Jun 10, 2008 12:20 am, edited 1 time in total.

alanimal
Junior Coder
Posts: 40
Joined: Thu Jun 09, 2005 11:57 pm

Post by alanimal » Mon Jun 09, 2008 10:00 pm

Thanks. Rain, yours works sweet as - but I get an error on JRL's post.

Rain, how can I get the output to be in Gb? instead of KB?

Thanks!

Me_again
Automation Wizard
Posts: 1101
Joined: Fri Jan 07, 2005 5:55 pm
Location: Somewhere else on the planet

Post by Me_again » Mon Jun 09, 2008 10:36 pm

You probably will want to round the result too, so to get the result in gigs add this line after the VBEval>FreeSpace etc. line

VBEval>Round(%fs%/1073741824,4),fs

The ",4" rounds to 4 decimals, you can change that to your choice.

User avatar
Rain
Automation Wizard
Posts: 550
Joined: Tue Aug 09, 2005 5:02 pm
Contact:

Post by Rain » Tue Jun 10, 2008 11:50 am

Here are the conversions from bytes to Kilobyte, Megabyte, Gigabyte, Terabyte and Petabyte in "plain" English I hope.

Code: Select all

VBSTART

Function FreeSpace(drvPath)
  Dim fso, drv, s
  Set fso = CreateObject("Scripting.FileSystemObject")
  Set drv = fso.GetDrive(fso.GetDriveName(drvPath))
  FreeSpace = drv.FreeSpace
End Function

VBEND

let>DIRECTORY=C:\
VBEval>FreeSpace("%DIRECTORY%"),BYTES_RESULT

VBEval>Round(%BYTES_RESULT%/1024,2),KB_RESULT
VBEval>Round(%BYTES_RESULT%/1024/1024,2),MB_RESULT
VBEval>Round(%BYTES_RESULT%/1024/1024/1024,2),GB_RESULT
VBEval>Round(%BYTES_RESULT%/1024/1024/1024/1024,10),TB_RESULT
VBEval>Round(%BYTES_RESULT%/1024/1024/1024/1024/1024,10),PB_RESULT
MDL>%BYTES_RESULT% Bytes%CRLF%%KB_RESULT% Kilobytes%crlf%%MB_RESULT% Megabytes%CRLF%%GB_RESULT% Gigabytes%CRLF%%TB_RESULT% Terabytes%CRLF%%PB_RESULT% Petabytes


alanimal
Junior Coder
Posts: 40
Joined: Thu Jun 09, 2005 11:57 pm

Post by alanimal » Tue Jun 10, 2008 8:55 pm

Cheers Guys,

All working a treat now - this was the final code: 8)

Code: Select all

VBSTART

Function FreeSpace(drvPath)
		 Dim fso, drv, s
		 Set fso = CreateObject("Scripting.FileSystemObject")
		 Set drv = fso.GetDrive(fso.GetDriveName(drvPath))
		 FreeSpace = drv.FreeSpace
End Function

VBEND

let>DIRECTORY=F:\
VBEval>FreeSpace("%DIRECTORY%"),fs
VBEval>Round(%fs%/1073741824,4),fs

//Check that the DiskSpace is more than 10GB to be safe

Let>GB=10

IF>{%fs%>%GB%}
MessageModal>Free Space On Drive %DIRECTORY%: %fs% GB OK
GoTo>END
Else
GoTo>Service

EndIF

Label>Service
//Restart the services
VBSTART

Sub StopService(sServiceName)
	strComputer = "."
  	Set wbemServices = GetObject("winmgmts:\\" & strComputer)
	sWQL = "Select state from Win32_Service " & "Where displayname='" & sServiceName & "'"
	Set oResults = wbemServices.ExecQuery(sWQL)
	For Each oService In oResults
  		If Trim(LCase(oService.State)) = LCase("Running") Then
    	   oService.StopService
  		End If
	Next
End Sub

Sub StopService(sServiceName)
	strComputer = "."
  	Set wbemServices = GetObject("winmgmts:\\" & strComputer)
	sWQL = "Select state from Win32_Service " & "Where displayname='" & sServiceName & "'"
	Set oResults = wbemServices.ExecQuery(sWQL)
	For Each oService In oResults
  		If Trim(LCase(oService.State)) = LCase("Running") Then
    	   oService.StopService
  		End If
	Next
End Sub

Sub StartService(sServiceName)
	strComputer = "."
  	Set wbemServices = GetObject("winmgmts:\\" & strComputer)
	sWQL = "Select state from Win32_Service " & "Where displayname='" & sServiceName & "'"
	Set oResults = wbemServices.ExecQuery(sWQL)
	For Each oService In oResults
  		If Trim(LCase(oService.State)) = LCase("Stopped") Then
    	   oService.StartService
  		End If
	Next
End Sub

Sub StartService(sServiceName)
	strComputer = "."
  	Set wbemServices = GetObject("winmgmts:\\" & strComputer)
	sWQL = "Select state from Win32_Service " & "Where displayname='" & sServiceName & "'"
	Set oResults = wbemServices.ExecQuery(sWQL)
	For Each oService In oResults
  		If Trim(LCase(oService.State)) = LCase("Stopped") Then
    	   oService.StartService
  		End If
	Next
End Sub

VBEND
VBRun>StopService,SQLSERVERAGENT
Wait>10
VBRun>StopService,MSSQLSERVER
Wait>15
VBRun>StartService,MSSQLSERVER
Wait>10
VBRun>StartService,SQLSERVERAGENT

Let>SENDMAIL_STATUS=1
Let>subject=SQL Server Log Drive Error
Let>[email protected]
Let>myname=AlanMM
Let>[email protected];
Let>body=The LOG drive on SQL Server has ran out of space. %CRLF% Restarting SQL services to clear log files now.%CRLF% There is %fs% GB free Space.
SMTPSendMail>recipients,mail.chsglobal.com,me,myname,subject,body,
MessageModal>There is insufficient Free Space on Drive is %DIRECTORY%: %fs% GB BAD!!%crlf%restarting services now.

Label>END


Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts