Yank a Server from NLB when it fails?


 

It’s about 30 minutes from the start of my session on H/A at the SPC. One of the things I talk about in the session is how easy it is to monitor your web servers for failure and yank them from NLB when they do. I’ve posted the example code below. It’s slimmed down greatly to fit in the presentation so I will try to update this later with more robust logic. I’ll try to get this in a code block just as soon as I find that the code gadget again.

 

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

HttpWebResponse response = (HttpWebResponse)request.GetResponse();

HttpStatusCode code = response.StatusCode;

if(code.ToString() != “200”)

{

ManagementScope NLBScope = new ManagementScope("\\\\" + server + "\\root\\MicrosoftNLB");

ManagementClass NLBClass = new ManagementClass(NLBScope, new ManagementPath("MicrosoftNLB_Node"), null);

NLBClass.Get();

foreach (ManagementObject node in NLBClass.GetInstances())

{

node.Get();

node.InvokeMethod("Stop", null);

}

}

Comments (7)

  1.   It's about 30 minutes from the start of my session on H/A at the SPC. One of the things I

  2. Carl Brothers says:

    Michael, where would you execute this code and how do you ensure this works for sites using host headers?  Automating removing a server from a NLB farm has been the primary reason why I lean to the HW LB devices when given a choice.  knowing how to best implement this would be a godsend.  Many of the MSCOM ops presentations make reference to their custom iteration of cluster sentinel, but do not share their version.

    Thanks,

    Carl

  3. asp to go says:

    Interesting info – thanks for posting it.  Our developers have been very happy since we signed on with Server Intellect.  They’re technicians can log into the server once a month on the date and time that specified, to prform security updates and software upgrades proactively, which is very helpful to us.  

  4. Trevor Bezzina says:

    I am having some strange error when executing the above code.. node.InvokeMethod("Stop", null);

    is generating a not supported error message.

    up till the get() is fine cause I can see the path of the node.

    any clues ?

    any help is greatly appreciated.

  5. MSDNArchive says:

    What is the actual exception message and stack? I haven’t seen this error before using the following code:

    public enum NLBAction { Start, Stop, Resume };

           private EventLog log = new EventLog();

           public void ControlNode(NLBAction action)

           {

               ManagementScope NLBScope = new ManagementScope("\\localhost\root\MicrosoftNLB");

               ManagementClass NLBClass = new ManagementClass(NLBScope, new ManagementPath("MicrosoftNLB_Node"), null);

               NLBClass.Get();

               foreach (ManagementObject node in NLBClass.GetInstances())

               {

                   node.Get();

                   Console.WriteLine(node.GetPropertyValue("Name"));

                   if (action == NLBAction.Start)

                   {

                       try

                       {

                           node.InvokeMethod("Start", null);

                       }

                       catch (Exception e)

                       {

                           log.LogEvent(e.Message, System.Diagnostics.EventLogEntryType.Error, 1004);

                           throw;

                       }

                   }

                   else if (action == NLBAction.Stop)

                   {

                       try

                       {

                           node.InvokeMethod("Stop", null);

                       }

                       catch (Exception e)

                       {

                           log.LogEvent(e.Message, System.Diagnostics.EventLogEntryType.Error, 1004);

                           throw;

                       }

                   }

                   else if (action == NLBAction.Resume)

                   {

                       try

                       {

                           node.InvokeMethod("Resume", null);

                       }

                       catch (Exception e)

                       {

                           log.LogEvent(e.Message, System.Diagnostics.EventLogEntryType.Error, 1004);

                           throw;

                       }

                   }

               }

           }

           public int GetNodeStatus()

           {

               ManagementScope NLBScope = new ManagementScope("\\localhost\root\MicrosoftNLB");

               ManagementClass NLBClass = new ManagementClass(NLBScope, new ManagementPath("MicrosoftNLB_Node"), null);

               try

               {

                   NLBClass.Get();

                   object status = null;

                   foreach (ManagementObject node in NLBClass.GetInstances())

                   {

                       node.Get();

                       status = node.GetPropertyValue("StatusCode");

                   }

                   return Convert.ToInt32(status);

               }

               catch (Exception e)

               {

                   log.LogEvent(e.Message, System.Diagnostics.EventLogEntryType.Error, 1004);

                   return 0;

               }

  6. Liam says:

    Hi.

    I am also getting a "Not Supported" error message when running the code you provided.

    The getting of node status works, but for the stop, start and resume methods i get a "Not Supported"

  7. Liam says:

    Hi.

    I found out the problem. The code above cycles through all the nodes. In my case the frist node encountered was not the local node and so the stop/start/resume methods were being called on a remote node, which is not supported.