|
@@ -7,6 +7,7 @@ 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;
|
|
@@ -17,10 +18,12 @@ namespace Venus_RT.Modules
|
|
|
{
|
|
|
public class ResourceMonitor
|
|
|
{
|
|
|
- private string processname = Process.GetCurrentProcess().ProcessName;
|
|
|
- private int _stringlen = 20;
|
|
|
+ private string processRTname = Process.GetCurrentProcess().ProcessName;
|
|
|
+ private string processUIname = "Venus_UI";
|
|
|
+ //private int _stringlen = 20;
|
|
|
private int threshold = 800;
|
|
|
- Timer _timer;
|
|
|
+ private Process pro;
|
|
|
+ private Timer _timer;
|
|
|
//数据波动监测可用
|
|
|
//List<double> _Numberofhandlesopened = new List<double>() { };
|
|
|
//List<double> _Numberofmodules = new List<double>() { };
|
|
@@ -40,29 +43,29 @@ namespace Venus_RT.Modules
|
|
|
//string ProcessName = string.Empty;
|
|
|
//string ProcessID = string.Empty;
|
|
|
//string Starttime = string.Empty;
|
|
|
- string Response = string.Empty;
|
|
|
- string MainWindowHandle = string.Empty;
|
|
|
+ private string Response = string.Empty;
|
|
|
+ private string MainWindowHandle = string.Empty;
|
|
|
//string MainWindowTitle = string.Empty;
|
|
|
- string AssociatedProcessHandle = string.Empty;
|
|
|
- string Numberofhandlesopened = string.Empty;
|
|
|
- string Numberofmodules = string.Empty;
|
|
|
- string Basicpriority = string.Empty;
|
|
|
- string Increasepriority = string.Empty;
|
|
|
- string processor = string.Empty;
|
|
|
- string Minimumworkingset = string.Empty;
|
|
|
- string MaximumWorkingSet = string.Empty;
|
|
|
- string Workset = string.Empty;
|
|
|
- string Peakworkingset = string.Empty;
|
|
|
- string Dedicatedmemorysize = string.Empty;
|
|
|
- string UnpagedMemorySize = string.Empty;
|
|
|
- string PagingMemorySize = string.Empty;
|
|
|
- string Peakpagingmemorysize = string.Empty;
|
|
|
- string VirtualMemorySize = string.Empty;
|
|
|
- string PeakVirtualMemorySize = string.Empty;
|
|
|
- string Occupytime = string.Empty;
|
|
|
- string PrivilegeOccupancytime = string.Empty;
|
|
|
- string Useroccupiedtime = string.Empty;
|
|
|
- PerformanceCounter CpuOccupied = new PerformanceCounter("Processor", "% Processor Time", "_Total");
|
|
|
+ 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()
|
|
|
{
|
|
|
|
|
@@ -94,10 +97,7 @@ namespace Venus_RT.Modules
|
|
|
|
|
|
}
|
|
|
|
|
|
- ~ResourceMonitor()
|
|
|
- {
|
|
|
- _timer.Dispose();//定时器销毁
|
|
|
- }
|
|
|
+ ~ResourceMonitor() => _timer.Dispose();//定时器销毁
|
|
|
|
|
|
public bool Initialize()
|
|
|
{
|
|
@@ -105,6 +105,7 @@ namespace Venus_RT.Modules
|
|
|
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;
|
|
@@ -142,83 +143,174 @@ namespace Venus_RT.Modules
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
- //判断是否存在该进程
|
|
|
- //有就开始记录
|
|
|
- if (Process.GetProcessesByName(processname).Length > 0)
|
|
|
- {
|
|
|
- Process pro = Process.GetProcessesByName(processname)[0];
|
|
|
-
|
|
|
- //_Numberofhandlesopened.Add(pro.HandleCount); //进程打开的句柄数
|
|
|
- //_Numberofmodules.Add(pro.Modules.Count); //模块数量
|
|
|
- //_Basicpriority.Add(pro.BasePriority); //基本优先级
|
|
|
- //_processor.Add(pro.ProcessorAffinity.ToInt32()); //处理器
|
|
|
- //_Minimumworkingset.Add(pro.MinWorkingSet.ToInt32()); //最小工作集
|
|
|
- //_MaximumWorkingSet.Add(pro.MaxWorkingSet.ToInt32()); //最大工作集
|
|
|
- //_Workset.Add(Convert.ToInt32(pro.WorkingSet64)); //工作集
|
|
|
- //_Peakworkingset.Add(Convert.ToInt32(pro.PeakWorkingSet64)); //峰值工作集
|
|
|
- //_Dedicatedmemorysize.Add(Convert.ToInt32(pro.PrivateMemorySize64 / 1048576)); //专用内存大小
|
|
|
- //_UnpagedMemorySize.Add(Convert.ToInt32(pro.NonpagedSystemMemorySize64 / 1048576)); //未分页内存大小
|
|
|
- //_PagingMemorySize.Add(Convert.ToInt32(pro.PagedMemorySize64 / 1048576)); //分页内存大小
|
|
|
- //_Peakpagingmemorysize.Add(Convert.ToInt32(pro.PeakPagedMemorySize64 / 1048576)); //峰值分页内存大小
|
|
|
- //_VirtualMemorySize.Add(Convert.ToInt32(pro.VirtualMemorySize64 / 1024)); //虚拟内存大小
|
|
|
- //_PeakVirtualMemorySize.Add(Convert.ToInt32(pro.PeakVirtualMemorySize64 / 1048576)); //峰值虚拟内存大小
|
|
|
-
|
|
|
- float cpu_use_per = CpuOccupied.NextValue();
|
|
|
- string cpu_use = $"CPU usage rate:{cpu_use_per:F2}%\t";
|
|
|
- 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";
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- LOG.Write(eEvent.INFO_WINRESOURCE, ModuleName.System,
|
|
|
- //$"{ProcessName}:{pro.ProcessName}" +
|
|
|
- //$"{pro.StartTime.ToLongDateString() + pro.StartTime.ToLongTimeString()}" +
|
|
|
- $"{Response}:{pro.Responding.ToString()}\t" +
|
|
|
- $"{MainWindowHandle}:{pro.MainWindowHandle.ToString()}\t" +
|
|
|
- //$"{pro.MainWindowTitle}" +
|
|
|
- $"{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" +
|
|
|
- $"{Minimumworkingset}:{pro.MinWorkingSet.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()}\t" +
|
|
|
- $"| PC => {drive_use}"+
|
|
|
- $"{cpu_use}");
|
|
|
-
|
|
|
- //如果虚拟内存大于800MB 即报警
|
|
|
- if (pro.PagedMemorySize64 / 1048576 >= threshold)
|
|
|
- {
|
|
|
- LOG.Write(eEvent.WARN_WINRESOURCE, ModuleName.System, $"警告!内存异常增长,超过{threshold}MB阈值!");
|
|
|
- MessageBox.Show($"警告!内存异常增长,超过{threshold}MB阈值!");
|
|
|
- }
|
|
|
- }
|
|
|
+ string rtmonoitor = process_use(processUIname);
|
|
|
+ string uimonoitor = process_use(processRTname);
|
|
|
+
|
|
|
+ string systemMonitor = $"PC => {drive_use()}" +
|
|
|
+ $"{cpu_use()}" +
|
|
|
+ $"{memory_use()}";
|
|
|
+
|
|
|
+ LOG.Write(eEvent.INFO_WINRESOURCE, ModuleName.System,
|
|
|
+ rtmonoitor +
|
|
|
+ uimonoitor +
|
|
|
+ systemMonitor
|
|
|
+ );
|
|
|
//没有就不做任何操作
|
|
|
-
|
|
|
+
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
LOG.Write(eEvent.ERR_WINRESOURCE, ModuleName.System,$"Monitor Error happened: {ex}");
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ //process
|
|
|
+ private string process_use(string processname)
|
|
|
+ {
|
|
|
+ string monitor = "";
|
|
|
+ //判断是否存在该进程
|
|
|
+ //有就开始记录
|
|
|
+ if (Process.GetProcessesByName(processname).Length > 0)
|
|
|
+ {
|
|
|
+ pro = Process.GetProcessesByName(processname)[0];
|
|
|
+
|
|
|
+ //_Numberofhandlesopened.Add(pro.HandleCount); //进程打开的句柄数
|
|
|
+ //_Numberofmodules.Add(pro.Modules.Count); //模块数量
|
|
|
+ //_Basicpriority.Add(pro.BasePriority); //基本优先级
|
|
|
+ //_processor.Add(pro.ProcessorAffinity.ToInt32()); //处理器
|
|
|
+ //_Minimumworkingset.Add(pro.MinWorkingSet.ToInt32()); //最小工作集
|
|
|
+ //_MaximumWorkingSet.Add(pro.MaxWorkingSet.ToInt32()); //最大工作集
|
|
|
+ //_Workset.Add(Convert.ToInt32(pro.WorkingSet64)); //工作集
|
|
|
+ //_Peakworkingset.Add(Convert.ToInt32(pro.PeakWorkingSet64)); //峰值工作集
|
|
|
+ //_Dedicatedmemorysize.Add(Convert.ToInt32(pro.PrivateMemorySize64 / 1048576)); //专用内存大小
|
|
|
+ //_UnpagedMemorySize.Add(Convert.ToInt32(pro.NonpagedSystemMemorySize64 / 1048576)); //未分页内存大小
|
|
|
+ //_PagingMemorySize.Add(Convert.ToInt32(pro.PagedMemorySize64 / 1048576)); //分页内存大小
|
|
|
+ //_Peakpagingmemorysize.Add(Convert.ToInt32(pro.PeakPagedMemorySize64 / 1048576)); //峰值分页内存大小
|
|
|
+ //_VirtualMemorySize.Add(Convert.ToInt32(pro.VirtualMemorySize64 / 1024)); //虚拟内存大小
|
|
|
+ //_PeakVirtualMemorySize.Add(Convert.ToInt32(pro.PeakVirtualMemorySize64 / 1048576)); //峰值虚拟内存大小
|
|
|
+
|
|
|
+ monitor = //$"{ProcessName}:{pro.ProcessName}" +
|
|
|
+ //$"{pro.StartTime.ToLongDateString() + pro.StartTime.ToLongTimeString()}" +
|
|
|
+ $"{processname} => {Response}:{pro.Responding.ToString()}\t" +
|
|
|
+ $"{MainWindowHandle}:{pro.MainWindowHandle.ToString()}\t" +
|
|
|
+ //$"{pro.MainWindowTitle}" +
|
|
|
+ $"{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" +
|
|
|
+ //$"{Minimumworkingset}:{pro.MinWorkingSet.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";
|
|
|
+
|
|
|
+ //如果虚拟内存大于800MB 即报警
|
|
|
+ if (pro.PagedMemorySize64 / 1048576 >= threshold)
|
|
|
+ {
|
|
|
+ LOG.Write(eEvent.WARN_WINRESOURCE, ModuleName.System, $"警告!进程 {processname} 内存异常增长,超过{threshold}MB阈值!");
|
|
|
+ MessageBox.Show($"警告!进程 {processname} 内存异常增长,超过{threshold}MB阈值!");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return monitor;
|
|
|
+ }
|
|
|
+
|
|
|
+ //CPU
|
|
|
+ private string cpu_use()
|
|
|
+ {
|
|
|
+
|
|
|
+ float cpu_use_per = CpuOccupied.NextValue();
|
|
|
+ string cpu_use = $"CPU usage rate:{cpu_use_per:F2}%\t";
|
|
|
+ return cpu_use;
|
|
|
+ }
|
|
|
+
|
|
|
+ //disk
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+ //memory
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+
|
|
|
+ //MemoryAvailable
|
|
|
+ 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;
|
|
|
+
|
|
|
+ }
|
|
|
+ //MemoryUsed
|
|
|
+ private double? GetMemoryUsed()
|
|
|
+ {
|
|
|
+ double? PhysicalMemory = GetPhysicalMemory();
|
|
|
+ double? MemoryAvailable = GetMemoryAvailable();
|
|
|
+ double? MemoryUsed = (double?)(PhysicalMemory - MemoryAvailable);
|
|
|
+ double currentMemoryUsed = (double)MemoryUsed;
|
|
|
+ return currentMemoryUsed;
|
|
|
+ }
|
|
|
+ //PhysicalMemory
|
|
|
+ private long? GetPhysicalMemory()
|
|
|
+ {
|
|
|
+ //获得物理内存
|
|
|
+ var managementClass = new ManagementClass("Win32_ComputerSystem");
|
|
|
+ var managementObjectCollection = managementClass.GetInstances();
|
|
|
+ long PhysicalMemory;
|
|
|
+ foreach (var managementBaseObject in managementObjectCollection)
|
|
|
+ if (managementBaseObject["TotalPhysicalMemory"] != null)
|
|
|
+ {
|
|
|
+ return long.Parse(managementBaseObject["TotalPhysicalMemory"].ToString()) / GbDiv;
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+
|
|
|
+ }
|
|
|
+ //MemoryUsedRate
|
|
|
+ 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
|
|
|
}
|
|
|
}
|