⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 monitorappclass.cs

📁 监控系统
💻 CS
字号:
using System;
using System.IO;
using System.Data.SqlClient;
using System.Data.OleDb;
using System.Diagnostics;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;

namespace MonitorSystem.BasicClass
{
	/// <summary>
	/// 目录监控规则
	/// </summary>
	public class CatalogRule:MonitorRule
	{
		public string WatchPath="";
		public string WatchFilter="";

		/// <summary>
		/// 获取指定目录下,指定文件的数量
		/// </summary>
		/// <returns>文件数量</returns>
		public int GetFileNumber(string strDirectory, string strExtension)
		{
			DirectoryInfo diMonitor = new DirectoryInfo(strDirectory);
			int number = 0;

			try
			{
				FileInfo[] fiArr = diMonitor.GetFiles();
				foreach (FileInfo fri in fiArr)
				{
					if (strExtension == fri.Extension || strExtension == ".*")
					{
						number++;
					}
				}
				return number;
			}
			catch(IOException) 
			{
				return -1;
			}
		}

		public override object ExcuteCollect()
		{
			this.LastExcuteTime = DateTime.Now;
			return GetFileNumber(this.WatchPath, this.WatchFilter);			
		}
	}

	/// <summary>
	/// 文件监控规则
	/// </summary>
	public class FileRule : MonitorRule
	{
		public string WatchFile="";

		/// <summary>
		/// 获取指定文件的大小。单位:KB
		/// </summary>
		/// <param name="strFileName">指定文件名(完整路径)</param>
		/// <returns>文件大小</returns>
		public long GetFileSize(string strFileName)
		{
			try 
			{
				FileInfo fiMonitor = new FileInfo(strFileName);
				return fiMonitor.Length/1024;
			}
			catch(IOException) 
			{
				return -1;
			}
		}

		public override object ExcuteCollect()
		{
			this.LastExcuteTime = DateTime.Now;
			return GetFileSize(this.WatchFile);
		}
	}


	/// <summary>
	/// 数据库表监控规则
	/// </summary>
	public class TableRule:MonitorRule
	{
		public string Connstring="";
		public string SqlScript="";
		public string KeyField="";

		/// <summary>
		/// 获取应用系统中数据库的监控信息
		/// </summary>
		/// <param name="strConnection">数据库连接字符串</param>
		/// <param name="strSql">查询语句</param>
		/// <returns>监控信息</returns>
		public object GetAppDBInfo(string strConnection, string strSql)
		{
			int ret=0;
			OleDbConnection conn=null;
			OleDbCommand comm=null;

			try
			{
				conn = new OleDbConnection(strConnection);
				comm = new OleDbCommand(strSql,conn);
				comm.Connection.Open();

				if(comm.ExecuteScalar() != null)
				{
					ret = Convert.ToInt32(comm.ExecuteScalar());
				}
			}
			catch(Exception ex)
			{
				string msg = String.Format("数据库监控异常:\r{0}\r{1}\r原因:{2}",strConnection,strSql,ex.Message);
				SystemLog m_SysLog = new SystemLog();
				m_SysLog.WriteToSysLog(0,msg);
				ret=-1;
			}
			finally
			{
				if(comm!=null)
				{
					comm=null;
				}
				if(conn!=null)
				{
					conn.Close();
				}
			}
			
			return ret;
		}

		public override object ExcuteCollect()
		{
			this.LastExcuteTime = DateTime.Now;
			return GetAppDBInfo(this.Connstring, this.SqlScript);
		}
	}

	/// <summary>
	/// 进程监控规则
	/// </summary>
	public class ProcessRule:MonitorRule
	{
		public string m_ProcessName="";
		/// <summary>
		/// 获取指定应用程序进程的监控信息
		/// </summary>
		/// <param name="strProcessName">进程名</param>
		/// <returns>监控信息</returns>
		public int GetProcessInfo(string strProcessName)
		{
			Process[] ps = Process.GetProcessesByName(strProcessName);

			return ps.Length;
		}

		public override object ExcuteCollect()
		{
			this.LastExcuteTime = DateTime.Now;
			return GetProcessInfo(this.m_ProcessName);
		}
	}




	/// <summary>
	/// 端口监控规则
	/// </summary>
	public class PortRule:MonitorRule
	{
		public int m_Port=-1;
		public string m_PortType="";
		/// 新增
		public string RemoteIp = ""; //Port/Listener

		[ StructLayout( LayoutKind.Sequential )]
			public struct MIB_TCPROW
		{
			public long dwState;
			public long dwLocalAddr;
			public long dwLocalPort;
			public long dwRemoteAddr;
			public long dwRemotePort;
		}

		[ StructLayout( LayoutKind.Sequential )]
			public class MIB_TCPTABLE
		{
			public long dwNumEntries;
			[MarshalAs(UnmanagedType.ByValTStr, SizeConst=100)]
			public MIB_TCPROW[] table;
		}

		[ DllImport( "iphlpapi.dll" )]
		public static extern void GetTcpTable(ref MIB_TCPTABLE pTcpTable,ref long pdwSize,long bOrder);

		/// <summary>
		/// 监视端口状态
		/// </summary>
		/// <param name="port">端口号</param>
		/// <param name="strPortType">端口类型tcp/udp</param>
		/// <returns>
		/// 0 - 端口打开
		/// 其他 - 错误代码
		/// </returns>
		public int GetPortInfo(int port, string strPortType)
		{
			strPortType = strPortType.ToUpper();
			if (strPortType == "TCP")
			{
				try 
				{ 
					TcpClient tcp = new TcpClient(); 
					tcp.Connect(IPAddress.Parse("127.0.0.1"), port); 
					//该处如果建立连接错误的话,将不执行下面的代码.. 
					tcp.Close();
					return 0;
				
				}
				catch(SocketException se)
				{
					return se.ErrorCode;
				}
			}
			else
			{
				try
				{
					UdpClient udp = new UdpClient();
					udp.Connect(IPAddress.Parse("127.0.0.1"), port); 
					//该处如果建立连接错误的话,将不执行下面的代码.. 
					udp.Close();
					return 0;
				}
				catch(SocketException se)
				{
					return se.ErrorCode;
				}
			}
		}

		public override object ExcuteCollect()
		{
			this.LastExcuteTime = DateTime.Now;

			MIB_TCPTABLE TcpTable = new MIB_TCPTABLE();
			long dwSize = Marshal.SizeOf(TcpTable );


			return GetPortInfo(this.m_Port, this.m_PortType);
		}
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -