?? monitorappclass.cs
字號(hào):
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>
/// 目錄監(jiān)控規(guī)則
/// </summary>
public class CatalogRule:MonitorRule
{
public string WatchPath="";
public string WatchFilter="";
/// <summary>
/// 獲取指定目錄下,指定文件的數(shù)量
/// </summary>
/// <returns>文件數(shù)量</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>
/// 文件監(jiān)控規(guī)則
/// </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>
/// 數(shù)據(jù)庫(kù)表監(jiān)控規(guī)則
/// </summary>
public class TableRule:MonitorRule
{
public string Connstring="";
public string SqlScript="";
public string KeyField="";
/// <summary>
/// 獲取應(yīng)用系統(tǒng)中數(shù)據(jù)庫(kù)的監(jiān)控信息
/// </summary>
/// <param name="strConnection">數(shù)據(jù)庫(kù)連接字符串</param>
/// <param name="strSql">查詢語(yǔ)句</param>
/// <returns>監(jiān)控信息</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("數(shù)據(jù)庫(kù)監(jiān)控異常:\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>
/// 進(jìn)程監(jiān)控規(guī)則
/// </summary>
public class ProcessRule:MonitorRule
{
public string m_ProcessName="";
/// <summary>
/// 獲取指定應(yīng)用程序進(jìn)程的監(jiān)控信息
/// </summary>
/// <param name="strProcessName">進(jìn)程名</param>
/// <returns>監(jiān)控信息</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>
/// 端口監(jiān)控規(guī)則
/// </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>
/// 監(jiān)視端口狀態(tài)
/// </summary>
/// <param name="port">端口號(hào)</param>
/// <param name="strPortType">端口類型tcp/udp</param>
/// <returns>
/// 0 - 端口打開(kāi)
/// 其他 - 錯(cuò)誤代碼
/// </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);
//該處如果建立連接錯(cuò)誤的話,將不執(zhí)行下面的代碼..
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);
//該處如果建立連接錯(cuò)誤的話,將不執(zhí)行下面的代碼..
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);
}
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -