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

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

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

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

  代码
  1 LinuxService
package com.sunfan.monitor.service;
import java.io.IOException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.sunfan.monitor.manager.pool.LinuxConnectionPool;
import com.sunfan.monitor.platform.IConnectable;
import com.sunfan.monitor.platform.linux.LinuxSessionHandle;
/**
*
* @author sunfan
*
*/
@Component
public class LinuxService {
private  String defaultTopComand = "top -b -n 1";
private  String defaultMpstatComand = "mpstat -P ALL";
private  String defaultFreeCommand = "free -m";
private  String defaultIostatCommand = "iostat -d -m";
@Autowired
private LinuxConnectionPool pool ;
@Autowired
private LinuxSessionHandle handle;
/**
* execute default command "top -b -n 1" and return String type reslut
*
* @param url  server's ip
* @param user  login name
* @param password login password
* @return
* @throws IOException
*/
public String topMonitor(String url,String user,String password) throws IOException{
return this.executeCommand(url, user, password, defaultTopComand);
}
/**
* execute default command "mpstat -P ALL" to get cpus performance
*
* @param url
* @param user
* @param password
* @return
* @throws IOException
*/
public String cpuMonitor(String url,String user,String password) throws IOException{
return this.executeCommand(url, user, password, defaultMpstatComand);
}
/**
* execute default command "free -m" to get memory performance
* @param url
* @param user
* @param password
* @return
* @throws IOException
*/
public String memoryMonitor(String url,String user,String password) throws IOException{
return this.executeCommand(url, user, password, defaultFreeCommand);
}
/**
* execute default command "free -m" to get memory performance
* @param url
* @param user
* @param password
* @return
* @throws IOException
*/
public String inputOutputMonitor(String url,String user,String password) throws IOException{
return this.executeCommand(url, user, password, defaultIostatCommand);
}
public String executeCommand(String url,String user,String password,String command) throws IOException{
IConnectable lc =  pool.borrowObject(url,user,password);
return handle.executeCommand(lc.getConnection(),command);
}
}
  2 LinuxConnectionPool
package com.sunfan.monitor.manager.pool;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import com.sunfan.monitor.manager.IConnectionPool;
import com.sunfan.monitor.platform.IConnectable;
import com.sunfan.monitor.platform.linux.LinuxConnection;
/**
*
* @author sunfan
*
*/
@Component
public class LinuxConnectionPool implements IConnectionPool {
private Logger logger = LoggerFactory.getLogger(getClass());
private Map<String, IConnectable> connectionPool = new HashMap<String, IConnectable>();
/**
* save connecion in the connectionPool,if conn is already exist return
* false else return true
*
* @param conn
* @return
*/
public synchronized Boolean saveObject(IConnectable conn) {
String key = rewardConnectionKey(conn);
if (isExist(key)) {
logger.info("this key{} has already exist", key);
return false;
}
connectionPool.put(key, conn);
return true;
}
/**
* borrow connection object in the connect-pool
*
* @param key
* @return
*/
public IConnectable borrowObject(String key) {
if (!isExist(key)) {
throw new IllegalArgumentException("key not found:" + key);
}
return connectionPool.get(key);
}
public IConnectable borrowObject(String url,String user,String password){
String key = this.rewardConnectionKey(url, user, password);
if (!isExist(key)){
try {
LinuxConnection connect = new LinuxConnection(url, user, password);
connectionPool.put(key, connect);
return connect;
} catch (IOException e) {
throw new RuntimeException("connection error"+url,e);
}
}else {
return connectionPool.get(key);
}
}
/**
* borrow connection object in the connect-pool if the connection hasn't in
* the connectionPool return null,else return connect object
*
* @param conn
* @return
*/
public IConnectable borrowObject(IConnectable conn) {
String key = rewardConnectionKey(conn);
return borrowObject(key);
}
/**
* close single connection in the connection-pool and close/release of this
* connection
*
* @param conn
* @throws IOException
*/
public void remove(IConnectable conn) throws IOException {
String key = rewardConnectionKey(conn);
remove(key);
}
/**
* close single connection in the connection-pool and close/release of this
* connection
*
* @param conn
* @throws IOException
*/
public synchronized void remove(String key) throws IOException {
if (!isExist(key)) {
throw new IllegalArgumentException(key + "is not exist");
}
connectionPool.get(key).close();
connectionPool.remove(key);
}
/**
* delete every connection in the connection-pool and also close/release of
* all connection
*
* @throws IOException
*/
public void clear() throws IOException {
for (String keyString : connectionPool.keySet()) {
connectionPool.get(keyString).close();
}
connectionPool.clear();
}
/**
* according to the connection to generate key if the connecion is not equal
* null return url/usr/password
*
* @param conn
* @return
*/
public String rewardConnectionKey(IConnectable conn) {
return conn.getUrl() + "/" + conn.getUser() + "/" + conn.getPassword();
}
public String rewardConnectionKey(String url, String user, String password) {
return url + "/" + user + "/" + password;
}
/**
* To confirm whether the connectionPool has this key if already has return
* true else return false
*
* @param key
* @return
*/
public Boolean isExist(String key) {
return connectionPool.containsKey(key);
}
}
42/4<1234>
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

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

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

沪ICP备05003035号

沪公网安备 31010102002173号