公司有幾百臺服務器,很多服務器使用了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的實時監控了.
發送的報警信息,會探測出目前哪臺服務器的狀況更差,更有針對性,方便系統組用戶處理服務器異常.