package com.freequery.migrate; import java.io.IOException; import java.io.InputStream; import java.lang.management.ManagementFactory; import java.lang.management.RuntimeMXBean; import java.sql.Connection; import java.text.DecimalFormat; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.TimeZone; import javax.management.Attribute; import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.freequery.migrate.utils.JmxUtils; import smartbi.connectionpool.ConnectionPool; import smartbi.freequery.repository.DataSource; import smartbi.freequery.repository.FreeQueryDAOFactory; import smartbi.net.sf.json.JSONObject; import smartbi.session.SessionManager; import smartbi.state.SessionMsg; import smartbi.util.StringUtil; @SuppressWarnings({ "unchecked", "serial" }) public class ServerStatus extends HttpServlet { org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(ServerStatus.class); protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Runtime r = Runtime.getRuntime(); Connection conn = null; String dsId = "DS.GBSSDEV"; String dbStatus = checkDataSource(dsId); //兼容以前的检测 Map dbStatusMap = new HashMap(); List lstDbs = FreeQueryDAOFactory.getDataSourceDAO().findAll(); for(int i=0;i= 24l * 3600 * 1000) { timeStr += uptime / (24l * 3600 * 1000) + " 天, "; uptime %= 24l * 3600 * 1000; } uptime -= TimeZone.getDefault().getOffset(0); timeStr += new SimpleDateFormat("HH 小时, mm 分钟, ss 秒").format(new Date( uptime)); DecimalFormat format = new DecimalFormat("###.###"); String maxConnectionsInUseCount = "0"; String inUseConnectionCount = "0"; String availableConnectionCount = "0"; String connectionCount = "0"; try { List attrs = null; // 获取连接池信息 // 先尝试获取JBOSS String jndiName = "SmartbiDS"; attrs = JmxUtils.getAttributeValue("jboss.jca:name=" + jndiName + ",service=ManagedConnectionPool", new String[] { "MaxConnectionsInUseCount", "InUseConnectionCount", "AvailableConnectionCount", "ConnectionCount" }); if (null != attrs && attrs.size() == 4) { Attribute a = (Attribute) attrs.get(0); if (a != null) { maxConnectionsInUseCount = a.getValue().toString(); } a = (Attribute) attrs.get(1); if (a != null) { inUseConnectionCount = a.getValue().toString(); } a = (Attribute) attrs.get(2); if (a != null) { availableConnectionCount = a.getValue().toString(); } a = (Attribute) attrs.get(3); if (a != null) { connectionCount = a.getValue().toString(); } } } catch (Exception e) { } String appVersion = ""; InputStream is = request.getSession().getServletContext() .getResourceAsStream("/vision/packageinfo.txt"); try { appVersion = new String(StringUtil.streamToByteArray(is)); } catch (Exception e) { } is.close(); JSONObject jObj = new JSONObject(); jObj.put("success", true); JSONObject retObj = new JSONObject(); retObj.put("online", getOnlineUserCount()); if(request.getAttribute("loginStatus")!=null && "true".equals(request.getAttribute("loginStatus").toString())){ retObj.put("loginStatus", "能正常登陆"); } else { retObj.put("loginStatus", "不能正常登陆"); } retObj.put("dbStatus", dbStatus); for(Iterator it = dbStatusMap.keySet().iterator();it.hasNext();){ String key = it.next(); retObj.put(key, dbStatusMap.get(key)); } retObj.put("upTime", timeStr); retObj.put("totalMemory", format.format(r.totalMemory() / 0x100000L)); retObj.put("freeMemory", format.format(r.freeMemory() / 0x100000L)); retObj.put("maxMemory", format.format(r.maxMemory() / 0x100000L)); retObj.put("maxConnectionsInUseCount", maxConnectionsInUseCount); retObj.put("inUseConnectionCount", inUseConnectionCount); retObj.put("availableConnectionCount", availableConnectionCount); retObj.put("connectionCount", connectionCount); retObj.put("activeThreadCount", Thread.getAllStackTraces().size()); retObj.put("appVersion", appVersion); jObj.put("returnObject", retObj); response.setContentType("application/json"); byte[] bs = jObj.toString().getBytes("UTF-8"); response.setContentLength(bs.length); //response.setCharacterEncoding("UTF-8"); //response.setHeader("content-type","text/html;charset=UTF-8"); ServletOutputStream oos = response.getOutputStream(); oos.write(bs); oos.flush(); } /** * 检测数据库是否正常 * @param dsId * @return */ private final String checkDataSource(String dsId){ DataSource dbDs = FreeQueryDAOFactory.getDataSourceDAO().load(dsId); Connection conn = null; String dbStatus = "数据库正常"; try { conn = ConnectionPool.getInstance().getConnection(dbDs); } catch (Exception e) { System.out.println("ServerStatus监测到异常:"+dsId+e.toString()); logger.debug("ServerStatus监测到异常:"+dsId+e.toString()); dbStatus = "数据库异常"; } finally { try { if (conn != null) conn.close(); if (!dbDs.getUrl().startsWith("JNDI:")) ConnectionPool.getInstance().closePool(dbDs); } catch (Throwable e) { System.out.println(""); } } return dbStatus; } private final int getOnlineUserCount() { int count = 0; List msgs = SessionManager.getInstance().getAllSessionMsg(); for(SessionMsg msg : msgs) { if(msg.getUserName() != null && !"".equals(msg.getUserName())) { count++; } } return count; } public static final String dateDifference(long difference, long resolution) { StringBuffer sb = new StringBuffer(); if (resolution > 0L) { resolution--; long months = difference / 0x9fa52400L; if (months > 0L) { difference %= 0x9fa52400L; sb.append(months + " 月, "); } } if (resolution <= 0L && sb.length() == 0) return "0 月"; resolution--; long days = difference / 0x5265c00L; if (days > 0L) { difference %= 0x5265c00L; sb.append(days + " 天, "); } if (resolution <= 0L && sb.length() == 0) return "0 天"; resolution--; long hours = difference / 0x36ee80L; if (hours > 0L) { difference %= 0x36ee80L; sb.append(hours + " 小时, "); } if (resolution <= 0L && sb.length() == 0) return "0 小时"; resolution--; long minutes = difference / 60000L; if (minutes > 0L) { difference %= 60000L; sb.append(minutes + " 分钟, "); } if (resolution <= 0L && sb.length() == 0) return "0 分钟"; resolution--; long seconds = difference / 1000L; if (seconds > 0L) { // difference %= 1000L; sb.append(seconds + " 秒, "); } if (resolution <= 0L && sb.length() == 0) return "0 秒"; if (sb.length() > 2) return sb.substring(0, sb.length() - 2); else return ""; } }