利用WCF的双工通讯实现一个简单的心跳监控系统

简介:

何为心跳监控系统?

故名思义,就是监控某个或某些个程序的运行状态,就好比医院里面的心跳监视仪一样,能够随时显示病人的心跳情况。

心跳监控的目的是什么?

与医院里面的心跳监视仪目的类似,监控程序运行状态,一旦出现问题(比如:一些自动运行的服务、程序等突然停止运行了),那么心跳监控系统就能“感知到”并及时的显示在监控界面上,同时可以通过微信、短信告之相关的人员,以便他们及时处理程序异常,从而避免一些自动运行的服务、程序等突然停止运行而造成的一系列损失

心跳监控系统实现的思路是怎样的?

核心技术: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的,故不建议)

 

好了本文就到此结束,可能功能相对简单,还有一些不足,欢迎大家评论交流,谢谢!

 本文转自 梦在旅途 博客园博客,原文链接:http://www.cnblogs.com/zuowj/p/5761011.html  ,如需转载请自行联系原作者


相关文章
|
JavaScript
wcf双工通讯
首先说一个服务引用不成功的解决办法: 需要把服务端配置文件中的Binding换成: 或: 下面是一个wcf的简单示例: 服务契约代码: using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.Text; namespace WcfService { // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IUser”。
999 0
|
监控 NoSQL
Wcf通讯基础框架方案(一)——基本结构
由于希望使用Wcf作为公司内的通讯框架,因此基于Wcf进行了一些扩展,主要的目的有以下几个方面: 1) 希望减少客户端调用的复杂度,调用方式简化为WcfServiceLocator.Create().Add(1,2)。
795 0
|
XML 监控 数据格式
Wcf通讯基础框架方案(二)——集中配置
从这次开始在几个方面简单阐述一下实现,集中配置是这个框架很大的一个目的,首先在数据库中会有这么一些表: 其实可以看到这些表的结构,应该是和配置节点中的层次有对应的 1) Service表描述的是服务,主要保存服务行为以及服务的配置。
597 0
|
监控
Wcf通讯基础框架方案(三)——客户端
假设定义了一个服务契约: [ServiceContract(Namespace = "WcfExtension.Services.Interface")] public interface ITestService { [OperationContract] ...
718 0
|
监控 负载均衡 缓存
Wcf通讯基础框架方案(四)——横切日志
在第一篇文章中已经列出了几种日志的概览: 所有的日志都有一个最终基类,来看看这个类: [DataContract(Namespace = "WcfExtension")] [KnownType(typeof(WcfExceptionInfo))] [KnownType(t...
635 0
|
缓存 NoSQL Redis
Wcf通讯基础框架方案(五)——更新通知
对于负载均衡环境,多服务器内存中缓存数据的话,需要解决的一个很重要的问题就是一旦数据库中数据有更新,怎么让缓存的数据立即更新? 如果可以容忍延迟或是差异性的话,可以考虑缓存的数据有一个过期时间。但是,最好的方式还是采用通知方式,或者说发布订阅方式。
811 0
|
SQL 监控 NoSQL
Wcf通讯基础框架方案(六)解决方案说明以及源代码
源代码见 http://wcfextension.codeplex.com/ 注意: 1) 本来没打算这么早开源,这只是一个比较原始的实现,请勿直接在商业环境使用 2) 请注意本框架的授权条款Apache License 2.
697 0
|
前端开发
WCF更新服务引用报错的原因之一
WCF更新服务引用报错的原因之一