打造自己的Linux服务器监控小工具

发表于:2014-7-18 10:40

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:sunfan    来源:51Testing软件测试网采编

  3 LinuxConnection
package com.sunfan.monitor.platform.linux;
import java.io.Closeable;
import java.io.IOException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.sunfan.monitor.platform.IConnectable;
import com.trilead.ssh2.Connection;
import com.trilead.ssh2.ConnectionInfo;
/**
*
* @author sunfan
*
*/
public class LinuxConnection implements IConnectable, Closeable {
Logger log = LoggerFactory.getLogger(LinuxConnection.class);
private String url, user, password;
private Connection connection;
/**
* init server's connect
*
* @param url
* @param user
* @param password
* @throws IOException
*/
public LinuxConnection(String url, String user, String password) throws IOException{
this.url = url;
this.user = user;
this.password = password;
this.connection = createConnection();
}
/**
* this method will establish connection unless username or password
* incorrect and remote server is not find
*
* @return connection
* @throws IOException
*/
private Connection createConnection() throws IOException {
connection = new Connection(url);
ConnectionInfo info = connection.connect(); // establish connection
if (false == connection.authenticateWithPassword(user, password)){
log.error("connect server failed,please check the username and password. " + this.user + " " + this.password);
throw new IllegalArgumentException("connection remote server fail");
}
return connection;
}
/**
* close connection
*/
@Override
public void close() throws IOException {
if (connection != null)
this.connection.close();
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Connection getConnection() {
return connection;
}
public void setConnection(Connection connection) {
this.connection = connection;
}
}
  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+"\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<String> result by split "\r\n"
* 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 "\r\n"
* @param result
* @return List<String>
*/
public List<String> transferList(String result){
String[] strs = result.split("\r\n");
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 "";
}
}
43/4<1234>
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号