123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314 |
- using Aitex.Core.RT.Log;
- using Aitex.Core.RT.SCCore;
- using MECF.Framework.Common.Equipment;
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.IO;
- using System.Linq;
- using System.Management;
- using System.Reflection;
- using System.ServiceModel.Channels;
- using System.Text;
- using System.Timers;
- using System.Windows;
- namespace CyberX8_RT.Modules
- {
- public class ResourceMonitor
- {
- private string processRTname = Process.GetCurrentProcess().ProcessName;
- private string processUIname = "Venus_UI";
-
- private int threshold = 800;
- private Process pro;
- private Timer _timer;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- private string Response = string.Empty;
- private string MainWindowHandle = string.Empty;
-
- private string AssociatedProcessHandle = string.Empty;
- private string Numberofhandlesopened = string.Empty;
- private string Numberofmodules = string.Empty;
- private string Basicpriority = string.Empty;
- private string Increasepriority = string.Empty;
- private string processor = string.Empty;
- private string Minimumworkingset = string.Empty;
- private string MaximumWorkingSet = string.Empty;
- private string Workset = string.Empty;
- private string Peakworkingset = string.Empty;
- private string Dedicatedmemorysize = string.Empty;
- private string UnpagedMemorySize = string.Empty;
- private string PagingMemorySize = string.Empty;
- private string Peakpagingmemorysize = string.Empty;
- private string VirtualMemorySize = string.Empty;
- private string PeakVirtualMemorySize = string.Empty;
- private string Occupytime = string.Empty;
- private string PrivilegeOccupancytime = string.Empty;
- private string Useroccupiedtime = string.Empty;
- private PerformanceCounter CpuOccupied;
- public ResourceMonitor()
- {
-
-
-
- Response = "Response or not";
- MainWindowHandle = "Main Window Handle";
-
- AssociatedProcessHandle = "Associated Process Handle ";
- Numberofhandlesopened = "Number of handles opened ";
- Numberofmodules = "Number of modules ";
- Basicpriority = "Basic priority ";
- Increasepriority = "Increase priority ";
- processor = "processor ";
- Minimumworkingset = "Minimum working set ";
- MaximumWorkingSet = "Maximum working Set ";
- Workset = "Work set ";
- Peakworkingset = "Peak working set ";
- Dedicatedmemorysize = "Dedicated memory size ";
- UnpagedMemorySize = "Unpaged Memory Size ";
- PagingMemorySize = "Paging Memory Size ";
- Peakpagingmemorysize = "Peak paging memory size ";
- VirtualMemorySize = "Virtual Memory Size ";
- PeakVirtualMemorySize = "Peak Virtual Memory Size ";
- Occupytime = "Occupy time ";
- PrivilegeOccupancytime = "Privilege Occupancy time ";
- Useroccupiedtime = "User occupied time ";
-
- }
- ~ResourceMonitor() => _timer.Dispose();
- public bool Initialize()
- {
-
- if (SC.GetValue<int>("System.CheckResourceInterval") > 0)
- {
- _timer = new Timer();
- CpuOccupied = new PerformanceCounter("Processor", "% Processor Time", "_Total");
- _timer.Enabled = true;
- _timer.Interval = SC.GetValue<int>("System.CheckResourceInterval") * 60 * 1000;
- _timer.Elapsed += GetProcessInfo;
- _timer.Start();
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- }
- return true;
- }
- private void GetProcessInfo(object sender, ElapsedEventArgs e)
- {
- try
- {
- string rtmonoitor = process_use(processRTname);
- string uimonoitor = process_use(processUIname);
- string systemMonitor = $"PC => {drive_use()}" +
- $"{cpu_use()}" +
- $"{memory_use()}";
- LOG.Write(eEvent.INFO_WINRESOURCE, ModuleName.System,
- rtmonoitor +
- " ".PadLeft(56, ' ') + uimonoitor +
- " ".PadLeft(56, ' ') + systemMonitor
- );
-
- }
- catch (Exception ex)
- {
- LOG.Write(eEvent.ERR_WINRESOURCE, ModuleName.System,$"Monitor Error happened: {ex}");
- }
- }
-
- private string process_use(string processname)
- {
- string monitor = "";
-
-
- if (Process.GetProcessesByName(processname).Length > 0)
- {
- pro = Process.GetProcessesByName(processname)[0];
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- monitor =
-
- $"{processname} => {Response}:{pro.Responding.ToString()}\t" +
- $"{MainWindowHandle}:{pro.MainWindowHandle.ToString()}\t" +
-
- $"{AssociatedProcessHandle}:{pro.Handle.ToString()}\t" +
- $"{Numberofhandlesopened}:{pro.HandleCount.ToString()}\t" +
- $"{Numberofmodules}:{pro.Modules.Count.ToString()}\t" +
- $"{Basicpriority}:{pro.BasePriority.ToString()} \t" +
- $"{Increasepriority}:{pro.PriorityBoostEnabled.ToString()}\t" +
- $"{processor}:{pro.ProcessorAffinity.ToInt32().ToString()}\t" +
-
- $"{MaximumWorkingSet}:{pro.MaxWorkingSet.ToInt32().ToString()}\t" +
- $"{Workset}:{pro.WorkingSet64.ToString()}\t" +
- $"{Peakworkingset}:{pro.PeakWorkingSet64.ToString()}\t" +
- $"{Dedicatedmemorysize}:{(pro.PrivateMemorySize64 / 1048576).ToString()}MB\t" +
- $"{UnpagedMemorySize}:{(pro.NonpagedSystemMemorySize64 / 1048576).ToString()}MB\t" +
- $"{PagingMemorySize}:{(pro.PagedMemorySize64 / 1048576).ToString()}MB\t" +
- $"{Peakpagingmemorysize}:{(pro.PeakPagedMemorySize64 / 1048576).ToString()}MB\t" +
- $"{VirtualMemorySize}:{(pro.VirtualMemorySize64 / 1048576).ToString()}MB\t" +
- $"{PeakVirtualMemorySize}:{(pro.PeakVirtualMemorySize64 / 1048576).ToString()}MB\t" +
- $"{Occupytime}:{pro.TotalProcessorTime.ToString()}\t" +
- $"{PrivilegeOccupancytime}:{pro.PrivilegedProcessorTime.ToString()}\t" +
- $"{Useroccupiedtime}:{pro.UserProcessorTime.ToString()}\r\n";
-
- if (pro.PagedMemorySize64 / 1048576 >= threshold)
- {
- LOG.Write(eEvent.WARN_WINRESOURCE, ModuleName.System, $"警告!进程 {processname} 内存异常增长,超过{threshold}MB阈值!");
- }
- }
- return monitor;
- }
-
- private string cpu_use()
- {
- float cpu_use_per = CpuOccupied.NextValue();
- string cpu_use = $"CPU usage rate:{cpu_use_per:F2}%\t";
- return cpu_use;
- }
-
-
- private string drive_use()
- {
- string drive_use = "";
- foreach (DriveInfo drive in DriveInfo.GetDrives())
- {
- if (drive.IsReady)
- {
- drive_use += $"{drive.Name}";
- drive_use += $" Total:{drive.TotalSize / 1024 / 1024 / 1024}GB";
- drive_use += $" Used:{(drive.TotalSize - drive.AvailableFreeSpace) / 1024 / 1024 / 1024}GB";
- drive_use += $" Free:{drive.AvailableFreeSpace / 1024 / 1024 / 1024}GB\t";
- }
- }
- return drive_use;
- }
-
- private string memory_use()
- {
- string memory_use = "Memory status:" +
- $"Memory Available:{GetMemoryAvailable():F2}GB\t" +
- $"Memory Used:{GetMemoryUsed():F2}GB\t" +
- $"Memory Used Rate:{GetMemoryUsedRate():F2}%\t";
- return memory_use;
- }
-
- private double? GetMemoryAvailable()
- {
- double availablebytes = 0;
- var managementClassOs = new ManagementClass("Win32_OperatingSystem");
- foreach (var managementBaseObject in managementClassOs.GetInstances())
- if (managementBaseObject["FreePhysicalMemory"] != null)
- availablebytes = 1024 * double.Parse(managementBaseObject["FreePhysicalMemory"].ToString());
- return availablebytes / GbDiv;
- }
-
- private double? GetMemoryUsed()
- {
- double? PhysicalMemory = GetPhysicalMemory();
- double? MemoryAvailable = GetMemoryAvailable();
- double? MemoryUsed = (double?)(PhysicalMemory - MemoryAvailable);
- double currentMemoryUsed = (double)MemoryUsed;
- return currentMemoryUsed;
- }
-
- private long? GetPhysicalMemory()
- {
-
- var managementClass = new ManagementClass("Win32_ComputerSystem");
- var managementObjectCollection = managementClass.GetInstances();
- foreach (var managementBaseObject in managementObjectCollection)
- if (managementBaseObject["TotalPhysicalMemory"] != null)
- {
- return long.Parse(managementBaseObject["TotalPhysicalMemory"].ToString()) / GbDiv;
- }
- return null;
- }
-
- private double? GetMemoryUsedRate()
- {
- double? PhysicalMemory = GetPhysicalMemory();
- double? MemoryAvailable = GetMemoryAvailable();
- double? MemoryUsedRate = (double?)(PhysicalMemory - MemoryAvailable) / PhysicalMemory;
- return MemoryUsedRate.HasValue ? Convert.ToDouble(MemoryUsedRate * 100) : 0;
- }
-
- #region 单位转换进制
- private const int KbDiv = 1024;
- private const int MbDiv = 1024 * 1024;
- private const int GbDiv = 1024 * 1024 * 1024;
- #endregion
- }
- }
|