Get Disk space usage from central server with WMI

Get Disk space usage from central server with WMI.

As a DBA job we have to check server health regularly. sometime that happens when you are working on multiple servers and its very difficult to monitor health of all servers. I faced the same issue last week. My application abnormally going down and i am unable to connect database, while i had tried to connect that particular server i found there is only 20KB free space in data drive.

It happens sometime that you don’t know how fast drive is filling and adversity occurs due to low disk space.
I have created one wmi application that observes disk space and mail us on your defined email id’s. This application will handles all server’s Disk usage from central server. For that you need to create one common user with WMI permission.
Please follow the below steps to manage disk space usage from all servers.

Step1 : All of your server must be enable for remote WMI connection.

Click on Start –> Run –> WMIMGMT.MSC

WMI1

Just right click on WMI Control ans select property.

On “WMI Control Property” go to “Security” tab and select “Root” click on Security button which will pop one more window for “Security for root”.

On “Security for Root” window mark on Remote enable for your “WMIUser” or “Everyone”.

WMI2

Step2 : Open Notepad and copy below WMI Code to notepad and save with “DiskUsageWithWMi.csv”.

Sub SendMail(Sender, Recipient, Subject, Message)
   Set objMessage = CreateObject("CDO.Message")
   objMessage.Subject = Subject
   objMessage.From = Sender
   objMessage.To = Recipient
   objMessage.htmlbody = Message
   objMessage.Send
End Sub

Dim strComputer
Dim objWMIService, objItem, colItems
Dim strDriveType,txt
Dim arrylist(4)
Dim pctFreeSpace,strFreeSpace,strusedSpace,strDiskSize

arrylist(0) = ("Server1")
arrylist(0) = ("Server2")
arrylist(0) = ("Server3")
arrylist(0) = ("Server4")
arrylist(0) = ("Server5")

txt = "<table cellspacing='1' cellpadding='0' border='0' bordercolor='black' id='shell'>"
txt = txt & "<tr bgcolor='#DDDDDD' height=30>"
txt = txt & "<th width='50' style='border-right: black thin solid; border-top: black thin solid; border-left: black thin solid; border-bottom: black thin solid'>Drive</th>"
txt = txt & "<th width='75' style='border-right: black thin solid; border-top: black thin solid; border-left: black thin solid; border-bottom: black thin solid'>Size(GB)</th>"
txt = txt & "<th width='75' style='border-right: black thin solid; border-top: black thin solid; border-left: black thin solid; border-bottom: black thin solid'>Used(GB)</th>"
txt = txt & "<th width='75' style='border-right: black thin solid; border-top: black thin solid; border-left: black thin solid; border-bottom: black thin solid'>Free(GB)</th>"
txt = txt & "<th width='75' style='border-right: black thin solid; border-top: black thin solid; border-left: black thin solid; border-bottom: black thin solid'>Free(%)</th>"
txt = txt & "</tr>"

For Each strComputer in arrylist
'On Error Resume Next

   Set objWMIService = Nothing
   Set colItems = Nothing

   Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
   Set colItems = objWMIService.ExecQuery("Select * from Win32_LogicalDisk WHERE DriveType=3 or DriveType=4")

   txt = txt & "<tr bgcolor='#DDDDDD'Bordercolor='#000000'><td colspan='5' style='border-right: black thin solid; border-top: black thin solid; border-left: black thin solid; border-bottom: black thin solid'><font size='2'>"& strComputer & "</font></td></tr>"

   For Each objItem in colItems

      pctFreeSpace = FormatNumber(((objItem.FreeSpace / objItem.Size) * 1000)/10,2)
      strDiskSize = FormatNumber(objItem.Size /1073741824,3)
      strFreeSpace = FormatNumber(objItem.FreeSpace /1073741824,3)
      strUsedSpace = objItem.Size-objItem.FreeSpace
      strUsedSpace = FormatNumber(strUsedSpace/1073741824,3)
      IF pctFreeSpace <= 10 Then
         txt = txt & "<tr bgcolor='#9C3333' Bordercolor='#000000'>"
      Else
         txt = txt & "<tr Bordercolor='#000000'>"
      End If
      txt = txt & "<td style='border-right: black thin solid; border-top: black thin solid; border-left: black thin solid; border-bottom: black thin solid'><font size='2'>" & objItem.Name & "</Font></td>"
      txt = txt & "<td align = 'Right' style='border-right: black thin solid; border-top: black thin solid; border-left: black thin solid; border-bottom: black thin solid'><font size='2'>"& strDiskSize &"</Font></td>"
      txt = txt & "<td align = 'Right' style='border-right: black thin solid; border-top: black thin solid; border-left: black thin solid; border-bottom: black thin solid'><font size='2'>"& strUsedSpace &"</Font></td>"
      txt = txt & "<td align = 'Right' style='border-right: black thin solid; border-top: black thin solid; border-left: black thin solid; border-bottom: black thin solid'><font size='2'>"& strFreeSpace &"</Font></td>"
      txt = txt & "<td align = 'Right' style='border-right: black thin solid; border-top: black thin solid; border-left: black thin solid; border-bottom: black thin solid'><font size='2'>"& pctFreeSpace &"</Font></td>"
      txt = txt & "</tr>"
      pctFreeSpace = 0
      strDiskSize = 0
      strFreeSpace = 0
      strUsedSpace = 0
      strUsedSpace = 0
      Set objWMIService = Nothing
      Set colItems = Nothing
   Next
Next
txt = txt & "</table>"
SendMail "emailid@sender.com","emailid.reciever.com", strComputer & "Disk Space Information was executed at " & Now(), txt

Step3 : You can create a schedule task which calls this VBS file daily on your required duration like every hour or every 3 hour or daily one time etc.

This code will send you a message like this.

Drive Size(GB) Used(GB) Free(GB) Free(%)
Server1
C: 465.150 213.078 252.073 54.19
D: 465.247 340.871 124.376 26.73
E: 465.247 425.474 39.773 8.55
F: 465.247 328.124 137.123 29.47
L: 169.853 125.357 44.496 26.20
P: 749.997 664.429 85.568 11.41
T: 169.991 72.388 97.603 57.42
Server2
C: 20.407 15.690 4.717 23.12
E: 54.052 35.063 18.988 35.13
F: 465.759 279.674 186.085 39.95
Server3
C: 22.399 20.217 2.182 9.74
D: 67.750 37.477 30.273 44.68
E: 45.281 37.560 7.721 17.05
G: 67.748 53.881 13.868 20.47
H: 272.243 244.077 28.166 10.35
W: 5,527.029 4,654.987 872.042 15.78
Server4
C: 20.216 18.012 2.204 10.90
E: 115.243 58.476 56.767 49.26

Some of them showing in red color which means that drive has lower than 10 % disk space.

Regards,

Nirav Gajjar