Search in sources :

Example 1 with TomcatInfo

use of com.shulie.instrument.simulator.module.model.tomcat.TomcatInfo in project LinkAgent by shulieTech.

the class TomcatModule method info.

@Command(value = "info", description = "查看 tomcat 信息")
public CommandResponse info(final Map<String, String> args) {
    try {
        int port = ParameterUtils.getInt(args, "port", 8006);
        // 如果请求tomcat信息失败,则不显示tomcat信息
        final String url = "http://localhost:" + port;
        if (!NetUtils.request(url).isSuccess()) {
            return CommandResponse.failure("tomcat unreachable with port:" + url);
        }
        TomcatInfo tomcatInfo = new TomcatInfo();
        String threadPoolPath = url + "/connector/threadpool";
        String connectorStatPath = url + "/connector/stats";
        NetUtils.Response connectorStatResponse = NetUtils.request(connectorStatPath);
        if (connectorStatResponse.isSuccess()) {
            List<TomcatInfo.ConnectorStats> connectorStats = new ArrayList<TomcatInfo.ConnectorStats>();
            List<JSONObject> tomcatConnectorStats = JSON.parseArray(connectorStatResponse.getContent(), JSONObject.class);
            for (JSONObject stat : tomcatConnectorStats) {
                String connectorName = stat.getString("name").replace("\"", "");
                long bytesReceived = stat.getLongValue("bytesReceived");
                long bytesSent = stat.getLongValue("bytesSent");
                long processingTime = stat.getLongValue("processingTime");
                long requestCount = stat.getLongValue("requestCount");
                long errorCount = stat.getLongValue("errorCount");
                tomcatRequestCounter.update(requestCount);
                tomcatErrorCounter.update(errorCount);
                tomcatReceivedBytesCounter.update(bytesReceived);
                tomcatSentBytesCounter.update(bytesSent);
                double qps = tomcatRequestCounter.rate();
                double rt = processingTime / (double) requestCount;
                double errorRate = tomcatErrorCounter.rate();
                long receivedBytesRate = new Double(tomcatReceivedBytesCounter.rate()).longValue();
                long sentBytesRate = new Double(tomcatSentBytesCounter.rate()).longValue();
                TomcatInfo.ConnectorStats connectorStat = new TomcatInfo.ConnectorStats();
                connectorStat.setName(connectorName);
                connectorStat.setQps(qps);
                connectorStat.setRt(rt);
                connectorStat.setError(errorRate);
                connectorStat.setReceived(receivedBytesRate);
                connectorStat.setSent(sentBytesRate);
                connectorStats.add(connectorStat);
            }
            tomcatInfo.setConnectorStats(connectorStats);
        }
        NetUtils.Response threadPoolResponse = NetUtils.request(threadPoolPath);
        if (threadPoolResponse.isSuccess()) {
            List<TomcatInfo.ThreadPool> threadPools = new ArrayList<TomcatInfo.ThreadPool>();
            List<JSONObject> threadPoolInfos = JSON.parseArray(threadPoolResponse.getContent(), JSONObject.class);
            for (JSONObject info : threadPoolInfos) {
                String name = info.getString("name").replace("\"", "");
                long busy = info.getLongValue("threadBusy");
                long total = info.getLongValue("threadCount");
                threadPools.add(new TomcatInfo.ThreadPool(name, busy, total));
            }
            tomcatInfo.setThreadPools(threadPools);
        }
        return CommandResponse.success(tomcatInfo);
    } catch (Throwable e) {
        return CommandResponse.failure(e);
    }
}
Also used : ArrayList(java.util.ArrayList) TomcatInfo(com.shulie.instrument.simulator.module.model.tomcat.TomcatInfo) NetUtils(com.shulie.instrument.simulator.module.util.NetUtils) JSONObject(com.alibaba.fastjson.JSONObject) Command(com.shulie.instrument.simulator.api.annotation.Command)

Aggregations

JSONObject (com.alibaba.fastjson.JSONObject)1 Command (com.shulie.instrument.simulator.api.annotation.Command)1 TomcatInfo (com.shulie.instrument.simulator.module.model.tomcat.TomcatInfo)1 NetUtils (com.shulie.instrument.simulator.module.util.NetUtils)1 ArrayList (java.util.ArrayList)1