監控給定的域名,一旦域名連續30秒(這是默認值,可以在源碼中修改)無法Ping通,立刻發郵件到指定郵箱,並發短信給站長
原理: 用後台線程循環每隔5秒鐘Ping結定的域名,如果連接5次Ping不通則發送郵件到給定郵箱。發短信是通過其他途徑實現的,現在最常用的有兩種方式:
一是利用QQ郵箱的手機綁定功能實現收到郵件短信提示
二是用www.ifttt.com提供的gmail跟sms服務實現收到郵件發短信提示
本工具用的是第一種。
主要程序:
主要是後台線程ping域名的應用,界面上顯示記錄的是一個richtextbox控件,其他幾個就不說了,代碼如下:
窗體後台程序:
代碼如下:
public partial class FormMain : Form, IFormWorkThread
{
private delegate void ControlDelegate(string record);
private WorkThread wtObj;
ControlDelegate cdObj;
public FormMain()
{
InitializeComponent();
cdObj = SetRecord;
}
private void FormMain_Load(object sender, EventArgs e)
{
}
private void btnBegin_Click(object sender, EventArgs e)
{
if (txtDomain.Text.Trim() == "")
{
MessageBox.Show("Domain(IP) can not be empty!");
return;
}
if (txtEmail.Text.Trim() == "")
{
MessageBox.Show("Email address can not be empty!");
return;
}
Regex regex = new Regex(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
if (!regex.IsMatch(txtEmail.Text.Trim()))
{
MessageBox.Show("Email address error!");
return;
}
wtObj = new WorkThread(this, 0, 3, txtDomain.Text.Trim(), txtEmail.Text.Trim());
wtObj.StartShowResult();
}
private void SetRecord(string record)
{
rtxtRecord.AppendText(record);
if (rtxtRecord.Lines.Length > 5 * 60 * 24 * 7)
{
rtxtRecord.Clear();
}
}
public void ShowResult(string record)
{
if (rtxtRecord.InvokeRequired)
{
Invoke(cdObj, record);
}
else
{
SetRecord(record);
}
}
private void btnEnd_Click(object sender, EventArgs e)
{
if (wtObj != null)
{
wtObj.EndShowResult();
}
}
private void FormMain_FormClosing(object sender, FormClosingEventArgs e)
{
if (wtObj != null)
{
wtObj.EndShowResult();
}
}
private void tslblName_Click(object sender, EventArgs e)
{
OpenMyBlog();
}
private void OpenMyBlog()
{
var Info = new System.Diagnostics.ProcessStartInfo("http://artwl.cnblogs.com");
System.Diagnostics.Process.Start(Info);
}
}
IFormWorkThread接口:
代碼如下:
public interface IFormWorkThread
{
void ShowResult(string record);
}
WorkThread代碼:
代碼如下:
public class WorkThread
{
private IFormWorkThread _formWorkThread;
private Thread thread;
private int count = 0, maxcount = 5;
private string domain = "";
private string emailaddress = "";
public WorkThread(IFormWorkThread formWorkThread,int count,int maxcount,string domain,string email)
{
this.count = count;
this.maxcount = maxcount;
this.domain = domain;
this.emailaddress = email;
_formWorkThread = formWorkThread;
}
private string Ping(string domainname)
{
Ping ping = new Ping();
PingReply pr = ping.Send(domainname);
if (pr.Status == IPStatus.Success)
{
count = count > 0 ? count - 1 : 0;
}
else
{
count++;
if (count > maxcount)
{
EmailHelp.SendEmail(emailaddress,
string.Format("【 {0} 】不能訪問啦!",domainname),
string.Format("【 {0} 】不能訪問啦!<br/><br/>【時間】{1}<br/><br/>【Ping】當前ping的結果為:{2}", domainname,DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), pr.Status),
true);
count = 0;
}
}
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";
}
public void Deal()
{
while (true)
{
Thread.Sleep(5000);
string result = Ping(domain);
_formWorkThread.ShowResult(result);
}
}
public void StartShowResult()
{
thread = new Thread(new ThreadStart(Deal));
thread.Name = "TimeThread";
thread.Start();
}
public void EndShowResult()
{
thread.Abort();
}
}
發郵件代碼:
代碼如下:
public class EmailHelp
{
public static void SendEmail(string[] emailAddress, string subject, string body, bool ishtml)
{
MailMessage msg = new MailMessage();
foreach (string o in emailAddress)
{
msg.To.Add(o);
}
msg.Subject = subject;//郵件標題
msg.Body = body;//郵件內容
msg.IsBodyHtml = ishtml;//是否是HTML郵件
Thread sendemail = new Thread(SendReplyEmail);
sendemail.Start(msg);
}
public static void SendEmail(string to, string subject, string body, bool ishtml)
{
SendEmail(new string[] { to }, subject, body, ishtml);
}
private static void SendEmail(MailMessage mailMessage)
{
mailMessage.From = new MailAddress("emailaddress", "網站檢測郵件", System.Text.Encoding.UTF8);
mailMessage.SubjectEncoding = System.Text.Encoding.UTF8;//郵件標題編碼
mailMessage.BodyEncoding = System.Text.Encoding.UTF8;//郵件內容編碼
mailMessage.Priority = MailPriority.Normal;//郵件優先級
SmtpClient client = new SmtpClient();
//注冊的郵箱和密碼
client.Credentials = new System.Net.NetworkCredential("emailaddress", "password");
client.Port = 25;
client.Host = "smtp.qq.com";
client.EnableSsl = false;
try
{
client.Send(mailMessage);
}
catch (System.Net.Mail.SmtpException ex)
{
//log.Error(ex.Message);
}
}
private static void SendReplyEmail(object model)
{
SendEmail((MailMessage)model);
}
}
注:發郵件的代碼中emailaddress跟password要換成你自己的郵件地址跟密碼。另外下載的exe文件為保證可用,我用的郵箱地址跟密碼沒有去掉,請大家也不要反編譯後查看(這只是一個coding時的測試郵箱,沒什麼價值,賺不到錢的)。
工具下載地址 Domain(IP)Watcher