何為心跳監控系統?
故名思義,就是監控某個或某些個程序的運行狀態,就好比醫院里面的心跳監視儀一樣,能夠隨時顯示病人的心跳情況。
心跳監控的目的是什么?
與醫院里面的心跳監視儀目的類似,監控程序運行狀態,一旦出現問題(比如:一些自動運行的服務、程序等突然停止運行了),那么心跳監控系統就能“感知到”並及時的顯示在監控界面上,同時可以通過微信、短信告之相關的人員,以便他們及時處理程序異常,從而避免一些自動運行的服務、程序等突然停止運行而造成的一系列損失
心跳監控系統實現的思路是怎樣的?
核心技術:WCF的雙工通訊
實現步驟:
1.定義WCF的服務契約接口以及回調接口,服務方法主要包括:Start(被監控的程序啟動時調用,即通知監控系統,我已經啟動了)、Stop(被監控的程序停止時調用,即通知監控系統,我已經停止了)、ReportRunning(被監控的程序運行中定時調用,即通知監控系統,我是正常的並在運行中,同時還起到檢測監控系統是否在運行的一個功能,一舉兩得),回調服務方法應有:Listen(這個是給監控系統主動去定時回調被監控的程序(心跳),如果被監控的程序能正常的返回狀態,那么就是正常的,否則有可能已經“死了”,這時監控系統就需要按照預設指令作出相應的操作,比如:監控主界面顯示異常的程序,同時發送異常通知消息給相關人員)
2.建立一個心跳監控系統Winform項目,並實現WCF服務,即集成實現WCF服務宿主程序,同時每一個WCF服務方法均需關聯界面,即:程序啟動了、停止了、運行中均會在界面實時顯示出來或做一些實時統計;
3.其它被監控的程序(指自動運行的服務、程序等)需要集成實現WCF回調接口及開啟WCF服務通訊的功能;
心跳監控系統的運行順序是怎樣的?如何保證監控方與被監控方實時不間斷通訊?
運行順序:
1.開啟心跳監控系統(即:同時開啟WCF服務宿主程序),確保監控服務正常運行;
2.開啟其它被監控的程序,確保開啟客戶端與監控系統的WCF雙工通訊服務正常;
注意:一定要先開啟心跳監控系統,否則其它被監控的程序因無法與監控系統的WCF雙工通訊服務正常連接而報錯或多次嘗試重連;
保證監控方與被監控方實時不間斷通訊:
在保證按照上面所說的運行順序依次開啟心跳監控系統,再開啟其它被監控的程序,正常建立一次通訊后,后續只要任何一方出現通訊中斷,均會自動嘗試重連(主要是客戶端重連,心跳監控系統除非停止運行,否則不會中斷,若因停止運行造成雙方通訊中斷,只需重啟心跳監控系統即可)
通訊模式:
推模式:被監控的程序通過主動的調用WCF服務方法:Start、Stop、ReportRunning 向心跳監控系統告之運行狀態,若通訊失敗,則自動嘗試重連,直至連接成功;
拉模式:心跳監控系統主動回調Listen方法,向被監控的程序索取運行狀態,若通訊失敗,則會在指定時間內多次重試回調客戶端,若客戶端在規定的時間范圍內仍無法返回消息,則視為客戶端異常,那么心跳監控系統則會進行異常的處理;
實現源代碼:
ProgramMonitor.Core 類庫項目:主要是定義WCF服務的相關接口以及實現回調的接口(因為每個客戶端都需實現一遍回調接口,故統一實現,客戶端集成后直接可以用,簡化集成客戶端的開發成本),心跳監控系統及需要被監控的程序均需要依賴該DLL;
ProgramMonitor WINFORM項目:心跳監控系統
整個解決方案如下圖示:

