001: /*
002: * Copyright (c) 2006, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.wso2.esb.services.tos;
018:
019: import org.apache.axis2.AxisFault;
020: import org.apache.axis2.description.AxisOperation;
021: import org.apache.axis2.description.AxisService;
022: import org.apache.axis2.description.AxisServiceGroup;
023: import org.apache.axis2.description.Parameter;
024: import org.apache.axis2.engine.AxisConfiguration;
025: import org.wso2.statistics.Counter;
026: import org.wso2.statistics.StatisticsConstants;
027: import org.wso2.statistics.module.StatisticsModule;
028:
029: import java.text.SimpleDateFormat;
030: import java.util.Date;
031: import java.util.Iterator;
032:
033: /*
034: * Holds information about the server status which is displayed on the home page
035: */
036: public class ServerStatus {
037:
038: private int responseCount;
039: private int faultCount;
040: private String serverName;
041: private String serverStartTime;
042: private String systemUpTime;
043: private int requestCount = 0;
044: private int services;
045: private String freeMemory;
046: private String totalMemory;
047: private double avgResponseTime;
048: private long minResponseTime;
049: private long maxResponseTime;
050:
051: private SimpleDateFormat dateFormatter = new SimpleDateFormat(
052: "yyyy-MM-dd HH:mm:ss");
053:
054: /**
055: * Generate server status data reading necessary information from the
056: * Axis2 configuration passed in
057: *
058: * @param axisConfig
059: * @throws AxisFault
060: */
061: public ServerStatus(AxisConfiguration axisConfig) throws AxisFault {
062:
063: Parameter systemStartTime = axisConfig
064: .getParameter(StatisticsConstants.SERVER_START_TIME);
065: long startTime = ((Long) systemStartTime.getValue())
066: .longValue();
067: Date stTime = new Date(startTime);
068: serverStartTime = dateFormatter.format(stTime);
069: systemUpTime = (getTime((System.currentTimeMillis() - startTime) / 1000));
070:
071: Parameter globalCounter = axisConfig
072: .getParameter(StatisticsConstants.GLOBAL_REQUEST_COUNTER);
073:
074: // need to handle responses as well, once the metrics are in order with Synapse.
075: if (globalCounter != null) {
076: Object value = globalCounter.getValue();
077: if (value instanceof Counter) {
078: requestCount = ((Counter) value).getCount();
079: }
080: }
081:
082: responseCount = ((org.wso2.statistics.Counter) axisConfig
083: .getParameter(
084: StatisticsConstants.GLOBAL_RESPONSE_COUNTER)
085: .getValue()).getCount();
086: faultCount = ((org.wso2.statistics.Counter) axisConfig
087: .getParameter(StatisticsConstants.GLOBAL_FAULT_COUNTER)
088: .getValue()).getCount();
089:
090: freeMemory = formatMemoryValue(Runtime.getRuntime()
091: .freeMemory());
092: totalMemory = formatMemoryValue(Runtime.getRuntime()
093: .totalMemory());
094:
095: AxisServiceGroup asg = axisConfig
096: .getServiceGroup(org.wso2.esb.ServiceBusConstants.ADMIN_SERVICE_GROUP);
097:
098: int adminservicecout = 0;
099: int activeServices = 0;
100: int totalRequestForAdmin = 0;
101: int totalResponseFromAdmin = 0;
102: int totalFaultCountForAdmin = 0;
103:
104: Iterator iter = asg.getServices();
105: while (iter.hasNext()) {
106:
107: AxisService axisService = (AxisService) iter.next();
108: Parameter parameter = axisService
109: .getParameter(StatisticsConstants.SERVICE_REQUEST_COUNTER);
110:
111: if (parameter != null) {
112: totalRequestForAdmin += ((Counter) parameter.getValue())
113: .getCount();
114: }
115:
116: Parameter faultCounterParameter = axisService
117: .getParameter(StatisticsConstants.SERVICE_FAULT_COUNTER);
118:
119: if (faultCounterParameter != null) {
120: totalFaultCountForAdmin += ((Counter) faultCounterParameter
121: .getValue()).getCount();
122: }
123:
124: totalResponseFromAdmin = totalResponseFromAdmin
125: + getServiceResponseCount(axisService);
126: adminservicecout++;
127: }
128:
129: Iterator services = axisConfig.getServices().values()
130: .iterator();
131:
132: while (services.hasNext()) {
133: AxisService axisService = (AxisService) services.next();
134:
135: if (axisService.isClientSide()) {
136: continue;
137: }
138:
139: if (axisService.isActive()) {
140: activeServices++;
141: }
142: }
143:
144: // counter includes Synapse Service as well
145: // Following code only Shows the awailable proxy services.
146: requestCount -= totalRequestForAdmin;
147: responseCount -= totalResponseFromAdmin;
148: faultCount -= totalFaultCountForAdmin;
149: this .services = activeServices - adminservicecout - 1;
150: serverName = org.wso2.esb.ServiceBusConstants.ESB_INSTANCE;
151:
152: avgResponseTime = StatisticsModule.responseTimeProcessor
153: .getAvgResponseTime();
154: maxResponseTime = StatisticsModule.responseTimeProcessor
155: .getMaxResponseTime();
156: minResponseTime = StatisticsModule.responseTimeProcessor
157: .getMinResponseTime();
158: }
159:
160: private String formatMemoryValue(long value) {
161: String formatterMemoryValue = "";
162:
163: if (value > 0) {
164: if (((value / (1024 * 1024 * 1024)) >= 1)) {
165: formatterMemoryValue = (value / (1024 * 1024 * 1024))
166: + " GB";
167: } else if (((value / (1024 * 1024)) >= 1)) {
168: formatterMemoryValue = (value / (1024 * 1024)) + " MB";
169: } else if (((value / (1024)) >= 1)) {
170: formatterMemoryValue = (value / (1024)) + " KB";
171: } else {
172: formatterMemoryValue = value + " bytes";
173: }
174: }
175:
176: return formatterMemoryValue;
177: }
178:
179: private String getTime(long seconds) {
180: StringBuffer sb = new StringBuffer();
181: long remainder = seconds;
182:
183: int days = (int) remainder / (60 * 60 * 24);
184: remainder = remainder - days * 60 * 60 * 24;
185: if (days > 0) {
186: sb.append(" ").append(days).append(
187: days == 1 ? " day" : " days");
188: }
189:
190: int hours = (int) remainder / (60 * 60);
191: remainder = remainder - hours * 60 * 60;
192: if (hours > 0) {
193: sb.append(" ").append(hours).append(
194: hours == 1 ? " hr" : " hrs");
195: }
196:
197: int minutes = (int) remainder / (60);
198: remainder = remainder - minutes * 60;
199: if (minutes > 0) {
200: sb.append(" ").append(minutes).append(
201: minutes == 1 ? " min" : " mins");
202: }
203:
204: if (remainder > 0) {
205: sb.append(" ").append(remainder).append(
206: remainder == 1 ? " sec" : " secs");
207: }
208:
209: if (sb.charAt(0) == ' ') {
210: sb.deleteCharAt(0);
211: }
212: return sb.toString();
213: }
214:
215: public int getServiceResponseCount(AxisService axisService)
216: throws AxisFault {
217:
218: int count = 0;
219: for (Iterator opIter = axisService.getOperations(); opIter
220: .hasNext();) {
221: AxisOperation axisOp = (AxisOperation) opIter.next();
222: Parameter parameter = axisOp
223: .getParameter(StatisticsConstants.OUT_OPERATION_COUNTER);
224: if (parameter != null) {
225: count += ((Counter) parameter.getValue()).getCount();
226: }
227: }
228: return count;
229: }
230:
231: public String getSystemUpTime() {
232: return systemUpTime;
233: }
234:
235: public String getServerStartTime() {
236: return serverStartTime;
237: }
238:
239: public String getServerName() {
240: return serverName;
241: }
242:
243: public int getRequestCount() {
244: return requestCount;
245: }
246:
247: public int getServices() {
248: return services;
249: }
250:
251: public String getFreeMemory() {
252: return freeMemory;
253: }
254:
255: public String getTotalMemory() {
256: return totalMemory;
257: }
258:
259: public double getAvgResponseTime() {
260: return avgResponseTime;
261: }
262:
263: public long getMinResponseTime() {
264: return minResponseTime;
265: }
266:
267: public long getMaxResponseTime() {
268: return maxResponseTime;
269: }
270:
271: public int getFaultCount() {
272: return faultCount;
273: }
274:
275: public void setFaultCount(int faultCount) {
276: this .faultCount = faultCount;
277: }
278:
279: public int getResponseCount() {
280: return responseCount;
281: }
282:
283: public void setResponseCount(int responseCount) {
284: this.responseCount = responseCount;
285: }
286: }
|