域名是否正常,网站是否可以正常访问是很头痛的问题,怎样简单地监控域名是否可以正常访问呢,这里发布一款域名监控小工具:Domain(IP)Watcher
功能:
监控给定的域名,一旦域名连续30秒(这是默认值,可以在源码中修改)无法Ping通,立刻发邮件到指定邮箱,并发短信给站长
效果图:
正常情况
异常情况
邮件
短信
原理:
用后台线程循环每隔5秒钟Ping结定的域名,如果连接5次Ping不通则发送邮件到给定邮箱。发短信是通过其他途径实现的,现在最常用的有两种方式:
一是利用QQ邮箱的手机绑定功能实现收到邮件短信提示
二是用www.ifttt.com提供的gmail跟sms服务实现收到邮件发短信提示
本工具用的是第一种。
主要程序:
主要是后台线程ping域名的应用,界面上显示记录的是一个richtextbox控件,其他几个就不说了,代码如下:
窗体后台程序:
1 public partial class FormMain : Form, IFormWorkThread
2 {
3 private delegate void ControlDelegate(string record);
4 private WorkThread wtObj;
5 ControlDelegate cdObj;
6
7 public FormMain()
8 {
9 InitializeComponent();
10 cdObj = SetRecord;
11 }
12
13 private void FormMain_Load(object sender, EventArgs e)
14 {
15 }
16
17 private void btnBegin_Click(object sender, EventArgs e)
18 {
19 if (txtDomain.Text.Trim() == "")
20 {
21 MessageBox.Show("Domain(IP) can not be empty!");
22 return;
23 }
24 if (txtEmail.Text.Trim() == "")
25 {
26 MessageBox.Show("Email address can not be empty!");
27 return;
28 }
29 Regex regex = new Regex(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
30 if (!regex.IsMatch(txtEmail.Text.Trim()))
31 {
32 MessageBox.Show("Email address error!");
33 return;
34 }
35 wtObj = new WorkThread(this, 0, 3, txtDomain.Text.Trim(), txtEmail.Text.Trim());
36 wtObj.StartShowResult();
37 }
38
39 private void SetRecord(string record)
40 {
41 rtxtRecord.AppendText(record);
42 if (rtxtRecord.Lines.Length > 5 * 60 * 24 * 7)
43 {
44 rtxtRecord.Clear();
45 }
46 }
47
48 public void ShowResult(string record)
49 {
50
51 if (rtxtRecord.InvokeRequired)
52 {
53 Invoke(cdObj, record);
54 }
55 else
56 {
57 SetRecord(record);
58 }
59 }
60
61 private void btnEnd_Click(object sender, EventArgs e)
62 {
63 if (wtObj != null)
64 {
65 wtObj.EndShowResult();
66 }
67 }
68
69 private void FormMain_FormClosing(object sender, FormClosingEventArgs e)
70 {
71 if (wtObj != null)
72 {
73 wtObj.EndShowResult();
74 }
75 }
76
77 private void tslblName_Click(object sender, EventArgs e)
78 {
79 OpenMyBlog();
80 }
81
82 private void OpenMyBlog()
83 {
84 var Info = new System.Diagnostics.ProcessStartInfo("http://artwl.cnblogs.com");
85 System.Diagnostics.Process.Start(Info);
86 }
87 }
IFormWorkThread接口:
1 public interface IFormWorkThread
2 {
3 void ShowResult(string record);
4 }
WorkThread代码:
1 public class WorkThread
2 {
3 private IFormWorkThread _formWorkThread;
4 private Thread thread;
5 private int count = 0, maxcount = 5;
6 private string domain = "";
7 private string emailaddress = "";
8
9 public WorkThread(IFormWorkThread formWorkThread,int count,int maxcount,string domain,string email)
10 {
11 this.count = count;
12 this.maxcount = maxcount;
13 this.domain = domain;
14 this.emailaddress = email;
15 _formWorkThread = formWorkThread;
16 }
17
18 private string Ping(string domainname)
19 {
20 Ping ping = new Ping();
21 PingReply pr = ping.Send(domainname);
22 if (pr.Status == IPStatus.Success)
23 {
24 count = count > 0 ? count - 1 : 0;
25 }
26 else
27 {
28 count++;
29 if (count > maxcount)
30 {
31 EmailHelp.SendEmail(emailaddress,
32 string.Format("【 {0} 】不能访问啦!",domainname),
33 string.Format("【 {0} 】不能访问啦!<br/><br/>【时间】{1}<br/><br/>【Ping】当前ping的结果为:{2}", domainname,DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), pr.Status),
34 true);
35 count = 0;
36 }
37 }
38 return "【Time】:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\t【Domain】:" + domainname + "\t【Status】:" + pr.Status.ToString() + "\t【RoundtripTime】:" + pr.RoundtripTime.ToString() + "\n";
39 }
40
41 public void Deal()
42 {
43 while (true)
44 {
45 Thread.Sleep(5000);
46 string result = Ping(domain);
47 _formWorkThread.ShowResult(result);
48 }
49 }
50
51 public void StartShowResult()
52 {
53 thread = new Thread(new ThreadStart(Deal));
54 thread.Name = "TimeThread";
55 thread.Start();
56 }
57
58 public void EndShowResult()
59 {
60 thread.Abort();
61 }
62 }
发邮件代码:
1 public class EmailHelp
2 {
3 public static void SendEmail(string[] emailAddress, string subject, string body, bool ishtml)
4 {
5 MailMessage msg = new MailMessage();
6 foreach (string o in emailAddress)
7 {
8 msg.To.Add(o);
9 }
10
11 msg.Subject = subject;//邮件标题
12 msg.Body = body;//邮件内容
13 msg.IsBodyHtml = ishtml;//是否是HTML邮件
14
15 Thread sendemail = new Thread(SendReplyEmail);
16 sendemail.Start(msg);
17 }
18
19 public static void SendEmail(string to, string subject, string body, bool ishtml)
20 {
21 SendEmail(new string[] { to }, subject, body, ishtml);
22 }
23
24 private static void SendEmail(MailMessage mailMessage)
25 {
26 mailMessage.From = new MailAddress("emailaddress", "网站检测邮件", System.Text.Encoding.UTF8);
27 mailMessage.SubjectEncoding = System.Text.Encoding.UTF8;//邮件标题编码
28 mailMessage.BodyEncoding = System.Text.Encoding.UTF8;//邮件内容编码
29 mailMessage.Priority = MailPriority.Normal;//邮件优先级
30 SmtpClient client = new SmtpClient();
31
32 //注册的邮箱和密码
33 client.Credentials = new System.Net.NetworkCredential("emailaddress", "password");
34 client.Port = 25;
35 client.Host = "smtp.qq.com";
36 client.EnableSsl = false;
37 try
38 {
39 client.Send(mailMessage);
40 }
41 catch (System.Net.Mail.SmtpException ex)
42 {
43 //log.Error(ex.Message);
44 }
45 }
46
47 private static void SendReplyEmail(object model)
48 {
49 SendEmail((MailMessage)model);
50 }
51 }
注:发邮件的代码中emailaddress跟password要换成你自己的邮件地址跟密码。另外下载的exe文件为保证可用,我用的邮箱地址跟密码没有去掉,请大家也不要反编译后查看(这只是一个coding时的测试邮箱,没什么价值,赚不到钱的)。
工具下载地址:
http://files.cnblogs.com/artwl/Domain(IP)Watcher.zip(至少要安装.net Framework 2.0)