Share via


健全狀況服務報告

適用於︰Windows Server 2016

什麼是報告

「健全狀況服務」可減少從「儲存空間直接存取」叢集取得即時效能和容量資訊需執行的工作量。 一個新的 Cmdlet 可提供重要計量的精選清單,這些計量會跨節點有效率地收集並動態彙總,其中包含偵測叢集成員資格的內建邏輯。 所有的值都是即時且為該時間點的值。

PowerShell 中的使用方式

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

Get-StorageSubSystem Cluster* | Get-StorageHealthReport

選用的 Count 參數會指示在一秒的時間間隔內要傳回多少組的值。

Get-StorageSubSystem Cluster* | Get-StorageHealthReport -Count <Count>

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

Get-Volume -FileSystemLabel <Label> | Get-StorageHealthReport -Count <Count>

Get-StorageNode -Name <Name> | Get-StorageHealthReport -Count <Count>

在 .NET 和 C# 中的使用方式

連線

若要查詢健全狀況服務,您將需要使用叢集建立 CimSession。 若要這樣做,您需要只有完整 .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;
}

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

建議您直接從使用者輸入即時建構 Password SecureString,因此其密碼絕不會以純文字儲存在記憶體中。 這有助於減輕各種安全性疑慮。 但在實務上,為了進行原型設計,如上所述建構很常見。

探索物件

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

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

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 MSFT_StorageHealth itself
    HealthService = Session.EnumerateAssociatedInstances(Cluster.CimSystemProperties.Namespace,
        Cluster, "MSFT_StorageSubSystemToStorageHealth", null, "StorageSubSystem", "StorageHealth").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());
}

叫用 GetReport,以開始串流專家製作的必要計量範例 (於節點間有效率地收集並動態彙總),並使用內建邏輯偵測叢集成員資格。 之後範例會每隔一秒到達。 所有的值都是即時且為該時間點的值。

可以串流三個範圍的計量:叢集、任何節點或任何磁碟區。

Windows Server 2016 中每個範圍可用的計量完整清單如下所述。

IObserver.OnNext()

此範例程式碼會使用觀察者設計模式,來實作其 OnNext() 方法將在每個新的計量範例抵達時叫用的觀察者。 如果/當串流節述時,將會呼叫 OnCompleted() 方法。 例如,您可能會使用此方法來重新起始串流,以便串流無限繼續執行。

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

        if (StreamedResult != null)
        {
            // For illustration, you could store the metrics in this dictionary
            Dictionary<string, string> Metrics = new Dictionary<string, string>();

            // Unpack
            CimInstance Report = (CimInstance)StreamedResult.ItemValue;
            IEnumerable<CimInstance> Records = (IEnumerable<CimInstance>)Report.CimInstanceProperties["Records"].Value;
            foreach (CimInstance Record in Records)
            {
                /// Each Record has "Name", "Value", and "Units"
                Metrics.Add(
                    Record.CimInstanceProperties["Name"].Value.ToString(),
                    Record.CimInstanceProperties["Value"].Value.ToString()
                    );
            }

            // 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();
    MetricsParams.Add(CimMethodParameter.Create("TargetObject", Target, CimType.Instance, CimFlags.In));
    MetricsParams.Add(CimMethodParameter.Create("Count", 999, CimType.UInt32, CimFlags.In));
    // Enable WMI Streaming
    CimOperationOptions Options = new CimOperationOptions();
    Options.EnableMethodResultStreaming = true;
    // Invoke API
    CimAsyncMultipleResults<CimMethodResultBase> InvokeHandler;
    InvokeHandler = Session.InvokeMethodAsync(
        HealthService.CimSystemProperties.Namespace, HealthService, "GetReport", MetricsParams, Options
        );
    // Subscribe the Observer
    MetricsObserver<CimMethodResultBase> Observer = new MetricsObserver<CimMethodResultBase>(this);
    IDisposable Disposeable = InvokeHandler.Subscribe(Observer);
}

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

報告的屬性

每個計量範例都是一份「報告」,其中包含許多對應至個別計量的「記錄」。

如需完整的結構描述,請查閱 storagewmi.mof 中的 MSFT_StorageHealthReportMSFT_HealthRecord 類別。

每個計量每個此資料表只有三個屬性。

屬性 範例
名稱 IOLatencyAverage
0.00021
單位 3

單位 = { 0, 1, 2, 3, 4 },其中 0 = "位元組"、1 = "BytesPerSecond"、2 = "CountPerSecond"、3 = "秒" 或 4 = "百分比"。

涵蓋範圍

以下是 Windows Server 2016 中每個範圍可用的計量。

MSFT_StorageSubSystem

名稱 單位
CPUUsage 4
CapacityPhysicalPooledAvailable 0
CapacityPhysicalPooledTotal 0
CapacityPhysicalTotal 0
CapacityPhysicalUnpooled 0
CapacityVolumesAvailable 0
CapacityVolumesTotal 0
IOLatencyAverage 3
IOLatencyRead 3
IOLatencyWrite 3
IOPSRead 2
IOPSTotal 2
IOPSWrite 2
IOThroughputRead 1
IOThroughputTotal 1
IOThroughputWrite 1
MemoryAvailable 0
MemoryTotal 0

MSFT_StorageNode

名稱 單位
CPUUsage 4
IOLatencyAverage 3
IOLatencyRead 3
IOLatencyWrite 3
IOPSRead 2
IOPSTotal 2
IOPSWrite 2
IOThroughputRead 1
IOThroughputTotal 1
IOThroughputWrite 1
MemoryAvailable 0
MemoryTotal 0

MSFT_Volume

名稱 單位
CapacityAvailable 0
CapacityTotal 0
IOLatencyAverage 3
IOLatencyRead 3
IOLatencyWrite 3
IOPSRead 2
IOPSTotal 2
IOPSWrite 2
IOThroughputRead 1
IOThroughputTotal 1
IOThroughputWrite 1

其他參考