' wmidiskwarn.vbs ' Greg Poma ' Last Update: 5/1/2007 '''' YOU MUST CONFIURE SETTINGS IN THIS AREA computers = array("", “SERVER1”, “SERVER2”) ' 90% warning threshold, 5% variance WarningThreshold = 90 ' 95% full disk means shows in red (changes email to indicate critical) CriticalThreshold = 95 ' 5% total daily variance, otherwise warning VarianceThreshold = 5 ''''''' SQL Server variables ' Pur your server name here strSqlServer = “sql1” ' SQL user name strSqlUser = “user” ' SQL password strSqlPassword = “Password” ' Database name strSqlDatabase = “DB” ' The from: line of the email -- please note the formatting of the quotes when changing emailMsgFrom = """Test User"" " ' The to: line of the email emailMsgTo = """Network Operations Group"" " '''' CONFIGURATION COMPLETE --- No need to edit below ' 128 if on, 0 if not.. this sets a 2 minute timeout on XP/2003 systems wbemConnectFlagUseMaxWait = 128 Set oConn=CreateObject("Adodb.connection") Set oRS = CreateObject("ADODB.Recordset") oconn.provider = "sqloledb" strSQLConn = "Driver={SQL Server};SERVER=“ & strSqlServer & _ “;UID= “ & strSqlUser & “;PWD=“ & strSqlPassword & _ “;Database=" & strSqlDB oconn.open strSQLConn ' ************************************************************************** on error resume next WarningCount = 0 WarningType = "WARNING" Set objMessage = CreateObject("CDO.Message") formatdate = Year(Date) if Month(Date) > 9 Then formatdate = formatdate & Month(Date) else formatdate = formatdate & "0" & Month(Date) end if if Day(Date) > 9 Then formatdate = formatdate & Day(Date) else formatdate = formatdate & "0" & Day(Date) end if ' Header MessageBody = "" & "" & vbCrlf Set WMI_Error = CreateObject("WbemScripting.SwbemLastError") For Each strComputer in computers ' Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") Set oLocator = CreateObject("WbemScripting.SWbemLocator") Set objWMIService = oLocator.ConnectServer(strComputer, "root\cimv2", , , , , wbemConnectFlagUseMaxWait) If Err <> 0 Then if strComputer <> "" Then ErrorNotes = ErrorNotes & "ERROR: Unable to connect to the WMI namespace for " & strComputer & "." & vbCrlf DisplayWMIError("Unable to connect to " & strComputer) Err.Clear Else Err.Clear End If Else Set colDisks = objWMIService.ExecQuery _ ("Select * from Win32_LogicalDisk where FileSystem='NTFS' and DriveType<>'4'") If Err <> 0 Then ErrorNotes = ErrorNotes & "ERROR: Reading Win32_LogicalDisk on " & strComputer & "." & vbCrlf DisplayWMIError("Win32_LogicalDisk on " & strComputer) Err.Clear Else For Each objDisk in colDisks diskSize = Round(objDisk.Size / 1000000000,2) diskUsed = Round((objDisk.Size - objDisk.FreeSpace) / 1000000000,2) diskFree = Round(objDisk.FreeSpace / 1000000000,2) diskFreePct = Round(objDisk.FreeSpace / objDisk.Size * 100,2) diskUsedPct = Round(100 - diskFreePct,2) txtSql = "SELECT top 1 used FROM NTWServerDriveUsage where server='" & strComputer & "' AND Drive='" & Left(objDisk.Name,1) & "' ORDER BY datetext DESC" set oRS = oConn.execute(txtSql) oRS.MoveFirst If not oRS.EOF then lastused = oRS.Fields("used") else lastused = "0" end if variance = Round((lastused - diskUsed) / diskSize * 100,2) If diskUsedPct > WarningThreshold or variance > variancethreshold Then MessageBody = MessageBody & "" If diskUsedPct > CriticalThreshold Then MessageBody = MessageBody & "" ElseIf diskUsedPct > WarningThreshold Then MessageBody = MessageBody & "" Else MessageBody = MessageBody & "" End If If variance > Variancethreshold Then MessageBody = MessageBody & "" & vbCrlf Else MessageBody = MessageBody & "" & vbCrlf End If WarningCount = WarningCount + 1 If diskUsedPct > CriticalThreshold Then WarningType = "CRITICAL" End If End If Next End If End If Next If WarningCount <> 0 Then objMessage.Subject = "Disk Space " & WarningType & " (" & WarningCount & ")" objMessage.From = emailMsgFrom objMessage.HTMLBody = "" & vbCrlf & _ MessageBody & "
" & _ "Server" & _ "Drive" & _ "Format" & _ "Total" & _ "Used" & _ "Free" & _ "% Used" & _ "% Free" & _ "Variance
" & UCase(strComputer) & "" & _ Left(objDisk.Name,1) & "" & _ objDisk.FileSystem & "" & _ diskSize & "" & _ diskUsed & "GB" & _ diskFree & "GB" & _ diskUsedPct & "“ & _ “" & diskFreePct & "“ & _ “" & diskFreePct & "" & diskFreePct & "“ & _ “" & variance & _ "%
" & variance & "
" objMessage.To = emailMsgTo objMessage.Send End If '*** END Main body Sub DisplayWMIError(strMessage) strError = VbCrLf & strMessage & _ VbCrLf & "Number (dec) : " & Err.Number & _ VbCrLf & "Number (hex) : &H" & Hex(Err.Number) & _ VbCrLf & "Description : " & Err.Description & _ VbCrLf & "Source : " & Err.Source 'Instantiate SWbemLastError object. Set WMI_Error = CreateObject("WbemScripting.SWbemLastError") strError = strError & VbCrLf & "Operation : " & WMI_Error.Operation & _ VbCrLf & "ParameterInfo: " & WMI_Error.ParameterInfo & _ VbCrLf & "ProviderName : " & WMI_Error.ProviderName WScript.Echo strError Err.Clear End Sub