Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated C# sample to .NET 6 #326

Merged
merged 2 commits into from
Sep 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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