001: /*
002: * <copyright>
003: *
004: * Copyright 2002-2007 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.qos.qrs.sysstat;
028:
029: import java.lang.management.ManagementFactory;
030: import java.lang.management.OperatingSystemMXBean;
031: import java.lang.reflect.Method;
032: import java.util.Map;
033:
034: import org.cougaar.qos.qrs.Constants;
035: import org.cougaar.qos.qrs.DataValue;
036: import org.cougaar.qos.qrs.Logging;
037: import org.cougaar.util.log.Logger;
038:
039: abstract public class SysStatHandler implements Constants {
040: static final String PROVENANCE = "ProcessStats";
041:
042: // As of 1.5, the various MXBeans might help with system status.
043: // For now we only use the OS bean, and only for load-average.
044: private static final OperatingSystemMXBean OSMXBean = ManagementFactory
045: .getOperatingSystemMXBean();
046:
047: // getSystemLoadAverage() is new in 1.6, so use reflection for now
048: private static final Object[] NO_ARGS = {};
049: private static Method getLoadAvg;
050: static {
051: try {
052: Class<?>[] NO_ARG_PTYPES = {};
053: getLoadAvg = OSMXBean.getClass().getMethod(
054: "getSystemLoadAverage", NO_ARG_PTYPES);
055: } catch (Exception e) {
056: // ignore
057: }
058: }
059:
060: abstract protected void initialize(String host, int pid);
061:
062: abstract protected void getData(Map<String, DataValue> map);
063:
064: protected double parseValue(String string)
065: throws NumberFormatException {
066: return Double.parseDouble(string);
067: }
068:
069: public Double getLoadAvgFromOS() {
070: if (getLoadAvg != null) {
071: Logger logger = Logging.getLogger(getClass());
072: logger
073: .info("OperatingSystemMXBean#getSystemLoadAverage() is available");
074: try {
075: return (Double) getLoadAvg.invoke(OSMXBean, NO_ARGS);
076: } catch (Exception e) {
077: logger
078: .error("OperatingSystemMXBean#getSystemLoadAverage() failed: "
079: + e.getMessage());
080: }
081: }
082: return null;
083: }
084:
085: // TODO: Make more use of MXBeans!
086:
087: public static SysStatHandler getHandler(String kind, String host,
088: int pid) throws NoSysStatHandler {
089: SysStatHandler handler = null;
090: String name = System.getProperty("os.name");
091: if (kind.equals("Jips")) {
092: handler = new Jips();
093: } else if (kind.equals("CPUCount")) {
094: handler = new CPUCount();
095: } else if (name.equals("Windows NT")) {
096: // NT handlers
097: // None so far
098: } else if (name.equals("Windows XP")) {
099: if (kind.equals("LoadAverage")) {
100: handler = new XPLoadAverage();
101: } else if (kind.equals("Sockets")) {
102: handler = new XPSockStat();
103: } else if (kind.equals("Memory")) {
104: handler = new XPMemory();
105: }
106: } else if (name.equals("Mac OS X")) {
107: // MacOSX handlers
108: if (kind.equals("LoadAverage")) {
109: handler = new MacOSXLoadAverage();
110: } else if (kind.equals("ProcessStats")) {
111: handler = new ProcessStats();
112: }
113: } else if (name.equals("Linux")) {
114: // Linux handlers
115: if (kind.equals("CPU")) {
116: handler = new LinuxCPUInfo();
117: } else if (kind.equals("Memory")) {
118: handler = new LinuxMemory();
119: } else if (kind.equals("LoadAverage")) {
120: handler = new LinuxLoadAverage();
121: } else if (kind.equals("Sockets")) {
122: handler = new LinuxSockStat();
123: } else if (kind.equals("ProcessStats")) {
124: handler = new ProcessStats();
125: }
126: } else if (name.equals("SunOS")) {
127: // Solaris handlers
128: if (kind.equals("LoadAverage")) {
129: handler = new SunOSLoadAverage();
130: } else if (kind.equals("ProcessStats")) {
131: handler = new ProcessStats();
132: }
133: }
134:
135: if (handler == null) {
136: throw new NoSysStatHandler("No handler for " + kind
137: + " on platform " + name);
138: }
139:
140: handler.initialize(host, pid);
141: return handler;
142: }
143:
144: public static class NoSysStatHandler extends Exception {
145: NoSysStatHandler(String message) {
146: super(message);
147: }
148: }
149: }
|