Udostępnij za pośrednictwem


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

Applies To: System Center 2012 - Operations Manager

[This topic is pre-release documentation and is subject to change in future releases. Blank topics are included as placeholders.]

In Operations Manager, 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;
using Microsoft.EnterpriseManagement.Common;

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
            )
        {
            IList<ManagementPack> managementPacks;

            managementPacks = mg.ManagementPacks.GetManagementPacks(new ManagementPackCriteria("ID='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
            )
        {
            IList<ManagementPackViewType> stateViewTypes;
            IList<ManagementPackClass> sql2005DBClasses;
            ManagementPackView view;
            XmlDocument viewConfigDoc;

            stateViewTypes = mg.Presentation.GetViewTypes(new ManagementPackViewTypeCriteria("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.EntityTypes.GetClasses(new ManagementPackClassCriteria("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,
            ManagementPackClass 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");

            List<PartialMonitoringObject> targets = new List<PartialMonitoringObject>();
            IObjectReader<PartialMonitoringObject> reader = mg.EntityObjects.GetObjectReader<PartialMonitoringObject>(monitoringClass, ObjectQueryOptions.Default);
            targets.AddRange(reader);
            if (targets.Count < 1)
                throw new ApplicationException("At least one database object is required");

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

            rootNode.AppendChild(target);

            return (configDoc);
        }

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

            stateViewTypes = mg.Presentation.GetViewTypes(new ManagementPackViewTypeCriteria("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.EntityTypes.GetClasses(
                new ManagementPackClassCriteria("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(
            ManagementPackClass 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);

            IList<ManagementPackProperty> properties = monitoringClass.GetProperties(BaseClassTraversalDepth.Recursive);

            foreach (ManagementPackProperty 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;
            IList<ManagementPackFolder> parentFolders;

            parentFolders = mg.Presentation.GetFolders(
                new ManagementPackFolderCriteria("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;
        }
    }
}

See Also

Other Resources

Automating Operations Manager Administration