Showing posts with label win32_performattedData_perfOS_System. Show all posts
Showing posts with label win32_performattedData_perfOS_System. Show all posts

Wednesday, January 30, 2008

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:

  1. Sub DisplayErrorInfo
  2. WScript.Echo "Error: : " & Err
  3. WScript.Echo "Error (hex) : &H" & Hex(Err)
  4. WScript.Echo "Source : " & Err.Source
  5. WScript.Echo "Description : " & Err.Description
  6. Err.Clear
  7. End Sub

#1 Bytes Converter Snippet (
One of my favorite snippet, pretty handy!)

  1. Function SetBytes(Bytes,fKB)
  2. If fKB=True then Bytes = Bytes * 1024
  3. If Bytes >= 1073741824 Then
  4. SetBytes = FormatNumber((Bytes / 1024 / 1024 / 1024),2,,-1,-1) & " GB"
  5. ElseIf Bytes >= 1048576 Then
  6. SetBytes = FormatNumber((Bytes / 1024 / 1024),2,,-1,-1) & " MB"
  7. ElseIf Bytes >= 1024 Then
  8. SetBytes = FormatNumber((Bytes / 1024),2,,-1,-1) & " KB"
  9. ElseIf Bytes <>
  10. SetBytes = Bytes & " Bytes"
  11. End If
  12. End Function

#2 Get Available Memory on the server

  1. strComputer = "."
  2. Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
  3. Set colItems = objWMIService.ExecQuery _
  4. ("Select * From Win32_PerfRawData_PerfOS_Memory")
  5. For Each objItem in colItems
  6. intValue = objItem.AvailableBytes
  7. Wscript.Echo "Available memory = " & SetBytes(intValue,false)
  8. Exit For
  9. Next

Note: This requires cooking. If you you have no idea how that works then just use AvailableMBytes instead.

#3 Get CPU Usage (%)

  1. Function GetCPUProcUsg(svr)
  2. c = 0
  3. strComputer = "."
  4. Set objWMIService = GetObject("winmgmts:\\" _
  5. & strComputer & "\root\cimv2:Win32_PerfRawData_PerfOS_Processor.Name='_Total'")
  6. If Err = 0 Then
  7. While (True)
  8. N1 = objWMIService.PercentProcessorTime
  9. D1 = objWMIService.TimeStamp_Sys100NS
  10. Wscript.Sleep(1000)
  11. Set objWMIService2 = GetObject("winmgmts:\\" _
  12. & strComputer & "\root\cimv2:Win32_PerfRawData_PerfOS_Processor.Name='_Total'")
  13. N2 = objWMIService2.PercentProcessorTime
  14. D2 = objWMIService2.TimeStamp_Sys100NS
  15. PercentProcessorTime = (1 - ((N2 - N1)/(D2-D1)))*100
  16. Wscript.Echo "Processor Usage: " & Round(PercentProcessorTime,2) & "%"
  17. GetCPUProcUsg = Round(PercentProcessorTime,2) & "%"
  18. Wend
  19. Else
  20. DisplayErrorInfo
  21. End If
  22. Set objWMIService = nothing
  23. Set objWMIService2 = nothing
  24. End Function

#4 Get Available Disk Space
  1. strComputer = "."
  2. strUser =
  3. strPass =
  4. Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")
  5. Set oWMI = objSWbemLocator.ConnectServer(strComputer, "root\cimv2", strUser, strPass)
  6. Set colDisks = oWMI.InstancesOf("win32_PerfRawData_PerfDisk_LogicalDisk.Name")
  7. For Each objDisk in colDisks
  8. intBaseValue = objDisk.PercentFreeSpace_Base
  9. dblActualFreeSpace = (100 * objDisk.PercentFreeSpace) / intBaseValue
  10. Wscript.Echo objDisk.Name & Int(dblActualFreeSpace)
  11. Next


If at first you fail, call it version 1.0

Tuesday, June 26, 2007

OS Performance monitoring (PowerShell)




Another useful powershell code snippet, monitoring the OS performance...


Code Snippet:

  1. $computer = "."
  2. $perf = get-wmiobject -class "Win32_PerfFormattedData_PerfOS_System" -computer $computer
  3. $uptime=$perf.SystemUpTime/3600
  4. write-host "Performance Stats: "
  5. write-host "Processes", $perf.Processes
  6. write-host "Threads", $perf.Threads
  7. write-host "System Up Time `(hours`)", $uptime.tostring("00.0")
  8. write-host "Alignment Fixups/sec", $perf.AlignmentFixupsPersec.tostring("###,##0.0")
  9. write-host "Context Switches/sec", $perf.ContextSwitchesPersec.tostring("###,##0.0")
  10. write-host "Exception Dispatches/sec", $perf.ExceptionDispatchesPersec.tostring("###,##0.0")
  11. write-host "File Control Bytes/sec", $perf.FileControlBytesPersec.tostring("###,##0.0")
  12. write-host "File Control Operations/sec", $perf.FileControlOperationsPersec.tostring("###,##0.0")
  13. write-host "File Data OperationsPersec", $perf.FileDataOperationsPersec.tostring("###,##0.0")
  14. write-host "File Read Bytes/sec", $perf.FileReadBytesPersec.tostring("###,##0.0")
  15. write-host "File Read Operations/sec", $perf.FileReadOperationsPersec.tostring("###,##0.0")
  16. write-host "File Write Bytes/sec", $perf.FileWriteBytesPersec.tostring("###,##0.0")
  17. write-host "File Write Operations/sec", $perf.FileWriteOperationsPersec.tostring("###,##0.0")
  18. write-host "Floating Emulations/rsec", $perf.FloatingEmulationsPersec.tostring("###,##0.0")
  19. write-host "Percent Registry Quota Used", $($perf.PercentRegistryQuotaInUse/100).tostring("P")
  20. write-host "Processor Queue Length", $perf.ProcessorQueueLength
  21. write-host "System Calls Persec", $perf.SystemCallsPersec
  22. write-host ""


Sample Output:
Performance Stats:
Processes 66
Threads 573
System Up Time (hours) 24.8
Alignment Fixups/sec 0.0
Context Switches/sec 9,857.0
Exception Dispatches/sec 0.0
File Control Bytes/sec 0.0
File Control Operations/sec 0.0
File Data OperationsPersec 1,232.0
File Read Bytes/sec 3,943,044.0
File Read Operations/sec 1,232.0
File Write Bytes/sec 0.0
File Write Operations/sec 0.0
Floating Emulations/rsec 0.0
Percent Registry Quota Used 9.00 %
Processor Queue Length 0
System Calls Persec 51752



If at first you fail, call it version 1.0