Skip to content

Commit

Permalink
SNMP polling
Browse files Browse the repository at this point in the history
  • Loading branch information
veniware committed Mar 20, 2024
1 parent 2e538e3 commit 51528e5
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Protest/Protest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
</ItemGroup>

<ItemGroup Condition="'$(Configuration)' == 'RELEASE'">
<ProjectReference Include="..\Protest-CacheGenerator\Protest-CacheGenerator.csproj" OutputItemType="Analyzer"/>
<ProjectReference Include="..\Protest-CacheGenerator\Protest-CacheGenerator.csproj" OutputItemType="Analyzer" />
</ItemGroup>

</Project>
51 changes: 39 additions & 12 deletions Protest/Protocols/Snmp.Polling.cs
Original file line number Diff line number Diff line change
@@ -1,36 +1,63 @@
using Lextm.SharpSnmpLib;
using Lextm.SharpSnmpLib.Messaging;
using Lextm.SharpSnmpLib.Security;

using System.Collections.Generic;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Protest.Protocols.Snmp;

internal static class Polling {
public static async Task PollAsync(IPAddress target, string communityString, string oidString) {
VersionCode version = VersionCode.V3;

public static byte[] Poll(IPAddress target, string community, string oid, int timeout) {
byte[] bytes = null;

Task.Run(async () => {
try {
IList<Variable> result = await PollAsync(target, community, oid, timeout);
bytes = Encoding.UTF8.GetBytes(result[0].Data.ToString());
}
catch (Exception ex) {
bytes = Encoding.UTF8.GetBytes(ex.Message);
}
}).GetAwaiter().GetResult();

return bytes;
}

private static async Task<IList<Variable>> PollAsync(IPAddress target, string communityString, string oidString, int timeout) {
VersionCode version = VersionCode.V2;
IPEndPoint endpoint = new IPEndPoint(target, 161);
OctetString community = new OctetString(communityString);

ObjectIdentifier oid = new ObjectIdentifier(oidString);
List<Variable> variables = new List<Variable> { new Variable(oid) };

IList<Variable> result = await Messenger.GetAsync(version, endpoint, community, variables);
CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(timeout);
CancellationToken cancellationToken = cancellationTokenSource.Token;

if (result is null) {
Console.WriteLine("No data received.");
return;
IList<Variable> result;
try {
result = await Messenger.GetAsync(version, endpoint, community, variables, cancellationToken);
}
catch (OperationCanceledException) {
throw new Exception("Operation timed out");
}
catch (Exception) {
throw;
}

if (result is null) {
throw new Exception("No data received");
}
if (result.Count == 0) {
Console.WriteLine("No result received.");
return;
throw new Exception("No result received");
}

Console.WriteLine("The value of OID {0} is {1}", oid, result[0].Data.ToString());
return result;
}

public static async void Test() {
await PollAsync(IPAddress.Parse("127.0.0.1"), "public", "");
}
}

0 comments on commit 51528e5

Please sign in to comment.