-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHeartBeatChecker.cs
366 lines (330 loc) · 12.7 KB
/
HeartBeatChecker.cs
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Mail;
using System.Threading;
using System.IO;
using System.Net;
namespace HeartBeatChecker
{
public partial class HeartBeatChecker : Form
{
string[] arrUrl = new string[20];
//string[] arrUrlDesc = new string[20];
//string strFileContent = "";
public HeartBeatChecker()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (txtSMTP.Text.Trim().Length < 3 || txtMail.Text.Trim().Length < 3 || txtPassword.Text.Trim().Length < 3 || txtToMail.Text.Trim().Length < 3)
{
MessageBox.Show("请完整填写界面中的内容项!");
return;
}
button1.Text = "监控中";
int j = ReadText();
if (j <=1 )
{
MessageBox.Show("在当前目录下未找到site.ini文件或文件中没有有效网址!");
return;
}
int intT = 1000;
try
{
intT = Convert.ToInt32(txtTime.Text) * intT;
if (intT < 3000)
{
MessageBox.Show("最小时间间隔为3秒" + intT.ToString());
txtTime.Focus();
return;
}
timer1.Interval = intT;
StartCheck();
}
catch
{
MessageBox.Show("最小时间间隔为3秒!" + intT.ToString());
txtTime.Focus();
return;
}
}
private void StartCheck()
{
string v = "";
int intError = 0;
timer1.Stop();
for (int i = 1; i <= arrUrl.Length; i++)
{
try
{
v = CheckServer(arrUrl[i].ToString());
if (v != "")
{
intError = 1;
if (CheckServer("http://www.baidu.com", "baidu") == "")
{
SendMail(v);
}
else
{
intError = 0; //如果BAIDU也出错说明网络问题,不发送邮件
}
}
}
catch (Exception e)
{
break;
}
}
if (intError != 0)
{
timer1.Stop();
Thread.Sleep(3 * 60 * 1000); //发过邮件后,过3分钟再继续测
//MessageBox.Show("ok");
StartCheck();
}
else
{
timer1.Start();
}
}
private string CheckServer(string strUrlV,string strV ="")
{
string strTitle = "";
string strUrl="";
string strKey = "err";
try
{
/*objXmlHTTP.open("GET", strUrl, false, null, null);
objXmlHTTP.send("");
if (objXmlHTTP.readyState == 4)
{
//MessageBox.Show(objXmlHTTP.status.ToString());
//Byte[] b = (Byte[])objXmlHTTP.responseBody;
//string andy = System.Text.Encoding.GetEncoding("GB2312").GetString(b).Trim();
if (objXmlHTTP.status.ToString() != "200")
{
strTitle = strUrl + "发生 HTTP" + objXmlHTTP.status.ToString() + " 错误!";
}
else
{
if (objXmlHTTP.responseText.IndexOf("ok") < 0 && objXmlHTTP.responseText.IndexOf("title") <= 0)
{
strTitle = strUrl + "运行环境可能发生错误!";
}
}
}*/
if (strV == "") //如果是带第二个KEY参数,说明是效验BAIDU能不能通过
{
string[] arrUrlDesc = strUrlV.Split('|');
strUrl = arrUrlDesc[0];
strV = arrUrlDesc[1];
}
else
{
strUrl = strUrlV;
}
string a = GetSiteHtml(strUrl);
if (a.IndexOf(strV) <= 0)
{
strTitle = strUrl + "运行环境可能发生错误!";
if (a.IndexOf("siteerror") > 0)
{
WriteFile("errlog.txt", a, "");
}
}
//objXmlHTTP.abort();
}
catch (Exception e)
{
MessageBox.Show(e.Message + e.TargetSite + strUrl);
//SendMail(arrUrl[1], strTitle);
strTitle = strUrlV + " 发生异常!";
}
finally {
//objXmlHTTP.abort();
}
return strTitle;
}
//获取某个页面。以返回流的编码方式,接收返回的信息
public string GetSiteHtml(string url)
{
//string tempurl = System.Web.HttpUtility.UrlEncode(url, System.Text.Encoding.GetEncoding("gb2312")); //url
string tempurl = url;
try
{
HttpWebRequest webr = (HttpWebRequest)WebRequest.Create(tempurl);
webr.Timeout = 60000;
//创建请求
HttpWebResponse wb = (HttpWebResponse)webr.GetResponse();
Stream sr = wb.GetResponseStream(); //得到返回数据流
Encoding encode;
switch (wb.CharacterSet.ToLower()) //返回数据流的编码方式
{
case "utf-8":
encode = System.Text.Encoding.GetEncoding("utf-8");
break;
case "gb2312":
encode = System.Text.Encoding.GetEncoding("gb2312");
break;
default:
encode = System.Text.Encoding.GetEncoding("utf-8"); //默认为中文
break;
}
StreamReader sr1 = new StreamReader(sr, encode);
string zz = sr1.ReadToEnd(); //读取完成
sr1.Close();
wb.Close();//关闭
return zz;
}
catch (Exception x4)
{
return " siteerror:" + x4.Message;
}
}
private void SendMail(string strTitle)
{
if (txtSMTP.Text.Trim() != "" && txtMail.Text.Trim() != "")
{
MailAddress objFrom = new MailAddress(txtMail.Text.Trim(), txtMail.Text.Trim());
MailAddress objTo = new MailAddress(txtToMail.Text, txtToMail.Text);
MailMessage objMail = new MailMessage(objFrom,objTo);
//objMail.CC.Add("[email protected]");
//objMail.CC.Add("[email protected]");
if (IsEmail(textCC1.Text))
{
objMail.CC.Add(textCC1.Text);
}
if (IsEmail(textCC2.Text))
{
objMail.CC.Add(textCC2.Text);
}
if (IsEmail(textCC3.Text))
{
objMail.CC.Add(textCC3.Text);
}
if (IsEmail(textCC4.Text))
{
objMail.CC.Add(textCC4.Text);
}
//objMail.CC.Add("[email protected]");
//objMail.CC.Add("[email protected]");
objMail.Subject = strTitle;
//objMail.Body=strUrl + "中好的";
objMail.Priority = MailPriority.High;
SmtpClient MySmtpClient = new SmtpClient(txtSMTP.Text,25);
MySmtpClient.UseDefaultCredentials = false;
//MySmtpClient.EnableSsl = true;
MySmtpClient.Credentials = new System.Net.NetworkCredential(txtSMTPUserName.Text, txtPassword.Text);
MySmtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
MySmtpClient.EnableSsl = false;
try
{
MySmtpClient.Send(objMail);
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
System.Environment.Exit(0);
}
objMail.Dispose();
}
}
private int ReadText()
{
try
{
FileStream aFile = new FileStream(Application.StartupPath + "\\Site.ini", FileMode.Open);
StreamReader sr = new StreamReader(aFile);
//strFileContent = sr.ReadToEnd();
string s = "";
//sr.BaseStream.Seek(0, SeekOrigin.Begin); //读一下,看看文件内有没有内容,为下一步循环 提供判断依据
//sr.ReadLine() 这里是 StreamReader的方法 可不是 console 中的~ string str = sr.ReadLine();//如果 文件有内容 while (str != null) { //输出字符串,str 在上面已经定义了 读入一行字符 Console.WriteLine("{0}", str); //这里我的理解是 当输出一行后,指针移动到下一行~ //下面这句话就是 判断 指针所指这行是否有内容~ str = sr.ReadLine();} //C#读取TXT文件之关闭文件,注意顺序,先对文件内部进行关闭,然后才是文件~ sr.Close(); fs.Close(); }
int j = 1;
int n = 1;
s = sr.ReadLine();
while (s != null && j<20)
{
if (((s.ToString()).ToLower()).IndexOf("http://") >= 0)
{
arrUrl[j] = s.ToString();
j++;
}
n++;
s = sr.ReadLine();
}
sr.Close();
sr.Dispose();
aFile.Close();
aFile.Dispose();
return j;
//MessageBox.Show(strFileContent);
//MessageBox.Show(strFileContent.IndexOf("\n").ToString());
}
catch(Exception e)
{
return -1;
}
}
private void button2_Click(object sender, EventArgs e)
{
ReadText();
}
private void timer1_Tick(object sender, EventArgs e)
{
StartCheck();
}
//文件名,内容,放在哪个子目录下,文件内容中是否加在尾上加上时间信息
public void WriteFile(string strLogFileName, string LogStr, string strSiteFolder = "", bool bolNeedDateTime = true)
{
StreamWriter sw = null;
try
{
if (bolNeedDateTime)
{
LogStr = DateTime.Now.ToLocalTime().ToString() + "\n" + LogStr;
}
if (strSiteFolder != "")
{
strLogFileName = Application.StartupPath.ToString() + "\\" + strLogFileName;
}
sw = new StreamWriter(strLogFileName, true, Encoding.Default);
sw.WriteLine(LogStr);
}
catch (Exception exf)
{
string v = exf.Message.ToString();
}
finally
{
if (sw != null)
{
sw.Close();
}
}
sw.Dispose();
}
private void txtPassword_TextChanged(object sender, EventArgs e)
{
}
public bool IsEmail(string str)
{
//string res = string.Empty;
string expression = @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";
if (System.Text.RegularExpressions.Regex.IsMatch(str, expression, System.Text.RegularExpressions.RegexOptions.Compiled))
{
return true;
}
return false;
}
private void HeartBeatChecker_Load(object sender, EventArgs e)
{
}
}
}