Recupero di dati statistici sulle prestazioni

In WMI è possibile definire dati statistici sulle prestazioni in base ai dati nelle classi di prestazioni formattate derivate da Win32_PerfFormattedData. Le statistiche disponibili sono medie, minime, massime, intervallo e varianza, come definito nei tipi di contatori statistici.

L'elenco seguente include i tipi di contatore statistici speciali:

Gli esempi seguenti illustrano come:

  • Creare un file MOF che definisce una classe di dati calcolati.
  • Scrivere uno script che crea un'istanza della classe e aggiorna periodicamente i dati nell'istanza con i valori statistici ricalcolati.

File MOF

Nell'esempio di codice MOF seguente viene creata una nuova classe di dati calcolata denominata Win32_PerfFormattedData_AvailableMBytes. Questa classe contiene dati dalla proprietà AvailableMBytes della classe non elaborata Win32_PerfRawData_PerfOS_Memory. La classe Win32_PerfFormattedData_AvailableBytes definisce le proprietà Average, Min, Max, Range e Variance.

Il file MOF usa i qualificatori di proprietà per le classi di contatori delle prestazioni formattate per definire l'origine dati delle proprietà e la formula di calcolo.

  • La proprietà Average ottiene dati non elaborati dalla Win32_PerfRawData_PerfOS_Memory. Proprietà AvailableMBytes .
  • Il qualificatore Counter per la proprietà Average specifica l'origine dati non elaborata.
  • Il qualificatore CookingType specifica la formula COOKER_MIN per calcolare i dati.
  • Il qualificatore SampleWindow specifica il numero di campioni da eseguire prima di eseguire il calcolo.
// Store the new Win32_PerfFormattedData_MemoryStatistics
//     class in the Root\Cimv2 namespace
#pragma autorecover
#pragma namespace("\\\\.\\Root\\CimV2")

qualifier vendor:ToInstance;
qualifier guid:ToInstance;
qualifier displayname:ToInstance;
qualifier perfindex:ToInstance;
qualifier helpindex:ToInstance;
qualifier perfdetail:ToInstance;
qualifier countertype:ToInstance;
qualifier perfdefault:ToInstance;
qualifier defaultscale:ToInstance;

qualifier dynamic:ToInstance;
qualifier hiperf:ToInstance;
qualifier AutoCook:ToInstance;
qualifier AutoCook_RawClass:ToInstance;
qualifier CookingType:ToInstance;
qualifier Counter:ToInstance;


// Define the Win32_PerFormattedData_MemoryStatistics
//     class, derived from Win32_PerfFormattedData
[
  dynamic,
  // Name of formatted data provider: "WMIPerfInst" for Vista 
  //   and later
  provider("HiPerfCooker_v1"), 
  // Text that will identify new counter in Perfmon
  displayname("My Calculated Counter"),                            
  // A high performance class     
  Hiperf,
  // Contains calculated data 
  Cooked, 
  // Value must be 1 
  AutoCook(1), 
  // Raw performance class to get data for calculations
  AutoCook_RawClass("Win32_PerfRawData_PerfOS_Memory"),
  // Value must be 1        
  AutoCook_RawDefault(1),
  // Name of raw class property to use for timestamp in formulas 
  PerfSysTimeStamp("Timestamp_PerfTime"), 
  // Name of raw class property to use for frequency in formulas
  PerfSysTimeFreq("Frequency_PerfTime"), 
  // Name of raw class property to use for timestamp in formulas
  Perf100NSTimeStamp("Timestamp_Sys100NS"),
  // Name of raw class property to use for frequency in formulas
  Perf100NSTimeFreq("Frequency_Sys100NS"),
  // Name of raw class property to use for timestamp in formulas
  PerfObjTimeStamp("Timestamp_Object"),
  // Name of raw class property to use for frequency in formulas 
  PerfObjTimeFreq("Frequency_Object"),
  // Only one instance of class allowed in namespace
  singleton                                                   
]

