心跳監控系統


心跳監控系統

何為心跳監控系統?

故名思義,就是監控某個或某些個程序的運行狀態,就好比醫院里面的心跳監視儀一樣,能夠隨時顯示病人的心跳情況。

心跳監控的目的是什么?

與醫院里面的心跳監視儀目的類似,監控程序運行狀態,一旦出現問題(比如:一些自動運行的服務、程序等突然停止運行了),那么心跳監控系統就能“感知到”並及時的顯示在監控界面上,同時可以通過微信、短信告之相關的人員,以便他們及時處理程序異常,從而避免一些自動運行的服務、程序等突然停止運行而造成的一系列損失

心跳監控系統實現的思路是怎樣的?

核心技術: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服務契約接口)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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:(監聽回調服務接口,主用被心跳監控系統調用)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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:(程序信息類,主要用於獲取被監控程序的基本信息)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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回調服務接口,主要用於被監控的程序)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
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心跳監控服務接口實現類)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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實時溝通與聯動)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
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:(心跳監控系統窗體類)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
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到自己的代碼中直接用)

被監控的客戶端程序集成WCF監聽客戶端很簡單,只需引用ProgramMonitor.Core,然后實例化ListenClient,最后就可以通過該ListenClient與心跳監控系統進行雙工通訊,在此就不貼出源代碼了。

上述代碼中還有用到兩個類:

DataAccess:數據訪問類,這個我之前的文章有介紹,詳見:DataAccess通用數據庫訪問類,簡單易用,功能強悍

WeChat:微信企業號發送消息類,注意是微信企業號,不是公眾號,這里我也貼出源代碼來,供大家了解:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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的,故不建議)

 

好了本文就到此結束,可能功能相對簡單,還有一些不足,歡迎大家評論交流,謝謝!

 

 
分類:  WinForm


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM