How to Create State Views, Diagram Views, and View Folders

Applies To: Operations Manager 2007 R2, Operations Manager 2007 SP1, System Center Operations Manager 2007

In Operations Manager 2007, views are groups of managed objects that have a commonality, which is defined in the view properties. When you select a view, a query is executed and the results of the query are displayed in the results pane.

Several views are created by default when Operations Manager is installed. Management Packs also contain views. When a Management Pack is imported, a folder is created in the Monitoring pane. This folder contains the views that are defined in the Management Pack. You cannot change these views, and you cannot add new views to the Management Pack folder. However, you can create a folder in the Monitoring pane and create views that appear in your folder.

There are several view types, each displaying a different aspect of monitoring data. The following example demonstrates how to create a state view and a diagram view. A state view displays relationships between components, computers, and computer groups. A diagram view displays a graphical view of a set of managed objects and how they relate to one another.

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.EnterpriseManagement;
using Microsoft.EnterpriseManagement.Configuration;
using System.Collections.ObjectModel;
using System.Xml;
using Microsoft.EnterpriseManagement.Monitoring;

namespace ViewCreation
{
    class Program
    {
        // ---------------------------------------------------------------------
        static void Main(string[] args)
        {
            ManagementGroup mg = new ManagementGroup("localhost");
            ManagementPack mp;
            ManagementPackView stateView;
            ManagementPackView diagramView;
            ManagementPackFolder viewFolder;
            string folderId;

            mp = GetDefaultMP(mg);

            folderId = "ViewSamples.SampleFolder" + Guid.NewGuid().ToString("N");

            viewFolder = CreateViewFolder(mp, mg, "Sample Folder", folderId);

            stateView = CreateStateView(mp, mg, viewFolder);
            diagramView = CreateDiagramView(mp, mg, viewFolder);

            mp.AcceptChanges();
        }

        // ---------------------------------------------------------------------
        private static ManagementPack GetDefaultMP(
            ManagementGroup mg
            )
        {
            ReadOnlyCollection<ManagementPack> managementPacks;

            managementPacks = mg.GetManagementPacks("Microsoft.SystemCenter.OperationsManager.DefaultUser");

            if (managementPacks.Count != 1)
            {
                throw new ApplicationException("Failed to retrieve the default mp");
            }

            return (managementPacks[0]);
        }

        // ---------------------------------------------------------------------
        private static ManagementPackView CreateDiagramView(
            ManagementPack mp,
            ManagementGroup mg,
            ManagementPackFolder viewFolder
            )
        {
            ReadOnlyCollection<MonitoringViewType> stateViewTypes;
            ReadOnlyCollection<MonitoringClass> sql2005DBClasses;
            ManagementPackView view;
            XmlDocument viewConfigDoc;

            stateViewTypes = mg.GetMonitoringViewTypes("Microsoft.SystemCenter.DiagramViewType");

            if (stateViewTypes.Count != 1)
            {
                throw new ApplicationException("Failed to load the state view type");
            }

            view = new ManagementPackView(mp, "ViewsSample.DiagramView1", ManagementPackAccessibility.Public);

            view.TypeID = stateViewTypes[0];
            view.DisplayName = "Sample Diagram View";

            sql2005DBClasses = mg.GetMonitoringClasses(new MonitoringClassCriteria("DisplayName='SQL 2005 DB'"));

            if (sql2005DBClasses.Count != 1)
            {
                throw new ApplicationException("Failed to retrieve the SQL 2005 DB class. Make sure the SQL 2005 MP is imported");
            }

            view.Target = sql2005DBClasses[0];
            view.Category = "Operations";

            viewConfigDoc = CreateDiagramViewConfigDoc(mg, sql2005DBClasses[0]);

            view.Configuration = viewConfigDoc.ChildNodes[0].InnerXml;

            ManagementPackFolderItem folderItem = new ManagementPackFolderItem(view, viewFolder);

            return (view);
        }

