取得叢集效能歷程記錄

適用於:Azure Stack HCI 版本 23H2 和 22H2;Windows Server 2022、Windows Server 2019

健全狀況服務可減少從儲存空間直接存取叢集取得即時效能和容量資訊的必要工作。 一個 Cmdlet 會提供基本計量的策劃清單,這些計量是透過內建邏輯偵測叢集成員資格,以有效率的動態方式收集自各節點並予以彙總。 所有的值都是即時且為該時間點的值。

PowerShell 中的使用方式

使用下列 Cmdlet 取得整個儲存空間直接存取叢集的計量:

Get-ClusterPerformanceHistory

提示

使用 Get-ClusterPerf 別名來儲存某些按鍵輸入。

您也可以取得某個特定磁碟區或伺服器的計量:

Get-Volume -FileSystemLabel <Label> | Get-ClusterPerformanceHistory

Get-StorageNode -Name <Name> | Get-ClusterPerformanceHistory

.NET 和 C# 中的使用方式

本節會說明如何連線到健全狀況服務、使用探索物件及實作觀察者,以開始串流計量。

連線

為了查詢健全狀況服務,您會建立具有該叢集的 CimSession。 若要這樣做,您需要一些只有完整 Microsoft .NET 才會提供的項目,這表示您無法直接從 Web 或行動應用程式執行這項作業。 本節中的程式碼範例會使用 C#,這是此資料存取層的最直接選項。

using System.Security;
using Microsoft.Management.Infrastructure;

public CimSession Connect(string Domain = "...", string Computer = "...", string Username = "...", string Password = "...")
{
    SecureString PasswordSecureString = new SecureString();
    foreach (char c in Password)
    {
        PasswordSecureString.AppendChar(c);
    }

    CimCredential Credentials = new CimCredential(
        PasswordAuthenticationMechanism.Default, Domain, Username, PasswordSecureString);
    WSManSessionOptions SessionOptions = new WSManSessionOptions();
    SessionOptions.AddDestinationCredentials(Credentials);
    Session = CimSession.Create(Computer, SessionOptions);
    return Session;
}

所提供使用者名稱應為目標電腦的本機系統管理員。

建議以即時方式直接從使用者輸入來建構密碼 SecureString,讓密碼永遠不會以純文字儲存在記憶體中。 這有助於降低各種安全性問題。 但在實務上,以上述方式建構通常是為了原型設計之用。

探索物件

建立 CimSession 之後,您就可以查詢叢集上的 Windows Management Instrumentation (WMI)。

您必須先取得數個相關物件的執行個體,才能取得錯誤或計量。 首先,取得代表叢集上儲存空間直接存取的 MSFT_StorageSubSystem。 您可以使用該物件,取得叢集中的每個 MSFT_StorageNode,以及資料磁碟區的每個 MSFT_Volume。 最後,您需要取得 MSCluster_ClusterHealthService,也就是健全狀況服務本身。

CimInstance Cluster;
List<CimInstance> Nodes;
List<CimInstance> Volumes;
CimInstance HealthService;

public void DiscoverObjects(CimSession Session)
{
    // Get MSFT_StorageSubSystem for Storage Spaces Direct
    Cluster = Session.QueryInstances(@"root\microsoft\windows\storage", "WQL", "SELECT * FROM MSFT_StorageSubSystem")
        .First(Instance => (Instance.CimInstanceProperties["FriendlyName"].Value.ToString()).Contains("Cluster"));

    // Get MSFT_StorageNode for each cluster node
    Nodes = Session.EnumerateAssociatedInstances(Cluster.CimSystemProperties.Namespace,
        Cluster, "MSFT_StorageSubSystemToStorageNode", null, "StorageSubSystem", "StorageNode").ToList();

    // Get MSFT_Volumes for each data volume
    Volumes = Session.EnumerateAssociatedInstances(Cluster.CimSystemProperties.Namespace,
        Cluster, "MSFT_StorageSubSystemToVolume", null, "StorageSubSystem", "Volume").ToList();

    // Get MSCluster_ClusterHealthService itself
    HealthService = session.QueryInstances(@"root\MSCluster", "WQL", "SELECT * FROM MSCluster_ClusterHealthService").First();
}

