Search in sources :

Example 6 with Result

use of com.sohu.cache.ssh.SSHTemplate.Result in project new-cloud by xie-summer.

the class ServerStatusCollector method collectServerStatus.

/**
 * 收集系统状况
 * @param ip
 * @param session
 */
private void collectServerStatus(String ip, SSHSession session) {
    final Server server = new Server();
    server.setIp(ip);
    Result result = session.executeCommand(COLLECT_SERVER_STATUS, new DefaultLineProcessor() {

        @Override
        public void process(String line, int lineNum) throws Exception {
            server.parse(line, null);
        }
    });
    if (!result.isSuccess()) {
        logger.error("collect " + ip + " err:" + result.getResult(), result.getExcetion());
    }
    // 保存服务器静态信息
    serverDataService.saveAndUpdateServerInfo(server);
    // 保存服务器状况信息
    serverDataService.saveServerStat(server);
}
Also used : DefaultLineProcessor(com.sohu.cache.ssh.SSHTemplate.DefaultLineProcessor) Server(com.sohu.cache.server.data.Server) Result(com.sohu.cache.ssh.SSHTemplate.Result)

Example 7 with Result

use of com.sohu.cache.ssh.SSHTemplate.Result in project cachecloud by sohutv.

the class ServerStatusCollector method collectServerStatus.

/**
 * 收集系统状况
 * @param ip
 * @param session
 */
private void collectServerStatus(String ip, SSHSession session) {
    final Server server = new Server();
    server.setIp(ip);
    Result result = session.executeCommand(COLLECT_SERVER_STATUS, new DefaultLineProcessor() {

        public void process(String line, int lineNum) throws Exception {
            server.parse(line, null);
        }
    });
    if (!result.isSuccess()) {
        logger.error("collect " + ip + " err:" + result.getResult(), result.getExcetion());
    }
    // 保存服务器静态信息
    serverDataService.saveAndUpdateServerInfo(server);
    // 保存服务器状况信息
    serverDataService.saveServerStat(server);
}
Also used : DefaultLineProcessor(com.sohu.cache.ssh.SSHTemplate.DefaultLineProcessor) Server(com.sohu.cache.server.data.Server) Result(com.sohu.cache.ssh.SSHTemplate.Result)

Example 8 with Result

use of com.sohu.cache.ssh.SSHTemplate.Result in project cachecloud by sohutv.

the class SSHUtil method execute.

/**
 * SSH 方式登录远程主机,执行命令,方法内部会关闭所有资源,调用方无须关心。
 *
 * @param ip       主机ip
 * @param username 用户名
 * @param password 密码
 * @param command  要执行的命令
 */
public static String execute(String ip, int port, String username, String password, final String command) throws SSHException {
    if (StringUtil.isBlank(command)) {
        return EMPTY_STRING;
    }
    port = IntegerUtil.defaultIfSmallerThan0(port, ConstUtils.SSH_PORT_DEFAULT);
    Result rst = sshTemplate.execute(ip, port, username, password, new SSHCallback() {

        public Result call(SSHSession session) {
            return session.executeCommand(command);
        }
    });
    if (rst.isSuccess()) {
        return rst.getResult();
    }
    return "";
}
Also used : SSHSession(com.sohu.cache.ssh.SSHTemplate.SSHSession) SSHCallback(com.sohu.cache.ssh.SSHTemplate.SSHCallback) Result(com.sohu.cache.ssh.SSHTemplate.Result)

Aggregations

Result (com.sohu.cache.ssh.SSHTemplate.Result)8 DefaultLineProcessor (com.sohu.cache.ssh.SSHTemplate.DefaultLineProcessor)4 SSHCallback (com.sohu.cache.ssh.SSHTemplate.SSHCallback)4 SSHSession (com.sohu.cache.ssh.SSHTemplate.SSHSession)4 MachineStats (com.sohu.cache.entity.MachineStats)2 IllegalParamException (com.sohu.cache.exception.IllegalParamException)2 SSHException (com.sohu.cache.exception.SSHException)2 Server (com.sohu.cache.server.data.Server)2 LineProcessor (com.sohu.cache.ssh.SSHTemplate.LineProcessor)2 DecimalFormat (java.text.DecimalFormat)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2