44 Replies Latest reply on Jan 27, 2014 10:45 AM by scottfraley

    How to create array of arguments for Unmanage/Remanage? (C#)

    fourmica

      I am trying to write a method for Unmanaging and Remanaging nodes in Orion, in C#. Using the CSClient example, I've been able to write a query Orion for a NodeID based on sysname. However, when I want to Invoke("Orion.Nodes", "Unmanage", args) I don't know how to correctly create the ArrayOfXMLElement that the method seems to want. I tried modifying the code used for the alert example, but it gives me "FaultException`1" when I try it. I created the data object to store the arguments based on the column names in the Nodes table. I am using VS2010, .net 4.0, Orion 2012.2.0, and SDK 1.6. I am very new at this so please be gentle :-) The portion of the code that gets the NodeID based on sysname works fine.

       

      This is what my code looks like:

       

      using System;

      using System.Net;

      using System.Runtime.Serialization;

      using System.Xml;

      using System.Linq;

      using System.Xml.Linq;

      using System.Xml.XPath;

      using CSClient.SWIS;

       

      namespace CSClient

      {

          class Program

          {

              static void Main()

              {

                  const string hostname = "myorionserver";

                  const string username = @"myuser";

                  const string password = "mypassword";

                 

                  ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, errors) => true;

       

                  var client = new InformationServiceClient("BasicHttpBinding_InformationService", string.Format("https://{0}:17778/SolarWinds/InformationService/v3/OrionBasic", hostname));

                  client.ClientCredentials.UserName.UserName = username;

                  client.ClientCredentials.UserName.Password = password;

                  client.Open();

                  var nodeIDInfo = client.QueryXml("SELECT TOP 1 NodeID from Orion.Nodes WHERE sysname like 'targetserver' RETURN XML AUTO", null);

                  nodeIDInfo.Save(@"c:\temp\orionoutput.xml");

                  var nodeID = nodeIDInfo.XPathSelectElement("//*[local-name()='NodeID']").Value;

                  Console.WriteLine("Node ID is " + nodeID.ToString());

       

                  string unmanageFrom = DateTime.Now.ToString();

                  string unmanageUntil = DateTime.Now.AddHours(6).ToString();

       

                  var unmanageInfo = new UnmanageInfo

                  {

                      NodeID = nodeID,

                      UnManageFrom = unmanageFrom,

                      UnManageUntil = unmanageUntil

                  };

                  var unmanaged = new[] { unmanageInfo };

       

                  var dcs = new DataContractSerializer(unmanaged.GetType());

                  var doc = new XmlDocument();

                  using (var writer = doc.CreateNavigator().AppendChild())

                  {

                      dcs.WriteObject(writer, unmanaged);

                  }

       

                  var arguments = new ArrayOfXmlElement { XDocument.Load(new XmlNodeReader(doc)).Root };

                  var result2 = client.Invoke("Orion.Nodes", "Unmanage", arguments);

                  Console.Write(result2);

                  Console.Read();

              }

       

              [DataContract(Name = "UnmanageInfo", Namespace = "http://schemas.solarwinds.com/2008/Orion")]

              public class UnmanageInfo

              {

                  [DataMember(Order = 1)]

                  public string NodeID;

       

                  [DataMember(Order = 2)]

                  public string UnManageFrom;

       

                  [DataMember(Order = 3)]

                  public string UnManageUntil;

       

              }

       

          }

      }

        • Re: How to create array of arguments for Unmanage/Remanage? (C#)
          tdanner

          Orion.AlertStatus.Acknowledge takes one argument that is an array of AlertInfo structures. Orion.Nodes.Unmanage takes four arguments: a net object id (basically "N:" and then the node id value), the DateTime to start the unmanage period, the DateTime to end the unmanage period, and a boolean for "is relative" (if you pass true for this last one, it will ignore the date portion of the third argument and use the time portion as the duration of the unmanage period; I recommend you compute the end of the unmanage period yourself and just pass false for the fourth argument). Here's a program that worked for me:

           

          using System;

          using System.Linq;

          using System.Net;

          using System.Runtime.Serialization;

          using System.Xml;

          using System.Xml.Linq;

          using System.Xml.XPath;

          using CSClient.SWIS;

           

           

          namespace CSClient

          {

              class Program

              {

                  static void Main()

                  {

                      const string hostname = "localhost";

                      const string username = "admin";

                      const string password = "";

                     

                      ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, errors) => true;

           

           

                      var client = new InformationServiceClient("BasicHttpBinding_InformationService",

                                                                string.Format("https://{0}:17778/SolarWinds/InformationService/v3/OrionBasic", hostname));

                      client.ClientCredentials.UserName.UserName = username;

                      client.ClientCredentials.UserName.Password = password;

                      client.Open();

           

           

                      var result = client.QueryXml("SELECT TOP 1 NodeID from Orion.Nodes WHERE sysname like 'Tok-foundry' RETURN XML AUTO", null);

           

           

                      var nodeID = result.XPathSelectElement("//*[local-name()='NodeID']").Value;

           

           

                      var arguments = PrepareArguments("N:" + nodeID, DateTime.Now, DateTime.Now.AddHours(6), false);

                      var result2 = client.Invoke("Orion.Nodes", "Unmanage", arguments);

           

           

                      Console.Write(result2);

           

           

                      client.Close();

                      Console.ReadLine();

                  }

           

           

                  static XElement PrepareArgument(object argument)

                  {

                      var dcs = new DataContractSerializer(argument.GetType());

                      var doc = new XmlDocument();

                      using (var writer = doc.CreateNavigator().AppendChild())

                      {

                          dcs.WriteObject(writer, argument);

                      }

           

           

                      return XDocument.Load(new XmlNodeReader(doc)).Root;

                  }

           

           

                  static ArrayOfXmlElement PrepareArguments(params object[] args)

                  {

                      var aoxe = new ArrayOfXmlElement();

                      aoxe.AddRange(args.Select(PrepareArgument));

                      return aoxe;

                  }

              }

          }

          • Re: How to create array of arguments for Unmanage/Remanage? (C#)
            fourmica

            Tdanner,

             

            Thank you again for helping me with this. May I please lean on you for a follow-up?

             

            How do I interpret the results returned by these methods to determine if the operation was successful? For example, if I unmanage an internal Node, the following XML is returned:


            <?xml version="1.0" encoding="utf-8" ?>

            <Return i:nil="true" xmlns="http://schemas.datacontract.org/2004/07/SolarWinds.InformationService.Contract" xmlns:d1p1="http://schemas.datacontract.org/2004/07/System" xmlns:i="http://www.w3.org/2001/XMLSchema-instance" />

            I can't figure out how to interpret this for success or failure; right now I'm while-looping against the Nodes table until Unmanaged == true. I'm guessing there's a better way than this?

            Thanks,

            Josh