這些是您在 PowerShell 中使用 Get-StorageSubSystemGet-StorageNodeGet-Volume 等 Cmdlet 取得的相同物件。

您可以存取儲存體管理 API 類別中記錄的所有相同屬性。

using System.Diagnostics;

foreach (CimInstance Node in Nodes)
{
    // For illustration, write each node's Name to the console. You could also write State (up/down), or anything else!
    Debug.WriteLine("Discovered Node " + Node.CimInstanceProperties["Name"].Value.ToString());
}

根據 MetricName 參數提供的計量名稱,叫用 GetMetric 開始串流基本計量專家策劃清單的範例,這些計量是透過內建邏輯偵測叢集成員資格,以有效率的動態方式收集自各節點並予以彙總。 樣本是根據 StreamName 參數所提供的時間範圍所建立。

如需可用計量的完整清單,請參閱儲存空間直接存取的效能歷程記錄

IObserver.OnNext()

此範例程式碼使用觀察者設計模式實作觀察者,在每個新計量樣本出現時叫用其 OnNext() 方法。 如果/當串流結束時,則會呼叫其 OnCompleted() 方法。 例如,您可以使用這個方法重新起始串流,進行無休止的作業。

class MetricsObserver<T> : IObserver<T>
{
    public void OnNext(T Result)
    {
        // Cast
        CimMethodStreamedResult StreamedResult = Result as CimMethodStreamedResult;

        if (StreamedResult != null)
        {
            CimInstance Metric = (CimInstance)StreamedResult.ItemValue;
            Console.WriteLine("MetricName: " + Metric.CimInstanceProperties["MetricId"].Value);
            IEnumerable<CimInstance> Records = (IEnumerable<CimInstance>)Metric.CimInstanceProperties["Records"].Value;

            foreach (CimInstance Record in Records)
            {
                // Each Record has "TimeStamp" and "Value. For Illustration, just print the metric"
                Console.WriteLine(record.CimInstanceProperties["TimeStamp"] + ": " + record.CimInstanceProperties["Value"]);

            }

            // TODO: Whatever you want!
        }
    }
    public void OnError(Exception e)
    {
        // Handle Exceptions
    }
    public void OnCompleted()
    {
        // Reinvoke BeginStreamingMetrics(), defined in the next section
    }
}

開始串流

定義觀察者之後,您就可以開始串流。

指定您想要限定計量範圍的目標 CimInstance。 其可以是叢集、任何節點或任何磁碟區。

Count 參數是串流結束之前的樣本數。

CimInstance Target = Cluster; // From among the objects discovered in DiscoverObjects()

public void BeginStreamingMetrics(CimSession Session, CimInstance HealthService, CimInstance Target)
{
    // Set Parameters
    CimMethodParametersCollection MetricsParams = new CimMethodParametersCollection();
    string[] metricNames = new string[] { "ClusterNode.Cpu.Usage,ClusterNode=RRN44-13-01", "ClusterNode.Cpu.Usage.Host,ClusterNode=RRN44-13-01" };
    MetricsParams.Add(CimMethodParameter.Create("MetricName", metricNames, CimType.StringArray, CimFlags.In));
    MetricsParams.Add(CimMethodParameter.Create("StreamName", "LastHour", CimType.String, CimFlags.In));

    // Enable WMI Streaming
    CimOperationOptions Options = new CimOperationOptions();
    Options.EnableMethodResultStreaming = true;
    // Invoke API
    CimAsyncMultipleResults<CimMethodResultBase> InvokeHandler;
    InvokeHandler = Session.InvokeMethodAsync(
        HealthService.CimSystemProperties.Namespace, HealthService, "GetMetric", MetricsParams, Options
        );
    // Subscribe the Observer
    MetricsObserver<CimMethodResultBase> Observer = new MetricsObserver<CimMethodResultBase>(this);
    IDisposable Disposeable = InvokeHandler.Subscribe(Observer);
}

這些計量可以視覺化、儲存在資料庫中,或以您認為適合的任何方式使用。

其他參考資料