| [ Team LiB ] |
|
26.6 Querying the Event LogsThe Event Logs are typically a system administrator's first line of inquiry when trying to troubleshoot problems. Since they are so important, it is also important to see how we can make use of them with WMI. The two major components that we need to be concerned with are the Event Logs themselves and the events contained within each Event Log. We will first focus on properties of Event Logs. The Win32_NTEventLogFile class represents an Event Log. Table 26-4 contains several Win32_NTEventLogFile properties that can be used to query or modify properties of a Event Log.
Let's look at an example that displays all of the properties listed in Table 26-4 for each Event Log and sets the MaxFileSize and OverwriteOutDated properties if they have not already been set to the correct values. Since we want to iterate over all Event Logs, we will pass Win32_NTEventLogFile to the InstancesOf method. Example 26-2 shows how to accomplish this. Example 26-2. Displaying properties of the Event Log using Win32_NTEventLogFilestrComputer = "."
intMaxFileSize = 10 * 1024 * 1024 ' << 10MB
intOverwriteOutDated = 180 ' << 6 months
Set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set objELF = objWMI.InstancesOf("Win32_NTEventLogFile")
' Iterate over each Event Log
for each objEL in objELF
WScript.Echo objEL.LogFileName & " Log:"
WScript.Echo " FileSize: " & objEL.FileSize
' If the size has not been set yet, set it
if objEL.MaxFileSize <> intMaxFileSize then
WScript.Echo " ** Setting MaxFileSize: " & intMaxFileSize & " (new) " & _
objEL.MaxFileSize & " (current)"
objEL.MaxFileSize = intMaxFileSize
objEL.Put_
else
WScript.Echo " MaxFileSize: " & objEL.MaxFileSize
end if
WScript.Echo " Name: " & objEL.Name
WScript.Echo " NumberOfRecords: " & objEL.NumberOfRecords
' If the overwrite date has not been set, set it
WScript.Echo " OverwritePolicy: " & objEL.OverwritePolicy
if objEL.OverwriteOutDated <> intOverwriteOutDated then
WScript.Echo " ** Setting OverwriteOutDated: " & _
intOverwriteOutDated & " (new) " & _
objEL.OverwriteOutDated & " (current)"
objEL.OverwriteOutDated = intOverwriteOutdated
objEL.Put_
else
WScript.Echo " OverwriteOutDated: " & objEL.OverwriteOutDated
end if
WScript.Echo ""
next
Note that for the MaxFileSize and OverwriteOutDated properties, we set them only if they haven't been set already. To set properties, simply set the property method equal to the new value. To commit the change, you must use the Put_ method. Using Put_ is very similar to SetInfo in ADSI. WMI implements a caching mechanism very similar to the Property Cache described in Chapter 19. If we did not call Put_, the new values would never have been written back to the system. The Event Logs contain a wealth of information about the health and status of the system and hosted applications. With WMI, system administrators can write simple to complex queries to find specific events in any of the Event Logs. The Win32_NTLogEvent class represents individual event entries in an Event Log. Table 26-5 contains several useful properties that are available for Win32_NTLogEvent objects.
In the next example, we will retrieve all events that match certain criteria. Let's say that we want to find all Information events in the System Event Log that have an event code of 5778 and were generated after November 1, 2002. The WQL for this query works out to be: Select * from Win32_NTLogEvent
Where Type = 'Information'
And Logfile = 'System'
and EventCode = 5778
and TimeGenerated > '2002/11/01'
Once we have the WQL query, the rest of the code is very similar to many of the previous examples. strComputer = "."
Set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set objEvents = objWMI.ExecQuery _
("Select * from Win32_NTLogEvent Where Logfile = 'System' " & _
"and EventCode = 5778 and Type = 'Information' " & _
"and TimeGenerated > '2002/11/01' ")
WScript.Echo "Total events that match criteria: " & objEvents.Count
for each objEvent in objEvents
WScript.Echo " CategoryString: " & objEvent.CategoryString
WScript.Echo " EventType: " & objEvent.EventType
WScript.Echo " LogFile: " & objEvent.LogFile
WScript.Echo " Message: " & objEvent.Message
WScript.Echo " RecordNumber: " & objEvent.RecordNumber
WScript.Echo " SourceName: " & objEvent.SourceName
WScript.Echo " TimeGenerated: " & objEvent.TimeGenerated
WScript.Echo " Type: " & objEvent.Type
WScript.Echo " User: " & objEvent.User
WScript.Echo ""
next
|
| [ Team LiB ] |
|