8 Replies Latest reply on Jan 19, 2017 10:11 AM by Steven Klassen

    Unmanage via Code

    robdogaz

      I am trying to unmanage nodes when I create them and here is my code, but they do not go unmanaged, What am I missing?

       

      try

                          {

                              DateTime.Now.ToString("MM-dd-yyyy");

                              await swisClient.InvokeAsync("Orion.Nodes", "Unmanage", "[N:\"" + nodeId + "\"" + ",\"" + DateTime.Now.ToString("MM-dd-yyyy") + " 12:00:00 AM \"" + ",\"" + DateTime.Now.AddMonths(2).ToString("MM-dd-yyyy") + " 12:00:00 AM \"" + ",\"" + " false" + "\"]");

      }

        • Re: Unmanage via Code
          tdanner

          I am assuming you are using the SwisClient class from OrionSDK/SwisClient.cs at master · solarwinds/OrionSDK · GitHub . If that's not the case then all bets are off.

           

          You have wrapped this in a "try" block. I don't see the "catch" block, but I am guessing this is throwing an exception from the server. Most likely the server is complaining that you have passed only 1 argument instead of the 4 that are expected. You are building up a json array as a string. You don't need to do that. InvokeAsync will take the arguments you supply and handle the json conversion.

           

          Instead, trying something like this:

           

          await swisClient.InvokeAsync("Orion.Nodes", "Unmanage", "N:" + nodeId, DateTime.UtcNow, DateTime.UtcNow.AddMonths(2), false);

           

          Note that you should use "UtcNow" when talking to SWIS, not "Now" or you will get unexpected results (unless you are in London!).

            • Re: Unmanage via Code
              robdogaz

              I have tried with and without the try catch

               

               

                              if(Task == "New")

                              {

                                 

                                  DateTime.Now.ToString("MM-dd-yyyy");

                                  await swisClient.InvokeAsync("Orion.Nodes", "Unmanage", "N:" + nodeId, DateTime.UtcNow, DateTime.UtcNow.AddMonths(2), false);

                                  LogMessageToFile("--------[Info]--EOC Directory Monitoring Tool: [N:" + nodeId + "," + DateTime.UtcNow + "," + DateTime.UtcNow.AddMonths(2) + "," + "false");                 

                              }

                              else

                              {

                                  LogMessageToFile(Task.ToString() + " did not run");

                              }

               

              But It still is not unmanaging the nodes, I do not need the N: with [N:] surrounded like in other examples?

                • Re: Unmanage via Code
                  tdanner

                  What is the value of "nodeId"?

                    • Re: Unmanage via Code
                      robdogaz

                      The node number that came from the insert for example 12036

                       

                      Thanks,

                      • Re: Unmanage via Code
                        robdogaz

                        Here is my entire function if that helps. Also for anyone else needing something like this

                         

                        AddNode(swisClient, dr["store"].ToString(), "192." + dr["Octet"].ToString() + ".50", dataRow["MachineType"].ToString(), dataRow["SysObjectID"].ToString(), "switch" + dr["Store"].ToString() + "-05", dataRow["NType"].ToString(), dataRow["Community"].ToString(), dataRow["RwCommunity"].ToString(), dataRow["PollingPriority"].ToString(), "Switch-01", dr["Task"].ToString()).Result;

                         

                        private static async Task<string> AddNode(ISwisClient swisClient, string storenumber, string IP, string MType, string sysOID, string Name, string NType, string Community, string RwCommunity, string PollingPriority, string Group, string Task)

                                {

                                    bool result = CheckIfInSolarWinds(IP);

                                    if ((result = CheckIfInSolarWinds(IP)) == false)

                                    {

                                        LogMessageToFile("--------[Info]--SolarWinds Directory Monitoring Tool: " + IP + " not in SolarWinds so adding");

                                        string storenum = storenumber;

                                        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Stores"].ConnectionString);

                                        conn.Open();

                                        string storesquery = @"select s.storenumber, s.name, a.streetline1, a.streetline2, a.city, a.ZipCode, l.abbreviation as state, c.abbreviation as country 

                                                                from store s, Address a, Lookup_StateProvinces l, Lookup_Countries c

                                                                where a.AddressId = s.AddressId

                                                                and a.StateProvinceId = l.stateprovinceid

                                                                and l.CountryID = c.CountryID

                                                                and s.storenumber = '" + storenum + "'";

                         

                         

                                        SqlCommand cmd = new SqlCommand(storesquery, conn);

                         

                         

                                        DataTable t1 = new DataTable();

                                        using (SqlDataAdapter a = new SqlDataAdapter(cmd))

                                        {

                                            a.Fill(t1);

                                        }

                         

                         

                                        var engineID = GetEngineID(swisClient);

                                        string nodeUri = await swisClient.CreateAsync("Orion.Nodes",

                                            new

                                            {

                                                IPAddress = IP,

                                                IPAddressType = "IPv4",

                                                EngineID = engineID,

                                                ObjectSubType = "SNMP",

                                                SNMPVersion = 2,

                                                Location = (t1.Rows[0]["streetline1"] + ", " + t1.Rows[0]["City"] + ", " + t1.Rows[0]["State"] + " " + t1.Rows[0]["ZipCode"]).ToString().ToUpper(),

                                                Contact = (t1.Rows[0]["name"]).ToString().ToUpper(),

                                                EntityType = "Orion.Nodes",

                                                NodeName = Name + ".company.com",

                                                DNS = Name + ".company.com",

                                                Community = Community,

                                                RwCommunity = RwCommunity,

                                                MachineType = MType,

                                                SysObjectID = sysOID,

                                                DynamicIP = "false",

                                                PollInterval = "120",

                                                RediscoveryInterval = "2880",

                                                StatCollection = "30"

                                            });               

                                        JObject node = await swisClient.ReadAsync(nodeUri);

                                        int nodeId = (int)node["NodeID"];

                                        LogMessageToFile("--------[Info]--SolarWinds Directory Monitoring Tool: " + nodeId + " added to Solarwinds");

                         

                         

                                        await swisClient.UpdateAsync(nodeUri + "/CustomProperties", new

                                        {

                                            Country = (t1.Rows[0]["Country"]).ToString().ToUpper(),

                                            Node_Type = NType,

                                            Address = (t1.Rows[0]["streetline1"]).ToString().ToUpper(),

                                            Polling_Priority = PollingPriority,

                                            RouterLocation = "STORES",

                                            City = (t1.Rows[0]["City"]).ToString().ToUpper(),

                                            State = (t1.Rows[0]["State"]).ToString().ToUpper(),

                                            voip = "YES",

                                            StoreNumber = storenum

                                        });

                                        LogMessageToFile("--------[Info]--SolarWinds Directory Monitoring Tool: " + storenum + " custom properties added to Solarwinds");

                         

                         

                                        DataTable dt = Tabulate(GetInterfaces(swisClient, nodeId.ToString()).ToString());

                                        if(sysOID == "1.3.6.1.4.1.9.1.2068" && Group == "Router-01")

                                        {

                                        EnumerableRowCollection<DataRow> query = from myRow in dt.AsEnumerable()

                                                           where myRow.Field<string>("Caption").StartsWith("Tunnel200") ||

                                                           myRow.Field<string>("Caption").StartsWith("GigabitEthernet0/0/0") ||

                                                           myRow.Field<string>("Caption").StartsWith("GigabitEthernet0/0/1") ||

                                                           myRow.Field<string>("Caption").StartsWith("T1 0/1/0") ||

                                                           myRow.Field<string>("Caption").StartsWith("Serial0/1/0:1") ||

                                                           myRow.Field<string>("Caption").StartsWith("Serial0/1/0:100")

                                                           select myRow;

                                            DataView view = query.AsDataView();

                         

                         

                                            for (int i = 0; i < view.Count; i++)

                                            {

                                                await swisClient.InvokeAsync("Orion.NPM.Interfaces", "AddInterfacesOnNode", nodeId, new[]

                                                {

                                                    new

                                                        {

                                                            ifIndex = view[i][0].ToString(),

                                                            Caption = view[i][1].ToString(),

                                                            ifType = view[i][2].ToString(),

                                                            ifSubType = view[i][3].ToString(),

                                                            InterfaceID = view[i][4].ToString(),

                                                            Manageable = view[i][5].ToString(),

                                                            ifSpeed = view[i][6].ToString(),

                                                            ifAdminStatus = view[i][7].ToString(),

                                                            ifOperStatus = view[i][8].ToString()

                                                        }

                                                }, "AddDefaultPollers");

                                            }

                                            LogMessageToFile("--------[Info]--SolarWinds Directory Monitoring Tool: " + storenum + " interfaces added to Solarwinds");

                                        }

                                        else if (sysOID == "1.3.6.1.4.1.9.1.2068" && Group == "Router-02")

                                        {

                                            EnumerableRowCollection<DataRow> query = from myRow in dt.AsEnumerable()

                                                                                     where myRow.Field<string>("Caption").StartsWith("GigabitEthernet0/0/0") ||

                                                                                     myRow.Field<string>("Caption").StartsWith("GigabitEthernet0/0/1") ||

                                                                                     myRow.Field<string>("Caption").StartsWith("GigabitEthernet0/0/2") ||

                                                                                     myRow.Field<string>("Caption").StartsWith("Tunnel200") ||

                                                                                     myRow.Field<string>("Caption").StartsWith("Cellular0/1/0")

                                                                                     select myRow;

                                            DataView view = query.AsDataView();

                         

                         

                                            for (int i = 0; i < view.Count; i++)

                                            {

                                                await swisClient.InvokeAsync("Orion.NPM.Interfaces", "AddInterfacesOnNode", nodeId, new[]

                                                {

                                                    new

                                                    {

                                                        ifIndex = view[i][0].ToString(),

                                                        Caption = view[i][1].ToString(),

                                                        ifType = view[i][2].ToString(),

                                                        ifSubType = view[i][3].ToString(),

                                                        InterfaceID = view[i][4].ToString(),

                                                        Manageable = view[i][5].ToString(),

                                                        ifSpeed = view[i][6].ToString(),

                                                        ifAdminStatus = view[i][7].ToString(),

                                                        ifOperStatus = view[i][8].ToString()

                                                    }

                                                }, "AddDefaultPollers");

                                            }

                                            LogMessageToFile("--------[Info]--SolarWinds Directory Monitoring Tool: " + storenum + " interfaces added to Solarwinds");

                                        }

                                        else if (sysOID == "1.3.6.1.4.1.9.1.1825" && Group == "Switch-01")

                                        {

                                            EnumerableRowCollection<DataRow> query = from myRow in dt.AsEnumerable()

                                                                                     where myRow.Field<string>("Caption").StartsWith("GigabitEthernet1/0/1") ||

                                                                                     myRow.Field<string>("Caption").StartsWith("GigabitEthernet1/0/18") ||

                                                                                     myRow.Field<string>("Caption").StartsWith("GigabitEthernet1/0/22") ||

                                                                                     myRow.Field<string>("Caption").StartsWith("GigabitEthernet1/0/24") ||

                                                                                     myRow.Field<string>("Caption").StartsWith("Port-channel1")

                                                                                     select myRow;

                                            DataView view = query.AsDataView();

                         

                         

                                            for (int i = 0; i < view.Count; i++)

                                            {

                                                await swisClient.InvokeAsync("Orion.NPM.Interfaces", "AddInterfacesOnNode", nodeId, new[]

                                                {

                                                    new

                                                        {

                                                            ifIndex = view[i][0].ToString(),

                                                            Caption = view[i][1].ToString(),

                                                            ifType = view[i][2].ToString(),

                                                            ifSubType = view[i][3].ToString(),

                                                            InterfaceID = view[i][4].ToString(),

                                                            Manageable = view[i][5].ToString(),

                                                            ifSpeed = view[i][6].ToString(),

                                                            ifAdminStatus = view[i][7].ToString(),

                                                            ifOperStatus = view[i][8].ToString()

                                                        }

                                                }, "AddDefaultPollers");

                                            }

                                            LogMessageToFile("--------[Info]--SolarWinds Directory Monitoring Tool: " + storenum + " interfaces added to Solarwinds");

                                        }

                                        else if (sysOID == "1.3.6.1.4.1.9.1.1825" && Group == "Switch-02")

                                        {

                                            EnumerableRowCollection<DataRow> query = from myRow in dt.AsEnumerable()

                                                                                     where myRow.Field<string>("Caption").StartsWith("GigabitEthernet1/0/18") ||

                                                                                     myRow.Field<string>("Caption").StartsWith("GigabitEthernet1/0/22") ||

                                                                                     myRow.Field<string>("Caption").StartsWith("GigabitEthernet1/0/22") ||

                                                                                     myRow.Field<string>("Caption").StartsWith("GigabitEthernet1/0/24") ||

                                                                                     myRow.Field<string>("Caption").StartsWith("Port-channel1")

                                                                                     select myRow;

                                            DataView view = query.AsDataView();

                         

                         

                                            for (int i = 0; i < view.Count; i++)

                                            {

                                                await swisClient.InvokeAsync("Orion.NPM.Interfaces", "AddInterfacesOnNode", nodeId, new[]

                                                {

                                                    new

                                                        {

                                                            ifIndex = view[i][0].ToString(),

                                                            Caption = view[i][1].ToString(),

                                                            ifType = view[i][2].ToString(),

                                                            ifSubType = view[i][3].ToString(),

                                                            InterfaceID = view[i][4].ToString(),

                                                            Manageable = view[i][5].ToString(),

                                                            ifSpeed = view[i][6].ToString(),

                                                            ifAdminStatus = view[i][7].ToString(),

                                                            ifOperStatus = view[i][8].ToString()

                                                        }

                                                }, "AddDefaultPollers");

                                            }

                                            LogMessageToFile("--------[Info]--SolarWinds Directory Monitoring Tool: " + storenum + " interfaces added to Solarwinds");

                                        }

                                       

                                       

                                        SqlConnection ncon = new SqlConnection(ConfigurationManager.ConnectionStrings["Dashboard"].ConnectionString);

                                        SqlCommand ncmd = new SqlCommand("SELECT Poller FROM tblPollers WHERE SysObjectID='" + sysOID + "'", ncon);

                                        SqlDataAdapter newda = new SqlDataAdapter(ncmd);

                                        DataTable newdt = new DataTable();

                                        newda.Fill(newdt);

                                        string[] pollers = new string[newdt.Rows.Count];

                                        int z = 0;

                                        foreach (DataRow newdr in newdt.Rows)

                                        {

                                            pollers[z++] = newdr["Poller"].ToString();

                                        }

                                        string[] pollerTypes = pollers;

                                        foreach (string pollerType in pollerTypes)

                                        {

                                            await swisClient.CreateAsync("Orion.Pollers", new

                                            {

                                                NetObject = "N:" + nodeId,

                                                NetObjectType = "N",

                                                NetObjectID = nodeId,

                                                PollerType = pollerType

                                            });

                                            LogMessageToFile("--------[Info]--SolarWinds Directory Monitoring Tool: " + storenum + " " + pollerType + " poller added to Solarwinds");

                                        }

                                        ICollection<KeyValuePair<String, String>> openWith =

                                        new Dictionary<String, String>();

                         

                         

                                        // Add some elements to the dictionary. When elements are 

                                        // added through the ICollection<T> interface, the keys

                                        // and values must be wrapped in KeyValuePair objects.

                                        //

                                        openWith.Add(new KeyValuePair<String, String>("NeedsInventory", "HWH"));

                                        openWith.Add(new KeyValuePair<String, String>("Core.TopologyPollInterval", "30"));

                                        openWith.Add(new KeyValuePair<String, String>("Core.WebBrowseTemplate", "http://{{ HrefIPAddress }}"));

                                        openWith.Add(new KeyValuePair<String, String>("HardwareHealth.PreferredCiscoMIB", "CISCO-ENVMON-MIB"));

                         

                         

                                        foreach (KeyValuePair<string, string> element in openWith)

                                        {

                                            await swisClient.CreateAsync("Orion.NodeSettings", new

                                            {

                                                NodeID = nodeId,

                                                SettingName = element.Key,

                                                SettingValue = element.Value

                                            });

                                            LogMessageToFile("--------[Info]--SolarWinds Directory Monitoring Tool: " + storenum + " changed MIB " + element.Key +", " + element.Value +  " to Cisco in Solarwinds");

                                        }

                                        String connString2 = ConfigurationManager.ConnectionStrings["Orion"].ConnectionString;

                         

                                        SqlConnection sqlConn2 = new SqlConnection(connString2);

                                        SqlCommand sqlComm = new SqlCommand();

                                        sqlComm = sqlConn2.CreateCommand();

                                        sqlComm.CommandText = @"UPDATE NodesData SET Category=@paramName WHERE NodeID=@conditionName";

                                        sqlComm.Parameters.Add("@paramName", SqlDbType.Int);

                                        sqlComm.Parameters["@paramName"].Value = 1;

                                        sqlComm.Parameters.Add("@conditionName", SqlDbType.Int);

                                        sqlComm.Parameters["@conditionName"].Value = nodeId;

                                        sqlConn2.Open();

                                        sqlComm.ExecuteNonQuery();

                                        sqlConn2.Close();

                         

                                        await swisClient.InvokeAsync("Orion.Nodes", "PollNow", "[N:\"" + nodeId + "\"]");

                         

                                        if(Task == "New")

                                        {

                                           

                                            DateTime.Now.ToString("MM-dd-yyyy");

                                            await swisClient.InvokeAsync("Orion.Nodes", "Unmanage", "N:" + nodeId, DateTime.UtcNow, DateTime.UtcNow.AddMonths(2), false);

                                            LogMessageToFile("--------[Info]--SolarWinds Directory Monitoring Tool: [N:" + nodeId + "," + DateTime.UtcNow + "," + DateTime.UtcNow.AddMonths(2) + "," + "false");                 

                                        }

                                        else

                                        {

                                            LogMessageToFile(Task.ToString() + " did not run");

                                        }

                         

                         

                                        return nodeUri;

                                    }

                                    return null;

                                }

                        1 of 1 people found this helpful