01: /*
02: * <copyright>
03: *
04: * Copyright 2002-2007 BBNT Solutions, LLC
05: * under sponsorship of the Defense Advanced Research Projects
06: * Agency (DARPA).
07: *
08: * You can redistribute this software and/or modify it under the
09: * terms of the Cougaar Open Source License as published on the
10: * Cougaar Open Source Website (www.cougaar.org).
11: *
12: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
13: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
14: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
15: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
16: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
17: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
18: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
22: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23: *
24: * </copyright>
25: */
26:
27: package org.cougaar.qos.qrs.sysstat;
28:
29: import java.io.FileReader;
30: import java.io.BufferedReader;
31: import java.util.Map;
32:
33: import org.cougaar.qos.qrs.DataValue;
34:
35: public class LinuxLoadAverage extends SysStatHandler {
36: private FileReader fr;
37: private BufferedReader br;
38: private String key;
39:
40: public void initialize(String host, int pid) {
41: key = "Host" + KEY_SEPR + host + KEY_SEPR + "CPU" + KEY_SEPR
42: + "loadavg";
43: }
44:
45: private void close() {
46: try {
47: br.close();
48: } catch (java.io.IOException ioe_ex) {
49: }
50: }
51:
52: public void getData(Map<String, DataValue> map) {
53: // Use the OperatingSystemMXBean if possible.
54: Double load = getLoadAvgFromOS();
55: if (load != null) {
56: map.put(key, new DataValue(load, SECOND_MEAS_CREDIBILITY,
57: "", PROVENANCE));
58: return;
59: }
60:
61: // If we get here, either getSystemLoadAverage is unavailable, or the call failed.
62: // Use /proc file system instead
63: String line = null;
64: fr = null;
65: try {
66: fr = new FileReader("/proc/loadavg");
67: } catch (java.io.FileNotFoundException fnf_ex) {
68: return;
69: }
70:
71: br = new BufferedReader(fr);
72: try {
73: line = br.readLine();
74: } catch (java.io.IOException io_ex) {
75: return;
76: } finally {
77: close();
78: }
79:
80: int index = line.indexOf(' ');
81: String doub = null;
82: if (index == -1) {
83: doub = line;
84: } else {
85: doub = line.substring(0, index);
86: }
87: map.put(key, new DataValue(Double.parseDouble(doub),
88: SECOND_MEAS_CREDIBILITY, "", PROVENANCE));
89: }
90:
91: }
|