打造自己的Linux服务器监控小工具
作者:网络转载 发布时间:[ 2014/7/18 14:26:40 ] 推荐标签:linux 操作系统
4 LinuxSessionHandle
|
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+"
");
}
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<String> result by split "
"
* remove the content above flag
* to transfer List<String> ---> list<String[]> by .split("\s{1,}")
* @param result
* @param flag
* @return
*/
public List<String[]> transferListofStringArray(String result,String flag){
List<String> resList = this.transferList(result);
List<String> contentList = this.removeResultHead(resList,flag);
return this.transferArrayOfList(contentList);
}
/**
* change String result to List<String> result by split "
"
* @param result
* @return List<String>
*/
public List<String> transferList(String result){
String[] strs = result.split("
");
List<String> list = new ArrayList<String>();
for(String s:strs){
list.add(s);
}
return list;
}
/**remove the content above flag
*
* @return List<String>
*/
public List<String> removeResultHead(List<String> resultList,String flag){
List<String> contentList = new ArrayList<String>();
contentList.addAll(resultList);
for(String res:resultList){
if(res.contains(flag)){
break;
}
contentList.remove(res);
}
return contentList;
}
/**to transfer List<String> ---> list<String[]> by .split("\s{1,}")
*
* @param contentList
* @return List<String[]>
*/
public List<String[]> transferArrayOfList(List<String> contentList){
List<String[]> 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<String> head,String[] infos,String title){
if(head.indexOf(title)>0){
return infos[head.indexOf(title)];
}
return "";
}
}
|
本文内容不用于商业目的,如涉及知识产权问题,请权利人联系SPASVO小编(021-61079698-8054),我们将立即处理,马上删除。
相关推荐
Linux下开源的DDR压力测试工具曝Linux恶意软件:让树莓派设备挖掘数字货币linux系统中不同颜色的文件夹及根目录介绍软件测试工程师必知必会Linux命令Linux下DNS服务器配置如何成为不可替代的Linux运维工程师?详解Linux进程(作业)的查看和杀死Linux 日志定时轮询流程详解比特币勒索病毒不只Windows系统有,Linux版的来了Linux日志定时轮询流程详解Linux iommu和vfio概念空间解构Linux系统如何低于TCP洪水攻击Linux无损调整分区大小Linux下防火墙配置实例Linux使用Jexus托管Asp.Net Core应用程序Linux中引号的那些事

sales@spasvo.com