packagecom.system.action;importjava.io.BufferedReader;importjava.io.IOException;importjava.io.InputStream;importjava.io.InputStreamReader;importjava.util.ArrayList;importjava.util.HashMap;importjava.util.List;importjava.util.Map;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Controller;importorg.springframework.ui.Model;importorg.springframework.web.bind.annotation.RequestMapping;importcom.system.service.MonitorService;importch.ethz.ssh2.Connection;importch.ethz.ssh2.Session;importch.ethz.ssh2.StreamGobbler;importutil.dataSource.SwitchDataSourceUtil;importutil.page.BaseAction;/*** 服务器监控
*@authorwangxiangyu
**/@Controller
@RequestMapping("/monitor")
@SuppressWarnings("all")public class MonitorController extendsBaseAction {private static List commands = null;private static final String JNDI = "pn";//爱运维管理员权限数据源
@Autowired
MonitorService monitorService;/*** 进入监控页面,获取服务器配置(ip,登录名,密码)
*@paramrequest
*@parammodel
*@return
*/@RequestMapping(value= "/index.do")publicString index(HttpServletRequest request, Model model){
String hostName= request.getParameter("hostName");
List> serverConfigList =monitorService.getServerConfig(hostName);
model.addAttribute("serverConfigList", serverConfigList);return "system/monitor/monitor";
}/*** 初始化命令集合*/
private voidinitCommands() {
commands= new ArrayList();
commands.add("top");
commands.add("free");
commands.add("df -hl");
commands.add("ps -ef|grep tomcat");
commands.add("ps aux|head -1;ps aux|grep -v PID|sort -rn -k +4|head");
}/*** 获取服务器运行状态
*@paramrequest
*@paramresponse
*@throwsException*/@RequestMapping("/findStatus.do")public void findStatus(HttpServletRequest request, HttpServletResponse response) throwsException {//获取服务器配置信息
String hostName = request.getParameter("hostName");
List> serverConfigList =monitorService.getServerConfig(hostName);
String userName= serverConfigList.get(0).get("USER_NAME").toString();
String password= serverConfigList.get(0).get("PASSWORD").toString();//预定义结果集
Map result = new HashMap();//预定义命令结果集
List> commandResults = new ArrayList>();try{//连接服务器
Connection conn = newConnection(hostName);
conn.connect();boolean isAuthenticated =conn.authenticateWithPassword(userName, password);if (isAuthenticated == false) {throw new IOException("Authentication failed.");
}//初始化命令参数
initCommands();for(String command : commands) {
Session sess=conn.openSession();
sess.execCommand(command);
InputStream stdout= newStreamGobbler(sess.getStdout());
BufferedReader br= new BufferedReader(newInputStreamReader(stdout));
StringBuffer details= new StringBuffer("");while (true){
String line=br.readLine();if (line == null) {break;
}
details.append(line).append("
");
System.out.println(details);
}//封装结果
Map commandResult = new HashMap();
commandResult.put("hostName", hostName);
commandResult.put("command", command);
commandResult.put("exitCode", null==sess.getExitStatus()?"无":sess.getExitStatus().toString());
commandResult.put("details", (null==details)?"无返回结果":details.toString());
commandResults.add(commandResult);//关闭流
br.close();
sess.close();
}
conn.close();
result.put("rows", commandResults);
}catch(IOException e) {
e.printStackTrace(System.err);
}
write(response, result);
}/*** 查看数据库表空间状态
*@paramrequest
*@paramresponse
*@throwsIOException*/@RequestMapping("/getDBStatus.do")public void getDBStatus(HttpServletRequest request, HttpServletResponse response) throwsIOException {
List> dbResult = new ArrayList>();try{//切换数据源
SwitchDataSourceUtil.setCurrentDataSource(JNDI);
dbResult=monitorService.getDBStatus();
SwitchDataSourceUtil.clearDataSource();
}catch(Exception e) {
e.printStackTrace();
}finally{
SwitchDataSourceUtil.clearDataSource();
}
Map result = new HashMap();
result.put("rows", dbResult);
write(response, result);
}
}