java监控监测linux服务器_打造自己的Linux服务器监控小工具

package com.sunfan.monitor.platform.linux;

import java.io.BufferedReader;

import java.io.Closeable;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.springframework.stereotype.Component;

import com.sunfan.monitor.platform.IMonitorable;

import com.trilead.ssh2.Connection;

import com.trilead.ssh2.Session;

import com.trilead.ssh2.StreamGobbler;

/**

*

* @author sunfan

*

*/

@Component

public class LinuxSessionHandle implements IMonitorable,Closeable{

Logger logger = LoggerFactory.getLogger(LinuxSessionHandle.class);

private Session session;

/**

* open session then execute commands on remote server and return the result of it

* @param command

* @return String

* @throws IOException

*/

public synchronized String executeCommand(Connection conn,String command) throws IOException {

String str="";

try {

session = conn.openSession();

session.execCommand(command);

str = this.read().toString();

} catch (Exception e) {

session.ping();

throw new IOException("session exception",e);

}

finally{

close();

}

return str;

}

/**

* read the result of remote server execute commands

* @return

* @throws IOException

*/

private StringBuffer read() throws IOException{

InputStream stdout = new StreamGobbler(session.getStdout());

BufferedReader br = new BufferedReader(new InputStreamReader(stdout));

String tempString = null;

// readLine()每次调用都默认会读一行

StringBuffer str = new StringBuffer();

while ((tempString = br.readLine()) != null) {

str.append(tempString+"\r\n");

}

br.close();

return str;

}

/**

* close session

*/

@Override

public void close() throws IOException {

if (this.session != null) this.session.close();

}

}

5 EntityBaseUtil

package com.sunfan.monitor.entity.util;

import java.util.ArrayList;

import java.util.List;

/**

*

* @author sunfan

*

*/

public class EntityBaseUtil {

/**change String result to List result by split "\r\n"

* remove the content above flag

* to transfer List ---> list by .split("\\s{1,}")

* @param result

* @param flag

* @return

*/

public List transferListofStringArray(String result,String flag){

List resList = this.transferList(result);

List contentList = this.removeResultHead(resList,flag);

return this.transferArrayOfList(contentList);

}

/**

* change String result to List result by split "\r\n"

* @param result

* @return List

*/

public List transferList(String result){

String[] strs = result.split("\r\n");

List list = new ArrayList();

for(String s:strs){

list.add(s);

}

return list;

}

/**remove the content above flag

*

* @return List

*/

public List removeResultHead(List resultList,String flag){

List contentList = new ArrayList();

contentList.addAll(resultList);

for(String res:resultList){

if(res.contains(flag)){

break;

}

contentList.remove(res);

}

return contentList;

}

/**to transfer List ---> list by .split("\\s{1,}")

*

* @param contentList

* @return List

*/

public List transferArrayOfList(List contentList){

List contentLists =new ArrayList<>();

for(String content:contentList){

contentLists.add(content.split("\\s{1,}"));

}

return contentLists;

}

/**get result of reference by title

*

* @param head   data of  reference title

* @param title   reference title

* @param infos  data of each  reference

* @return  String result of reference by title ,if title is not matched ,return " "

*/

public String resolveValueByTagName(List head,String[] infos,String title){

if(head.indexOf(title)>0){

return infos[head.indexOf(title)];

}

return "";

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值