Showing posts with label root\wmi. Show all posts
Showing posts with label root\wmi. 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

Wednesday, September 12, 2007

Event Log

Event Log Manipulations:

1. Reading the Event Log
2. Clearing the Event Log
3. Creating backup of the Event Log

Code Snippet #1:

  1. strComputer = "."
  2. Set objWMIService = GetObject("winmgmts:" _
  3. & "{impersonationLevel=impersonate}!\\" _
  4. & strComputer & "\root\cimv2")
  5. Set colLoggedEvents = objWMIService.ExecQuery _
  6. ("Select * from Win32_NTLogEvent " _
  7. & "Where Logfile = 'System'")
  8. For Each objEvent in colLoggedEvents
  9. Wscript.Echo "Category: " &amp; objEvent.Category & VBNewLine _
  10. &amp;amp; "Computer Name: " & objEvent.ComputerName & VBNewLine _
  11. & "Event Code: " & objEvent.EventCode & VBNewLine _
  12. & "Message: " & objEvent.Message & VBNewLine _
  13. & "Record Number: " & objEvent.RecordNumber & VBNewLine _
  14. & "Source Name: " & objEvent.SourceName & VBNewLine _
  15. & "Time Written: " & objEvent.TimeWritten & VBNewLine _
  16. &amp;amp; "Event Type: " & objEvent.Type & VBNewLine _
  17. & "User: " & objEvent.User
  18. Next


Code Snippet #2:

  1. strComputer = "."
  2. Set objWMIService = GetObject("winmgmts:" _
  3. & "{impersonationLevel=impersonate,(Backup)}!\\" & _
  4. strComputer & "\root\cimv2")
  5. Set colLogFiles = objWMIService.ExecQuery _
  6. ("Select * from Win32_NTEventLogFile " _
  7. & "Where LogFileName='Application'")
  8. For Each objLogfile in colLogFiles
  9. objLogFile.ClearEventLog()
  10. WScript.Echo "Cleared application event log file"
  11. Next

Code Snippet #3:


  1. strComputer = "."
  2. Set objWMIService = GetObject("winmgmts:" _
  3. & "{impersonationLevel=impersonate,(Backup)}!\\" & _
  4. strComputer & "\root\cimv2")
  5. Set colLogFiles = objWMIService.ExecQuery _
  6. ("Select * from Win32_NTEventLogFile " _
  7. & "Where LogFileName='Application'")
  8. For Each objLogfile in colLogFiles
  9. errBackupLog = objLogFile.BackupEventLog( _
  10. "c:\scripts\application.evt")
  11. WScript.Echo "File saved as c:\scripts\applications.evt"
  12. Next
If at first you fail, call it version 1.0

Tuesday, June 26, 2007

WMI Ping (Win32_PingStatus)

The code below is an example in how to use Win32_PingStatus class in WMI to check a remote machine's status on the network.

Code Snippet:

  1. Function PingHost(sTarget)
  2. Set cPingResults = GetObject("winmgmts:{impersonationLevel=impersonate}//" & _
  3. sHost & "/root/cimv2"). ExecQuery("SELECT * FROM Win32_PingStatus " & _
  4. "WHERE Address = '" + sTarget + "'")
  5. For Each oPingResult In cPingResults
  6. If oPingResult.StatusCode = 0 Then
  7. If LCase(sTarget) = oPingResult.ProtocolAddress Then
  8. WScript.Echo sTarget & " is responding"
  9. Else
  10. WScript.Echo sTarget & "(" & oPingResult.ProtocolAddress &amp; ") is responding"
  11. End If
  12. Wscript.Echo "Bytes = " & vbTab & oPingResult.BufferSize & _
  13. vbTab & "Time (ms) = " & vbTab & oPingResult.ResponseTime & _
  14. vbTab & "TTL (s) = " & vbTab & oPingResult.ResponseTimeToLive & _
  15. vbTab & "Hostname = " & vbTab & oPingResult.ProtocolAddressResolved
  16. Else
  17. WScript.Echo sTarget & " is not responding"
  18. WScript.Echo "Status code is " & oPingResult.StatusCode
  19. WScript.Echo "*********************************"
  20. End If
  21. Next
  22. End Function

If at first you fail, call it version 1.0

Software Inventory in a jiffy (Powershell)

WMIC and WSH are good enough to perform software inventory collection... but the coding's just a little bit longer...

Well to query wmi is not a big task, it's formatting the output that will get your code extra few lines of unnecessary vbtab's and vbcrlf's.

Install Windows Power Shell, then it will take only three (3) lines of codes to get it running and the output is as lovely as a well defined tabular echo's on your console.

Code Snippet:

  1. #Software Inventory
  2. $computer = "."
  3. $prod = Get-WmiObject -class "win32_product" -computer $computer
  4. $prod | sort name | ft Name, Version, Vendor, Installdate -a
Just save it as *.ps1 then execute it with powershell...

If at first you fail, call it version 1.0

Thursday, May 17, 2007

CPU Temp Probes

I have searched the web for a script that performs temperature probes but had no luck in getting the real picture until wbemtest helped me find classes not included in M$oft's Scriptomatic.

Follow the instructions below to test if the script will work on your machine for CPU temperature probe:

1. Launch wbemtest


2. Click connect


