List<Server> servers = new ArrayList(){{
add(new Server(name, "", ""));
add(new Server(name, "", ""));
add(new Server(name, "", ""));
add(new Server(name, "", ""));
}};
Timer timer = new Timer();
// 延迟1秒,10分钟执行一次
timer.schedule(new TimerTask() {
@Override
public void run() {
servers.parallelStream().forEach(s -> {
int used = getUsedIO(s);
s.setPercent(used);
// 发送短信
});
}
}, 1000, 1000 * 10 * 60);
public static int getUsedIO(String name, String pwd, String host) {
JSchUtils jSchUtils = new JSchUtils();
try {
jSchUtils.connect(name, pwd, host, 22);
String cmd = "df -lh | awk '{print $5}'";
String res = jSchUtils.execCmd(cmd);
if ("".equals(res)) {
System.out.println("ssh 命令执行失败: " + cmd);
return 0;
}
String[] percents = res.split("%");
for (String p : percents) {
if (p.matches("\\d+")) {
Integer integer = Integer.valueOf(p);
return integer;
}
}
return 0;
} catch (JSchException e) {
e.printStackTrace();
return 0;
} catch (Exception e) {
e.printStackTrace();
return 0;
} finally {
jSchUtils.close();
}
}
static class Server{
String name,pwd,host;
int percent;
public Server() {
}
public Server(String name, String pwd, String host) {
this.name = name;
this.pwd = pwd;
this.host = host;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("{");
sb.append("\"name\":\"")
.append(name).append('\"');
sb.append(",\"pwd\":\"")
.append(pwd).append('\"');
sb.append(",\"host\":\"")
.append(host).append('\"');
sb.append(",\"percent\":")
.append(percent);
sb.append('}');
return sb.toString();
}
}
public class JSchUtils {
private JSch jsch;
private Session session;
/**
* 连接到指定的IP
*
* @throws JSchException
*/
public void connect(String user, String passwd, String host, int port) throws JSchException {
jsch = new JSch();// 创建JSch对象
session = jsch.getSession(user, host, port);// 根据用户名、主机ip、端口号获取一个Session对象
session.setPassword(passwd);// 设置密码
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);// 为Session对象设置properties
session.setTimeout(1500);// 设置超时
session.connect();// 通过Session建立连接
}
/**
* 关闭连接
*/
public void close() {
session.disconnect();
}
/**
* 执行相关的命令
*
* @throws JSchException
*/
public String execCmd(String command) throws JSchException {
BufferedReader reader = null;
Channel channel = null;
String res = "";
try {
if (command != null) {
channel = session.openChannel("exec");
((ChannelExec) channel).setCommand(command);
// ((ChannelExec) channel).setErrStream(System.err);
channel.connect();
InputStream in = channel.getInputStream();
reader = new BufferedReader(new InputStreamReader(in));
String buf = null;
while ((buf = reader.readLine()) != null) {
res += buf;
}
}
return res;
} catch (IOException e) {
e.printStackTrace();
} catch (JSchException e) {
e.printStackTrace();
} finally {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
channel.disconnect();
}
return "";
}
public int exec( String cmd )
throws Exception{
ChannelExec channelExec = (ChannelExec)session.openChannel( "exec" );
channelExec.setCommand( cmd );
channelExec.setInputStream( null );
channelExec.setErrStream( System.err );
InputStream in = channelExec.getInputStream();
channelExec.connect();
int res = -1;
StringBuffer buf = new StringBuffer( 1024 );
byte[] tmp = new byte[ 1024 ];
while ( true ) {
while ( in.available() > 0 ) {
int i = in.read( tmp, 0, 1024 );
if ( i < 0 ){ break;}
buf.append( new String( tmp, 0, i ) );
}
if ( channelExec.isClosed() ) {
res = channelExec.getExitStatus();
System.out.println(String.format( "Exit-status: %d", res ) );
break;
}
// Wand.waitA( 100 );
}
System.out.println( buf.toString() );
channelExec.disconnect();
return res;
}
/**
* 上传文件
*
* @param directory
* 上传的目录
* @param uploadFile
* 要上传的文件
* @throws JSchException
* @throws SftpException
* @throws FileNotFoundException
*/
public void upload(String directory, String uploadFile) throws JSchException, FileNotFoundException, SftpException {
ChannelSftp channelSftp = (ChannelSftp) session.openChannel("sftp");
channelSftp.cd(directory);
File file = new File(uploadFile);
channelSftp.put(new FileInputStream(file), file.getName());
System.out.println("Upload Success!");
}
/**
* 下载文件
*
* @param src
* @param dst
* @throws JSchException
* @throws SftpException
*/
public void download(String src, String dst) throws JSchException, SftpException {
// src linux服务器文件地址,dst 本地存放地址
ChannelSftp channelSftp = (ChannelSftp) session.openChannel("sftp");
channelSftp.connect();
channelSftp.get(src, dst);
channelSftp.quit();
}
/**
* 删除文件
*
* @param directory
* 要删除文件所在目录
* @param deleteFile
* 要删除的文件
* @throws SftpException
* @throws JSchException
*/
public void delete(String directory, String deleteFile) throws SftpException, JSchException {
ChannelSftp channelSftp = (ChannelSftp) session.openChannel("sftp");
channelSftp.cd(directory);
channelSftp.rm(deleteFile);
}
/**
* 列出目录下的文件
*
* @param directory
* 要列出的目录
* @return
* @throws SftpException
* @throws JSchException
*/
@SuppressWarnings("rawtypes")
public Vector listFiles(String directory) throws JSchException, SftpException {
ChannelSftp channelSftp = (ChannelSftp) session.openChannel("sftp");
return channelSftp.ls(directory);
}
public static void main(String[] args) {
JSchUtils jSchUtils = new JSchUtils();
try {
// 1.连接到指定的服务器
jSchUtils.connect("root", "root0314", "192.168.3.94", 22);
// 2.执行相关的命令
// getContainersPeer();
// String res = execCmd("/usr/bin/bash /root/fabric-samples/first-network/createTx.sh test");
// System.out.println(res);
// exec("cd /root/fabric-samples/first-network/ && /root/bin/configtxgen -profile TwoOrgsChannel -outputCreateChannelTx ./channel-artifacts/test.tx -channelID test");
jSchUtils.download("/root/fabric-samples/first-network/channel-artifacts/test.tx", "d:/");
} catch (JSchException e) {
e.printStackTrace();
} catch (SftpException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
// 4.关闭连接
jSchUtils.close();
}
}
public static void getContainersPeer() throws JSchException {
JSchUtils jSchUtils = new JSchUtils();
String res = jSchUtils.execCmd("docker ps -a|grep protocol|awk '{print $2}'");
System.out.println(res);
String[] split = res.split("dev-");
float maxVersion = 0f;
for (String p : split){
if ("".equals(p)){
continue;
}
String peer = p.substring(0, p.indexOf("-"));
p = p.substring(p.indexOf("-") + 1);
String chaincode = p.substring(0, p.indexOf("-"));
p = p.substring(p.indexOf("-") + 1);
Float version = Float.valueOf(p.substring(0, p.indexOf("-")));
if (version > maxVersion){
maxVersion = version;
}
System.out.println("节点名称: " + peer + "; 链码名称: " + chaincode + "; version: " + version);
}
for (String p : split){
if ("".equals(p)){
continue;
}
String peer = p.substring(0, p.indexOf("-"));
p = p.substring(p.indexOf("-") + 1);
String chaincode = p.substring(0, p.indexOf("-"));
p = p.substring(p.indexOf("-") + 1);
Float version = Float.valueOf(p.substring(0, p.indexOf("-")));
if (version == maxVersion){
continue;
}
String id = jSchUtils.execCmd("docker ps -a|grep dev-"+peer+"-"+chaincode+"-"+version+"|awk '{print $1}'");
if (id.length() > 13){
continue;
}
System.out.println("节点id: " + id + ";节点名称: " + peer + "; 链码名称: " + chaincode + "; version: " + version);
jSchUtils.execCmd("docker rm " + id);
}
jSchUtils.close();
}
}