This project is read-only.

Samples Code:

Properties Default Values  for ClusterManager:

Domain = Environment.UserDomainName
Machine = Environment.MachineName
UserName = Environment.UserName
Authentication = AuthenticationLevel.PacketPrivacy

Sample 1: Get all resources in cluster and show name and State using Cache options

            using (var cluster = new ClusterManager{Username = "UserName",Machine = "ClusterName"})
            {   
                cluster.Connect();
                //Using no Cacheobjects
                cluster.CacheInstances = false;
                var start = DateTime.Now.Ticks;
                foreach (var res in cluster.Resources.GetResources())
                {
                    Console.WriteLine("Resource: " + res.Name + " State: " + res.State);
                }
                var end = DateTime.Now.Ticks;
                Console.WriteLine();
                Console.WriteLine("elapsed time(ms) with nocache : " + new TimeSpan(end - start).TotalMilliseconds);
                Console.WriteLine();
                //at now the cluster manager read all objects and save in cache
                cluster.CacheInstances = true;
                start = DateTime.Now.Ticks;
                foreach (var res in cluster.Resources.GetResources())
                {
                    Console.WriteLine("Resource: " + res.Name + " State: " + res.State);
                }
                end = DateTime.Now.Ticks;
                Console.WriteLine();
                Console.WriteLine("elapsed time(ms) with cache : " + new TimeSpan(end - start).TotalMilliseconds);
            }
            Console.ReadKey();

            ResultSamples1 
Sample 2:  Gets specific group in cluster and show name and State
            using (var cluster = new ClusterManager{Username = "UserName",Machine = "ClusterName"})
            {   
                cluster.Connect();
                GroupProperty grp = cluster.Groups.GetGroup("GroupTest");
                Console.WriteLine("Group: " + grp.Name + " State: " + grp.State);
            }
            Console.ReadKey();
Sample 3:  Gets all resources of specific group in cluster and show name and State
            using (var cluster = new ClusterManager{Username = "UserName",Machine = "ClusterName"})
            {   
                cluster.Connect();
                ResourceProperty[] resgrp = cluster.Groups.GetResources("GroupTest");
                Console.WriteLine("Group: GroupTest");
                foreach (var res in resgrp)
                {
                    Console.WriteLine("  Resource: " + res.Name + " State: " + res.State);

                }
            }
            Console.ReadKey(); 
Sample 4:  BringOnline and TakeOffline  a specific resource in cluster
            using (var cluster = new ClusterManager{
                       Username = "UserName", Machine = "ClusterName", CacheInstances = true})
            {   
                cluster.Connect();
                cluster.Resources.TakeOffline("ResourceTest",3);
                Console.WriteLine("state resoruce: " + cluster.Resources.GetResource("ResourceTest").State);
                cluster.Resources.BringOnline("ResourceTest", 3);
                Console.WriteLine("state resoruce: " + cluster.Resources.GetResource("ResourceTest").State);
            }
            Console.ReadKey();
 Sample 5:  Move a specific resource to a new Group
            using (var cluster = new ClusterManager{Username = "UserName",Machine = "MYCLUSTER2003"})
            {   
                cluster.Connect();
                //if the group does not exist it will be created
                cluster.Resources.MoveToNewGroup("ResourceTest","NewGroup");
            }
            Console.ReadKey();
 Sample 6: Watcher Resources
        using (var cluster = new ClusterAware.ClusterManager())
        {
            cluster.Username = "UserName";
            cluster.Machine = "ClusterName";

            cluster.ResourceStateChange += Cluster_ResourceStateChange;
            cluster.Connect();
            cluster.EnabledWatcherResourceStateChange();
            cluster.Resources.TakeOffline("ResourceTest", 10);
            cluster.Resources.BringOnline("ResourceTest", 10);
        }
        Console.ReadKey();
        }

        void Cluster_ResourceStateChange(object sender, ClusterAwareProperties.EventResourceStateChangePropety ev)
        {
            Console.WriteLine(@"Group : " + ev.EventGroup);
            Console.WriteLine(@"Node : " + ev.EventNode);
            Console.WriteLine(@"Resource : " + ev.EventObjectName);
            Console.WriteLine(@"State : " + Enum.Parse(typeof(ClusterAwareProperties.StateResources),ev.EventNewState.ToString()));
        }
	ResultSamples6
Sample 7: Create 2 Groups , Add resources with dependence and SetprerredOwners node to group
             var Cluster = new ClusterManagerAdmin
            {
                Username = “UserName”,
Machine = “CluserName” }; Cluster.Connect(); if (!Cluster.Groups.ExistGroup("NewGroup")) { Cluster.Groups.Create("NewGroup"); } if (!Cluster.Groups.ExistGroup("TestGroup")) { Cluster.Groups.Create("TestGroup"); } if (Cluster.OperationSystem == WindowsOperationSystem.Windows2008 || Cluster.OperationSystem == WindowsOperationSystem.Windows2008R2) { var node = Cluster.Groups.GetActiveNode("TestGroup").Name; Cluster.Groups.SetPreferredOwners("TestGroup", new[] { node }); } if (!Cluster.Resources.ExistResource("ResourceDependence")) { Cluster.Resources.Create("TestGroup", "ResourceDependence", ResourceTypeNamesConstants.ResourcetypeGenericApplication, false); var res = Cluster.Resources.GetResource("ResourceDependence"); res.PrivateProperties.SetPropertyValue("CommandLine", "Notepad.exe"); res.PrivateProperties.SetPropertyValue("CurrentDirectory", @"C:\")); res.PrivateProperties.SetPropertyValue("InteractWithDesktop"), true); Cluster.Resources.Save(res); } if (!Cluster.Resources.ExistResource("ResourceTest"))) { Cluster.Resources.Create("TestGroup", "ResourceTest", ResourceTypeNamesConstants.ResourcetypeGenericApplication, false); var res = Cluster.Resources.GetResource("ResourceTest"); res.PrivateProperties.SetPropertyValue("CommandLine", "Notepad.exe"); res.PrivateProperties.SetPropertyValue("CurrentDirectory", @"C:\")); res.PrivateProperties.SetPropertyValue("InteractWithDesktop"), true); Cluster.Resources.Save(res); Cluster.Resources.AddDependency("ResourceTest","ResourceDependence"); } Cluster.Disconnect();

 

There are more samples of interest? So please tell us !

Last edited Aug 31, 2010 at 5:07 AM by FCerqueira, version 20

Comments

No comments yet.