Latest Project: Server Monitoring Using WMI
It was a while since my last post. Year ender is quite busy and the new year was as much.
I've been working on a latest project for our team. I set up a monitoring server running WMI scripts against remote servers in intervals of 2 or 30mins (depending on the frequency of data required).
Below are few simple snippets I used in to collect remote data and pump it in an html template and send it via email to the team (if ever performance thresholds were exceeded).
Code Snippets:
- Sub DisplayErrorInfo
- WScript.Echo "Error: : " & Err
- WScript.Echo "Error (hex) : &H" & Hex(Err)
- WScript.Echo "Source : " & Err.Source
- WScript.Echo "Description : " & Err.Description
- Err.Clear
- End Sub
#1 Bytes Converter Snippet (One of my favorite snippet, pretty handy!)
- Function SetBytes(Bytes,fKB)
- If fKB=True then Bytes = Bytes * 1024
- If Bytes >= 1073741824 Then
- SetBytes = FormatNumber((Bytes / 1024 / 1024 / 1024),2,,-1,-1) & " GB"
- ElseIf Bytes >= 1048576 Then
- SetBytes = FormatNumber((Bytes / 1024 / 1024),2,,-1,-1) & " MB"
- ElseIf Bytes >= 1024 Then
- SetBytes = FormatNumber((Bytes / 1024),2,,-1,-1) & " KB"
- ElseIf Bytes <>
- SetBytes = Bytes & " Bytes"
- End If
- End Function
#2 Get Available Memory on the server
- strComputer = "."
- Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
- Set colItems = objWMIService.ExecQuery _
- ("Select * From Win32_PerfRawData_PerfOS_Memory")
- For Each objItem in colItems
- intValue = objItem.AvailableBytes
- Wscript.Echo "Available memory = " & SetBytes(intValue,false)
- Exit For
- Next
Note: This requires cooking. If you you have no idea how that works then just use AvailableMBytes instead.
#3 Get CPU Usage (%)
- Function GetCPUProcUsg(svr)
- c = 0
- strComputer = "."
- Set objWMIService = GetObject("winmgmts:\\" _
- & strComputer & "\root\cimv2:Win32_PerfRawData_PerfOS_Processor.Name='_Total'")
- If Err = 0 Then
- While (True)
- N1 = objWMIService.PercentProcessorTime
- D1 = objWMIService.TimeStamp_Sys100NS
- Wscript.Sleep(1000)
- Set objWMIService2 = GetObject("winmgmts:\\" _
- & strComputer & "\root\cimv2:Win32_PerfRawData_PerfOS_Processor.Name='_Total'")
- N2 = objWMIService2.PercentProcessorTime
- D2 = objWMIService2.TimeStamp_Sys100NS
- PercentProcessorTime = (1 - ((N2 - N1)/(D2-D1)))*100
- Wscript.Echo "Processor Usage: " & Round(PercentProcessorTime,2) & "%"
- GetCPUProcUsg = Round(PercentProcessorTime,2) & "%"
- Wend
- Else
- DisplayErrorInfo
- End If
- Set objWMIService = nothing
- Set objWMIService2 = nothing
- End Function
#4 Get Available Disk Space
- strComputer = "."
- strUser =
- strPass =
- Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")
- Set oWMI = objSWbemLocator.ConnectServer(strComputer, "root\cimv2", strUser, strPass)
- Set colDisks = oWMI.InstancesOf("win32_PerfRawData_PerfDisk_LogicalDisk.Name")
- For Each objDisk in colDisks
- intBaseValue = objDisk.PercentFreeSpace_Base
- dblActualFreeSpace = (100 * objDisk.PercentFreeSpace) / intBaseValue
- Wscript.Echo objDisk.Name & Int(dblActualFreeSpace)
- Next
If at first you fail, call it version 1.0