Skip to content

Commit

Permalink
Merge pull request #326 from solarwinds/feature/csharp-sample-net6
Browse files Browse the repository at this point in the history
Updated C# sample to .NET 6
  • Loading branch information
danjagnow authored Sep 20, 2022
2 parents b48661e + 537d22a commit eea01a6
Show file tree
Hide file tree
Showing 3 changed files with 234 additions and 234 deletions.
21 changes: 10 additions & 11 deletions Samples/CSharp/CSRestClient.csproj
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net45</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Net.Http" Version="2.2.22" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
</ItemGroup>
</Project>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
</ItemGroup>
</Project>
213 changes: 109 additions & 104 deletions Samples/CSharp/Program.cs
Original file line number Diff line number Diff line change
@@ -1,104 +1,109 @@
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;

namespace CSRestClient
{
internal class Program
{
private const string Hostname = "localhost";
private const string Username = "admin";
private const string Password = "";

private static void Main()
{
try
{
var swisClient = new SwisClient(Hostname, Username, Password);

var alertObjectId = GetOneAlert(swisClient);
var invokeResult = AcknowledgeAlert(swisClient, alertObjectId, "Ack from API");
Console.WriteLine(invokeResult);

Console.WriteLine(AddNode(swisClient).Result);
}
catch (Exception ex)
{
Console.WriteLine(ex);
}

if (Debugger.IsAttached)
{
Console.WriteLine("Press enter to exit.");
Console.ReadLine();
}
}

private static int GetOneAlert(ISwisClient swisClient)
{
const string query = @"SELECT TOP 1 AlertObjectID
FROM Orion.AlertActive
WHERE ISNULL(Acknowledged,0)!=1
ORDER BY TriggeredDateTime DESC";

JToken queryResult = swisClient.QueryAsync(query).Result;
Console.WriteLine(queryResult);

var alertObjectId = (int)queryResult["results"][0]["AlertObjectID"];
return alertObjectId;
}

private static JToken AcknowledgeAlert(ISwisClient swisClient, int alertObjectId, string note)
{
JToken invokeResult = swisClient.InvokeAsync("Orion.AlertActive", "Acknowledge", new[] {alertObjectId}, note).Result;
return invokeResult;
}

private static async Task<string> AddNode(ISwisClient swisClient)
{
string nodeUri = await swisClient.CreateAsync("Orion.Nodes",
new
{
IPAddress = "10.199.4.3",
EngineID = 1,
ObjectSubType = "SNMP",
SNMPVersion = 2,
Community = "public"
});

JObject node = await swisClient.ReadAsync(nodeUri);
int nodeId = (int)node["NodeID"];

string[] pollerTypes = {
"N.Status.ICMP.Native",
"N.ResponseTime.ICMP.Native",
"N.Details.SNMP.Generic",
"N.Uptime.SNMP.Generic",
"N.Cpu.SNMP.CiscoGen3",
"N.Memory.SNMP.CiscoGen3",
"N.HardwareHealthMonitoring.SNMP.NPM.Cisco"
};

foreach (string pollerType in pollerTypes)
{
await swisClient.CreateAsync("Orion.Pollers", new
{
NetObject = "N:" + nodeId,
NetObjectType = "N",
NetObjectID = nodeId,
PollerType = pollerType
});
}

await swisClient.CreateAsync("Orion.NodeSettings", new
{
NodeID = nodeId,
SettingName = "NeedsInventory",
SettingValue = "HWH"
});

return nodeUri;
}
}
}
using System;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;

namespace CSRestClient
{
internal class Program
{
private const string Hostname = "localhost";
private const string Username = "admin";
private const string Password = "";

private static void Main()
{
try
{
var swisClient = new SwisClient(Hostname, Username, Password);

int? alertObjectId = GetOneAlert(swisClient);

if (alertObjectId.HasValue)
{
var invokeResult = AcknowledgeAlert(swisClient, alertObjectId.Value, "Ack from API");
Console.WriteLine(invokeResult);
}

Console.WriteLine(AddNode(swisClient).Result);
}
catch (Exception ex)
{
Console.WriteLine(ex);
}

if (Debugger.IsAttached)
{
Console.WriteLine("Press enter to exit.");
Console.ReadLine();
}
}

private static int? GetOneAlert(ISwisClient swisClient)
{
const string query = @"SELECT TOP 1 AlertObjectID
FROM Orion.AlertActive
WHERE ISNULL(Acknowledged,0)!=1
ORDER BY TriggeredDateTime DESC";

JToken queryResult = swisClient.QueryAsync(query).Result;
Console.WriteLine(queryResult);

JToken firstAlert = queryResult["results"].FirstOrDefault();
return firstAlert == null ? null : (int)firstAlert["AlertObjectID"];
}

private static JToken AcknowledgeAlert(ISwisClient swisClient, int alertObjectId, string note)
{
JToken invokeResult = swisClient.InvokeAsync("Orion.AlertActive", "Acknowledge", new[] { alertObjectId }, note).Result;
return invokeResult;
}

private static async Task<string> AddNode(ISwisClient swisClient)
{
string nodeUri = await swisClient.CreateAsync("Orion.Nodes",
new
{
IPAddress = "10.199.4.3",
EngineID = 1,
ObjectSubType = "SNMP",
SNMPVersion = 2,
Community = "public"
});

JObject node = await swisClient.ReadAsync(nodeUri);
int nodeId = (int)node["NodeID"];

string[] pollerTypes = {
"N.Status.ICMP.Native",
"N.ResponseTime.ICMP.Native",
"N.Details.SNMP.Generic",
"N.Uptime.SNMP.Generic",
"N.Cpu.SNMP.CiscoGen3",
"N.Memory.SNMP.CiscoGen3",
"N.HardwareHealthMonitoring.SNMP.NPM.Cisco"
};

foreach (string pollerType in pollerTypes)
{
await swisClient.CreateAsync("Orion.Pollers", new
{
NetObject = "N:" + nodeId,
NetObjectType = "N",
NetObjectID = nodeId,
PollerType = pollerType
});
}

await swisClient.CreateAsync("Orion.NodeSettings", new
{
NodeID = nodeId,
SettingName = "NeedsInventory",
SettingValue = "HWH"
});

return nodeUri;
}
}
}
Loading

0 comments on commit eea01a6

Please sign in to comment.