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.io.BufferedReader;
030: import java.io.InputStream;
031: import java.io.InputStreamReader;
032: import java.util.Map;
033: import java.util.regex.Matcher;
034: import java.util.regex.Pattern;
035:
036: import org.cougaar.qos.qrs.DataValue;
037: import org.cougaar.qos.qrs.Logging;
038: import org.cougaar.util.log.Logger;
039:
040: public class SunOSLoadAverage extends SysStatHandler {
041: private static final String command = "/usr/bin/uptime";
042: private static final Pattern pattern = Pattern
043: .compile(": \\d*\\.\\d\\d\\D");
044:
045: // private static final DataValue ClockSpeed =
046: // new DataValue(Hardware.getClockSpeed(), SECOND_MEAS_CREDIBILITY);
047:
048: private String key;
049:
050: public void initialize(String host, int pid) {
051: key = "Host" + KEY_SEPR + host + KEY_SEPR + "CPU" + KEY_SEPR
052: + "loadavg";
053: // clock_key = "Host" + KEY_SEPR + host +
054: // KEY_SEPR + "CPU" + KEY_SEPR + "clockspeed";
055: }
056:
057: public void getData(Map<String, DataValue> map) {
058: // Use the OperatingSystemMXBean if possible.
059: Double load = getLoadAvgFromOS();
060: if (load != null) {
061: map.put(key, new DataValue(load, SECOND_MEAS_CREDIBILITY,
062: "", PROVENANCE));
063: return;
064: }
065:
066: // If we get here, either getSystemLoadAverage is unavailable, or the call failed.
067: // Exec 'uptime' instead and parse the string.
068: String line = null;
069: try {
070: Process process = Runtime.getRuntime().exec(command);
071: InputStream stdOut = process.getInputStream();
072: InputStreamReader rdr = new InputStreamReader(stdOut);
073: BufferedReader bufferedStdOut = new BufferedReader(rdr);
074: line = bufferedStdOut.readLine();
075: bufferedStdOut.close();
076: process.destroy();
077: } catch (java.io.IOException ioex) {
078: Logger logger = Logging.getLogger(getClass());
079: logger.warn("Error running uptime: " + ioex.getMessage());
080: return;
081: }
082:
083: Matcher matcher = pattern.matcher(line);
084: if (!matcher.find()) {
085: Logger logger = Logging.getLogger(getClass());
086: logger.warn("Matcher failed on " + line);
087: return;
088: }
089:
090: String match = matcher.group();
091: // ": xx.xx,"
092: String la = match.substring(2, match.length() - 1);
093: double loadavg = Double.parseDouble(la);
094: map.put(key, new DataValue(loadavg, SECOND_MEAS_CREDIBILITY,
095: "", PROVENANCE));
096:
097: // put the "speed" too
098: // map.put(clock_key, ClockSpeed);
099: }
100:
101: }
|