Creating a performance collection rule using the SDK

Here is a sample that demonstrates how to create a performance collection rule using the SDK. The only caviat is that this rule will not work for agentless monitoring. A powershell version will follow soon.

using System;

using System.Collections.Generic;

using System.Text;

using Microsoft.EnterpriseManagement;

using Microsoft.EnterpriseManagement.Configuration;

namespace RuleCreationSample

{

class Program

{

static void Main(string[] args)

{

ManagementGroup mg;

ManagementPack mp;

ManagementPackRule rule;

mg = new ManagementGroup("localhost");

mp = mg.GetManagementPacks("Test")[0];

rule = new ManagementPackRule(mp, "SamplePerformanceCollectionRule");

CreateDataSourceModule(rule, mg);

CreateWriteActionModules(rule, mg);

rule.Target = mg.GetMonitoringClasses("Microsoft.Windows.Server.2003.OperatingSystem")[0];

rule.Category = ManagementPackCategoryType.PerformanceCollection;

rule.DisplayName = "Sample Performance Counter collection rule";

rule.Description = "This rule was created by a sample that demonstrates how to create a performance collection rule using the SDK";

mp.AcceptChanges();

}

//---------------------------------------------------------------------

private static void CreateWriteActionModules(

ManagementPackRule rule,

ManagementGroup mg

)

{

ManagementPackModuleType dbWriteActionModuleType;

ManagementPackModuleType dwWriteActionModuleType;

ManagementPackWriteActionModule dbWriteActionModule;

ManagementPackWriteActionModule dwWriteActionModule;

dbWriteActionModuleType = mg.GetMonitoringModuleTypes("Microsoft.SystemCenter.CollectPerformanceData")[0];

dwWriteActionModuleType = mg.GetMonitoringModuleTypes("Microsoft.SystemCenter.DataWarehouse.PublishPerformanceData")[0];

dbWriteActionModule = new ManagementPackWriteActionModule(rule, "WriteToDB");

dwWriteActionModule = new ManagementPackWriteActionModule(rule, "WriteToDW");

dbWriteActionModule.TypeID = (ManagementPackWriteActionModuleType)dbWriteActionModuleType;

dwWriteActionModule.TypeID = (ManagementPackWriteActionModuleType)dwWriteActionModuleType;

rule.WriteActionCollection.Add(dbWriteActionModule);

rule.WriteActionCollection.Add(dwWriteActionModule);

}

//---------------------------------------------------------------------

private static void CreateDataSourceModule(

ManagementPackRule rule,

ManagementGroup mg

)

{

ManagementPackModuleType dsModuleType;

ManagementPackDataSourceModule dsModule;

dsModuleType = mg.GetMonitoringModuleTypes("System.Performance.DataProvider")[0];

dsModule = new ManagementPackDataSourceModule(rule, "DS");

dsModule.TypeID = (ManagementPackDataSourceModuleType)dsModuleType;

dsModule.Configuration = CreateConfig("% Processor Time", "_Total", "Processor", 10);

rule.DataSourceCollection.Add(dsModule);

}

//---------------------------------------------------------------------

static string CreateConfig(

string counterName,

string instanceName,

string objectName,

int frequencyInSeconds

)

{

StringBuilder builder = new StringBuilder();

builder.Append("<ComputerName></ComputerName>");

builder.AppendFormat("<CounterName>{0}</CounterName>", counterName);

builder.AppendFormat("<ObjectName>{0}</ObjectName>", objectName);

builder.AppendFormat("<InstanceName>{0}</InstanceName>", instanceName);

builder.Append("<AllInstances>false</AllInstances>");

builder.AppendFormat("<Frequency>{0}</Frequency>", frequencyInSeconds);

return (builder.ToString());

}

}

}

Comments

  • Anonymous
    November 01, 2007
    PingBack from http://msdnrss.thecoderblogs.com/2007/11/01/creating-a-performance-collection-rule-using-the-sdk/
  • Anonymous
    February 22, 2008
    What do I do with this? Is there an import function or a way to add more than one performance counter? Thank you.
  • Anonymous
    February 22, 2008
    How do I call or use this?
  • Anonymous
    February 22, 2008
    This is an SDK sample of how you can create performance collection rules. In order to use this sample, you need to compile the code and run it. I am hoping to post soon a PS sample which you could invoke using PowerShell.
  • Anonymous
    February 25, 2008
    I guess I'm way behind the curve on this. Once I compile the code how do I get it into the management console? Or for a lack of a better term How do I use it?Do you know of a book or guide that starts off at square one on how to code in C# and/or XML for System Centerp Operations Manager? Kind of like The SDK for Dummies.Thanks again for your blog and help.