        // ---------------------------------------------------------------------
        private static XmlDocument CreateDiagramViewConfigDoc(
            ManagementGroup mg,
            MonitoringClass monitoringClass
            )
        {
            XmlDocument configDoc = new XmlDocument();
            XmlElement rootNode = configDoc.CreateElement("Configuration");
            XmlElement presentationNode = configDoc.CreateElement("Presentation");
            XmlElement criteriaNode = configDoc.CreateElement("Criteria");

            configDoc.AppendChild(rootNode);
            rootNode.AppendChild(criteriaNode);
            rootNode.AppendChild(presentationNode);

            presentationNode.InnerXml = @"<DiagramViewCriteria>
                                            <DiagramViewDisplay>
                                              <NodesPerRow>3</NodesPerRow>
                                              <ContainmentLine>
                                                <Color />
                                                <SourceArrow IsFilled=""false"">
                                                  <ArrowStyle>NoAnchor</ArrowStyle>
                                                </SourceArrow>
                                                <TargetArrow IsFilled=""false"">
                                                  <ArrowStyle>ArrowAnchor</ArrowStyle>
                                                </TargetArrow>
                                              </ContainmentLine>
                                              <NonContainmentLine>
                                                <Color Red=""6"" Green=""134"" Blue=""253"" />
                                                <SourceArrow IsFilled=""false"">
                                                  <ArrowStyle>NoAnchor</ArrowStyle>
                                                </SourceArrow>
                                                <TargetArrow IsFilled=""false"">
                                                  <ArrowStyle>ArrowAnchor</ArrowStyle>
                                                </TargetArrow>
                                                <LineStyle>DashDotDot</LineStyle>
                                              </NonContainmentLine>
                                            </DiagramViewDisplay>
                                          </DiagramViewCriteria>";

            XmlElement target = configDoc.CreateElement("Target");

            ReadOnlyCollection<PartialMonitoringObject> dbMonitoringObjects;

            dbMonitoringObjects = mg.GetPartialMonitoringObjects(monitoringClass);

            if (dbMonitoringObjects.Count < 1)
            {
                throw new ApplicationException("At least one database object is required");
            }

            target.InnerText = dbMonitoringObjects[0].Id.ToString();

            rootNode.AppendChild(target);

            return (configDoc);
        }

        // ---------------------------------------------------------------------
        private static ManagementPackView CreateStateView(
            ManagementPack mp,
            ManagementGroup mg,
            ManagementPackFolder viewFolder
            )
        {
            ReadOnlyCollection<MonitoringViewType> stateViewTypes;
            ReadOnlyCollection<MonitoringClass> sql2005DBClasses;
            ManagementPackView view;
            XmlDocument viewConfigDoc;

            stateViewTypes = mg.GetMonitoringViewTypes("Microsoft.SystemCenter.StateViewType");

            if (stateViewTypes.Count != 1)
            {
                throw new ApplicationException("Failed to load the state view type");
            }

            view = new ManagementPackView(
                mp, "ViewsSample.SampleStateView2", ManagementPackAccessibility.Public);

            view.TypeID = stateViewTypes[0];
            view.DisplayName = "Sample State View2";

            sql2005DBClasses = mg.GetMonitoringClasses(
                new MonitoringClassCriteria("DisplayName='SQL 2005 DB'"));

            if (sql2005DBClasses.Count != 1)
            {
                throw new ApplicationException("Failed to retrieve the SQL 2005 DB class. Make sure the SQL 2005 MP is imported");
            }

            view.Target = sql2005DBClasses[0];
            view.Category = "Operations";

            viewConfigDoc = CreateViewConfigDoc(sql2005DBClasses[0]);

            view.Configuration = viewConfigDoc.ChildNodes[0].InnerXml;

            ManagementPackFolderItem folderItem = new ManagementPackFolderItem(view, viewFolder);

            return (view);
        }

