001: /*
002: * <copyright>
003: *
004: * Copyright 1997-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026:
027: package org.cougaar.core.qos.metrics;
028:
029: import java.io.PrintWriter;
030: import java.text.DateFormat;
031: import java.util.ArrayList;
032: import java.util.Date;
033: import java.util.Iterator;
034: import java.util.List;
035: import java.util.Set;
036:
037: import javax.servlet.http.HttpServlet;
038: import javax.servlet.http.HttpServletRequest;
039: import javax.servlet.http.HttpServletResponse;
040:
041: import org.cougaar.core.agent.AgentContainer;
042: import org.cougaar.core.component.ServiceBroker;
043: import org.cougaar.core.mts.MessageAddress;
044: import org.cougaar.core.node.NodeControlService;
045: import org.cougaar.core.node.NodeIdentificationService;
046: import org.cougaar.core.qos.metrics.Constants;
047: import org.cougaar.core.service.ServletService;
048:
049: public class MetricsRSSFeedServlet extends HttpServlet implements
050: Constants {
051: private static final DateFormat DateFormatter = DateFormat
052: .getDateTimeInstance(DateFormat.FULL, DateFormat.FULL);
053:
054: private MetricsService metricsService;
055: private MessageAddress nodeID;
056: private AgentContainer agentContainer;
057:
058: public MetricsRSSFeedServlet(ServiceBroker sb) {
059: // Register our servlet with servlet service
060: ServletService servletService = (ServletService) sb.getService(
061: this , ServletService.class, null);
062: if (servletService == null) {
063: throw new RuntimeException(
064: "Unable to obtain ServletService");
065: }
066: try {
067: servletService.register(getPath(), this );
068: } catch (Exception e) {
069: throw new RuntimeException(
070: "Unable to register servlet at path <" + getPath()
071: + ">: " + e.getMessage());
072: }
073:
074: // get metrics service
075: try {
076: metricsService = (MetricsService) sb.getService(this ,
077: MetricsService.class, null);
078: } catch (Exception e) {
079: throw new RuntimeException(
080: "Unable to get MetricsService at path <"
081: + getPath() + ">: " + e.getMessage());
082: }
083:
084: NodeIdentificationService nis = (NodeIdentificationService) sb
085: .getService(this , NodeIdentificationService.class, null);
086: nodeID = nis.getMessageAddress();
087: sb.releaseService(this , NodeIdentificationService.class, nis);
088:
089: NodeControlService ncs = (NodeControlService) sb.getService(
090: this , NodeControlService.class, null);
091: if (ncs != null) {
092: agentContainer = ncs.getRootContainer();
093: sb.releaseService(this , NodeControlService.class, ncs);
094: }
095:
096: }
097:
098: public String getPath() {
099: return "/metrics/society/rss";
100: }
101:
102: public String getTitle() {
103: return "Really Simple Syndication feed";
104: }
105:
106: void printItem(String path, String description, PrintWriter out) {
107: out.print("<item>\n");
108: out.print("<title>");
109: out.print(path);
110: out.print("</title>\n");
111: out.print("<description>");
112: out.print(description);
113: out.print("</description>\n");
114: out.print("</item>\n");
115: }
116:
117: void printItem(String path, Metric metric, PrintWriter out) {
118: out.print("<item>\n");
119: out.print("<title>");
120: out.print(path);
121: out.print("</title>\n");
122:
123: if (metric == null) {
124: out.print("<description>");
125: out.print("null");
126: out.print("</description>\n");
127: } else {
128: Object data = metric.getRawValue();
129: out.print("<description>");
130: out.print(" Type=");
131: if (data instanceof Float || data instanceof Double)
132: out.print("Double");
133: else if (data instanceof Number)
134: out.print("Long");
135: else if (data instanceof Boolean)
136: out.print("Boolean");
137: else if (data instanceof String)
138: out.print("String");
139: else if (data instanceof Character)
140: out.print("Character");
141:
142: out.print(" Value=");
143: out.print(data.toString());
144:
145: out.print(" Credibility=");
146: out.print(metric.getCredibility());
147:
148: if (metric.getUnits() != null) {
149: out.print(" Units=");
150: out.print(metric.getUnits());
151: }
152:
153: out.print(" Halflife=");
154: out.print(metric.getHalflife());
155: out.print("</description>\n");
156:
157: if (metric.getProvenance() != null) {
158: out.print("<source>");
159: out.print(metric.getProvenance());
160: out.print("</source>\n");
161: }
162:
163: // pubDate
164: out.print("<pubDate>");
165: Date timestamp = new Date(metric.getTimestamp());
166: // out.print(timestamp.toGMTString());
167: out.print(DateFormatter.format(timestamp));
168: out.print("</pubDate>\n");
169:
170: }
171:
172: out.print("</item>\n");
173: }
174:
175: public void printChannel(String url, List paths, PrintWriter out) {
176: // build string of xml metrics
177: out.print("<?xml version=\"1.0\"?>\n");
178: out.print("<rss version=\"2.0\">\n<channel>\n");
179: out.print("<description>");
180: out.print("Agent load");
181: out.print("</description>");
182: out.print("<title>");
183: out.print("Load data for Node " + nodeID);
184: out.print("</title>");
185: out.print("<link>");
186: out.print(url);
187: out.print("</link>");
188: out.print("<ttl>10</ttl>\n");
189: if (paths != null) {
190: int count = paths.size();
191: for (int i = 0; i < count; i++) {
192: String path = (String) paths.get(i);
193: try {
194: Metric pathMetric = metricsService.getValue(path);
195: printItem(path, pathMetric, out);
196: } catch (Exception e) {
197: printItem(path, e.toString(), out);
198: }
199: }
200: }
201:
202: out.print("</channel>\n</rss>\n");
203: }
204:
205: void addPaths(String type, MessageAddress agentID, List paths) {
206: String path = type + "(" + agentID + ")" + PATH_SEPR;
207: paths.add(path + CPU_LOAD_AVG + "(" + _10_SEC_AVG + ")");
208: paths.add(path + CPU_LOAD_MJIPS + "(" + _10_SEC_AVG + ")");
209: paths.add(path + MSG_IN + "(" + _10_SEC_AVG + ")");
210: paths.add(path + MSG_OUT + "(" + _10_SEC_AVG + ")");
211: paths.add(path + BYTES_IN + "(" + _10_SEC_AVG + ")");
212: paths.add(path + BYTES_OUT + "(" + _10_SEC_AVG + ")");
213: paths.add(path + PERSIST_SIZE_LAST);
214: }
215:
216: public void printPage(HttpServletRequest request, PrintWriter out) {
217:
218: StringBuffer url_buf = request.getRequestURL();
219: String url = url_buf.toString();
220:
221: ArrayList paths = new ArrayList();
222:
223: Set agents = agentContainer.getAgentAddresses();
224: Iterator itr = agents.iterator();
225: while (itr.hasNext()) {
226: MessageAddress agentID = (MessageAddress) itr.next();
227: addPaths("Agent", agentID, paths);
228: }
229:
230: try {
231: // parses params
232: printChannel(url, paths, out);
233: } catch (Exception e) {
234: // also log here
235: System.out.println("Exception: " + e);
236: e.printStackTrace();
237: }
238: }
239:
240: // servlet requirement - pass to our print method to handle
241: public void doGet(HttpServletRequest request,
242: HttpServletResponse response) throws java.io.IOException {
243: PrintWriter out = response.getWriter();
244: printPage(request, out);
245: }
246: }
|