use of msec.org.RemoteShell in project MSEC by Tencent.
the class AddSecondLevelServiceIPInfo method exec.
public AddSecondLevelServiceIPInfoResponse exec(AddSecondLevelServiceIPInfoRequest request) {
Logger logger = Logger.getLogger("AddSecondLevelServiceIPInfo");
AddSecondLevelServiceIPInfoResponse response = new AddSecondLevelServiceIPInfoResponse();
int instance_memory = request.getInstance_type() * INSTANCE_MEMORY_STEP;
response.setMessage("unkown error.");
response.setStatus(100);
String result = checkIdentity();
if (!result.equals("success")) {
response.setStatus(99);
response.setMessage(result);
return response;
}
if (request.getIp() == null || request.getIp().equals("")) {
response.setMessage("Some request field is empty.");
response.setStatus(100);
return response;
}
ArrayList<String> ips = Tools.splitBySemicolon(request.getIp());
Set<String> set_ips = new HashSet<>(ips);
if (ips.size() < 2 || set_ips.size() != ips.size()) {
response.setMessage("IP input error!");
response.setStatus(100);
return response;
}
if (ips.size() % request.getCopy() != 0) {
response.setMessage("the number of IPs is not divisible by the number of copies!");
response.setStatus(100);
return response;
}
DBUtil util = new DBUtil();
if (util.getConnection() == null) {
response.setMessage("DB connect failed.");
response.setStatus(100);
return response;
}
try {
//check if the IP has been installed in set...
String sql = "select ip, port, group_id, memory, master, status from t_service_info where first_level_service_name=? and second_level_service_name=? and ip in (";
List<Object> params = new ArrayList<>();
params.add(request.getFirst_level_service_name());
params.add(request.getSecond_level_service_name());
for (int i = 0; i < ips.size(); i++) {
if (i == 0) {
params.add(ips.get(i));
sql += "?";
} else {
params.add(ips.get(i));
sql += ",?";
}
}
sql += ")";
try {
List<ServerInfo> serverList = util.findMoreRefResult(sql, params, ServerInfo.class);
if (serverList.size() > 0) {
response.setStatus(101);
response.setMessage("Input IP(s) is already installed.");
return response;
}
} catch (Exception e) {
response.setStatus(100);
response.setMessage("db query exception!");
e.printStackTrace();
return response;
}
int group_id_index = 0;
sql = "select COALESCE(max(group_id),0) as group_id from t_service_info where first_level_service_name=? and second_level_service_name=?";
params.clear();
params.add(request.getFirst_level_service_name());
params.add(request.getSecond_level_service_name());
try {
Map<String, Object> result_map = util.findSimpleResult(sql, params);
if (result_map.size() != 1) {
response.setStatus(101);
response.setMessage("db set error!");
return response;
}
group_id_index = ((Long) result_map.get("group_id")).intValue();
} catch (Exception e) {
response.setStatus(100);
response.setMessage("db query exception!");
e.printStackTrace();
return response;
}
ArrayList<AddSecondLevelServiceIPInfoResponse.IPInfo> addedIPs = new ArrayList<>();
for (String ip : ips) {
AddSecondLevelServiceIPInfoResponse.IPInfo info = response.new IPInfo();
info.setIp(ip);
RemoteShell remoteShell = new RemoteShell();
StringBuffer output = new StringBuffer();
result = remoteShell.SendCmdsToRunAndGetResultBack(ipinfo_cmd, ip, output);
if (result == null || !result.equals("success")) {
logger.info(String.format("remote error:%s|%s", ip, result));
info.setStatus_message("shell");
} else {
logger.info(output);
String[] ipinfo_results = output.toString().split(" ");
if (ipinfo_results.length != 4) {
info.setStatus_message("shell");
} else {
//core mhz mem cpu
info.setCpu_cores(Integer.parseInt(ipinfo_results[0]));
info.setCpu_mhz(Integer.parseInt(ipinfo_results[1]));
info.setMemory_avail(Integer.parseInt(ipinfo_results[2]) / 1024);
info.setCpu_load(Integer.parseInt(ipinfo_results[3]));
}
}
addedIPs.add(info);
}
response.setAdded_ips(addedIPs, instance_memory);
ArrayList<String> set_ids = new ArrayList<>();
//get set ids
int set_num = ips.size() / request.getCopy();
sql = "select name from t_set_id_name where name not in (select distinct set_id from t_service_info where first_level_service_name=? and second_level_service_name=?) order by name asc limit ?";
params.clear();
params.add(request.getFirst_level_service_name());
params.add(request.getSecond_level_service_name());
params.add(set_num);
try {
ArrayList<Map<String, Object>> result_list = util.findModeResult(sql, params);
if (result_list.size() != set_num) {
response.setStatus(101);
response.setMessage("db set error!");
return response;
}
for (int i = 0; i < result_list.size(); i++) set_ids.add(result_list.get(i).get("name").toString());
} catch (SQLException e) {
response.setStatus(100);
response.setMessage("db query exception!");
e.printStackTrace();
return response;
}
CalcAddedIPForServers(response, request.getCopy(), set_ids, group_id_index, instance_memory);
if (response.getMessage().equals("success") && group_id_index == 0) {
//update t_second_level_service
logger.info(String.format("Create|Update copy_num & mem_per_instance|%d|%d", group_id_index, instance_memory));
sql = "update t_second_level_service set copy_num=?, memory_per_instance=? where first_level_service_name=? and second_level_service_name=?";
params.clear();
params.add(request.getCopy());
params.add(instance_memory);
params.add(request.getFirst_level_service_name());
params.add(request.getSecond_level_service_name());
try {
int addNum = util.updateByPreparedStatement(sql, params);
if (addNum < 0) {
response.setMessage("failed to update table");
response.setStatus(100);
return response;
}
} catch (SQLException e) {
response.setMessage("update status failed:" + e.toString());
response.setStatus(100);
e.printStackTrace();
return response;
}
} else
logger.info(group_id_index);
return response;
} finally {
util.releaseConn();
}
}
use of msec.org.RemoteShell in project MSEC by Tencent.
the class ESCmd method exec.
public ESCmdResponse exec(ESCmdRequest request) {
Logger logger = Logger.getLogger(ESCmd.class);
ESCmdResponse resp = new ESCmdResponse();
String result = checkIdentity();
String result_message = "";
if (!result.equals("success")) {
resp.setStatus(99);
resp.setMessage(result);
return resp;
}
logger.info(request.getCommand());
if (request.getCommand().equals("restart")) {
RemoteShell remoteShell = new RemoteShell();
String ip = request.getHost().split(":")[0];
String cmd = "MSG=`/data/stop.sh | grep -v ok`\n" + "if [ ! -z \"$MSG\" ]; then echo $MSG; exit 1; fi\n" + "sleep 2;/data/start.sh | grep -v ok\n";
StringBuffer output = new StringBuffer();
result = remoteShell.SendCmdsToRunAndGetResultBack(cmd, ip, output);
if (result == null || !result.equals("success")) {
logger.error(String.format("remote error:%s|%s", ip, result));
resp.setStatus(100);
resp.setMessage("Restart fails.");
} else {
logger.info(String.format("%s|%s|%s", ip, output, cmd));
if (!output.toString().isEmpty()) {
logger.error(String.format("restart es node error:%s|%s", ip, output));
result_message = "[ERROR] " + output.toString();
} else {
result_message = "Server restarts.";
}
}
}
resp.setResult(result_message);
resp.setMessage("success");
resp.setStatus(0);
return resp;
}
Aggregations