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.FileReader;
030: import java.io.BufferedReader;
031: import java.io.IOException;
032: import java.util.Map;
033:
034: import org.cougaar.qos.qrs.DataValue;
035:
036: public class LinuxMemory extends SysStatHandler {
037: private static final String FreeLineKey = "MemFree";
038: private static final String TotalLineKey = "MemTotal";
039:
040: private String free_key, total_key, util_key;
041:
042: public void initialize(String host, int pid) {
043: free_key = "Host" + KEY_SEPR + host + KEY_SEPR + "Memory"
044: + KEY_SEPR + "Physical" + KEY_SEPR + "Free";
045: total_key = "Host" + KEY_SEPR + host + KEY_SEPR + "Memory"
046: + KEY_SEPR + "Physical" + KEY_SEPR + "Total";
047: util_key = "Host" + KEY_SEPR + host + KEY_SEPR + "Memory"
048: + KEY_SEPR + "Physical" + KEY_SEPR + "Utilization";
049:
050: }
051:
052: private double parse_one(String line) {
053: int start = 8;
054: while (!Character.isDigit(line.charAt(start))) {
055: ++start;
056: }
057: int end = start;
058: while (Character.isDigit(line.charAt(end))) {
059: ++end;
060: }
061: String doub = line.substring(start, end);
062: return Double.parseDouble(doub);
063: }
064:
065: public void getData(Map<String, DataValue> map) {
066: double free = -1;
067: double total = -1;
068: FileReader fr;
069: try {
070: fr = new FileReader("/proc/meminfo");
071: } catch (java.io.FileNotFoundException fnf_ex) {
072: return;
073: }
074:
075: BufferedReader br = new BufferedReader(fr);
076: while (true) {
077: try {
078: String line = br.readLine();
079: if (line == null) {
080: break;
081: }
082: if (line.startsWith(FreeLineKey)) {
083: free = parse_one(line);
084: DataValue free_dv = new DataValue(free,
085: SECOND_MEAS_CREDIBILITY, "KB", PROVENANCE);
086: map.put(free_key, free_dv);
087: } else if (line.startsWith(TotalLineKey)) {
088: total = parse_one(line);
089: DataValue total_dv = new DataValue(total,
090: SECOND_MEAS_CREDIBILITY, "KB", PROVENANCE);
091: map.put(total_key, total_dv);
092: }
093: } catch (java.io.IOException io_ex) {
094: break;
095: }
096: }
097: try {
098: fr.close();
099: } catch (IOException e) {
100: // don't care
101: }
102: if (free >= 0 && total > 0) {
103: double util = (total - free) / total;
104: DataValue util_dv = new DataValue(util,
105: SECOND_MEAS_CREDIBILITY, "", PROVENANCE);
106: map.put(util_key, util_dv);
107: }
108: }
109: }
|