class Win32_PerfFormattedData_MemoryStatistics
          : Win32_PerfFormattedData
{

// Define the properties for the class. 
// All the properties perform different
//     statistical operations on the same
//     property, AvailableMBytes, in the raw class

// Define the Average property,
//     which uses the "COOKER_AVERAGE" counter type and 
//     gets its data from the AvailableMBytes 
//     property in the raw class

    [
     CookingType("COOKER_AVERAGE"),
     Counter("AvailableMBytes"),
     SampleWindow(10)
    ]
    uint64 Average = 0;

// Define the Min property, which uses
//     the "COOKER_MIN" counter type and 
//     gets its data from the AvailableMBytes
//     property in the raw class

    [
     CookingType("COOKER_MIN"),
     Counter("AvailableMBytes"),
     SampleWindow(10)
    ]
    uint64 Min = 0;

// Define the Max property, which uses
//     the "COOKER_MAX" counter type and 
//     gets its data from the
//     AvailableMBytes property in the raw class

    [
     CookingType("COOKER_MAX"),
     Counter("AvailableMBytes"),
     SampleWindow(10)
    ]
    uint64 Max = 0;

// Define the Range property, which uses
//     the "COOKER_RANGE" counter type and 
//     gets its data from the AvailableMBytes
//     property in the raw class

    [
     CookingType("COOKER_RANGE"),
     Counter("AvailableMBytes"),
     SampleWindow(10)
    ]
    uint64 Range = 0;

// Define the Variance property, which uses
//     the "COOKER_VARIANCE" counter type and 
//     gets its data from the AvailableMBytes
//     property in the raw class

    [
     CookingType("COOKER_VARIANCE"),
     Counter("AvailableMBytes"),
     SampleWindow(10)
    ]
    uint64 Variance = 0;
};

Script

Nell'esempio di codice di script seguente vengono ottenute statistiche sulla memoria disponibile, in megabyte, usando il MOF creato in precedenza. Lo script usa l'oggetto script SWbemRefresher per creare un aggiornamento contenente un'istanza della classe statistiche creata dal file MOF, che è Win32_PerfFormattedData_MemoryStatistics. Per altre informazioni sull'uso di script, vedere Aggiornamento dei dati WMI negli script. Se funziona in C++, vedere Accesso ai dati sulle prestazioni in C++.

Nota

SWbemRefreshableItem.Object deve essere chiamato dopo aver ottenuto l'elemento dalla chiamata a SWbemRefresher.Add o lo script avrà esito negativo. SWbemRefresher.Refresh deve essere chiamato prima di entrare nel ciclo per ottenere valori di base o le proprietà statistiche sono zero (0) nel primo passaggio.

 

' Connect to the Root\Cimv2 namespace
strComputer = "."
Set objService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" _
    & strComputer & "\root\cimv2")

' Create a refresher
Set Refresher = CreateObject("WbemScripting.SWbemRefresher")
If Err <> 0 Then
WScript.Echo Err
WScript.Quit
End If

' Add the single instance of the statistics
'    class to the refresher
Set obMemoryStatistics = Refresher.Add(objService, _
    "Win32_PerfFormattedData_MemoryStatistics=@").Object

' Refresh once to obtain base values for cooking calculations
Refresher.Refresh

Const REFRESH_INTERVAL = 10

' Refresh every REFRESH_INTERVAL seconds 
For I=1 to 3
  WScript.Sleep REFRESH_INTERVAL * 1000
  Refresher.Refresh

  WScript.Echo "System memory statistics" _
      & "(Available Megabytes) " _
      & "for the past 100 seconds - pass " & i 
  WScript.Echo "Average = " _
     & obMemoryStatistics.Average & VBNewLine & "Max = " _
     & obMemoryStatistics.Max & VBNewLine & "Min = " _
     & obMemoryStatistics.Min & VBNewLine & "Range = " _ 
     & obMemoryStatistics.Range & VBNewLine & "Variance = " _
     & obMemoryStatistics.Variance 
Next

Esecuzione dello script

La procedura seguente descrive come eseguire l'esempio.

Per eseguire lo script di esempio

  1. Copiare sia il codice MOF che lo script nei file nel computer.
  2. Assegnare al file MOF un'estensione mof e il file di script una descrizione di .vbs.
  3. Nella riga di comando passare alla directory in cui vengono archiviati i file ed eseguire Mofcomp nel file MOF. Ad esempio, se si chiama il file CalculatedData.mof, il comando è MofcompCalculatedData.mof
  4. Eseguire lo script.

Monitoraggio dei dati sulle prestazioni

Accesso alle classi di prestazioni preinstallate di WMI

Qualificatori delle proprietà per classi di contatori delle prestazioni formattate

Tipi di contatori statistici