locked
How to create a mp with SCOM 2012 SDK RRS feed

  • Question

  • Hi, guys.

         I start with this sample "http://msdn.microsoft.com/en-us/library/hh329038.aspx" .Obviously, is not much different from 2007 sdk sample.

    The compile is successful. But when I debug it, it breaks with error "Failed to validate item: SampleMP.RouterHostsPort". I have no idea about what happen. Error occurs when add relationship into the mp.

    public void AddRelationships()
            {
                ManagementPackRelationship routerHostsPort;
               
                routerHostsPort = new ManagementPackRelationship(m_mp,
                                                                 "SampleMP.RouterHostsPort",
                                                                 ManagementPackAccessibility.Public);
                routerHostsPort.Abstract    = false;
                routerHostsPort.Base        = m_libraryMp.GetRelationship("System.Hosting");
                routerHostsPort.Description = "Defines the hosting relationship between a router and its ports";
                routerHostsPort.DisplayName = "Router Hosts Port";
                routerHostsPort.Source = new ManagementPackRelationshipEndpoint(m_routerClass, "Source");
                routerHostsPort.Target = new ManagementPackRelationshipEndpoint(m_portClass, "Target"); ;

                routerHostsPort.Status = ManagementPackElementStatus.PendingAdd;
            }

    Tuesday, October 30, 2012 6:38 AM

