package com.suning.search.admin.web.util;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.expression.ParseException;
import org.springframework.scheduling.quartz.CronTriggerBean;
import com.suning.search.admin.service.bean.SearchIpStatusBean;
public class ScheduleUtils {
SearchIpStatusUtil ssu=new SearchIpStatusUtil();
static String bootSrc = "/opt/search/admin/asbsTXT/";
private static Logger logger = LoggerFactory
.getLogger(ScheduleUtils.class);
/**
* 定时调用任务
*/
public void scheduleCheckVirsion(){
int virsionFailsize=0;
String fileName = bootSrc + "allasbs.txt";
BufferedReader reader = null;
List<SearchIpStatusBean> searchbsList = new ArrayList<SearchIpStatusBean>();
SearchIpStatusBean sisb = null;
String tempString = null;
List<String> list = new ArrayList<String>();
List<String> listStatus = new ArrayList<String>();
int line = 1;
try {
reader = new BufferedReader(new FileReader(fileName));
while ((tempString = reader.readLine()) != null) {
list.add(tempString);
if (line % 4 == 0) {
sisb = new SearchIpStatusBean();
ssu.virsoncommon(sisb, list, listStatus);
sisb.setBsStatusI(listStatus.get(0));
sisb.setBsStatusII(listStatus.get(1));
if ("O".equals(ssu.validate(listStatus))) {
sisb.setStatus("ok");
}else if ("X".equals(ssu.validate(listStatus))){
virsionFailsize++;
}
listStatus.clear();
searchbsList.add(sisb);
list.clear();
}
line++;
}
} catch (FileNotFoundException e) {
logger.error("FileNotFoundException");
} catch (IOException e) {
logger.error("IOException",e);
}finally{
try {
reader.close();
} catch (IOException e) {
logger.error("IOException",e);
}
}
if(virsionFailsize>1){
ShortMessageUtil.sendMessage("ADMIN","IndexVirsionCheck","有"+virsionFailsize+"组版本不一致");
}
}
public void scheduleCheckASBS(){
int errorsize=0;
String fileName = bootSrc + "allasbs.txt";
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(fileName));
URL u;
List<SearchIpStatusBean> searchbsList = new ArrayList<SearchIpStatusBean>();
SearchIpStatusBean sisb = null;
String tempString = null;
List<String> list = new ArrayList<String>();
List<String> listStatus = new ArrayList<String>();
int line = 1;
int checasbs = 1;
while ((tempString = reader.readLine()) != null) {
list.add(tempString);
if (line % 4 == 0) {
sisb = new SearchIpStatusBean();
sisb.setAsipI(list.get(0));
sisb.setAsipII(list.get(1));
sisb.setBsipI(list.get(2));
sisb.setBsipII(list.get(3));
try {
for (String str : list) {
if (checasbs <= 2) {
u = new URL("http://" + str
+ ":9080/emall/checkStatus");
} else {
u = new URL("http://" + str
+ ":9080/solr/electric/admin/index.jsp");
}
HttpURLConnection uConnection = null;
try {
uConnection = (HttpURLConnection) u
.openConnection();
uConnection.setConnectTimeout(300);
uConnection.setReadTimeout(300);
uConnection.connect();
if (uConnection.getResponseCode() == 200) {
listStatus.add("ok");
} else {
listStatus.add("fail");
}
} catch (MalformedURLException e) {
logger.error("MalformedURLException", e);
listStatus.add("exception");
} catch (SocketTimeoutException e) {
logger.error("timeout", e);
listStatus.add("timeout");
} catch (IOException e) {
logger.error("IOException", e);
listStatus.add("timeout");
} finally {
if (uConnection != null) {
uConnection.disconnect();
}
}
checasbs++;
if (checasbs > 4) {
checasbs = 1;
}
}
} catch (IOException e) {
logger.error("IOException", e);
listStatus.add("timeout");
}
sisb.setAsStatusI(listStatus.get(0));
sisb.setAsStatusII(listStatus.get(1));
sisb.setBsStatusI(listStatus.get(2));
sisb.setBsStatusII(listStatus.get(3));
if ("ok".equals(listStatus.get(0))
&& "ok".equals(listStatus.get(1))
&& "ok".equals(listStatus.get(2))
&& "ok".equals(listStatus.get(3))
) {
sisb.setStatus("ok");
}else if ("X".equals(validate(listStatus))){
errorsize++;
}
listStatus.clear();
searchbsList.add(sisb);
list.clear();
}
line++;
}
} catch (FileNotFoundException e1) {
logger.error("FileNotFoundException",e1);
} catch (IOException e) {
logger.error("IOException",e);
}finally{
try {
reader.close();
} catch (IOException e) {
logger.error("IOException",e);
}
}
if(errorsize>1){
ShortMessageUtil.sendMessage("ADMIN","ASBSCheck","有"+errorsize+"组asbs运行不正常");
}
}
public String validate(List<String> listStatus) {
for (String status : listStatus) {
if (status.contains("fail")) {
return "X";
}
}
return "O";
}
}
------------------------------------------
package com.suning.search.admin.web.util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.methods.GetMethod;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import cern.colt.list.IntArrayList;
import com.suning.search.admin.web.controller.SearchIpStatusController;
public class SNSolrUtil {
private HttpClient client;
private static Logger logger = LoggerFactory
.getLogger(SNSolrUtil.class);
public SNSolrUtil() {
MultiThreadedHttpConnectionManager mgr = new MultiThreadedHttpConnectionManager();
mgr.getParams().setDefaultMaxConnectionsPerHost(10);
mgr.getParams().setMaxTotalConnections(10);
client = new HttpClient(mgr);
client.setConnectionTimeout(5000);
client.setTimeout(5000);
}
private String getServerURL(String ip){
return "http://"+ip+":9080/solr/index.jsp" ;
}
private String getServerReplicationURL(String ip) {
return "http://" + ip+ ":9080/solr-index/mainSearch-A/admin/replication/index.jsp";
}
private void executeURL(String url) {
HttpMethod method = null;
method = new GetMethod(url);
try {
int statusCode = client.executeMethod(method);
if (statusCode == 200)
System.out.println("success=@@@@@@" + url);
else
System.err.println(url);
} catch (HttpException e) {
System.err.println(url);
} catch (IOException e) {
System.err.println(url);
}
finally{
method.releaseConnection();
}
}
public String executeReplicationText(String url) {
if(url.contains("fail")){
return "fail";
}else{
HttpMethod method = null;
method = new GetMethod(url);
BufferedReader br =null;
String line;
String string="";
try {
client.setConnectionTimeout(500);
client.setTimeout(500);
int statusCode = client.executeMethod(method);
if (statusCode == 200) {
br= new BufferedReader(new InputStreamReader(
method.getResponseBodyAsStream(), "UTF-8"));
while ((line = br.readLine()) != null) {
//索引大小
if (line.contains("Size")) {
string = line.trim().substring(9).trim();
}
}
} else{
logger.error("executeReplicationText"+url);
string="fail";
}
} catch (HttpException e) {
logger.error("executeReplicationText"+url,e);
string="fail";
} catch (IOException e) {
logger.error("executeReplicationText"+url,e);
string="fail";
} finally {
method.releaseConnection();
if(br!=null)
try {
br.close();
} catch (IOException e) {
logger.error("executeReplicationText"+url,e);
}
}
return string;
}
}
public String executeVirson(String url) {
HttpMethod method = null;
method = new GetMethod(url);
BufferedReader br =null;
String line;
String string="";
try {
client.setConnectionTimeout(500);
client.setTimeout(500);
int statusCode = client.executeMethod(method);
if (statusCode == 200) {
br= new BufferedReader(new InputStreamReader(
method.getResponseBodyAsStream(), "UTF-8"));
while ((line = br.readLine()) != null) {
//版本号
if (line.contains("Index Version")&&(!line.contains("Latest"))) {
string =string+ line.split(",")[0].split(":")[1]+";";
}
}
int length=string.length();
string=string.substring(0,length-1);
} else{
logger.error("executeVirson"+url);
string="fail";
}
} catch (HttpException e) {
logger.error("executeVirson"+url,e);
string="fail";
} catch (IOException e) {
logger.error("executeVirson"+url,e);
string="fail";
} finally {
method.releaseConnection();
if(br!=null)
try {
br.close();
} catch (IOException e) {
logger.error("executeVirson"+url,e);
}
}
return string;
}
public String getMasterUrl(String url) {
HttpMethod method = null;
method = new GetMethod(url);
BufferedReader br =null;
String line;
String master="";
try {
client.setConnectionTimeout(500);
client.setTimeout(500);
int statusCode = client.executeMethod(method);
if (statusCode == 200) {
br= new BufferedReader(new InputStreamReader(
method.getResponseBodyAsStream(), "UTF-8"));
while ((line = br.readLine()) != null) {
//版本号
if (line.contains("mainSearch-A")) {
master=line.trim().replace("replication", "")+"admin/replication/index.jsp";
}
}
} else{
logger.error("getMasterUrl"+url);
master="fail";
}
} catch (HttpException e) {
logger.error("getMasterUrl"+url,e);
master="fail";
} catch (IOException e) {
logger.error("getMasterUrl"+url,e);
master="fail";
} finally {
method.releaseConnection();
if(br!=null)
try {
br.close();
} catch (IOException e) {
logger.error("getMasterUrl"+url,e);
}
}
return master;
}
public static void main(String args[]){
String ips[]={"http://192.168.199.193:9080/solr/electric/admin/replication/index.jsp"};
SNSolrUtil util=new SNSolrUtil();
for(int i=0;i<ips.length;i++){
String ip=ips[i];
util.getMasterUrl(ip);
}
}
}