公司有几百台服务器,很多服务器使用了LVS,同一个应用会部署在很多不同的服务器上,然后在上层加LVS,这个时候,当后台一台或几台服务服务器宕掉了,前端应用是正常的,通过对URL的监控,不能发现问题.
上周末托管在深圳电信的机器,有一个机柜9台服务器同时断掉,经过查找,最后是外网交换机出现了问题.但这个时候前端应用是正常的,而监控,没有发出报警信息,昨天在监控上面加上新功能,穿过LVS,直接到后端服务器进行监控.
这个服务器的监控,分为两种.
1:通过SNMP对服务器进行监控.
2:通过对应用的URL对服务器进行监控.
SNMP主要监控服务器的运行状态.
URL监控,主要监控应用的实时运行状态.
费话少说,对应用加IP的探测代码如下:

public static Long getResponseTimeByIp(String urlAddress, String ip)
{
URL url;
StringBuffer sb = new StringBuffer("");
HttpURLConnection conn = null;
Long responseTime = new Long(0);

try
{
Long openTime = System.currentTimeMillis();
// url = new URL("http://m.easou.com/");
url = new URL(urlAddress);
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(buildInetAddress(ip), 80));
conn = (HttpURLConnection) url.openConnection(proxy);
conn.setConnectTimeout(50000);
conn.setReadTimeout(50000);
conn.setRequestMethod("GET");
conn.setDoOutput(true);
conn.setDoInput(true);
BufferedReader bReader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String temp;
boolean remaining = true;

while (remaining)
{
temp = bReader.readLine();

if (null != temp)
{
sb.append(temp);

} else
{
remaining = false;
}
}
int code = conn.getResponseCode();

if (code == 200)
{
Long returnTime = System.currentTimeMillis();
responseTime = returnTime - openTime;

} else
{
responseTime = new Long("50000" + new Long(code).toString());
}

} catch (MalformedURLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
responseTime = new Long("60000000");

} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
responseTime = new Long("60000000");

} finally
{

if (null != conn)
{
conn.disconnect();
}
}
return responseTime;
}

使用这段代码,就可以对于做了负载均衡的服务器,进行URL的实时监控了.
发送的报警信息,会探测出目前哪台服务器的状况更差,更有针对性,方便系统组用户处理服务器异常.