Export (0) Print
Expand All

Monitoring Service Availability

Microsoft® Windows® 2000 Scripting Guide

When you monitor a service for availability, you verify only that the service is running. If you need to know whether the service is running at peak efficiency, you need to use a more in-depth type of monitoring (such as performance monitoring). Although relatively simple, availability monitoring is extremely important; other questions, such as whether the service is performing at the expected level, are meaningless if the service is not even running.

Availability monitoring generally involves a probe that returns the status of a service. By saving the results of each probe to a database, you can calculate the availability of a service. For example, if you issue 100 probes and the service responds 99 times, the service has an availability of 99 percent.

Availability is often expressed as the amount of time during a year that the service was not available. For example, a service with an availability of 99 percent means the service was unavailable for a total of 3.7 days. This could be the result of one outage of 3.7 days or several outages that, combined, add up to 3.7 days.

To increase the availability of a service, you can do one of two things:

  • Increase the mean time between failures.

    Unfortunately, a service failure is often the result of a bug, either in the service or in the operating system. Unless you wrote the code for both the service and the operating system, it might be difficult for you to increase the mean time between failures.

  • Decrease the time it takes to restart the service.

    If you manually restart the service each time it fails, the service does not return to full functionality unless you are available to restart it. To increase availability, you can write a script that monitors the service state periodically and restarts the service automatically each time it fails.

Scripting Steps

Reporting on the availability of services can be done by:

  • Reporting the status of all services.

  • Reporting on services that are in a specific state (for example, services that are running or services that are not running).

Reporting the state of all services

Listing 15.1 contains a script that monitors service availability; it does this by querying the list of services installed on a computer and reporting the current state of each service. To carry out this task, the script must perform the following steps:

  1. Create a variable to specify the target computer name.

  2. Use a GetObject call to connect to the WMI namespace root\cimv2 on the target computer, and set the impersonation level to "impersonate."

  3. Use the ExecQuery method to query the Win32_Service class. This returns a collection consisting of all the services installed on the computer.

  4. For each service in the collection, echo the service display name and the current state of each service.

Listing 15.1 Monitoring Service Availability

  
1
2
3
4
5
6
7


strComputer = "."
Set objWMIService = GetObject("winmgmts:" & _
 "{impersonationLevel=Impersonate}!\\" & strComputer & "\root\cimv2")
Set colServices = objWMIService.ExecQuery("SELECT * FROM Win32_Service")
For Each objService in colServices
 Wscript.Echo objService.DisplayName & " = " & objService.State
Next

Reporting on services that are not running

Listing 15.2 contains a script that monitors service availability; it does this by querying the list of services installed on a computer and reporting on all services that are not running. To carry out this task, the script must perform the following steps:

  1. Create a variable to specify the target computer name.

  2. Use a GetObject call to connect to the WMI namespace root\cimv2 on the target computer, and set the impersonation level to "impersonate."

  3. Use the ExecQuery method to query the Win32_Service class. To restrict data retrieval to the set of services that are not running, the query includes a Where clause that limits the return to services with a state that does not equal "Running."

    Unlike the WMI Query Language WQL statement in Listing 15.1 that retrieved all the properties for every service, the query in Listing 15.2 retrieves only the values of the display name and state properties. This minimizes the amount of data that must be sent across the network.

  4. For each service in the collection, echo the service display name and the service state.

Listing 15.2 Monitoring Inactive Services

  
1
2
3
4
5
6
7
8


strComputer = "."
Set objWMIService = GetObject("winmgmts:" & _
 "{impersonationLevel=Impersonate}!\\" & strComputer & "\root\cimv2")
Set colStoppedServices = objWMIService.ExecQuery _
 ("SELECT DisplayName,State FROM Win32_Service WHERE State <> 'Running'")
For Each objService in colStoppedServices
 Wscript.Echo objService.DisplayName & " = " & objService.State
Next

An attempt to echo a Win32_Service property other than display name and state would result in an error based on the property list defined in the WQL query.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft