Skip to content

Commit

Permalink
Issues, cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
veniware committed Aug 20, 2024
1 parent cf9ce54 commit 1f00931
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 20 deletions.
30 changes: 17 additions & 13 deletions Protest/Tools/LiveStats.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public static async void UserStats(HttpListenerContext ctx) {
}

if (Issues.CheckPasswordStrength(entry, true, out Issues.Issue? weakPsIssue)) {
WsWriteText(ws, weakPsIssue?.ToJsonBytes(), mutex);
WsWriteText(ws, weakPsIssue?.ToLiveStatsJsonBytes(), mutex);
}
}
catch (WebSocketException ex) when (ex.WebSocketErrorCode == WebSocketError.ConnectionClosedPrematurely) {
Expand Down Expand Up @@ -195,7 +195,7 @@ public static async void DeviceStats(HttpListenerContext ctx) {
if (OperatingSystem.IsWindows()
&& _os?.value?.Contains("windows", StringComparison.OrdinalIgnoreCase) == true
&& firstAlive is not null && firstReply.Status == IPStatus.Success) {
WmiQuery(ws, mutex, firstAlive, ref wmiHostname);
WmiQuery(ws, mutex, entry.filename, firstAlive, ref wmiHostname);
}

if (firstAlive is not null
Expand Down Expand Up @@ -295,7 +295,7 @@ public static async void DeviceStats(HttpListenerContext ctx) {
}

if (Issues.CheckPasswordStrength(entry, false, out Issues.Issue? weakPsIssue)) {
WsWriteText(ws, weakPsIssue?.ToJsonBytes(), mutex);
WsWriteText(ws, weakPsIssue?.ToLiveStatsJsonBytes(), mutex);
}
}
catch (WebSocketException ex) when (ex.WebSocketErrorCode == WebSocketError.ConnectionClosedPrematurely) {
Expand All @@ -317,7 +317,7 @@ public static async void DeviceStats(HttpListenerContext ctx) {
}

[SupportedOSPlatform("windows")]
private static void WmiQuery(WebSocket ws, object mutex, string firstAlive, ref string wmiHostname) {
private static void WmiQuery(WebSocket ws, object mutex, string file, string firstAlive, ref string wmiHostname) {
try {
ManagementScope scope = Protocols.Wmi.Scope(firstAlive, 3_000);
if (scope is not null && scope.IsConnected) {
Expand All @@ -339,19 +339,19 @@ private static void WmiQuery(WebSocket ws, object mutex, string firstAlive, ref

WsWriteText(ws, $"{{\"drive\":\"{caption}\",\"total\":{nSize},\"used\":{nSize - nFree},\"path\":\"{Data.EscapeJsonText($"\\\\{firstAlive}\\{caption.Replace(":", String.Empty)}$")}\",\"source\":\"WMI\"}}", mutex);

if (Issues.CheckDiskCapacity(firstAlive, percent, caption, out Issues.Issue? diskIssue)) {
WsWriteText(ws, diskIssue?.ToJsonBytes(), mutex);
if (Issues.CheckDiskCapacity(file, firstAlive, percent, caption, out Issues.Issue? diskIssue)) {
WsWriteText(ws, diskIssue?.ToLiveStatsJsonBytes(), mutex);
}
}

using ManagementObjectCollection currentTime = new ManagementObjectSearcher(scope, new SelectQuery("SELECT * FROM Win32_UTCTime")).Get();
foreach (ManagementObject o in currentTime.Cast<ManagementObject>()) {
int year = (int)(uint)o.GetPropertyValue("Year");
int month = (int)(uint)o.GetPropertyValue("Month");
int day = (int)(uint)o.GetPropertyValue("Day");
int hour = (int)(uint)o.GetPropertyValue("Hour");
int minute = (int)(uint)o.GetPropertyValue("Minute");
int second = (int)(uint)o.GetPropertyValue("Second");
int year = Convert.ToInt32(o.GetPropertyValue("Year"));
int month = Convert.ToInt32(o.GetPropertyValue("Month"));
int day = Convert.ToInt32(o.GetPropertyValue("Day"));
int hour = Convert.ToInt32(o.GetPropertyValue("Hour"));
int minute = Convert.ToInt32(o.GetPropertyValue("Minute"));
int second = Convert.ToInt32(o.GetPropertyValue("Second"));

DateTime current = new DateTime(year, month, day, hour, minute, second);
DateTime now = DateTime.UtcNow;
Expand Down Expand Up @@ -392,6 +392,10 @@ private static void SnmpQuery(WebSocket ws, object mutex, string file, string fi
Dictionary<string, string> formatted = Protocols.Snmp.Polling.ParseResponse(result);

if (formatted is not null && formatted.TryGetValue(Protocols.Snmp.Oid.SYSTEM_UPTIME, out string snmpUptime)) {
int dotIndex = snmpUptime.LastIndexOf('.');
if (dotIndex > -1) {
snmpUptime = snmpUptime.Substring(0, dotIndex);
}
WsWriteText(ws, $"{{\"info\":\"Uptime: {Data.EscapeJsonText(snmpUptime)}\",\"source\":\"SNMP\"}}", mutex);
}

Expand Down Expand Up @@ -425,7 +429,7 @@ private static void SnmpQuery(WebSocket ws, object mutex, string file, string fi

if (Issues.CheckPrinterComponent(file, ipAddress, profile, out Issues.Issue[] issues)) {
for (int i = 0; i < issues.Length; i++) {
WsWriteText(ws, issues[i].ToJsonBytes(), mutex);
WsWriteText(ws, issues[i].ToLiveStatsJsonBytes(), mutex);
}
}

Expand Down
13 changes: 6 additions & 7 deletions Protest/Workers/Issues.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,10 @@ public struct Issue {
private static TaskWrapper task;
private static ConcurrentBag<Issue> issues = new ConcurrentBag<Issue>();

public static byte[] ToJsonBytes(this Issue issue) => JsonSerializer.SerializeToUtf8Bytes(new Dictionary<string, string> {
public static byte[] ToLiveStatsJsonBytes(this Issue issue) => JsonSerializer.SerializeToUtf8Bytes(new Dictionary<string, string> {
{ issue.severity.ToString(), issue.message },
{ "target", issue.target },
{ "category", issue.category},
{ "source", issue.source },
{ "file", issue.file},
});

public static byte[] List() {
Expand Down Expand Up @@ -254,7 +252,7 @@ public static bool CheckPasswordStrength(Database.Entry entry, bool isUser, out
return false;
}

public static bool CheckDiskCapacity(string target, double percent, string diskCaption, out Issue? issue) {
public static bool CheckDiskCapacity(string file, string target, double percent, string diskCaption, out Issue? issue) {
string message = $"{percent}% free space on disk {Data.EscapeJsonText(diskCaption)}";

if (percent <= 1) {
Expand All @@ -265,6 +263,7 @@ public static bool CheckDiskCapacity(string target, double percent, string diskC
category = "Disk drive",
source = "WMI",
isUser = false,
file = file,
timestamp = DateTime.UtcNow.Ticks,
};
return true;
Expand All @@ -278,6 +277,7 @@ public static bool CheckDiskCapacity(string target, double percent, string diskC
category = "Disk drive",
source = "WMI",
isUser = false,
file = file,
timestamp = DateTime.UtcNow.Ticks,
};
return true;
Expand All @@ -291,7 +291,8 @@ public static bool CheckDiskCapacity(string target, double percent, string diskC
category = "Disk drive",
source = "WMI",
isUser = false,
timestamp = DateTime.UtcNow.Ticks,
file = file,
timestamp = DateTime.UtcNow.Ticks,
};
return true;
}
Expand Down Expand Up @@ -340,8 +341,6 @@ public static bool CheckPrinterComponent(string file, IPAddress ipAddress, SnmpP
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));
Dictionary<string, string> componentCurrent = Protocols.Snmp.Polling.ParseResponse(Protocols.Snmp.Polling.SnmpQuery(ipAddress, profile, new string[] { Protocols.Snmp.Oid.PRINTER_TONER_CURRENT }, Protocols.Snmp.Polling.SnmpOperation.Walk));

Console.WriteLine(profile?.name);

if (componentName is not null && componentCurrent is not null && componentMax is not null &&
componentName.Count == componentCurrent.Count && componentCurrent.Count == componentMax.Count) {

Expand Down

0 comments on commit 1f00931

Please sign in to comment.