Skip to content

Commit

Permalink
Issues, SNMP generalization
Browse files Browse the repository at this point in the history
  • Loading branch information
veniware committed Aug 19, 2024
1 parent 14745e9 commit 53204d4
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 38 deletions.
15 changes: 5 additions & 10 deletions Protest/Tools/LiveStats.cs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ public static async void DeviceStats(HttpListenerContext ctx) {
&& firstReply.Status == IPStatus.Success
&& entry.attributes.TryGetValue("type", out Database.Attribute _type)
&& entry.attributes.TryGetValue("snmp profile", out Database.Attribute _snmpProfile)) {
SnmpQuery(ws, mutex, firstAlive, _type?.value.ToLower(), _snmpProfile);
SnmpQuery(ws, mutex, firstAlive, _type?.value.ToLower(), _snmpProfile.value);
}

if (OperatingSystem.IsWindows() && _hostname?.value?.Length > 0) {
Expand Down Expand Up @@ -380,15 +380,10 @@ private static void WmiQuery(WebSocket ws, object mutex, string firstAlive, ref
catch { }
}

private static void SnmpQuery(WebSocket ws, object mutex, string firstAlive, string type, Database.Attribute _snmpProfile) {
SnmpProfiles.Profile profile = null;
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) continue;
profile = profiles[i];
break;
}
private static void SnmpQuery(WebSocket ws, object mutex, string firstAlive, string type, string snmpProfileGuid) {

if (!SnmpProfiles.FromGuid(snmpProfileGuid, out SnmpProfiles.Profile profile)) {
return;
}

if (profile is null) { return; }
Expand Down
27 changes: 26 additions & 1 deletion Protest/Tools/SnmpProfiles.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ public static byte[] List() {
try {
Profile[] profiles = Load();
byte[] json = JsonSerializer.SerializeToUtf8Bytes(profiles, snmpProfilesSerializerOptions);

return json;
}
catch {
Expand Down Expand Up @@ -158,6 +157,32 @@ public static byte[] Save(HttpListenerContext ctx) {

return Data.CODE_OK.Array;
}

public static bool FromGuid(string profileGuid, out Profile profile, SnmpProfiles.Profile[] snmpProfiles = null) {
if (Guid.TryParse(profileGuid, out Guid guid)) {
FromGuid(guid, out profile);
return true;
}

profile = null;
return false;
}

public static bool FromGuid(Guid profileGuid, out Profile profile, SnmpProfiles.Profile[] snmpProfiles = null) {
if (snmpProfiles is null) {
snmpProfiles = SnmpProfiles.Load();
}

for (int i = 0; i < snmpProfiles.Length; i++) {
if (snmpProfiles[i].guid != profileGuid) continue;
profile = snmpProfiles[i];
return true;
}

profile = null;
return false;
}

}

internal sealed class SnmpProfilesJsonConverter : JsonConverter<SnmpProfiles.Profile[]> {
Expand Down
15 changes: 4 additions & 11 deletions Protest/Workers/Fetch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,25 +54,18 @@ public static byte[] SingleDeviceSerialize(Dictionary<string, string> parameters
parameters.TryGetValue("target", out string target);
parameters.TryGetValue("wmi", out string wmi);
parameters.TryGetValue("kerberos", out string kerberos);
parameters.TryGetValue("snmp", out string snmp);
parameters.TryGetValue("snmp", out string snmpProfileGuid);
parameters.TryGetValue("portscan", out string portScan);

if (target is null) {
return Data.CODE_INVALID_ARGUMENT.Array;
}

SnmpProfiles.Profile snmpProfile = null;
if (snmp is not null && Guid.TryParse(snmp, out Guid guid)) {
SnmpProfiles.Profile[] profiles = SnmpProfiles.Load();
for (int i = 0; i < profiles.Length; i++) {
if (profiles[i].guid != guid) { continue; }
snmpProfile = profiles[i];
break;
}
SnmpProfiles.Profile[] snmpProfiles = null;
if (SnmpProfiles.FromGuid(snmpProfileGuid, out SnmpProfiles.Profile snmpProfile)) {
snmpProfiles = new SnmpProfiles.Profile[] { snmpProfile };
}

SnmpProfiles.Profile[] snmpProfiles = snmpProfile is null ? null : new SnmpProfiles.Profile[] { snmpProfile };

ConcurrentDictionary<string, string[]> data = SingleDevice(
target,
true,
Expand Down
44 changes: 38 additions & 6 deletions Protest/Workers/Issues.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
using Lextm.SharpSnmpLib.Security;
using Protest.Http;
using Protest.Tools;
using static Protest.Tools.DebitNotes;
using static Protest.Workers.Issues;

namespace Protest.Workers;

Expand Down Expand Up @@ -136,9 +134,6 @@ public static async void WebSocketHandler(HttpListenerContext ctx) {
IEnumerable<Issue> filtered = issues.Where(o => o.timestamp > lastTimestamp);

if (filtered.Any()) {

Console.WriteLine(filtered.Count());

byte[] bytes = JsonSerializer.SerializeToUtf8Bytes(filtered.Select(o => new {
severity = o.severity,
issue = o.message,
Expand Down Expand Up @@ -224,7 +219,8 @@ public static bool CheckPasswordStrength(Database.Entry entry, bool isUser, out
else {
target = entry.filename;
}
} else {
}
else {
if (entry.attributes.TryGetValue("ip", out Database.Attribute ipAttr)) {
target = ipAttr.value;
}
Expand Down Expand Up @@ -299,6 +295,42 @@ public static bool CheckDiskCapacity(string target, double percent, string diskC
return false;
}

public static bool CheckPrinterComponent(Database.Entry entry, out Issue[] issuse) {

if (!entry.attributes.TryGetValue("snmp profile", out Database.Attribute snmpGuidAttribute)) {
issuse = null;
return false;
}

if (!SnmpProfiles.FromGuid(snmpGuidAttribute.value, out SnmpProfiles.Profile profile)) {
issuse = null;
return false;
}

entry.attributes.TryGetValue("ip", out Database.Attribute _ip);
entry.attributes.TryGetValue("hostname", out Database.Attribute _hostname);

string[] targetsArray = Array.Empty<string>();
if (_ip?.value?.Length > 0) {
targetsArray = _ip.value.Split(';').Select(o => o.Trim()).ToArray();
}
else if (_hostname?.value?.Length > 0) {
targetsArray = _hostname.value.Split(';').Select(o => o.Trim()).ToArray();
}

if (targetsArray.Length == 0) {
issuse = null;
return false;
}

if (!IPAddress.TryParse(targetsArray[0], out IPAddress ipAddress)) {
issuse = null;
return false;
}

return CheckPrinterComponent(ipAddress, profile, out issuse);
}

public static bool CheckPrinterComponent(IPAddress ipAddress, SnmpProfiles.Profile profile, out Issue[] issues) {
Dictionary<string, string> componentName = Protocols.Snmp.Polling.ParseResponse(Protocols.Snmp.Polling.SnmpQuery(ipAddress, profile, new string[] { Protocols.Snmp.Oid.PRINTER_TONERS }, Protocols.Snmp.Polling.SnmpOperation.Walk));
Dictionary<string, string> componentMax = Protocols.Snmp.Polling.ParseResponse(Protocols.Snmp.Polling.SnmpQuery(ipAddress, profile, new string[] { Protocols.Snmp.Oid.PRINTER_TONERS_MAX }, Protocols.Snmp.Polling.SnmpOperation.Walk));
Expand Down
12 changes: 2 additions & 10 deletions Protest/Workers/Lifeline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Threading;
using Protest.Tools;
using Lextm.SharpSnmpLib;
using static Protest.Workers.Issues;

namespace Protest.Workers;

Expand Down Expand Up @@ -381,16 +382,7 @@ private static void SnmpPrinterQuery(string file, string host, string _profile,
catch { }
}

SnmpProfiles.Profile profile = null;
if (!String.IsNullOrEmpty(_profile) && Guid.TryParse(_profile, out Guid guid)) {
for (int i = 0; i < snmpProfiles.Length; i++) {
if (snmpProfiles[i].guid != guid) { continue; }
profile = snmpProfiles[i];
break;
}
}

if (profile is null) {
if (!SnmpProfiles.FromGuid(_profile, out SnmpProfiles.Profile profile, snmpProfiles)) {
return;
}

Expand Down

0 comments on commit 53204d4

Please sign in to comment.