注:由於源代碼相對較多,故不再一一說明,只對重點的代碼作解釋
ProgramMonitor.Core
IListenService:(心跳監控WCF服務契約接口)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
namespace ProgramMonitor.Core
{
[ServiceContract(Namespace = "http://www.zuowenjun.cn", SessionMode = SessionMode.Required, CallbackContract = typeof(IListenCall))]
public interface IListenService
{
[OperationContract(IsOneWay = true)]
void Start(ProgramInfo programInfo);
[OperationContract(IsOneWay = true)]
void Stop(string programId);
[OperationContract(IsOneWay = true)]
void ReportRunning(ProgramInfo programInfo);
}
}
IListenCall:(監聽回調服務接口,主用被心跳監控系統調用)
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
namespace ProgramMonitor.Core
{
public interface IListenCall
{
[OperationContract]
int Listen(string programId);
}
}
ProgramInfo:(程序信息類,主要用於獲取被監控程序的基本信息)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.Runtime.Serialization;
namespace ProgramMonitor.Core
{
[DataContract(Namespace = "http://www.zuowenjun.cn")]
public class ProgramInfo
{
public ProgramInfo()
{
}
[DataMember]
public string Id { get; internal set; }
[DataMember]
public string Name { get; set; }
[DataMember]
public string Version { get; set; }
[DataMember]
public string InstalledLocation { get; set; }
[DataMember]
public string Description { get; set; }
private int runState = -1;
/// <summary>
/// 運行狀態,-1:表示停止,0表示啟動,1表示運行中
/// </summary>
[DataMember]
public int RunState
{
get
{
return runState;
}
set
{
this.UpdateStateTime = DateTime.Now;
if (value < 0)
{
runState = -1;
this.StopTime = this.UpdateStateTime;
}
else if (value == 0)
{
runState = 0;
this.StartTime = this.UpdateStateTime;
}
else
{
runState = 1;
}
}
}
[DataMember]
public DateTime UpdateStateTime { get; private set; }
[DataMember]
public DateTime StartTime { get; private set; }
[DataMember]
public DateTime StopTime { get; private set; }
}
}
ListenClient:(監聽客戶端類,實現WCF回調服務接口,主要用於被監控的程序)
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.Text;
namespace ProgramMonitor.Core
{
public class ListenClient : IListenCall
{
private static object syncObject = new object();
private static ListenClient instance = null;
private ProgramInfo programInfo = null;
private string serviceHostAddr = null;
private int autoReportRunningInterval = 300;
private DuplexChannelFactory<IListenService> listenServiceFactory = null;
private IListenService proxyListenService = null;
private System.Timers.Timer reportRunningTimer = null;
private ListenClient(ProgramInfo programInfo, string serviceHostAddr = null, int autoReportRunningInterval = 300)
{
programInfo.Id = CreateProgramId();
this.programInfo = programInfo;
this.serviceHostAddr = serviceHostAddr;
if (autoReportRunningInterval >= 60) //最低1分鍾的間隔
{
this.autoReportRunningInterval = autoReportRunningInterval;
}
BuildAutoReportRunningTimer();
}
private void BuildAutoReportRunningTimer()
{
reportRunningTimer = new System.Timers.Timer(autoReportRunningInterval * 1000);
reportRunningTimer.Elapsed += (s, e) =>
{
ReportRunning();
};
}
private void BuildListenClientService()
{
if (listenServiceFactory == null)
{
if (string.IsNullOrEmpty(serviceHostAddr))
{
serviceHostAddr = System.Configuration.ConfigurationManager.AppSettings["ServiceHostAddr"];
}
InstanceContext instanceContext = new InstanceContext(instance);
NetTcpBinding binding = new NetTcpBinding();
binding.ReceiveTimeout = new TimeSpan(0, 5, 0);
binding.SendTimeout = new TimeSpan(0, 5, 0);
Uri baseAddress = new Uri(string.Format("net.tcp://{0}/ListenService", serviceHostAddr));
listenServiceFactory = new DuplexChannelFactory<IListenService>(instanceContext, binding, new EndpointAddress(baseAddress));
}
proxyListenService = listenServiceFactory.CreateChannel();
}
public static ListenClient GetInstance(ProgramInfo programInfo, string serviceHostAddr = null, int autoReportRunningInterval = 300)
{
if (instance == null)
{
lock (syncObject)
{
if (instance == null)
{
instance = new ListenClient(programInfo, serviceHostAddr, autoReportRunningInterval);
instance.BuildListenClientService();
}
}
}
return instance;
}
public void ReportStart()
{
proxyListenService.Start(programInfo);
reportRunningTimer.Start();
}
public void ReportStop()
{
proxyListenService.Stop(programInfo.Id);
reportRunningTimer.Stop();
}
public void ReportRunning()
{
try
{
proxyListenService.ReportRunning(programInfo);
}
catch
{
BuildListenClientService();
}
}
int IListenCall.Listen(string programId)
{
if (programInfo.Id.Equals(programId, StringComparison.OrdinalIgnoreCase))
{
if (programInfo.RunState >= 0)
{
return 1;
}
}
return -1;
}
private string CreateProgramId()
{
Process currProcess = Process.GetCurrentProcess();
int procCount = Process.GetProcessesByName(currProcess.ProcessName).Length;
string currentProgramPath = currProcess.MainModule.FileName;
return GetMD5HashFromFile(currentProgramPath) + "_" + procCount;
}
private string GetMD5HashFromFile(string fileName)
{
try
{
byte[] hashData = null;
using (FileStream fs = new FileStream(fileName, System.IO.FileMode.Open, FileAccess.Read))
{
MD5 md5 = new MD5CryptoServiceProvider();
hashData = md5.ComputeHash(fs);
fs.Close();
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < hashData.Length; i++)
{
sb.Append(hashData[i].ToString("x2"));
}
return sb.ToString();
}
catch (Exception ex)
{
throw new Exception("GetMD5HashFromFile Error:" + ex.Message);
}
}
}
}
這里特別說一下:CreateProgramId方法,因為心跳監控系統主要是依據ProgramId來識別每個不同的程序,故ProgramId非常重要,而我這里采用的是文件的 MD5值+進程數作為ProgramId,有些人可能要問,為什么要加進程數呢?原因很簡單,因為有些程序是允許開啟多個的實例的,如果不加進程數,那么心跳監控系統就無法識別多個同一個程序到底是哪個。
ProgramMonitor
ListenService:(WCF心跳監控服務接口實現類)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ProgramMonitor.Core;
using System.ServiceModel;
namespace ProgramMonitor.Service
{
[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Reentrant, InstanceContextMode = InstanceContextMode.PerCall)]
public class ListenService : IListenService
{
public void Start(ProgramInfo programInfo)
{
var listenCall = OperationContext.Current.GetCallbackChannel<IListenCall>();
Common.SaveProgramStartInfo(programInfo, listenCall);
}
public void Stop(string programId)
{
Common.SaveProgramStopInfo(programId);
}
public void ReportRunning(ProgramInfo programInfo)
{
var listenCall = OperationContext.Current.GetCallbackChannel<IListenCall>();
Common.SaveProgramRunningInfo(programInfo, listenCall);
}
}
}
Common:(通用業務邏輯類,主要用於WCF與UI實時溝通與聯動)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ProgramMonitor.Core;
using System.Collections.Concurrent;
using System.Timers;
using System.Threading;
using Timer = System.Timers.Timer;
using ProgramMonitor.Service;
using System.Data.SqlClient;
using log4net;
namespace ProgramMonitor
{
public static class Common
{
public static ConcurrentDictionary<string, ProgramInfo> ProgramInfos = null;
public static ConcurrentDictionary<string, IListenCall> ListenCalls = null;
public static ConcurrentBag<string> ManualStopProgramIds = null;
public static System.Timers.Timer loadTimer = null;
public static Timer listenTimer = null;
public static SynchronizationContext SyncContext = null;
public static Action<ProgramInfo, bool> RefreshListView;
public static Action<ProgramInfo, bool> RefreshTabControl;
public static int ClearInterval = 5;
public static int ListenInterval = 2;
public static bool Listening = false;
public static string DbConnString = null;
public static string[] NoticePhoneNos = null;
public static string NoticeWxUserIds = null;
public static ILog Logger = LogManager.GetLogger("ProgramMonitor");
public const string SqlProviderName = "System.Data.SqlClient";
public static void SaveProgramStartInfo(ProgramInfo programInfo, IListenCall listenCall)
{
programInfo.RunState = 0;
ProgramInfos.AddOrUpdate(programInfo.Id, programInfo, (key, value) => programInfo);
ListenCalls.AddOrUpdate(programInfo.Id, listenCall, (key, value) => listenCall);
RefreshListView(programInfo, false);
RefreshTabControl(programInfo, true);
WriteLog(string.Format("程序名:{0},版本:{1},已啟動運行", programInfo.Name, programInfo.Version), false);
}
public static void SaveProgramStopInfo(string programId)
{
ProgramInfo programInfo;
if (ProgramInfos.TryGetValue(programId, out programInfo))
{
programInfo.RunState = -1;
RefreshListView(programInfo, false);
IListenCall listenCall = null;
ListenCalls.TryRemove(programId, out listenCall);
RefreshTabControl(programInfo, true);
}
WriteLog(string.Format("程序名:{0},版本:{1},已停止運行", programInfo.Name, programInfo.Version), false);
}
public static void SaveProgramRunningInfo(ProgramInfo programInfo, IListenCall listenCall)
{
if (!ProgramInfos.ContainsKey(programInfo.Id) || !ListenCalls.ContainsKey(programInfo.Id))
{
SaveProgramStartInfo(programInfo, listenCall);
}
programInfo.RunState = 1;
RefreshTabControl(programInfo, true);
WriteLog(string.Format("程序名:{0},版本:{1},正在運行中", programInfo.Name, programInfo.Version), false);
}
public static void AutoLoadProgramInfos()
{
if (loadTimer == null)
{
loadTimer = new Timer(1 * 60 * 1000);
loadTimer.Elapsed += delegate(object sender, ElapsedEventArgs e)
{
var timer = sender as Timer;
try
{
timer.Stop();
foreach (var item in ProgramInfos)
{
var programInfo = item.Value;
RefreshListView(programInfo, false);
}
}
finally
{
if (Listening)
{
timer.Start();
}
}
};
}
else
{
loadTimer.Interval = 1 * 60 * 1000;
}
loadTimer.Start();
}
public static void AutoListenPrograms()
{
if (listenTimer == null)
{
listenTimer = new Timer(ListenInterval * 60 * 1000);
listenTimer.Elapsed += delegate(object sender, ElapsedEventArgs e)
{
var timer = sender as Timer;
try
{
timer.Stop();
foreach (var item in ListenCalls)
{
bool needUpdateStatInfo = false;
var listenCall = item.Value;
var programInfo = ProgramInfos[item.Key];
int oldRunState = programInfo.RunState;
try
{
programInfo.RunState = listenCall.Listen(programInfo.Id);
}
catch
{
if (programInfo.RunState != -1)
{
programInfo.RunState = -1;
needUpdateStatInfo = true;
}
}
if (programInfo.RunState == -1 && programInfo.StopTime.AddMinutes(5) < DateTime.Now) //如果停了5分鍾,則發一次短信
{
SendNoticeSms(programInfo);
SendNoticeWeiXin(programInfo);
programInfo.RunState = -1;//重新刷新狀態
}
if (oldRunState != programInfo.RunState)
{
needUpdateStatInfo = true;
WriteLog(string.Format("程序名:{0},版本:{1},運行狀態變更為:{2}", programInfo.Name, programInfo.Version,programInfo.RunState), false);
}
RefreshTabControl(programInfo, needUpdateStatInfo);
}
}
finally
{
if (Listening)
{
timer.Start();
}
}
};
}
else
{
listenTimer.Interval = ListenInterval * 60 * 1000;
}
listenTimer.Start();
}
public static void SendNoticeSms(ProgramInfo programInfo)
{
if (NoticePhoneNos == null || NoticePhoneNos.Length <= 0) return;
using (DataAccess da = new DataAccess(Common.DbConnString, Common.SqlProviderName))
{
da.UseTransaction();
foreach (string phoneNo in NoticePhoneNos)
{
var parameters = da.ParameterHelper.AddParameter("@Mbno", phoneNo)
.AddParameter("@Msg", string.Format("程序名:{0},版本:{1},安裝路徑:{2},已停止運行了,請盡快處理!",
programInfo.Name, programInfo.Version, programInfo.InstalledLocation))
.AddParameter("@SendTime", DateTime.Now)
.AddParameter("@KndType", "監控異常通知")
.ToParameterArray();
da.ExecuteCommand("insert into OutBox(Mbno,Msg,SendTime,KndType) values(@Mbno,@Msg,@SendTime,@KndType)", paramObjs: parameters);
}
da.Commit();
WriteLog(string.Format("程序名:{0},版本:{1},已停止運行超過5分鍾,成功發送短信通知到:{2}",
programInfo.Name, programInfo.Version, string.Join(",", NoticePhoneNos)), false);
}
}
public static void SendNoticeWeiXin(ProgramInfo programInfo)
{
if (string.IsNullOrEmpty(NoticeWxUserIds)) return;
string msg = string.Format("程序名:{0},版本:{1},安裝路徑:{2},已停止運行了,請盡快處理!",
programInfo.Name, programInfo.Version, programInfo.InstalledLocation);
var wx = new WeChat();
var result = wx.SendMessage(NoticeWxUserIds, msg);
if (result["errmsg"].ToString().Equals("ok", StringComparison.OrdinalIgnoreCase))
{
WriteLog(string.Format("程序名:{0},版本:{1},已停止運行超過5分鍾,成功發送微信通知到:{2}", programInfo.Name, programInfo.Version,NoticeWxUserIds), false);
}
}
public static void BuildConnectionString(string server, string db, string uid, string pwd)
{
SqlConnectionStringBuilder connStrBuilder = new SqlConnectionStringBuilder();
connStrBuilder.DataSource = server;
connStrBuilder.InitialCatalog = db;
connStrBuilder.UserID = uid;
connStrBuilder.Password = pwd;
connStrBuilder.IntegratedSecurity = false;
connStrBuilder.ConnectTimeout = 15;
DbConnString = connStrBuilder.ToString();
}
public static void WriteLog(string msg, bool isError = false)
{
if (isError)
{
Logger.Error(msg);
}
else
{
Logger.Info(msg);
}
}
}
}
FrmMain:(心跳監控系統窗體類)
using ProgramMonitor.Core;
using ProgramMonitor.Service;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace ProgramMonitor
{
public partial class FrmMain : Form
{
private ServiceHost serviceHost = null;
public FrmMain()
{
InitializeComponent();
tabControl1.SizeMode = TabSizeMode.Fixed;
tabControl1.ItemSize = new Size(0, 1);
#if (DEBUG)
btnTestSend.Visible = true;
#else
btnTestSend.Visible = false;
#endif
Common.SyncContext = SynchronizationContext.Current;
Common.ProgramInfos = new ConcurrentDictionary<string, ProgramInfo>();
Common.ListenCalls = new ConcurrentDictionary<string, IListenCall>();
Common.ManualStopProgramIds = new ConcurrentBag<string>();
Common.RefreshListView = RefreshListView;
Common.RefreshTabControl = RefreshTabControl;
}
#region 自定義方法區域
private void RefreshListView(ProgramInfo programInfo, bool needUpdateStatInfo)
{
Common.SyncContext.Post(o =>
{
string listViewItemKey = string.Format("lvItem_{0}", programInfo.Id);
if (!listView1.Items.ContainsKey(listViewItemKey))
{
var lstItem = listView1.Items.Add(listViewItemKey, programInfo.Name, 0);
lstItem.Name = listViewItemKey;
lstItem.Tag = programInfo.Id;
lstItem.SubItems.Add(programInfo.Version);
lstItem.SubItems.Add(programInfo.InstalledLocation);
lstItem.ToolTipText = programInfo.Description;
if (needUpdateStatInfo)
{
UpdateProgramListenStatInfo();
}
}
else
{
if (!Common.ListenCalls.ContainsKey(programInfo.Id) && programInfo.RunState == -1 && Common.ClearInterval > 0
&& programInfo.StopTime.AddMinutes(Common.ClearInterval) < DateTime.Now) //當屬於正常關閉的程序在指定時間后從監控列表中移除
{
RemoveListenItem(programInfo.Id);
}
}
}, null);
}
private void RefreshTabControl(ProgramInfo programInfo, bool needUpdateStatInfo)
{
Common.SyncContext.Post(o =>
{
string tabPgName = string.Format("tabpg_{0}", programInfo.Id);
string msgCtrlName = string.Format("{0}_MsgText", tabPgName);
if (!tabControl1.TabPages.ContainsKey(tabPgName))
{
RichTextBox rchTextBox = new RichTextBox();
rchTextBox.Name = msgCtrlName;
rchTextBox.Dock = DockStyle.Fill;
rchTextBox.ReadOnly = true;
AppendTextToRichTextBox(rchTextBox, programInfo);
var tabPg = new TabPage();
tabPg.Name = tabPgName;
tabPg.Controls.Add(rchTextBox);
tabControl1.TabPages.Add(tabPg);
}
else
{
var tabPg = tabControl1.TabPages[tabPgName];
var rchTextBox = tabPg.Controls[msgCtrlName] as RichTextBox;
AppendTextToRichTextBox(rchTextBox, programInfo);
}
if (needUpdateStatInfo)
{
UpdateProgramListenStatInfo();
}
}, null);
}
private void UpdateProgramListenStatInfo()
{
int runCount = Common.ProgramInfos.Count(p => p.Value.RunState >= 0);
labRunCount.Text = string.Format("{0}個", runCount);
labStopCount.Text = string.Format("{0}個", Common.ProgramInfos.Count - runCount);
foreach (ListViewItem lstItem in listView1.Items)
{
string programId = lstItem.Tag.ToString();
if (Common.ProgramInfos[programId].RunState == -1)
{
lstItem.ForeColor = Color.Red;
}
else
{
lstItem.ForeColor = Color.Black;
}
}
}
private void RemoveListenItem(string programInfoId)
{
ProgramInfo programInfo = Common.ProgramInfos[programInfoId];
listView1.Items.RemoveByKey(string.Format("lvItem_{0}", programInfo.Id));
tabControl1.TabPages.RemoveByKey(string.Format("tabpg_{0}", programInfo.Id));
Common.ProgramInfos.TryRemove(programInfo.Id, out programInfo);
IListenCall listenCall = null;
Common.ListenCalls.TryRemove(programInfoId, out listenCall);
UpdateProgramListenStatInfo();
}
private void AppendTextToRichTextBox(RichTextBox rchTextBox, Core.ProgramInfo programInfo)
{
Color msgColor = Color.Black;
string lineMsg = string.Format("{0:yyyy-MM-dd HH:mm}\t{1}({2})\t{3} \n", DateTime.Now, programInfo.Name, programInfo.Version, GetRunStateString(programInfo.RunState, out msgColor));
rchTextBox.SelectionColor = msgColor;
rchTextBox.SelectionStart = rchTextBox.Text.Length;
rchTextBox.AppendText(lineMsg);
rchTextBox.SelectionLength = rchTextBox.Text.Length;
if(rchTextBox.Lines.Length>1000)
{
}
}
private string GetRunStateString(int runState, out Color msgColor)
{
if (runState < 0)
{
msgColor = Color.Red;
return "程序已停止運行";
}
else if (runState == 0)
{
msgColor = Color.Blue;
return "程序已啟動運行";
}
else
{
msgColor = Color.Black;
return "程序已在運行中";
}
}
private void StartListenService()
{
if (serviceHost == null)
{
string serviceHostAddr = System.Configuration.ConfigurationManager.AppSettings["ServiceHostAddr"];
string serviceMetaHostAddr = System.Configuration.ConfigurationManager.AppSettings["ServiceMetaHostAddr"];
serviceHost = new ServiceHost(typeof(ListenService));
NetTcpBinding binding = new NetTcpBinding();
binding.ReceiveTimeout = new TimeSpan(0, 5, 0);
binding.SendTimeout = new TimeSpan(0, 5, 0);
serviceHost.AddServiceEndpoint(typeof(IListenService), binding, string.Format("net.tcp://{0}/ListenService", serviceHostAddr));
if (serviceHost.Description.Behaviors.Find<ServiceMetadataBehavior>() == null)
{
ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();
behavior.HttpGetEnabled = true;
behavior.HttpGetUrl = new Uri(string.Format("http://{0}/ListenService/metadata", serviceMetaHostAddr));
serviceHost.Description.Behaviors.Add(behavior);
}
serviceHost.Opened += (s, arg) =>
{
SetUIStyle("S");
Common.Listening = true;
Common.AutoLoadProgramInfos();
Common.AutoListenPrograms();
};
serviceHost.Closed += (s, arg) =>
{
SetUIStyle("C");
Common.loadTimer.Stop();
Common.listenTimer.Stop();
Common.Listening = false;
};
}
serviceHost.Open();
}
private void StopListenService()
{
try
{
if (serviceHost != null && serviceHost.State != CommunicationState.Closed)
{
serviceHost.Close();
}
serviceHost = null;
}
catch
{ }
}
private void SetUIStyle(string state)
{
if (state == "S")
{
labSericeState.BackColor = Color.Green;
txtRefreshInterval.Enabled = false;
txtListenInterval.Enabled = false;
btnExec.Tag = "C";
btnExec.Text = "停止監控";
panel1.Enabled = false;
panel2.Enabled = false;
}
else
{
labSericeState.BackColor = Color.Red;
txtRefreshInterval.Enabled = true;
txtListenInterval.Enabled = true;
btnExec.Tag = "S";
btnExec.Text = "開啟監控";
panel1.Enabled = true;
panel2.Enabled = true;
}
}
private void InitListViewStyle()
{
ImageList imgList = new ImageList();
imgList.ImageSize = new Size(32, 32);
imgList.Images.Add(Properties.Resources.monitor);
listView1.SmallImageList = imgList;
listView1.LargeImageList = imgList;
listView1.View = View.Details;
listView1.GridLines = false;
listView1.FullRowSelect = true;
listView1.Columns.Add("程序名稱", -2);
listView1.Columns.Add("版本");
listView1.Columns.Add("運行路徑");
int avgWidth = listView1.Width / 3;
}
private void InitNoticeSetting()
{
if (chkSendSms.Checked)
{
bool dbConnected = false;
Common.BuildConnectionString(txtServer.Text, txtDb.Text, txtUID.Text, txtPwd.Text);
using (var da = new DataAccess(Common.DbConnString, Common.SqlProviderName))
{
try
{
da.ExecuteScalar<DateTime>("select getdate()");
dbConnected = true;
}
catch (Exception ex)
{
MessageBox.Show("數據庫測試連接失敗,原因:" + ex.Message);
}
}
if (dbConnected)
{
if (txtPhoneNos.Text.Trim().IndexOf(",") >= 0)
{
Common.NoticePhoneNos = txtPhoneNos.Text.Trim().Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries);
}
else
{
Common.NoticePhoneNos = new[] { txtPhoneNos.Text.Trim() };
}
}
}
else
{
Common.NoticePhoneNos = null;
}
if (chkSendWx.Checked)
{
Common.NoticeWxUserIds = txtWxUIDs.Text.Trim();
}
else
{
Common.NoticeWxUserIds = null;
}
}
private bool IsRightClickSelectedItem(Point point)
{
foreach (ListViewItem item in listView1.SelectedItems)
{
if (item.Bounds.Contains(point))
{
return true;
}
}
return false;
}
#endregion
private void btnExec_Click(object sender, EventArgs e)
{
string state = (btnExec.Tag ?? "S").ToString();
if (state == "S")
{
InitNoticeSetting();
Common.ClearInterval = int.Parse(txtRefreshInterval.Text);
Common.ListenInterval = int.Parse(txtListenInterval.Text);
StartListenService();
}
else
{
StopListenService();
}
}
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listView1.SelectedItems.Count <= 0) return;
string programId = listView1.SelectedItems[0].Tag.ToString();
string tabPgName = string.Format("tabpg_{0}", programId);
if (tabControl1.TabPages.ContainsKey(tabPgName))
{
tabControl1.SelectedTab = tabControl1.TabPages[tabPgName];
}
else
{
MessageBox.Show("未找到相應的程序監控記錄!");
listView1.SelectedItems[0].ForeColor = Color.Red;
}
}
private void FrmMain_Load(object sender, EventArgs e)
{
InitListViewStyle();
}
private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
{
if (MessageBox.Show("您確定要退出嗎?退出后將無法正常監控各程序的運行狀況", "退出提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.Cancel)
{
e.Cancel = true;
return;
}
StopListenService();
}
private void btnTestSend_Click(object sender, EventArgs e)
{
var wx = new WeChat();
var msg = wx.SendMessage("kyezuo", "測試消息,有程序停止運行了,趕緊處理!");
MessageBox.Show(msg["errmsg"].ToString());
}
private void listView1_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right && IsRightClickSelectedItem(e.Location))
{
ctxMuPop.Show(listView1, e.Location);
}
}
private void removeToolStripMenuItem_Click(object sender, EventArgs e)
{
if (listView1.SelectedItems.Count <= 0) return;
string programId = listView1.SelectedItems[0].Tag.ToString();
if (Common.ProgramInfos[programId].RunState != -1)
{
MessageBox.Show("只有被監控的程序處於已停止狀態的監控項才能移除,除外情況請務必保持正常!");
return;
}
RemoveListenItem(programId);
}
}
}
窗體類中主要是用到了幾個更新UI上控件信息的方法以及開啟、關閉WCF服務的方法,很簡單,一看就明白,無需多講。
FrmMain.Designer.cs:(Form窗體設計類,系統自動生成的,再此貼出是便於大家可以直接COPY到自己的代碼中直接用)
namespace ProgramMonitor
{
partial class FrmMain
{
/// <summary>
/// 必需的設計器變量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的資源。
/// </summary>
/// <param name="disposing">如果應釋放托管資源,為 true;否則為 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗體設計器生成的代碼
/// <summary>
/// 設計器支持所需的方法 - 不要
/// 使用代碼編輯器修改此方法的內容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel();
this.splitContainer1 = new System.Windows.Forms.SplitContainer();
this.listView1 = new System.Windows.Forms.ListView();
this.tabControl1 = new System.Windows.Forms.TabControl();
this.tabControl2 = new System.Windows.Forms.TabControl();
this.tabPage1 = new System.Windows.Forms.TabPage();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.btnTestSend = new System.Windows.Forms.Button();
this.labSericeState = new System.Windows.Forms.Label();
this.btnExec = new System.Windows.Forms.Button();
this.txtListenInterval = new System.Windows.Forms.TextBox();
this.txtRefreshInterval = new System.Windows.Forms.TextBox();
this.labStopCount = new System.Windows.Forms.Label();
this.labRunCount = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label();
this.tabPage2 = new System.Windows.Forms.TabPage();
this.labLine = new System.Windows.Forms.Label();
this.panel1 = new System.Windows.Forms.Panel();
this.txtPwd = new System.Windows.Forms.TextBox();
this.txtUID = new System.Windows.Forms.TextBox();
this.txtDb = new System.Windows.Forms.TextBox();
this.txtServer = new System.Windows.Forms.TextBox();
this.label8 = new System.Windows.Forms.Label();
this.label9 = new System.Windows.Forms.Label();
this.txtPhoneNos = new System.Windows.Forms.TextBox();
this.label7 = new System.Windows.Forms.Label();
this.label6 = new System.Windows.Forms.Label();
this.label5 = new System.Windows.Forms.Label();
this.chkSendSms = new System.Windows.Forms.CheckBox();
this.panel2 = new System.Windows.Forms.Panel();
this.txtWxUIDs = new System.Windows.Forms.TextBox();
this.chkSendWx = new System.Windows.Forms.CheckBox();
this.label10 = new System.Windows.Forms.Label();
this.ctxMuPop = new System.Windows.Forms.ContextMenuStrip(this.components);
this.removeToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.tableLayoutPanel1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).BeginInit();
this.splitContainer1.Panel1.SuspendLayout();
this.splitContainer1.Panel2.SuspendLayout();
this.splitContainer1.SuspendLayout();
this.tabControl2.SuspendLayout();
this.tabPage1.SuspendLayout();
this.groupBox1.SuspendLayout();
this.tabPage2.SuspendLayout();
this.panel1.SuspendLayout();
this.panel2.SuspendLayout();
this.ctxMuPop.SuspendLayout();
this.SuspendLayout();
//
// tableLayoutPanel1
//
this.tableLayoutPanel1.ColumnCount = 1;
this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));
this.tableLayoutPanel1.Controls.Add(this.splitContainer1, 0, 1);
this.tableLayoutPanel1.Controls.Add(this.tabControl2, 0, 0);
this.tableLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill;
this.tableLayoutPanel1.Location = new System.Drawing.Point(0, 0);
this.tableLayoutPanel1.Name = "tableLayoutPanel1";
this.tableLayoutPanel1.RowCount = 2;
this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 150F));
this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle());
this.tableLayoutPanel1.Size = new System.Drawing.Size(1039, 722);
this.tableLayoutPanel1.TabIndex = 0;
//
// splitContainer1
//
this.splitContainer1.Dock = System.Windows.Forms.DockStyle.Fill;
this.splitContainer1.FixedPanel = System.Windows.Forms.FixedPanel.Panel1;
this.splitContainer1.Location = new System.Drawing.Point(3, 153);
this.splitContainer1.Name = "splitContainer1";
//
// splitContainer1.Panel1
//
this.splitContainer1.Panel1.AutoScroll = true;
this.splitContainer1.Panel1.Controls.Add(this.listView1);
this.splitContainer1.Panel1MinSize = 150;
//
// splitContainer1.Panel2
//
this.splitContainer1.Panel2.Controls.Add(this.tabControl1);
this.splitContainer1.Panel2MinSize = 500;
this.splitContainer1.Size = new System.Drawing.Size(1033, 608);
this.splitContainer1.SplitterDistance = 296;
this.splitContainer1.TabIndex = 0;
//
// listView1
//
this.listView1.Dock = System.Windows.Forms.DockStyle.Fill;
this.listView1.Location = new System.Drawing.Point(0, 0);
this.listView1.Name = "listView1";
this.listView1.Size = new System.Drawing.Size(296, 608);
this.listView1.TabIndex = 0;
this.listView1.UseCompatibleStateImageBehavior = false;
this.listView1.View = System.Windows.Forms.View.Details;
this.listView1.SelectedIndexChanged += new System.EventHandler(this.listView1_SelectedIndexChanged);
this.listView1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.listView1_MouseUp);
//
// tabControl1
//
this.tabControl1.Dock = System.Windows.Forms.DockStyle.Fill;
this.tabControl1.Location = new System.Drawing.Point(0, 0);
this.tabControl1.Margin = new System.Windows.Forms.Padding(3, 0, 3, 3);
this.tabControl1.Name = "tabControl1";
this.tabControl1.SelectedIndex = 0;
this.tabControl1.Size = new System.Drawing.Size(733, 608);
this.tabControl1.TabIndex = 0;
//
// tabControl2
//
this.tabControl2.Controls.Add(this.tabPage1);
this.tabControl2.Controls.Add(this.tabPage2);
this.tabControl2.Dock = System.Windows.Forms.DockStyle.Fill;
this.tabControl2.Location = new System.Drawing.Point(3, 3);
this.tabControl2.Name = "tabControl2";
this.tabControl2.SelectedIndex = 0;
this.tabControl2.Size = new System.Drawing.Size(1033, 144);
this.tabControl2.TabIndex = 1;
//
// tabPage1
//
this.tabPage1.Controls.Add(this.groupBox1);
this.tabPage1.Location = new System.Drawing.Point(4, 22);
this.tabPage1.Name = "tabPage1";
this.tabPage1.Padding = new System.Windows.Forms.Padding(3);
this.tabPage1.Size = new System.Drawing.Size(1025, 118);
this.tabPage1.TabIndex = 0;
this.tabPage1.Text = "監控基本信息";
this.tabPage1.UseVisualStyleBackColor = true;
//
// groupBox1
//
this.groupBox1.Controls.Add(this.btnTestSend);
this.groupBox1.Controls.Add(this.labSericeState);
this.groupBox1.Controls.Add(this.btnExec);
this.groupBox1.Controls.Add(this.txtListenInterval);
this.groupBox1.Controls.Add(this.txtRefreshInterval);
this.groupBox1.Controls.Add(this.labStopCount);
this.groupBox1.Controls.Add(this.labRunCount);
this.groupBox1.Controls.Add(this.label4);
this.groupBox1.Controls.Add(this.label3);
this.groupBox1.Controls.Add(this.label2);
this.groupBox1.Controls.Add(this.label1);
this.groupBox1.Dock = System.Windows.Forms.DockStyle.Fill;
this.groupBox1.Location = new System.Drawing.Point(3, 3);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(1019, 112);
this.groupBox1.TabIndex = 1;
this.groupBox1.TabStop = false;
//
// btnTestSend
//
this.btnTestSend.Location = new System.Drawing.Point(14, 73);
this.btnTestSend.Name = "btnTestSend";
this.btnTestSend.Size = new System.Drawing.Size(123, 23);
this.btnTestSend.TabIndex = 4;
this.btnTestSend.Text = "測試發送消息";
this.btnTestSend.UseVisualStyleBackColor = true;
this.btnTestSend.Click += new System.EventHandler(this.btnTestSend_Click);
//
// labSericeState
//
this.labSericeState.BackColor = System.Drawing.Color.Red;
this.labSericeState.Location = new System.Drawing.Point(578, 40);
this.labSericeState.Name = "labSericeState";
this.labSericeState.Size = new System.Drawing.Size(34, 22);
this.labSericeState.TabIndex = 3;
this.labSericeState.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// btnExec
//
this.btnExec.Location = new System.Drawing.Point(487, 40);
this.btnExec.Name = "btnExec";
this.btnExec.Size = new System.Drawing.Size(75, 23);
this.btnExec.TabIndex = 2;
this.btnExec.Text = "開啟監控";
this.btnExec.UseVisualStyleBackColor = true;
this.btnExec.Click += new System.EventHandler(this.btnExec_Click);
//
// txtListenInterval
//
this.txtListenInterval.Location = new System.Drawing.Point(147, 41);
this.txtListenInterval.Name = "txtListenInterval";
this.txtListenInterval.Size = new System.Drawing.Size(68, 21);
this.txtListenInterval.TabIndex = 1;
this.txtListenInterval.Text = "2";
//
// txtRefreshInterval
//
this.txtRefreshInterval.Location = new System.Drawing.Point(411, 41);
this.txtRefreshInterval.Name = "txtRefreshInterval";
this.txtRefreshInterval.Size = new System.Drawing.Size(68, 21);
this.txtRefreshInterval.TabIndex = 1;
this.txtRefreshInterval.Text = "5";
//
// labStopCount
//
this.labStopCount.AutoSize = true;
this.labStopCount.Font = new System.Drawing.Font("宋體", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.labStopCount.ForeColor = System.Drawing.Color.Red;
this.labStopCount.Location = new System.Drawing.Point(841, 42);
this.labStopCount.Name = "labStopCount";
this.labStopCount.Size = new System.Drawing.Size(40, 19);
this.labStopCount.TabIndex = 0;
this.labStopCount.Text = "0個";
//
// labRunCount
//
this.labRunCount.AutoSize = true;
this.labRunCount.Font = new System.Drawing.Font("宋體", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.labRunCount.ForeColor = System.Drawing.Color.Green;
this.labRunCount.Location = new System.Drawing.Point(721, 42);
this.labRunCount.Name = "labRunCount";
this.labRunCount.Size = new System.Drawing.Size(40, 19);
this.labRunCount.TabIndex = 0;
this.labRunCount.Text = "0個";
//
// label4
//
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(782, 45);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(53, 12);
this.label4.TabIndex = 0;
this.label4.Text = "已停止:";
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(645, 45);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(53, 12);
this.label3.TabIndex = 0;
this.label3.Text = "運行中:";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(12, 45);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(125, 12);
this.label2.TabIndex = 0;
this.label2.Text = "監聽探測間隔(分):";
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(232, 45);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(173, 12);
this.label1.TabIndex = 0;
this.label1.Text = "正常停止清除列表時長(分):";
//
// tabPage2
//
this.tabPage2.Controls.Add(this.labLine);
this.tabPage2.Controls.Add(this.panel1);
this.tabPage2.Controls.Add(this.panel2);
this.tabPage2.Location = new System.Drawing.Point(4, 22);
this.tabPage2.Name = "tabPage2";
this.tabPage2.Padding = new System.Windows.Forms.Padding(3);
this.tabPage2.Size = new System.Drawing.Size(1025, 118);
this.tabPage2.TabIndex = 1;
this.tabPage2.Text = "消息通知設置";
this.tabPage2.UseVisualStyleBackColor = true;
//
// labLine
//
this.labLine.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(224)))), ((int)(((byte)(224)))), ((int)(((byte)(224)))));
this.labLine.Dock = System.Windows.Forms.DockStyle.Bottom;
this.labLine.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.labLine.Location = new System.Drawing.Point(3, 59);
this.labLine.Name = "labLine";
this.labLine.Size = new System.Drawing.Size(1019, 2);
this.labLine.TabIndex = 0;
//
// panel1
//
this.panel1.Controls.Add(this.txtPwd);
this.panel1.Controls.Add(this.txtUID);
this.panel1.Controls.Add(this.txtDb);
this.panel1.Controls.Add(this.txtServer);
this.panel1.Controls.Add(this.label8);
this.panel1.Controls.Add(this.label9);
this.panel1.Controls.Add(this.txtPhoneNos);
this.panel1.Controls.Add(this.label7);
this.panel1.Controls.Add(this.label6);
this.panel1.Controls.Add(this.label5);
this.panel1.Controls.Add(this.chkSendSms);
this.panel1.Dock = System.Windows.Forms.DockStyle.Top;
this.panel1.Location = new System.Drawing.Point(3, 3);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(1019, 52);
this.panel1.TabIndex = 0;
//
// txtPwd
//
this.txtPwd.Location = new System.Drawing.Point(925, 24);
this.txtPwd.Name = "txtPwd";
this.txtPwd.PasswordChar = '*';
this.txtPwd.Size = new System.Drawing.Size(91, 21);
this.txtPwd.TabIndex = 5;
//
// txtUID
//
this.txtUID.Location = new System.Drawing.Point(779, 24);
this.txtUID.Name = "txtUID";
this.txtUID.PasswordChar = '*';
this.txtUID.Size = new System.Drawing.Size(91, 21);
this.txtUID.TabIndex = 4;
//
// txtDb
//
this.txtDb.Location = new System.Drawing.Point(628, 24);
this.txtDb.Name = "txtDb";
this.txtDb.Size = new System.Drawing.Size(96, 21);
this.txtDb.TabIndex = 3;
this.txtDb.Text = "";
//
// txtServer
//
this.txtServer.Location = new System.Drawing.Point(423, 24);
this.txtServer.Name = "txtServer";
this.txtServer.Size = new System.Drawing.Size(150, 21);
this.txtServer.TabIndex = 2;
this.txtServer.Text = "";
//
// label8
//
this.label8.AutoSize = true;
this.label8.Location = new System.Drawing.Point(876, 27);
this.label8.Name = "label8";
this.label8.Size = new System.Drawing.Size(41, 12);
this.label8.TabIndex = 7;
this.label8.Text = "密碼:";
//
// label9
//
this.label9.AutoSize = true;
this.label9.Location = new System.Drawing.Point(579, 27);
this.label9.Name = "label9";
this.label9.Size = new System.Drawing.Size(53, 12);
this.label9.TabIndex = 7;
this.label9.Text = "數據庫:";
//
// txtPhoneNos
//
this.txtPhoneNos.Location = new System.Drawing.Point(213, 22);
this.txtPhoneNos.Name = "txtPhoneNos";
this.txtPhoneNos.Size = new System.Drawing.Size(155, 21);
this.txtPhoneNos.TabIndex = 1;
//
// label7
//
this.label7.AutoSize = true;
this.label7.Location = new System.Drawing.Point(730, 27);
this.label7.Name = "label7";
this.label7.Size = new System.Drawing.Size(53, 12);
this.label7.TabIndex = 7;
this.label7.Text = "用戶名:";
//
// label6
//
this.label6.AutoSize = true;
this.label6.Location = new System.Drawing.Point(374, 27);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(53, 12);
this.label6.TabIndex = 7;
this.label6.Text = "服務器:";
//
// label5
//
this.label5.AutoSize = true;
this.label5.Location = new System.Drawing.Point(3, 27);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(185, 12);
this.label5.TabIndex = 7;
this.label5.Text = "通知手機號(多個請用,分隔):";
//
// chkSendSms
//
this.chkSendSms.AutoSize = true;
this.chkSendSms.Location = new System.Drawing.Point(3, 3);
this.chkSendSms.Name = "chkSendSms";
this.chkSendSms.Size = new System.Drawing.Size(72, 16);
this.chkSendSms.TabIndex = 0;
this.chkSendSms.Text = "短信通知";
this.chkSendSms.UseVisualStyleBackColor = true;
//
// panel2
//
this.panel2.Controls.Add(this.txtWxUIDs);
this.panel2.Controls.Add(this.chkSendWx);
this.panel2.Controls.Add(this.label10);
this.panel2.Dock = System.Windows.Forms.DockStyle.Bottom;
this.panel2.Location = new System.Drawing.Point(3, 61);
this.panel2.Name = "panel2";
this.panel2.Size = new System.Drawing.Size(1019, 54);
this.panel2.TabIndex = 1;
//
// txtWxUIDs
//
this.txtWxUIDs.Location = new System.Drawing.Point(188, 15);
this.txtWxUIDs.Name = "txtWxUIDs";
this.txtWxUIDs.Size = new System.Drawing.Size(828, 21);
this.txtWxUIDs.TabIndex = 0;
this.txtWxUIDs.Text = "@all";
//
// chkSendWx
//
this.chkSendWx.AutoSize = true;
this.chkSendWx.Location = new System.Drawing.Point(3, 3);
this.chkSendWx.Name = "chkSendWx";
this.chkSendWx.Size = new System.Drawing.Size(72, 16);
this.chkSendWx.TabIndex = 6;
this.chkSendWx.Text = "微信通知";
this.chkSendWx.UseVisualStyleBackColor = true;
//
// label10
//
this.label10.AutoSize = true;
this.label10.Location = new System.Drawing.Point(3, 24);
this.label10.Name = "label10";
this.label10.Size = new System.Drawing.Size(179, 12);
this.label10.TabIndex = 7;
this.label10.Text = "通知微信ID(多個請用|分隔):";
//
// ctxMuPop
//
this.ctxMuPop.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.removeToolStripMenuItem});
this.ctxMuPop.Name = "ctxMuPop";
this.ctxMuPop.Size = new System.Drawing.Size(125, 26);
//
// removeToolStripMenuItem
//
this.removeToolStripMenuItem.Name = "removeToolStripMenuItem";
this.removeToolStripMenuItem.Size = new System.Drawing.Size(124, 22);
this.removeToolStripMenuItem.Text = "移除監控";
this.removeToolStripMenuItem.Click += new System.EventHandler(this.removeToolStripMenuItem_Click);
//
// FrmMain
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1039, 722);
this.Controls.Add(this.tableLayoutPanel1);
this.Name = "FrmMain";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "程序運行狀態監控系統";
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.FrmMain_FormClosing);
this.Load += new System.EventHandler(this.FrmMain_Load);
this.tableLayoutPanel1.ResumeLayout(false);
this.splitContainer1.Panel1.ResumeLayout(false);
this.splitContainer1.Panel2.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).EndInit();
this.splitContainer1.ResumeLayout(false);
this.tabControl2.ResumeLayout(false);
this.tabPage1.ResumeLayout(false);
this.groupBox1.ResumeLayout(false);
this.groupBox1.PerformLayout();
this.tabPage2.ResumeLayout(false);
this.panel1.ResumeLayout(false);
this.panel1.PerformLayout();
this.panel2.ResumeLayout(false);
this.panel2.PerformLayout();
this.ctxMuPop.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.TableLayoutPanel tableLayoutPanel1;
private System.Windows.Forms.SplitContainer splitContainer1;
private System.Windows.Forms.ListView listView1;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.TextBox txtListenInterval;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label labStopCount;
private System.Windows.Forms.Label labRunCount;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Button btnExec;
private System.Windows.Forms.TabControl tabControl1;
private System.Windows.Forms.Label labSericeState;
private System.Windows.Forms.TextBox txtRefreshInterval;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TabControl tabControl2;
private System.Windows.Forms.TabPage tabPage1;
private System.Windows.Forms.TabPage tabPage2;
private System.Windows.Forms.Panel panel2;
private System.Windows.Forms.CheckBox chkSendWx;
private System.Windows.Forms.Label label10;
private System.Windows.Forms.TextBox txtWxUIDs;
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.TextBox txtPwd;
private System.Windows.Forms.TextBox txtUID;
private System.Windows.Forms.TextBox txtDb;
private System.Windows.Forms.TextBox txtServer;
private System.Windows.Forms.Label label8;
private System.Windows.Forms.Label label9;
private System.Windows.Forms.TextBox txtPhoneNos;
private System.Windows.Forms.Label label7;
private System.Windows.Forms.Label label6;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.CheckBox chkSendSms;
private System.Windows.Forms.Label labLine;
private System.Windows.Forms.Button btnTestSend;
private System.Windows.Forms.ContextMenuStrip ctxMuPop;
private System.Windows.Forms.ToolStripMenuItem removeToolStripMenuItem;
}
}
被監控的客戶端程序集成WCF監聽客戶端很簡單,只需引用ProgramMonitor.Core,然后實例化ListenClient,最后就可以通過該ListenClient與心跳監控系統進行雙工通訊,在此就不貼出源代碼了。
上述代碼中還有用到兩個類:
DataAccess:數據訪問類,這個我之前的文章有介紹,詳見:DataAccess通用數據庫訪問類,簡單易用,功能強悍
WeChat:微信企業號發送消息類,注意是微信企業號,不是公眾號,這里我也貼出源代碼來,供大家了解:
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
namespace ProgramMonitor.Service
{
public class WeChat
{
private readonly string _url = null;
private readonly string _corpid = null;
private readonly string _secret = null;
public WeChat()
{
_url = "https://qyapi.weixin.qq.com/cgi-bin";
_corpid = "CorpID是企業號的標識,每個企業號擁有一個唯一的CorpID";
_secret = "secret是管理組憑證密鑰,系統管理員在企業號管理后台創建管理組時,企業號后台為該管理組分配一個唯一的secret";
}
public string GetToken(string url_prefix = "/")
{
string urlParams = string.Format("corpid={0}&corpsecret={1}", HttpUtility.UrlEncodeUnicode(_corpid), HttpUtility.UrlEncodeUnicode(_secret));
string url = _url + url_prefix + "gettoken?" + urlParams;
string result = HttpGet(url);
var content = JObject.Parse(result);
return content["access_token"].ToString();
}
public JObject PostData(dynamic data, string url_prefix = "/")
{
string dataStr = JsonConvert.SerializeObject(data);
string url = _url + url_prefix + "message/send?access_token=" + GetToken();
string result = HttpPost(url, dataStr);
return JObject.Parse(result);
}
public JObject SendMessage(string touser, string message)
{
var data = new { touser = touser, toparty = "1", msgtype = "text", agentid = "2", text = new { content = message }, safe = "0" };
var jResult = PostData(data);
return jResult;
}
private string HttpPost(string Url, string postDataStr)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
byte[] data = Encoding.UTF8.GetBytes(postDataStr);
request.ContentLength = data.Length;
Stream myRequestStream = request.GetRequestStream();
myRequestStream.Write(data, 0, data.Length);
myRequestStream.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);
string retString = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();
return retString;
}
public string HttpGet(string Url, string urlParams = null)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url + (string.IsNullOrEmpty(urlParams) ? "" : "?") + urlParams);
request.Method = "GET";
request.ContentType = "text/html;charset=UTF-8";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);
string retString = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();
return retString;
}
}
}
具體的關於微信企業號開發文檔,可參見:http://qydev.weixin.qq.com/wiki/index.php
最后的效果如下:
心跳監控程序監控效果:

手機收到異常消息:
(《-這是企業號發出的消息)
(《-這里短信消息,當然發短信是我公司的平台接口發出的,發短信是需要RMB的,故不建議)
好了本文就到此結束,可能功能相對簡單,還有一些不足,歡迎大家評論交流,謝謝!
