From 1f009315d7ec9359d395519e12456e3cc18c639d Mon Sep 17 00:00:00 2001 From: venizelou andreas Date: Tue, 20 Aug 2024 20:02:24 +0300 Subject: [PATCH] Issues, cleanup --- Protest/Tools/LiveStats.cs | 30 +++++++++++++++++------------- Protest/Workers/Issues.cs | 13 ++++++------- 2 files changed, 23 insertions(+), 20 deletions(-) diff --git a/Protest/Tools/LiveStats.cs b/Protest/Tools/LiveStats.cs index a1b0ca08..84e1018a 100644 --- a/Protest/Tools/LiveStats.cs +++ b/Protest/Tools/LiveStats.cs @@ -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) { @@ -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 @@ -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) { @@ -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) { @@ -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()) { - 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; @@ -392,6 +392,10 @@ private static void SnmpQuery(WebSocket ws, object mutex, string file, string fi Dictionary 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); } @@ -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); } } diff --git a/Protest/Workers/Issues.cs b/Protest/Workers/Issues.cs index 2dc55bd2..4d5065f0 100644 --- a/Protest/Workers/Issues.cs +++ b/Protest/Workers/Issues.cs @@ -40,12 +40,10 @@ public struct Issue { private static TaskWrapper task; private static ConcurrentBag issues = new ConcurrentBag(); - public static byte[] ToJsonBytes(this Issue issue) => JsonSerializer.SerializeToUtf8Bytes(new Dictionary { + public static byte[] ToLiveStatsJsonBytes(this Issue issue) => JsonSerializer.SerializeToUtf8Bytes(new Dictionary { { issue.severity.ToString(), issue.message }, { "target", issue.target }, - { "category", issue.category}, { "source", issue.source }, - { "file", issue.file}, }); public static byte[] List() { @@ -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) { @@ -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; @@ -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; @@ -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; } @@ -340,8 +341,6 @@ public static bool CheckPrinterComponent(string file, IPAddress ipAddress, SnmpP Dictionary 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 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) {