        // ---------------------------------------------------------------------
        private static XmlDocument CreateViewConfigDoc(
            MonitoringClass monitoringClass
            )
        {
            XmlDocument configDoc = new XmlDocument();
            XmlElement rootNode = configDoc.CreateElement("Configuration");
            XmlElement presentationNode = configDoc.CreateElement("Presentation");
            XmlElement criteriaNode = configDoc.CreateElement("Criteria");

            XmlElement maintenanceModeProp = configDoc.CreateElement("InMaintenanceMode");

            maintenanceModeProp.InnerText = "false";

            criteriaNode.AppendChild(maintenanceModeProp);

            configDoc.AppendChild(rootNode);
            rootNode.AppendChild(criteriaNode);
            rootNode.AppendChild(presentationNode);

            CreateColumnXmlNode(presentationNode, 0, "State", monitoringClass.Name, "Descending", true);
            CreateColumnXmlNode(presentationNode, -1, "Maintenance Mode", "InMaintenanceMode", "Ascending", true);
            CreateColumnXmlNode(presentationNode, -1, "Name", "Name", "Ascending", true);
            CreateColumnXmlNode(presentationNode, -1, "Path", "Path", "Ascending", false);

            ReadOnlyCollection<MonitoringClassProperty> properties;

            properties = monitoringClass.GetMonitoringProperties(BaseClassTraversalDepth.Recursive);

            foreach (MonitoringClassProperty prop in properties)
            {
                CreateColumnXmlNode(
                    presentationNode, -1, prop.DisplayName, prop.Name, "Ascending", true);
            }

            return (configDoc);
        }

        // ---------------------------------------------------------------------
        private static ManagementPackFolder CreateViewFolder(
            ManagementPack mp,
            ManagementGroup mg,
            string folderDisplayName,
            string folderId
            )
        {
            ManagementPackFolder newFolder;
            ReadOnlyCollection<MonitoringFolder> parentFolders;

            parentFolders = mg.GetMonitoringFolders(
                new MonitoringFolderCriteria("Name='Microsoft.SystemCenter.Monitoring.ViewFolder.Root'"));

            if (parentFolders.Count != 1)
            {
                throw new ApplicationException("Failed to retrieve the root folder");
            }

            newFolder = new ManagementPackFolder(mp, folderId, ManagementPackAccessibility.Public);

            newFolder.DisplayName = folderDisplayName;

            newFolder.ParentFolder = (ManagementPackFolder)parentFolders[0];

            ManagementPackFolderItem folderItem = new ManagementPackFolderItem(newFolder, parentFolders[0]);

            mp.AcceptChanges();

            return (newFolder);
        }

        

        // ---------------------------------------------------------------------
        static void CreateColumnXmlNode(
            XmlElement rootNode,
            int sortingIndex,
            string columnName,
            string columnId,
            string sortOrder,
            bool isVisible
            )
        {
            XmlElement columnElement = rootNode.OwnerDocument.CreateElement("ColumnInfo");

            columnElement.SetAttribute("Index", rootNode.ChildNodes.Count.ToString());
            columnElement.SetAttribute("SortIndex", sortingIndex.ToString());
            columnElement.SetAttribute("Width", "100");
            columnElement.SetAttribute("Grouped", "false");

            rootNode.AppendChild(columnElement);

            if (sortingIndex >= 0)
            {
                columnElement.SetAttribute("Sorted", "true");
            }
            else
            {
                columnElement.SetAttribute("Sorted", "false");
            }

            columnElement.SetAttribute("IsSortable", "true");

            if (isVisible)
            {
                columnElement.SetAttribute("Visible", "true");
            }
            else
            {
                columnElement.SetAttribute("Visible", "false");
            }

            columnElement.SetAttribute("SortOrder", sortOrder);

            XmlElement nameElement = rootNode.OwnerDocument.CreateElement("Name");
            XmlElement idElement = rootNode.OwnerDocument.CreateElement("Id");

            columnElement.AppendChild(nameElement);
            columnElement.AppendChild(idElement);

            nameElement.InnerText = columnName;
            idElement.InnerText = columnId;
        }
    }
}
Imports System
Imports System.Collections.Generic
Imports System.Collections.ObjectModel
Imports System.Text
Imports System.Xml
Imports Microsoft.EnterpriseManagement
Imports Microsoft.EnterpriseManagement.Configuration
Imports Microsoft.EnterpriseManagement.Monitoring


