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.util.Map;
032:
033: import org.cougaar.qos.qrs.DataValue;
034:
035: public class LinuxSockStat extends SysStatHandler {
036: private static final String TcpLineKey = "TCP: inuse ";
037: private static final String UdpLineKey = "UDP: inuse ";
038:
039: private FileReader fr;
040: private BufferedReader br;
041: private String tcp_key, udp_key;
042:
043: public void initialize(String host, int pid) {
044: tcp_key = "Host" + KEY_SEPR + host + KEY_SEPR + "Network"
045: + KEY_SEPR + "TCP" + KEY_SEPR + "sockets" + KEY_SEPR
046: + "inuse";
047: udp_key = "Host" + KEY_SEPR + host + KEY_SEPR + "Network"
048: + KEY_SEPR + "UDP" + KEY_SEPR + "sockets" + KEY_SEPR
049: + "inuse";
050: }
051:
052: private void close() {
053: try {
054: br.close();
055: } catch (java.io.IOException ioe_ex) {
056: }
057: }
058:
059: private DataValue parse(String line_key, String line) {
060: int start = line_key.length();
061: int end = line.indexOf(' ', start);
062: String doub = null;
063: if (end == -1) {
064: doub = line.substring(start);
065: } else {
066: doub = line.substring(start, end);
067: }
068: return new DataValue(Double.parseDouble(doub),
069: SECOND_MEAS_CREDIBILITY, "", PROVENANCE);
070: }
071:
072: public void getData(Map<String, DataValue> map) {
073: String line = null;
074: fr = null;
075: try {
076: fr = new FileReader("/proc/net/sockstat");
077: } catch (java.io.FileNotFoundException fnf_ex) {
078: return;
079: }
080:
081: br = new BufferedReader(fr);
082:
083: while (true) {
084: try {
085: line = br.readLine();
086: if (line == null) {
087: break;
088: }
089: if (line.startsWith(TcpLineKey)) {
090: map.put(tcp_key, parse(TcpLineKey, line));
091: } else if (line.startsWith(UdpLineKey)) {
092: map.put(udp_key, parse(UdpLineKey, line));
093: }
094: } catch (java.io.IOException io_ex) {
095: close();
096: return;
097: }
098: }
099:
100: close();
101: }
102:
103: }
|