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 b89eba6 commit e7279ee
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 27 deletions.
7 changes: 4 additions & 3 deletions Protest/Protocols/Snmp.Oid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ public static class Oid {

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

public static string[] LIVESTATS_PRINTER_OID = new string[] {
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
46 changes: 27 additions & 19 deletions Protest/Protocols/Snmp.Polling.cs
Original file line number Diff line number Diff line change
Expand Up @@ -295,40 +295,48 @@ public static (IList<Variable>, SnmpProfiles.Profile) SnmpQueryTrialAndError(IPA
}

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(
IList<Variable> result = result = Protocols.Snmp.Polling.SnmpRequestV3(
target,
3000,
profile,
Protocols.Snmp.Oid.GENERIC_OID,
oids,
Protocols.Snmp.Polling.SnmpOperation.Get
);
return result;
}
catch {
return null;
}
catch { }
}
else {
VersionCode version = profile.version switch
{
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
);
List<Variable> result = new List<Variable>();

for (int i = 0; i < oids.Length; i++) {
try {
IList<Variable> single = Protocols.Snmp.Polling.SnmpRequestV1V2(
target,
version,
3000,
profile.community,
new string[] { oids[i] },
Protocols.Snmp.Polling.SnmpOperation.Get
);

for (int j = 0; j < single.Count; j++) {
result.Add(single[j]);
}
}
catch { }
}
catch { }
}

return result;
return result.Count > 0 ? result : null;
}
}
}
31 changes: 26 additions & 5 deletions Protest/Tools/LiveStats.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ firstAlive is not null &&

if (_snmpProfile is not null) {
SnmpProfiles.Profile profile = null;
if (String.IsNullOrEmpty(_snmpProfile.value) && Guid.TryParse(_snmpProfile.value, out Guid guid)) {
if (!String.IsNullOrEmpty(_snmpProfile.value) && Guid.TryParse(_snmpProfile.value, out Guid guid)) {
SnmpProfiles.Profile[] profiles = SnmpProfiles.Load();
for (int i = 0; i < profiles.Length; i++) {
if (profiles[i].guid == guid) {
Expand All @@ -194,18 +194,39 @@ firstAlive is not null &&
&& firstAlive is not null
&& firstReply.Status == IPStatus.Success
&& IPAddress.TryParse(firstAlive, out IPAddress ipAddress)) {

IList<Variable> result = Protocols.Snmp.Polling.SnmpQuery(ipAddress, profile, Protocols.Snmp.Oid.LIVESTATS_OID);

for (int i = 0; i < result?.Count; i++) {
string dataString = result[i].Data.ToString();
if (String.IsNullOrEmpty(dataString)) { continue; }
switch (result[i].Id.ToString()) {
case Protocols.Snmp.Oid.SYSTEM_UPTIME : WsWriteText(ws, $"{{\"info\":\"Uptime: {Data.EscapeJsonText(dataString)}\",\"source\":\"SNMP\"}}", mutex); break;
case Protocols.Snmp.Oid.SYSTEM_TEMPERATURE : WsWriteText(ws, $"{{\"info\":\"Temperature: {Data.EscapeJsonText(dataString)}\",\"source\":\"SNMP\"}}", mutex); break;
case Protocols.Snmp.Oid.PRINTER_STATUS : WsWriteText(ws, $"{{\"info\":\"Printer status: {Data.EscapeJsonText(dataString)}\",\"source\":\"SNMP\"}}", mutex); break;
case Protocols.Snmp.Oid.PRINTER_MESSAGE : WsWriteText(ws, $"{{\"info\":\"Printer message: {Data.EscapeJsonText(dataString)}\",\"source\":\"SNMP\"}}", mutex); break;
case Protocols.Snmp.Oid.PRINTER_JOBS : WsWriteText(ws, $"{{\"info\":\"Printer jobs: {Data.EscapeJsonText(dataString)}\",\"source\":\"SNMP\"}}", mutex); break;
case Protocols.Snmp.Oid.SYSTEM_TEMPERATURE : WsWriteText(ws, $"{{\"info\":\"Temperature: {Data.EscapeJsonText(dataString)}\",\"source\":\"SNMP\"}}", mutex); break; }
}

switch (_type.value) {
case "fax":
case "multiprinter":
case "ticket printer":
case "print":
IList<Variable> printerResult = Protocols.Snmp.Polling.SnmpQuery(ipAddress, profile, Protocols.Snmp.Oid.LIVESTATS_PRINTER_OID);
for (int i = 0; i < printerResult?.Count; i++) {
string dataPrintString = printerResult[i].Data.ToString();
if (String.IsNullOrEmpty(dataPrintString)) { continue; }
switch (result[i].Id.ToString()) {
case Protocols.Snmp.Oid.PRINTER_STATUS : WsWriteText(ws, $"{{\"info\":\"Printer status: {Data.EscapeJsonText(dataPrintString)}\",\"source\":\"SNMP\"}}", mutex); break;
case Protocols.Snmp.Oid.PRINTER_MESSAGE : WsWriteText(ws, $"{{\"info\":\"Printer message: {Data.EscapeJsonText(dataPrintString)}\",\"source\":\"SNMP\"}}", mutex); break;
case Protocols.Snmp.Oid.PRINTER_JOBS : WsWriteText(ws, $"{{\"info\":\"Total jobs: {Data.EscapeJsonText(dataPrintString)}\",\"source\":\"SNMP\"}}", mutex); break;
}
}
break;

case "firewall":
case "router":
case "switch":
//TODO:
break;
}
}
}
Expand Down

0 comments on commit e7279ee

Please sign in to comment.