locked
How can I read all nodes and subnodes of the health explorer in C#? RRS feed

  • Question

  • Hello,

    I want to read all nodes in the health explorer in C#.

    Does anyone know how to read all nodes in health explorer?

    I have not found in google.

    Regards

    Alex

     

    Thursday, November 19, 2015 9:31 AM

Answers

  • Hi, Yes these examples are for scom 2007 but logic is the same. And there is also an updated article for scom 2012 where they use GetStateHierarchy method.

    https://msdn.microsoft.com/en-us/library/hh329063.aspx?f=255&MSPPError=-2147217396

    As I can see you miss the situation when a node item has class type ExternalRollupMonitoringState but not MonitoringState, so if the class type is ExternalRollupMonitoringState you should check other subcomponents by executing GetExternalStateHierarchies method. 

    You can check my quick and dirty powershell script (without treeview!) and can easily update it to C#

    function GetChildNodes ($ChildNodes) 
    {
    $Local:StateHierarchy=@()
    $Level=$Level+1
    
    foreach ($child in $ChildNodes)
    {
     
    $hash = @{ Level = $Level; MonitorName = $child.Item.MonitorDisplayName; HealthState =$child.Item.HealthState; }
    $obj = New-Object PSObject -Property $hash; $StateHierarchy += $obj
     
     if ($child.TotalChildNodeCount -gt 0)
                {
    
                    GetChildNodes($child.ChildNodes) 
    
                }
                else
                {
                if ($child.item.gettype().name -eq "ExternalRollupMonitoringState") 
                    {
                    GetChildNodes ($child.item.GetExternalStateHierarchies())
                    }
                }
    
    }
    $StateHierarchy
    }
    
    function GetState([Microsoft.EnterpriseManagement.Monitoring.MonitoringObject]$mObject)
    {
    
    $state = $mObject.GetStateHierarchy()
    
      if ($state.ParentNode -ne $null){}
                else { 
                      $hash = @{ Level = 0; MonitorName = $state.Item.MonitorDisplayName; HealthState =$state.Item.HealthState; }
                      $obj = New-Object PSObject -Property $hash; $StateHierarchy += $obj
                     }
    
                if ($state.TotalChildNodeCount -gt 0)
                {
    
                      $All= GetChildNodes ($state.ChildNodes)
                      $All+=$StateHierarchy
                      $All | Sort-Object Level,MonitorName, HealthState | Out-GridView
    
                }
    
    }
    
    $instances=Get-SCOMClass -name "Microsoft.SystemCenter.NTService" | Get-SCOMClassInstance 
    GetState $instances[0]
    

    • Proposed as answer by Elton_Ji Sunday, December 6, 2015 3:11 PM
    • Marked as answer by Elton_Ji Tuesday, December 8, 2015 2:37 PM
    Tuesday, November 24, 2015 10:22 PM

All replies

  • Hi, see the following

    https://msdn.microsoft.com/en-us/library/microsoft.enterprisemanagement.monitoring.partialmonitoringobject.getmonitoringstatehierarchy.aspx

    https://msdn.microsoft.com/en-us/library/bb960484.aspx

    Thursday, November 19, 2015 11:19 AM
  • Hi, thank you for your answer,

    To the both links: the method GetMonitoringStateHierarchy is obsolete. Visual studio suggests the method GetStateHierarchy.

    I had to try about the MonitoringObject with the method GetStateHierarchy to access to all nodes. I get a node, but not all subnodes. That are the top nodes.  For example: the configuration-node and his subnodes. And this subnodes have too subnudes, but C# displays in debugger 0 subnodes. I don't unterstand why. I know that the health explorer have more nodes.

    Then I try through the PartialMonitoringObject with the method GetStateHierarchy to access to the nodes. Then I get the subnodes. They are group by computernames. The problem is they are not accessible from the top nodes.

    I have the top nodes und the subnodes, but how I link/join the subnodes to the top nodes?? I can read the top nodes and subnodes only separably. I want to display the complete hierarchy of the health explorer: the parent and his childs. I suppose over some ID??

    Or exist a method, where I get the root element? So that I iterate from root-node until last subnode?

    Regards

    Alex
    Saturday, November 21, 2015 6:19 PM
  • Hi, Yes these examples are for scom 2007 but logic is the same. And there is also an updated article for scom 2012 where they use GetStateHierarchy method.

    https://msdn.microsoft.com/en-us/library/hh329063.aspx?f=255&MSPPError=-2147217396

    As I can see you miss the situation when a node item has class type ExternalRollupMonitoringState but not MonitoringState, so if the class type is ExternalRollupMonitoringState you should check other subcomponents by executing GetExternalStateHierarchies method. 

    You can check my quick and dirty powershell script (without treeview!) and can easily update it to C#

    function GetChildNodes ($ChildNodes) 
    {
    $Local:StateHierarchy=@()
    $Level=$Level+1
    
    foreach ($child in $ChildNodes)
    {
     
    $hash = @{ Level = $Level; MonitorName = $child.Item.MonitorDisplayName; HealthState =$child.Item.HealthState; }
    $obj = New-Object PSObject -Property $hash; $StateHierarchy += $obj
     
     if ($child.TotalChildNodeCount -gt 0)
                {
    
                    GetChildNodes($child.ChildNodes) 
    
                }
                else
                {
                if ($child.item.gettype().name -eq "ExternalRollupMonitoringState") 
                    {
                    GetChildNodes ($child.item.GetExternalStateHierarchies())
                    }
                }
    
    }
    $StateHierarchy
    }
    
    function GetState([Microsoft.EnterpriseManagement.Monitoring.MonitoringObject]$mObject)
    {
    
    $state = $mObject.GetStateHierarchy()
    
      if ($state.ParentNode -ne $null){}
                else { 
                      $hash = @{ Level = 0; MonitorName = $state.Item.MonitorDisplayName; HealthState =$state.Item.HealthState; }
                      $obj = New-Object PSObject -Property $hash; $StateHierarchy += $obj
                     }
    
                if ($state.TotalChildNodeCount -gt 0)
                {
    
                      $All= GetChildNodes ($state.ChildNodes)
                      $All+=$StateHierarchy
                      $All | Sort-Object Level,MonitorName, HealthState | Out-GridView
    
                }
    
    }
    
    $instances=Get-SCOMClass -name "Microsoft.SystemCenter.NTService" | Get-SCOMClassInstance 
    GetState $instances[0]
    

    • Proposed as answer by Elton_Ji Sunday, December 6, 2015 3:11 PM
    • Marked as answer by Elton_Ji Tuesday, December 8, 2015 2:37 PM
    Tuesday, November 24, 2015 10:22 PM