Process monitoring for multiple instance services

Several products, such as SQL Server, allow for multiple instances of services.  It often not easy to determine exactly which Windows service created which process in taskmgr or perfmon, especially if the process name is dynamically created at start-time.
I handled this using a script and WMI.  The attached rule XML is modified to work for SQL Server, which can be multiple instance.  I looked through the MPs and could not find a monitor or rule that does this, but I’m still going to feel really stupid if there’s a better answer out there (e.g. "that’s built into the Windows Server MP….").  Feel free to leave a comment.
The rule uses a script and a property (ServiceName) to match the targeted object by PID through WMI.  Then it creates a property bag and maps the Property Bag to normal performance data.
     <Rule ID="MyMP.ServiceProcessStats" Enabled="true" Target="SQL!Microsoft.SQLServer.DBEngine" ConfirmDelivery="false" Remotable="true" Priority="Normal" DiscardLevel="100">
          <DataSource ID="DS" TypeID="Windows!Microsoft.Windows.TimedScript.PerformanceProvider">
            <SyncTime />
            <Arguments />
    Option Explicit
    Dim oWMI, obj, PIDs, PID, oResults, CounterValue
    Dim ServiceInstanceName, CounterInstanceName
    Dim SourceID, ManagedEntityId, oAPI, oPropertyBag, objItem
    ‘**** Could do the whole Args() thing here, but this works fine.
    SourceID = "$MPElement$"
    ManagedEntityId = "$Target/Id$"
    Set oAPI = CreateObject("MOM.ScriptAPI")
    set oWMI = GetObject("winmgmts:{impersonationlevel=impersonate}//./root/cimv2")
    ‘***************  Get the service name (e.g. MSSQLServer.1) *******************************
    ServiceInstanceName = "$Target/Property[Type="SQL!Microsoft.SQLServer.DBEngine"]/ServiceName$"
    ‘ *************** Get the Process ID of the Win32 process for that specific service ***************
    set PIDs = oWMI.ExecQuery("Select * from Win32_Service where name = ‘" & ServiceInstanceName &"’")
    ‘ *************** Should return just one PID, but this ensures that we use the last one ***********
    For each obj in PIDs
      PID = obj.ProcessID
      ‘*************** Get the performance data for that specific process ***********************
      set oResults = oWMI.ExecQuery("select * from Win32_PerfFormattedData_PerfProc_Process where idprocess=" & PID)
      ‘************** Should just return one result in the result set ***************************
      for each obj in oResults
        CounterValue = obj.PrivateBytes
      set oPropertyBag = oAPI.CreatePropertyBag()
      with oPropertyBag
        .AddValue "ObjectName", "Process"
        .AddValue "Counter", "PrivateBytes"
        .AddValue "InstanceName", CounterInstanceName
        .AddValue "Value", CounterValue
      end with
    Call oAPI.Return(oPropertyBag)
          <WriteAction ID="WriteToDB" TypeID="SC!Microsoft.SystemCenter.CollectPerformanceData" />
          <WriteAction ID="WriteToDW" TypeID="SCDW!Microsoft.SystemCenter.DataWarehouse.PublishPerformanceData" />
This entry was posted in Rules. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s