Partilhar via


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

  • Anonymous
    March 05, 2008
      It's about 30 minutes from the start of my session on H/A at the SPC. One of the things I

  • Anonymous
    March 05, 2008
    The comment has been removed

  • Anonymous
    March 26, 2008
    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.  

  • Anonymous
    October 15, 2008
    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.

  • Anonymous
    October 15, 2008
    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;            }

  • Anonymous
    August 08, 2010
    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"

  • Anonymous
    August 08, 2010
    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.