Namespace SDKSamples
    Class Program
        Public Overloads Shared Function Main(ByVal args() As String) As Integer

            Dim mg As ManagementGroup = New ManagementGroup("localhost")
            Dim mp As ManagementPack
            Dim stateView As ManagementPackView
            Dim diagramView As ManagementPackView
            Dim viewFolder As ManagementPackFolder
            Dim folderId As String

            mp = GetDefaultMP(mg)

            folderId = "ViewSamples.SampleFolder" & Guid.NewGuid().ToString("N")

            viewFolder = CreateViewFolder(mp, mg, "Sample Folder", folderId)

            stateView = CreateStateView(mp, mg, viewFolder)
            diagramView = CreateDiagramView(mp, mg, viewFolder)

            mp.AcceptChanges()
        End Function

        ' ---------------------------------------------------------------------
        Private Shared Function GetDefaultMP( _
            ByVal mg As ManagementGroup) As ManagementPack

            Dim managementPacks As ReadOnlyCollection(Of ManagementPack)

            managementPacks = mg.GetManagementPacks("Microsoft.SystemCenter.OperationsManager.DefaultUser")

            If (managementPacks.Count <> 1) Then

                Throw New ApplicationException("Failed to retrieve the default mp")
            End If

            Return managementPacks(0)
        End Function

        ' ---------------------------------------------------------------------
        Private Shared Function CreateDiagramView( _
            ByVal mp As ManagementPack, _
            ByVal mg As ManagementGroup, _
            ByVal viewFolder As ManagementPackFolder) As ManagementPackView

            Dim stateViewTypes As ReadOnlyCollection(Of MonitoringViewType)
            Dim sql2005DBClasses As ReadOnlyCollection(Of MonitoringClass)
            Dim view As ManagementPackView
            Dim viewConfigDoc As XmlDocument

            stateViewTypes = mg.GetMonitoringViewTypes("Microsoft.SystemCenter.DiagramViewType")

            If (stateViewTypes.Count <> 1) Then
                Throw New ApplicationException("Failed to load the state view type")
            End If

            view = New ManagementPackView(mp, "ViewsSample.DiagramView1", ManagementPackAccessibility.Public)

            view.TypeID = stateViewTypes(0)
            view.DisplayName = "Sample Diagram View"

            sql2005DBClasses = mg.GetMonitoringClasses(New MonitoringClassCriteria("DisplayName='SQL 2005 DB'"))

            If (sql2005DBClasses.Count <> 1) Then
                Throw New ApplicationException("Failed to retrieve the SQL 2005 DB class. Make sure the SQL 2005 MP is imported")
            End If

            view.Target = sql2005DBClasses(0)
            view.Category = "Operations"

            viewConfigDoc = CreateDiagramViewConfigDoc(mg, sql2005DBClasses(0))

            view.Configuration = viewConfigDoc.ChildNodes(0).InnerXml

            Dim folderItem As ManagementPackFolderItem = New ManagementPackFolderItem(view, viewFolder)

            Return view
        End Function

        ' ---------------------------------------------------------------------
        Private Shared Function CreateDiagramViewConfigDoc( _
            ByVal mg As ManagementGroup, _
            ByVal monitoringClass As MonitoringClass) As XmlDocument

            Dim configDoc As XmlDocument = New XmlDocument()
            Dim rootNode As XmlElement = configDoc.CreateElement("Configuration")
            Dim presentationNode As XmlElement = configDoc.CreateElement("Presentation")
            Dim criteriaNode As XmlElement = configDoc.CreateElement("Criteria")

            configDoc.AppendChild(rootNode)
            rootNode.AppendChild(criteriaNode)
            rootNode.AppendChild(presentationNode)

            presentationNode.InnerXml = "<DiagramViewCriteria>" & _
                                            "<DiagramViewDisplay>" & _
                                             "<NodesPerRow>3</NodesPerRow>" & _
                                              "<ContainmentLine>" & _
                                                "<Color />" & _
                                                "<SourceArrow IsFilled=""false"">" & _
                                                  "<ArrowStyle>NoAnchor</ArrowStyle>" & _
                                                "</SourceArrow>" & _
                                                "<TargetArrow IsFilled=""false"">" & _
                                                  "<ArrowStyle>ArrowAnchor</ArrowStyle>" & _
                                                "</TargetArrow>" & _
                                              "</ContainmentLine>" & _
                                              "<NonContainmentLine>" & _
                                                "<Color Red=""6"" Green=""134"" Blue=""253"" />" & _
                                                "<SourceArrow IsFilled=""false"">" & _
                                                  "<ArrowStyle>NoAnchor</ArrowStyle>" & _
                                                "</SourceArrow>" & _
                                                "<TargetArrow IsFilled=""false"">" & _
                                                  "<ArrowStyle>ArrowAnchor</ArrowStyle>" & _
                                                "</TargetArrow>" & _
                                                "<LineStyle>DashDotDot</LineStyle>" & _
                                              "</NonContainmentLine>" & _
                                            "</DiagramViewDisplay>" & _
                                          "</DiagramViewCriteria>"

            Dim target As XmlElement = configDoc.CreateElement("Target")

            Dim dbMonitoringObjects As ReadOnlyCollection(Of PartialMonitoringObject)

            dbMonitoringObjects = mg.GetPartialMonitoringObjects(monitoringClass)

            If (dbMonitoringObjects.Count < 1) Then
                Throw New ApplicationException("At least one database object is required")
            End If

            target.InnerText = dbMonitoringObjects(0).Id.ToString()

            rootNode.AppendChild(target)

            Return configDoc
        End Function

        ' ---------------------------------------------------------------------
        Private Shared Function CreateStateView( _
            ByVal mp As ManagementPack, _
            ByVal mg As ManagementGroup, _
            ByVal viewFolder As ManagementPackFolder) As ManagementPackView

            Dim stateViewTypes As ReadOnlyCollection(Of MonitoringViewType)
            Dim sql2005DBClasses As ReadOnlyCollection(Of MonitoringClass)
            Dim view As ManagementPackView
            Dim viewConfigDoc As XmlDocument

            stateViewTypes = mg.GetMonitoringViewTypes("Microsoft.SystemCenter.StateViewType")

            If (stateViewTypes.Count <> 1) Then
                Throw New ApplicationException("Failed to load the state view type")
            End If

            view = New ManagementPackView( _
                mp, "ViewsSample.SampleStateView2", ManagementPackAccessibility.Public)

            view.TypeID = stateViewTypes(0)
            view.DisplayName = "Sample State View2"

            sql2005DBClasses = mg.GetMonitoringClasses( _
                New MonitoringClassCriteria("DisplayName='SQL 2005 DB'"))

            If (sql2005DBClasses.Count <> 1) Then
                Throw New ApplicationException("Failed to retrieve the SQL 2005 DB class. Make sure the SQL 2005 MP is imported")
            End If

            view.Target = sql2005DBClasses(0)
            view.Category = "Operations"

            viewConfigDoc = CreateViewConfigDoc(sql2005DBClasses(0))

            view.Configuration = viewConfigDoc.ChildNodes(0).InnerXml

            Dim folderItem As ManagementPackFolderItem = New ManagementPackFolderItem(view, viewFolder)

            Return view
        End Function

        ' ---------------------------------------------------------------------
        Private Shared Function CreateViewConfigDoc( _
                    ByVal monitoringClass As MonitoringClass) As XmlDocument

            Dim configDoc As XmlDocument = New XmlDocument()
            Dim rootNode As XmlElement = configDoc.CreateElement("Configuration")
            Dim presentationNode As XmlElement = configDoc.CreateElement("Presentation")
            Dim criteriaNode As XmlElement = configDoc.CreateElement("Criteria")

            Dim maintenanceModeProp As XmlElement = configDoc.CreateElement("InMaintenanceMode")

            maintenanceModeProp.InnerText = "false"

            criteriaNode.AppendChild(maintenanceModeProp)

            configDoc.AppendChild(rootNode)
            rootNode.AppendChild(criteriaNode)
            rootNode.AppendChild(presentationNode)

            CreateColumnXmlNode(presentationNode, 0, "State", monitoringClass.Name, "Descending", True)
            CreateColumnXmlNode(presentationNode, -1, "Maintenance Mode", "InMaintenanceMode", "Ascending", True)
            CreateColumnXmlNode(presentationNode, -1, "Name", "Name", "Ascending", True)
            CreateColumnXmlNode(presentationNode, -1, "Path", "Path", "Ascending", False)

            Dim properties As ReadOnlyCollection(Of MonitoringClassProperty)

            properties = monitoringClass.GetMonitoringProperties(BaseClassTraversalDepth.Recursive)

            For Each prop As MonitoringClassProperty In properties
                CreateColumnXmlNode( _
                    presentationNode, -1, prop.DisplayName, prop.Name, "Ascending", True)
            Next

            Return configDoc
        End Function

        ' ---------------------------------------------------------------------
        Private Shared Function CreateViewFolder(ByVal mp As ManagementPack, _
            ByVal mg As ManagementGroup, _
            ByVal folderDisplayName As String, _
            ByVal folderId As String) As ManagementPackFolder

            Dim newFolder As ManagementPackFolder
            Dim parentFolders As ReadOnlyCollection(Of MonitoringFolder)

            parentFolders = mg.GetMonitoringFolders(New MonitoringFolderCriteria("Name='Microsoft.SystemCenter.Monitoring.ViewFolder.Root'"))

            If parentFolders.Count <> 1 Then
                Throw New ApplicationException("Failed to retrieve the root folder")
            End If

            newFolder = New ManagementPackFolder(mp, folderId, ManagementPackAccessibility.Public)

            newFolder.DisplayName = folderDisplayName

            newFolder.ParentFolder = CType(parentFolders(0), ManagementPackFolder)

            Dim folderItem As New ManagementPackFolderItem(newFolder, parentFolders(0))

            mp.AcceptChanges()

            Return newFolder
        End Function


        ' ---------------------------------------------------------------------
        Shared Sub CreateColumnXmlNode(ByVal rootNode As XmlElement, _
        ByVal sortingIndex As Integer, _
        ByVal columnName As String, _
        ByVal columnId As String, _
        ByVal sortOrder As String, _
        ByVal isVisible As Boolean)

            Dim columnElement As XmlElement = rootNode.OwnerDocument.CreateElement("ColumnInfo")

            columnElement.SetAttribute("Index", rootNode.ChildNodes.Count.ToString())
            columnElement.SetAttribute("SortIndex", sortingIndex.ToString())
            columnElement.SetAttribute("Width", "100")
            columnElement.SetAttribute("Grouped", "false")

            rootNode.AppendChild(columnElement)


            If (sortingIndex >= 0) Then
                columnElement.SetAttribute("Sorted", "true")
            Else
                columnElement.SetAttribute("Sorted", "false")
            End If

            columnElement.SetAttribute("IsSortable", "true")

            If (isVisible) Then
                columnElement.SetAttribute("Visible", "true")
            Else
                columnElement.SetAttribute("Visible", "false")
            End If

            columnElement.SetAttribute("SortOrder", sortOrder)

            Dim nameElement As XmlElement = rootNode.OwnerDocument.CreateElement("Name")
            Dim idElement As XmlElement = rootNode.OwnerDocument.CreateElement("Id")

            columnElement.AppendChild(nameElement)
            columnElement.AppendChild(idElement)

            nameElement.InnerText = columnName
            idElement.InnerText = columnId
        End Sub
    End Class
End Namespace

See Also

Other Resources

Automating Operations Manager Administration