3. Change the namespace to "root\WMI" then click Connect


4. Click Query, supply the query "Select * from MSAcpi_ThermalZoneTemperature" , then click Apply


5. Double click on the result to view the class properties:


If you are able to view the results then it's ok to proceed with the scriplet.

Below is the script that probes for CPU temperature via MSAcpi_ThermalZoneTemperature. The initial results are in 10ths of degrees in Kelvin so I had to convert the results to Celcius and Farenheit.

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' CPU Temperature Probe
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\WMI")
Set colItems = objWMIService.ExecQuery("Select * from MSAcpi_ThermalZoneTemperature",,48)
For Each objItem in colItems

With objItem
WScript.Echo "Active: " & .Active & vbcrlf

FarTemp = ((9/5)*((.CurrentTemperature - 2732)/10)) + 32
CelTemp = ((FarTemp - 32) * (5/9))
WScript.Echo "Current Temperature: " & vbcrlf & _
FarTemp & " (Degrees Farenheit)" &amp;amp;amp;amp;amp; _
" | " & CelTemp & " (Degrees Celcius)" & vbcrlf

FarTemp=((9/5)*((.CriticalTripPoint - 2732)/10)) + 32
CelTemp = ((FarTemp - 32) * (5/9))
WScript.Echo "Critical Temperature (Temperature at which the OS must shutdown the system): " &amp;amp;amp;amp; vbcrlf & _
FarTemp & " Degrees Farenheit" & _
" | " & CelTemp & " Degrees Celcius" & vbcrlf

If .PassiveTripPoint <> 0 Then
FarTemp=((9/5)*((.PassiveTripPoint - 2732)/10)) + 32
CelTemp = ((FarTemp - 32) * (5/9))
WScript.Echo "Passive Temperature (Temperature at which the OS must activate CPU throttling): " &amp;amp;amp;amp; vbcrlf & _
FarTemp & " Degrees Farenheit" & _
vbcrlf & CelTemp & " Degrees Celcius" & vbcrlf
End If

WScript.Echo "Thermal Stamp: " & .ThermalStamp & vbcrlf
End With
Next
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

CPU Max Temperatures

AMD Althon, Althon Opteron, Duron & Sempron Series:

  • AMD Athlon (socket) upto 1Ghz 90°C
  • AMD Athlon (slot) all speeds 70°C
  • AMD Athlon Thunderbird 1.1Ghz+ 95°C
  • AMD Athlon MP 1.33Ghz+ 95°C
  • AMD Athlon XP 1.33Ghz+ 90°C
  • AMD Athlon XP T-Bred upto 2100+ 90°C
  • AMD Athlon XP T-Bred over 2100+ 85°C
  • AMD Athlon XP Barton 85°C
  • AMD Duron up to 1Ghz 90°C
  • AMD Duron 1Ghz+ 90°C
  • AMD Duron Applebred 85°C
  • AMD Opteron 65 - 71°C
  • AMD Athlon 64 70°C
  • AMD Athlon 64 (Socket 939, 1.4 volts) 65°C
  • AMD Athlon 64 FX 70°C
  • AMD Athlon 64 X2 71°C
  • AMD Sempron (T-bred/Barton core) 90°C
  • AMD Sempron (Paris core) 70°C
  • AMD Mobile Sempron 95°C

AMD K6 Series
  • AMD K6/K6-2/K6-III (All except below) 70°C
  • AMD K6-2/K6-III (model number ending in X) 65°C
  • AMD K6-2+/K6-III+ 85°C

Intel Pentium III Series
  • Pentium III Slot 1 500-866Mhz 80°C
  • Pentium III Slot and socket 933Mhz 75°C
  • Pentium III Slot 1 1Ghz 60 - 70°C
  • Pentium III Slot 1 1.13Ghz 62°C

Intel Celeron Series
  • Intel Celeron 266-433Mhz 85°C
  • Intel Celeron 466-533Mhz 70°C
  • Intel Celeron 566-600Mhz (Coppermine) 90°C
  • Intel Celeron 633-667Mhz 82°C
  • Intel Celeron 700 - 850Mhz 80°C
  • Intel Celeron 900Mhz - 1.6Ghz 69 - 70°C
  • Intel Celeron 1.7Ghz and Higher 67 - 77°C

Intel Pentium II
  • Intel Pentium II (First Generation "Klamath") 72 - 75°C
  • Intel Pentium II (Second Generation, 266-333Mhz) 65°C
  • Intel Pentium II (350 - 400Mhz) 75°C
  • Intel Pentium II (450Mhz) 70°C

Intel Pentium 4, Pentium M (notebooks)
  • Intel Pentium 4 64 - 78°C
There are no specific stats for Pentium 4 CPU’s as P4’s have an ability to slow themselves down when they are getting too hot and thus, in theory they should never be able to burn themselves out. To get specifics consult Intel’s specifications for your particular model.

  • Intel Pentium M (notebooks) 100°C

Intel Pentium D (dual core)
  • Intel Pentium D 820 (2.8Ghz) 63°C
  • Intel Pentium D 830 & 840 (3.0 - 3.2Ghz) 69.8°C

Intel Pentium Pro
  • Intel Pentium Pro. 256 or 512K L2 Cache 85°C
  • Intel Pentium Pro. 1MB L2 Cache 80°C