Skip to content

Commit

Permalink
SNMP, live stats
Browse files Browse the repository at this point in the history
  • Loading branch information
veniware committed Jun 30, 2024
1 parent 1c465e3 commit b89eba6
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 80 deletions.
10 changes: 10 additions & 0 deletions Protest/Protocols/Snmp.Oid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ public static class Oid {
INTERFACE_TOTAL
};

public static string[] LIVESTATS_OID = new string[] {
SYSTEM_UPTIME,
SYSTEM_TEMPERATURE,
PRINTER_STATUS,
PRINTER_MESSAGE,
PRINTER_JOBS
};



public const string SYSTEM_DESCRIPTOR = "1.3.6.1.2.1.1.1.0";
public const string SYSTEM_OBJECT_ID = "1.3.6.1.2.1.1.2.0";
public const string SYSTEM_UPTIME = "1.3.6.1.2.1.1.3.0";
Expand Down
51 changes: 51 additions & 0 deletions Protest/Protocols/Snmp.Polling.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Lextm.SharpSnmpLib;
using Lextm.SharpSnmpLib.Messaging;
using Lextm.SharpSnmpLib.Security;
using Protest.Tools;

namespace Protest.Protocols.Snmp;

Expand Down Expand Up @@ -280,4 +281,54 @@ private static byte[] ParseResponse(IList<Variable> result) {
builder.Append(']');
return Encoding.UTF8.GetBytes(builder.ToString());
}

public static (IList<Variable>, SnmpProfiles.Profile) SnmpQueryTrialAndError(IPAddress target, SnmpProfiles.Profile[] snmpProfiles, string[] oids) {
for (int i = 0; i < snmpProfiles.Length; i++) {
IList<Variable> result = SnmpQuery(target, snmpProfiles[i], oids);

if (result is not null) {
return (result, snmpProfiles[i]);
}
}

return (null, null);
}

public static IList<Variable> SnmpQuery(IPAddress target, SnmpProfiles.Profile profile, string[] oids) {
IList<Variable> result = null;

if (profile.version == 3) {
try {
result = Protocols.Snmp.Polling.SnmpRequestV3(
target,
3000,
profile,
Protocols.Snmp.Oid.GENERIC_OID,
Protocols.Snmp.Polling.SnmpOperation.Get
);
}
catch { }
}
else {
VersionCode version = profile.version switch
{
1 => VersionCode.V1,
_ => VersionCode.V2
};

try {
result = Protocols.Snmp.Polling.SnmpRequestV1V2(
target,
version,
3000,
profile.community,
Protocols.Snmp.Oid.GENERIC_OID,
Protocols.Snmp.Polling.SnmpOperation.Get
);
}
catch { }
}

return result;
}
}
56 changes: 5 additions & 51 deletions Protest/Tasks/Fetch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using Protest.Http;
using Protest.Tools;
using Lextm.SharpSnmpLib;
using Protest.Protocols.Snmp;

namespace Protest.Tasks;

Expand Down Expand Up @@ -392,11 +393,11 @@ public static ConcurrentDictionary<string, string[]> SingleDevice(string target,
profile = null;
}
else if (snmpProfiles.Length == 1) {
result = SnmpQuery(ipAddress, snmpProfiles[0], Protocols.Snmp.Oid.GENERIC_OID);
result = Protocols.Snmp.Polling.SnmpQuery(ipAddress, snmpProfiles[0], Protocols.Snmp.Oid.GENERIC_OID);
profile = snmpProfiles[0];
}
else {
(result, profile) = SnmpQueryTrialAndError(ipAddress, snmpProfiles, Protocols.Snmp.Oid.GENERIC_OID);
(result, profile) = Protocols.Snmp.Polling.SnmpQueryTrialAndError(ipAddress, snmpProfiles, Protocols.Snmp.Oid.GENERIC_OID);
}

for (int i = 0; i < result?.Count; i++) {
Expand All @@ -419,7 +420,7 @@ public static ConcurrentDictionary<string, string[]> SingleDevice(string target,
case "multiprinter":
case "ticket printer":
case "print":
IList<Variable> printerResult = SnmpQuery(ipAddress, profile, Protocols.Snmp.Oid.PRINTERS_OID);
IList<Variable> printerResult = Protocols.Snmp.Polling.SnmpQuery(ipAddress, profile, Protocols.Snmp.Oid.PRINTERS_OID);
for (int i = 0; i < printerResult?.Count; i++) {
string dataString = printerResult[i].Data.ToString();
if (String.IsNullOrEmpty(dataString)) { continue; }
Expand All @@ -432,7 +433,7 @@ public static ConcurrentDictionary<string, string[]> SingleDevice(string target,
case "firewall":
case "router":
case "switch":
IList<Variable> switchResult = SnmpQuery(ipAddress, profile, Protocols.Snmp.Oid.SWITCH_OID);
IList<Variable> switchResult = Protocols.Snmp.Polling.SnmpQuery(ipAddress, profile, Protocols.Snmp.Oid.SWITCH_OID);
for (int i = 0; i < switchResult?.Count; i++) {
string dataString = switchResult[i].Data.ToString();
if (String.IsNullOrEmpty(dataString)) { continue; }
Expand All @@ -452,54 +453,7 @@ public static ConcurrentDictionary<string, string[]> SingleDevice(string target,
return data;
}

private static (IList<Variable>, SnmpProfiles.Profile) SnmpQueryTrialAndError(IPAddress target, SnmpProfiles.Profile[] snmpProfiles, string[] oids) {
for (int i = 0; i < snmpProfiles.Length; i++) {
IList<Variable> result = SnmpQuery(target, snmpProfiles[i], oids);

if (result is not null) {
return (result, snmpProfiles[i]);
}
}

return (null, null);
}

private static IList<Variable> SnmpQuery(IPAddress target, SnmpProfiles.Profile profile, string[] oids) {
IList<Variable> result = null;

if (profile.version == 3) {
try {
result = Protocols.Snmp.Polling.SnmpRequestV3(
target,
3000,
profile,
Protocols.Snmp.Oid.GENERIC_OID,
Protocols.Snmp.Polling.SnmpOperation.Get
);
}
catch { }
}
else {
VersionCode version = profile.version switch {
1 => VersionCode.V1,
_ => VersionCode.V2
};

try {
result = Protocols.Snmp.Polling.SnmpRequestV1V2(
target,
version,
3000,
profile.community,
Protocols.Snmp.Oid.GENERIC_OID,
Protocols.Snmp.Polling.SnmpOperation.Get
);
}
catch { }
}

return result;
}

public static byte[] SingleUserSerialize(Dictionary<string, string> parameters) {
parameters.TryGetValue("target", out string target);
Expand Down
Loading

0 comments on commit b89eba6

Please sign in to comment.