All replies

  • Can you provide full details (stack trace, etc.) of the exception you're getting from the SDK?

    Marcin Jastrzębski http://blogs.technet.com/marcin_jastrzebski

    • Proposed as answer by Nicholas Li Tuesday, November 6, 2012 8:21 AM
    Thursday, November 1, 2012 5:43 PM
  • Hi,

    just stumbled across this thread as I wanted to check if they have fixed the documentation yet.
    The issue I think is that they pretty much ran a copy&paste from the old documentation and then adjusted the code samples by simply replacing the obsolete functions, not verifying the code functionality.

    Anyway, what's missing in the documentation is that you have to provide the type for source and target and the constructor has to use the Relationship.

     routerHostsPort = new ManagementPackRelationship(m_mp,
                                                                 "SampleMP.RouterHostsPort",
                                                                 ManagementPackAccessibility.Public);
                routerHostsPort.Abstract = false;
                routerHostsPort.Base = m_libraryMp.GetRelationship("System.Hosting");
                routerHostsPort.Description = "Defines the hosting relationship between a router and its ports";
                routerHostsPort.DisplayName = "Router Hosts Port";
                routerHostsPort.Source = new ManagementPackRelationshipEndpoint(routerHostsPort, "Source");
                routerHostsPort.Source.Type = (ManagementPackElementReference<ManagementPackClass>)m_routerClass;
                routerHostsPort.Target = new ManagementPackRelationshipEndpoint(routerHostsPort, "Target");
                routerHostsPort.Target.Type = (ManagementPackElementReference<ManagementPackClass>)m_portClass;

                routerHostsPort.Status = ManagementPackElementStatus.PendingAdd;
    • Edited by AndreasZuckerhut Wednesday, December 12, 2012 2:42 PM
    • Proposed as answer by Hebbarab Monday, April 15, 2013 8:13 AM
    Wednesday, December 12, 2012 2:31 PM
  • I got this working too from your post.. thanks for that.. I've included two necessary things... One is where the new 2012 DLL's are and the other is the complete code.

    Read this first: http://msdn.microsoft.com/en-us/library/hh352630.aspx


    - Chris Temple

    Wednesday, April 23, 2014 6:16 PM
  • using System;
    
    
    using System.Collections.Generic;
    
    
    using System.Text;
    
    
    using Microsoft.EnterpriseManagement;
    
    
    using Microsoft.EnterpriseManagement.Common;
    
    
    using Microsoft.EnterpriseManagement.Configuration;
    
    
    using Microsoft.EnterpriseManagement.Configuration.IO;
    
    
    using Microsoft.EnterpriseManagement.Monitoring;
    
    
    
    namespace
    
    
     MPCreation
    
    {
    
        
    
    class Program
    
    
        {
    
            
    
    static void Main(string[] args)
    
            {
    
                
    
    MPCreator mpCreator = new MPCreator();
    
                
    
                mpCreator.AddReferences();
    
                mpCreator.AddClasses();
    
                mpCreator.AddRelationships();
    
                mpCreator.AddViews();
    
                mpCreator.AddTasks();
    
                mpCreator.AddRules();
    
                mpCreator.AddMonitorType();
    
                mpCreator.SaveMPToDisk(
    
    @"C:\mp\BluestripeManagedCode");
    
            }
    
        }
    
        
    
    class MPCreator
    
    
        {
    
            
    
    ManagementPack m_mp;
    
            
    
            
    
    ManagementPack m_libraryMp;
    
            Microsoft.EnterpriseManagement.Configuration.
    
            
    
    ManagementPack m_windowsLibraryMp;
    
            
    
    ManagementPack m_healthLibraryMp;
    
            
    
    ManagementPackFileStore m_mpStore;
    
            
    
    ManagementPackClass m_routerClass;
    
            
    
    ManagementPackClass m_portClass;
    
            
    
    ManagementPackUnitMonitorType m_unitMonitorType;
    
            
    
    //---------------------------------------------------------------------
    
    
            
    
    public MPCreator()
    
            {
    
                m_mpStore = 
    
    new ManagementPackFileStore();
    
                m_mpStore.AddDirectory(
    
    @"C:\mp\MPFiles2012");
    
                m_mp = 
    
    new ManagementPack("SampleMP", "Sample Management Pack", new Version(1, 0), m_mpStore);
    
                m_libraryMp = 
    
    new ManagementPack(@"C:\mp\MPFiles2012\System.Library.mp", m_mpStore);
    
                m_windowsLibraryMp = 
    
    new ManagementPack(@"C:\mp\MPFiles2012\Microsoft.SystemCenter.Library.mp", m_mpStore);
    
                m_healthLibraryMp = 
    
    new ManagementPack(@"C:\mp\MPFiles2012\System.Health.Library.mp", m_mpStore);
    
                m_mp.Description = 
    
    "Sample Description";
    
                m_mp.DisplayName = 
    
    "Sample Management Pack";
    
                m_mp.DefaultLanguageCode = 
    
    "ENU";
    
            }
    
            
    
    //---------------------------------------------------------------------
    
    
            
    
    public void AddClasses()
    
            {
    
                m_routerClass = 
    
    new ManagementPackClass(m_mp,
    
                                                        
    
    "SampleMP.Router",
    
                                                        
    
    ManagementPackAccessibility.Public);
    
                m_portClass = 
    
    new ManagementPackClass(m_mp,
    
                                                      
    
    "SampleMP.Port",
    
                                                      
    
    ManagementPackAccessibility.Public);
    
                m_routerClass.Abstract = 
    
    false;
    
                m_routerClass.Base = m_libraryMp.GetClass(
    
    "System.NetworkDevice");
    
                m_portClass.Abstract = 
    
    false;
    
                m_portClass.Hosted = 
    
    true;
    
                m_portClass.Base = m_libraryMp.GetClass(
    
    "System.LogicalEntity");
    
                AddRouterClassProperties();
    
                AddPortClassProperties();
    
            }
    
            
    
    //---------------------------------------------------------------------
    
    
            
    
    public void AddRelationships()
    
            {
    
                
    
    ManagementPackRelationship routerHostsPort;
    
                routerHostsPort = 
    
    new ManagementPackRelationship(m_mp,
    
                                                                 
    
    "SampleMP.RouterHostsPort",
    
                                                                 
    
    ManagementPackAccessibility.Public);
    
                routerHostsPort.Abstract = 
    
    false;
    
                routerHostsPort.Base = m_libraryMp.GetRelationship(
    
    "System.Hosting");
    
                routerHostsPort.Description = 
    
    "Defines the hosting relationship between a router and its ports";
    
                routerHostsPort.DisplayName = 
    
    "Router Hosts Port";
    
                routerHostsPort.Source = 
    
    new ManagementPackRelationshipEndpoint(m_routerClass, "Source");
    
                routerHostsPort.Target = 
    
    new ManagementPackRelationshipEndpoint(m_portClass, "Target");
    
                routerHostsPort.Source.Type = (
    
    ManagementPackElementReference<ManagementPackClass>)m_routerClass;
    
                routerHostsPort.Target = 
    
    new ManagementPackRelationshipEndpoint(routerHostsPort, "Target");
    
                routerHostsPort.Target.Type = (
    
    ManagementPackElementReference<ManagementPackClass>)m_portClass;
    
     
    
                routerHostsPort.Status = 
    
    ManagementPackElementStatus.PendingAdd;
    
            }
    
            
    
    //---------------------------------------------------------------------
    
    
            
    
    public void AddReferences()
    
            {
    
                m_mp.References.Add(
    
    "SCLibrary", new ManagementPackReference(m_libraryMp));
    
                m_mp.References.Add(
    
    "SCWinLibrary", new ManagementPackReference(m_windowsLibraryMp));
    
                m_mp.References.Add(
    
    "SCHealth", new ManagementPackReference(m_healthLibraryMp));
    
            }
    
            
    
    //---------------------------------------------------------------------
    
    
            
    
    public void AddViews()
    
            {
    
                
    
    ManagementPackFolderItem routerStateFolderItem;
    
                
    
    ManagementPackFolder viewFolder;
    
                
    
    ManagementPackView routerStateView;
    
                routerStateView = 
    
    new ManagementPackView(m_mp, "SampleMP.RouterStateView", ManagementPackAccessibility.Public);
    
                viewFolder = 
    
    new ManagementPackFolder(m_mp, "SampleMP.ViewFolder", ManagementPackAccessibility.Public);
    
                viewFolder.Description = 
    
    "Sample MP View Folder";
    
                viewFolder.DisplayName = 
    
    "Sample MP";
    
                viewFolder.Status = 
    
    ManagementPackElementStatus.PendingAdd;
    
                viewFolder.ParentFolder = m_windowsLibraryMp.GetFolder(
    
    "Microsoft.SystemCenter.Monitoring.ViewFolder.Root");
    
                routerStateView.Target = m_routerClass;
    
                routerStateView.TypeID = m_windowsLibraryMp.GetViewType(
    
    "Microsoft.SystemCenter.StateViewType");
    
                routerStateView.Description = 
    
    "Router State View";
    
                routerStateView.DisplayName = 
    
    "Router State View";
    
                routerStateView.Category = 
    
    "Operations";
    
                routerStateView.Configuration = 
    
    @"<Criteria><InMaintenanceMode>false</InMaintenanceMode></Criteria>";
    
                routerStateFolderItem = 
    
    new ManagementPackFolderItem(routerStateView, viewFolder);
    
                routerStateFolderItem.Status = 
    
    ManagementPackElementStatus.PendingAdd;
    
                routerStateView.Status = 
    
    ManagementPackElementStatus.PendingAdd;
    
            }
    
            
    
    //---------------------------------------------------------------------
    
    
            
    
    public void AddTasks()
    
            {
    
                
    
    ManagementPackTask task;
    
                
    
    ManagementPackProbeActionModule probeAction;
    
                task = 
    
    new ManagementPackTask(m_mp, "ResetRouter", ManagementPackAccessibility.Public);
    
                probeAction = 
    
    new ManagementPackProbeActionModule(task, "ResetRouterProbe");
    
                task.Description = 
    
    "Resets a router";
    
                task.DisplayName = 
    
    "Reset Router";
    
                task.Target = m_routerClass;
    
                task.Status = 
    
    ManagementPackElementStatus.PendingAdd;
    
                task.ProbeAction = probeAction;
    
                probeAction.TypeID = (
    
    ManagementPackProbeActionModuleType)m_libraryMp.GetModuleType("System.CommandExecuterProbe");
    
                probeAction.Configuration = 
    
    @"<ApplicationName>%WINDIR%\System32\NET.EXE</ApplicationName><WorkingDirectory/><CommandLine>USER</CommandLine><TimeoutSeconds>30</TimeoutSeconds><RequireOutput>true</RequireOutput><Files/>";
    
            }
    
            
    
    public void AddRules()
    
            {
    
                
    
    ManagementPackDataSourceModule sdkEventDS;
    
                
    
    ManagementPackWriteActionModule writeEventToDBMWA;
    
                
    
    ManagementPackConditionDetectionModule conditionDetection;
    
                
    
    ManagementPackRule rule;
    
                rule = 
    
    new ManagementPackRule(m_mp, "CollectEventsRule");
    
                writeEventToDBMWA = 
    
    new ManagementPackWriteActionModule(rule, "WriteToDB");
    
                writeEventToDBMWA.TypeID = (
    
    ManagementPackWriteActionModuleType)m_windowsLibraryMp.GetModuleType("Microsoft.SystemCenter.CollectEvent");
    
                writeEventToDBMWA.Description = 
    
    "Write Event to DB";
    
                writeEventToDBMWA.DisplayName = 
    
    "Write Event to DB";
    
                sdkEventDS = 
    
    new ManagementPackDataSourceModule(rule, "CollectSDKEvent");
    
                sdkEventDS.TypeID = (
    
    ManagementPackDataSourceModuleType)m_windowsLibraryMp.GetModuleType("Microsoft.SystemCenter.SdkEventProvider");
    
                sdkEventDS.DisplayName = 
    
    "Collect SDK Event";
    
                sdkEventDS.Description = 
    
    "Collect SDK Event";
    
                conditionDetection = 
    
    new ManagementPackConditionDetectionModule(rule, "EventConditionDetection");
    
                conditionDetection.TypeID = (
    
    ManagementPackConditionDetectionModuleType)m_libraryMp.GetModuleType("System.ExpressionFilter");
    
                conditionDetection.Configuration = 
    
    @"<Expression><SimpleExpression><ValueExpression><XPathQuery>EventNumber</XPathQuery></ValueExpression><Operator>Equal</Operator><ValueExpression><Value>1</Value></ValueExpression></SimpleExpression></Expression>";
    
                rule.ConfirmDelivery = 
    
    true;
    
                rule.Description = 
    
    "Collect Events Rule";
    
                rule.DisplayName = 
    
    "Collect Router Events";
    
                rule.Priority = 
    
    ManagementPackWorkflowPriority.Normal;
    
                rule.Target = m_routerClass;
    
                rule.DataSourceCollection.Add(sdkEventDS);
    
                rule.WriteActionCollection.Add(writeEventToDBMWA);
    
                rule.ConditionDetection = conditionDetection;
    
                rule.Status = 
    
    ManagementPackElementStatus.PendingAdd;
    
            }
    
            
    
    //---------------------------------------------------------------------
    
    
            
    
    private void AddRouterClassProperties()
    
            {
    
                AddPropertyToClass(
    
    "Id",
    
                                   
    
    "Id",
    
                                   
    
    "Contains the Id of the router",
    
                                   
    
    true,
    
                                   
    
    false,
    
                                   1,
    
                                   
    
    ManagementPackEntityPropertyTypes.@string,
    
                                   m_routerClass);
    
                AddPropertyToClass(
    
    "Name",
    
                                   
    
    "Name",
    
                                   
    
    "Contains the name of the router",
    
                                   
    
    false,
    
                                   
    
    false,
    
                                   1,
    
                                   
    
    ManagementPackEntityPropertyTypes.@string,
    
                                   m_routerClass);
    
                AddPropertyToClass(
    
    "ModelNumber",
    
                                   
    
    "Model Number",
    
                                   
    
    "Contains the model number of the router",
    
                                   
    
    false,
    
                                   
    
    false,
    
                                   1,
    
                                   
    
    ManagementPackEntityPropertyTypes.@string,
    
                                   m_routerClass);
    
                m_routerClass.Status = 
    
    ManagementPackElementStatus.PendingAdd;
    
            }
    
            
    
    //---------------------------------------------------------------------
    
    
            
    
    private void AddPortClassProperties()
    
            {
    
                AddPropertyToClass(
    
    "Id",
    
                                   
    
    "Id",
    
                                   
    
    "Contains the Id of the port",
    
                                   
    
    true,
    
                                   
    
    false,
    
                                   1,
    
                                   
    
    ManagementPackEntityPropertyTypes.@string,
    
                                   m_portClass);
    
                AddPropertyToClass(
    
    "PortType",
    
                                   
    
    "Port Type",
    
                                   
    
    "Contains the type of the port",
    
                                   
    
    false,
    
                                   
    
    false,
    
                                   1,
    
                                   
    
    ManagementPackEntityPropertyTypes.@string,
    
                                   m_portClass);
    
                AddPropertyToClass(
    
    "PortStatus",
    
                                   
    
    "Port Status",
    
                                   
    
    "Determines whether the port is on or off",
    
                                   
    
    false,
    
                                   
    
    false,
    
                                   1,
    
                                   
    
    ManagementPackEntityPropertyTypes.@string,
    
                                   m_portClass);
    
                m_portClass.Status = 
    
    ManagementPackElementStatus.PendingAdd;
    
            }
    
            
    
    //---------------------------------------------------------------------
    
    
            
    
    internal void SaveMPToDisk(string directoryName)
    
            {
    
                
    
    ManagementPackXmlWriter mpXmlWriter = new ManagementPackXmlWriter(directoryName);
    
                m_mp.AcceptChanges();
    
                mpXmlWriter.WriteManagementPack(m_mp);
    
                
    
    Console.WriteLine("Management pack created in the " + directoryName + m_mp.Name + ".xml file.");
    
            }
    
            
    
    //---------------------------------------------------------------------
    
    
            
    
    private void AddPropertyToClass(
    
                
    
    string name,
    
                
    
    string displayName,
    
                
    
    string description,
    
                
    
    bool isKey,
    
                
    
    bool isCaseSensitive,
    
                
    
    int minLength,
    
                
    
    ManagementPackEntityPropertyTypes type,
    
                
    
    ManagementPackClass mpClass
    
                )
    
            {
    
                
    
    ManagementPackProperty property = new ManagementPackProperty(mpClass, name);
    
                property.CaseSensitive = isCaseSensitive;
    
                property.Description = description;
    
                property.Type = type;
    
                property.MinLength = minLength;
    
                property.Key = isKey;
    
                property.DisplayName = displayName;
    
                mpClass.PropertyCollection.Add(property);
    
            }
    
            
    
    internal void AddMonitorType()
    
            {
    
                
    
    ManagementPackConfigurationSchemaType monitorSchemaType = new ManagementPackConfigurationSchemaType();
    
                
    
    ManagementPackModuleTypeReference dsRef;
    
                
    
    ManagementPackModuleTypeReference conditionDetectionUnderThresholdRef;
    
                
    
    ManagementPackModuleTypeReference conditionDetectionOverThresholdRef;
    
                
    
    ManagementPackMonitorTypeState underThresholdState;
    
                
    
    ManagementPackMonitorTypeState overThresholdState;
    
                
    
    ManagementPackMonitorTypeDetection underThresholdDetection;
    
                
    
    ManagementPackMonitorTypeDetection overThresholdDetection;
    
                
    
    ManagementPackModuleCompositionNodeType dataSourceNodeType;
    
                m_unitMonitorType = 
    
    new ManagementPackUnitMonitorType(m_mp, "SdkPerfDataUnitMonitor", ManagementPackAccessibility.Public);
    
                monitorSchemaType.Schema = 
    
    @"<xsd:element name=""CounterName"" type=""xsd:string"" /><xsd:element name=""ObjectName"" type=""xsd:string"" /><xsd:element name=""Threshold"" type=""xsd:string""/><xsd:element name=""InstanceName"" type=""xsd:string"" minOccurs=""0"" maxOccurs=""1"" />";
    
                m_unitMonitorType.Configuration = monitorSchemaType;
    
                dsRef = 
    
    new ManagementPackModuleTypeReference(m_unitMonitorType, "Datasource");
    
                dsRef.TypeID = m_windowsLibraryMp.GetModuleType(
    
    "Microsoft.SystemCenter.SdkPerformanceDataProvider");
    
                conditionDetectionUnderThresholdRef = 
    
    new ManagementPackModuleTypeReference(m_unitMonitorType, "UnderThresholdFilter");
    
                conditionDetectionUnderThresholdRef.TypeID = m_libraryMp.GetModuleType(
    
    "System.ExpressionFilter");
    
                conditionDetectionUnderThresholdRef.Configuration = 
    
    @"<Expression><SimpleExpression><ValueExpression><XPathQuery Type=""Double"">Value</XPathQuery></ValueExpression><Operator>LessEqual</Operator><ValueExpression><Value Type=""Double"">$Config/Threshold$</Value></ValueExpression></SimpleExpression></Expression>";
    
                conditionDetectionOverThresholdRef = 
    
    new ManagementPackModuleTypeReference(m_unitMonitorType, "OverThresholdFilter");
    
                conditionDetectionOverThresholdRef.TypeID = m_libraryMp.GetModuleType(
    
    "System.ExpressionFilter");
    
                conditionDetectionOverThresholdRef.Configuration = 
    
    @"<Expression><SimpleExpression><ValueExpression><XPathQuery Type=""Double"">Value</XPathQuery></ValueExpression><Operator>Greater</Operator><ValueExpression><Value Type=""Double"">$Config/Threshold$</Value></ValueExpression></SimpleExpression></Expression>";
    
                m_unitMonitorType.DataSourceCollection.Add(dsRef);
    
                m_unitMonitorType.ConditionDetectionCollection.Add(conditionDetectionUnderThresholdRef);
    
                m_unitMonitorType.ConditionDetectionCollection.Add(conditionDetectionOverThresholdRef);
    
                underThresholdState = 
    
    new ManagementPackMonitorTypeState(m_unitMonitorType, "UnderThreshold");
    
                overThresholdState = 
    
    new ManagementPackMonitorTypeState(m_unitMonitorType, "OverThreshold");
    
                m_unitMonitorType.MonitorTypeStateCollection.Add(underThresholdState);
    
                m_unitMonitorType.MonitorTypeStateCollection.Add(overThresholdState);
    
                underThresholdDetection = 
    
    new ManagementPackMonitorTypeDetection();
    
                overThresholdDetection = 
    
    new ManagementPackMonitorTypeDetection();
    
                underThresholdDetection.MonitorTypeStateID = 
    
    "UnderThreshold";
    
                overThresholdDetection.MonitorTypeStateID = 
    
    "OverThreshold";
    
                dataSourceNodeType = 
    
    new ManagementPackModuleCompositionNodeType();
    
                dataSourceNodeType.ID = 
    
    "Datasource";
    
                underThresholdDetection.Node.ID = 
    
    "UnderThresholdFilter";
    
                underThresholdDetection.Node.NodeCollection.Add(dataSourceNodeType);
    
                overThresholdDetection.Node.ID = 
    
    "OverThresholdFilter";
    
                overThresholdDetection.Node.NodeCollection.Add(dataSourceNodeType);
    
                m_unitMonitorType.RegularDetectionCollection.Add(underThresholdDetection);
    
                m_unitMonitorType.RegularDetectionCollection.Add(overThresholdDetection);
    
                m_unitMonitorType.Status = 
    
    ManagementPackElementStatus.PendingAdd;
    
            }
    
     
    
        }
    
    }
    
    


    - Chris Temple

    Wednesday, April 23, 2014 6:17 PM