最近开始做一个网站监控系统,涉及的功能包括:站点监控、服务器性能监控、服务监控等。站点监控的类型包括:HTTP/HTTPS、PING、FTP、DNS、TCP、UDP、SMTP。可用率和响应时间是两个重要的性能指标。
其实有些实现还是比较简单的,只要模拟连接服务器就可以了。
先贴一下FTP、SMTP的实现代码(只是简单实现,还有深入的东西要去考虑)
FTP
import sun.net.ftp.*;
public class FtpTool{
/*
* 可用率
* 响应时间
*
*
* */
public static boolean connectServer(String host,int port,String username,String password)
throws Exception{
boolean isSuccess = false;
try {
FtpClient ftpclient = new FtpClient();
ftpclient.openServer(host, port);
ftpclient.login(username, password);
isSuccess = true;
} catch (Exception e) {
throw new Exception("Connect ftp server error:"+e.getMessage());
}
return isSuccess;
}
public static void main(String[] args) {
long start = System.currentTimeMillis();
long end=0;
long response=0;
try {
boolean isSuccess=FtpTool.connectServer("www.linuxidc.com", 21, "www.linuxidc.com", "www.muu.cc");
//boolean isSuccess=FtpTool.connectServer("218.4.56.235", 21, "anonymous", "");
end = System.currentTimeMillis();
response = end-start;
System.out.println("Connection successful");
System.out.println("响应时间:"+response+"ms");
} catch (Exception e) {
end = System.currentTimeMillis();
response = end-start;
System.out.println(e.getMessage());
System.out.println("响应时间:"+response+"ms");
}
}
}
SMTP
import sun.net.smtp.*;
public class SmtpTool {
public static boolean connectServer(String mailhost,int port)throws Exception{
boolean isSuccess=false;
SmtpClient smtpclient;
try {
smtpclient = new SmtpClient(mailhost);
smtpclient.openServer(mailhost, port);
} catch (Exception e) {
throw new Exception("Connect server error:"+e.getMessage());
}
isSuccess=true;
return isSuccess;
}
public static void main(String[] args) {
long start = System.currentTimeMillis();
long end=0;
long response=0;
try {
SmtpTool.connectServer("smtp.sina.com.cn",25);
end=System.currentTimeMillis();
response=end-start;
System.out.println("Connection successfull");
System.out.println("响应时间:"+response+"ms");
} catch (Exception e) {
end=System.currentTimeMillis();
response=end-start;
System.out.println("Connect server error:"+e.getMessage());
System.out.println("响应时间:"+response+"ms");
}
}
}
未完待续 简单记录
参考论文:http://wenku.baidu.com/view/c79b5a2c453610661ed9f4b9.html
作者:像少年啦飞驰
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
浙公网安备 33010602011771号