How to get counter type for WMI SQL counters?

  • Hi,
     
    I am using WMI to build a performance monitor. One of the counters I am looking at is DB Transactions per second.

    I am using Win32_PerfRawData_MSSQLSERVER_SQLServerDatabases class for fetching the counter.

     
    It gives me values like 6560, 6566 etc. which are raw values. But I want to convert them to actual values or cooked value

    which should be same what windows performance monitor will display.

     
    For that I need its counter type from which I will have to come up with some formula to get cooked value from raw value.

    But my problem is I don't see this class documented any where and hence I don't know the counter type for these SQL classes.

     
    It will be great help if anybody can tell me how to get counter type of any counter.
     
    Thanks,

    Niranjan

  • Hi,

    The following class that contains TransactionPerSec counter is documneted:

    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/win32_perfformatteddata_msdtc_distributedtransactioncoordinator.asp

    Win32_PerfFormattedData_MSDTC_DistributedTransactionCoordinator

    One can just assume that the type could be similar (NO Garantee). Type for it say uint32. TransactionsPerSec

    Data type: uint32

    Access type: Read-only

    Qualifiers: CookingType("PERF_COUNTER_RAWCOUNT"), Counter("TransactionsPerSec"), PerfTimeStamp("Timestamp_PerfTime"), PerfTimeFreq("Frequency_PerfTime") Transactions performed per second

    There is an unformatted class that contain the same property, looks the type is the same, but everything else is different.

    Win32_PerfRawData_MSDTC_DistributedTransactionCoordinator

    TransactionsPerSec
    Data type: uint32

    Access type: Read-only

    Qualifiers: CounterType(65536), DefaultScale(0), PerfDetail(100) Transactions performed per second.

    See also Win32_PerfFormattedData_ASP_ActiveServerPages

    Let us know if it helped you and send me a code example if you think it is OK

    Yelena

    Regards,Yelena Varsha

  • Hi Yelena,

    Thanks for the info that you provided!

    I explored the things little more and found that the Win32_PerfFormattedData classes are only

    applicable to windows XP and not for Win 2k or NT versions. There you get only the Win32_PerfRawData

    classes. Problem is Win32_PerfRawData_MSSQLSERVER classes are not documented and hence we can not get the counter types

    for the counters in these classes and hence we can not decide what formula to be used for counter value calculation.

    There is another way to get cooked values. We can use Perfmon's interface to get the values, but there, for many counters you need to know the instance name in advance whereas if you are using WMI, you can loop through all instances whitout having to know their names. As far as I know Perfmon also uses WMI to retrieve counter values.

    Here is my code. It is very simple. It accepts the name of the counter and class in which the counter lies and retrieves the value.

    I have removed some validation part from it to make it look simple.

    Public Function GetMetricValue(ByVal ClassName As String, ByVal MetricName As String, Optional ByVal NameSpaceToBeUsed As String = "\root\cimv2", _

                                       Optional ByVal AdditionalMetricName As String = "", Optional ByVal Filter As String = "") As String

            Dim objConnection As New ConnectionOptions

            Dim objScope As ManagementScope

            Dim objQuery As ObjectQuery

            Dim objPC As ManagementObjectSearcher

            Dim objMgmt As ManagementObject

            Dim strLocalComputerName As String

            Dim strMetricValue As String

            Dim strLogMessage As String

            Dim strAdditionalMetricName As String

            Dim strAdditionalMetricValue As String

            Dim strFilter As String

            Dim strWMIQuery As String

            Try

             

                If strLocalComputerName <> m_strComputerName Then

                    objConnection.Username = m_strUserName

                    objConnection.Password = m_strPassword

                End If

                objScope = New ManagementScope("\\" & m_strComputerName & NameSpaceToBeUsed, objConnection)

                If strAdditionalMetricName <> "" Then

                    If strFilter <> "" Then

                        strWMIQuery = "SELECT " & strAdditionalMetricName & "," & MetricName & " FROM " & ClassName

                        strWMIQuery = strWMIQuery & " WHERE " & strAdditionalMetricName & " = '" & strFilter & "'"

                    Else

                        strWMIQuery = "SELECT " & strAdditionalMetricName & "," & MetricName & " FROM " & ClassName

                    End If

                Else

                    strWMIQuery = "SELECT " & MetricName & " FROM " & ClassName

                End If

                objQuery = New ObjectQuery(strWMIQuery)

                objPC = New ManagementObjectSearcher(objScope, objQuery)

                For Each objMgmt In objPC.Get

                    If objMgmt(MetricName) Is Nothing Then

                        GetMetricValue += " , "

                    Else                                     

                        GetMetricValue += objMgmt(MetricName).ToString+ ", "                 

                    End If

                Next          

            Catch ex As Exception

                GetMetricValue = ex.Message

                'Log Error.

                strLogMessage = Now() + vbTab + "GetMetricValue" + vbTab + ErrorCodes.UNIDENTIFIED_ERROR.ToString + " : " + ex.Message + vbTab + ClassName + vbTab + MetricName + vbTab + NameSpaceToBeUsed + vbNewLine

                Call LogInformation(m_strLogFileName, strLogMessage)

            Finally

                dispose objects here.

            End Try

        End Function

    Thanks,

    Niranjan

    P.S. - I was not able to update the thread for few days, sorry for that.

     

  • The easiest way to grab this information is to pull them out of wbemtest which can enumerate and present information on every WMI class.

    This is how I retrieved them and use it now as it has been faster than trawling through documentation on msdn.

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply