001: package org.cougaar.logistics.plugin.utils;
002:
003: import java.text.SimpleDateFormat;
004: import java.io.BufferedInputStream;
005: import java.io.BufferedReader;
006: import java.io.InputStreamReader;
007: import java.io.Reader;
008: import java.util.Date;
009: import java.util.List;
010: import java.util.ArrayList;
011: import java.util.Collections;
012: import java.util.HashSet;
013: import java.util.HashMap;
014: import java.util.Map;
015: import java.util.Iterator;
016: import java.util.Set;
017: import java.io.StreamTokenizer;
018:
019: public class TopRunner implements Runnable {
020: int samples = 1;
021: String machine;
022: long period = 10000;
023: long last = -1;
024: SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
025: Map timeToResult;
026: boolean showIntermediate = false;
027:
028: public TopRunner(int samples, long period) {
029: this .samples = samples;
030: this .machine = machine;
031: }
032:
033: public TopRunner(int samples, long period, String machine,
034: Map timeToResult) {
035: this .samples = samples;
036: this .machine = machine;
037: this .period = period;
038: this .timeToResult = timeToResult;
039: }
040:
041: public void run() {
042: while (samples-- > 0) {
043: last = System.currentTimeMillis();
044:
045: BufferedInputStream stream = null;
046: try {
047: String command = "ssh " + machine + " top -n 1 -b";
048: // System.err.println ("doing command " + command);
049: Process proc = Runtime.getRuntime().exec(command);
050: stream = new BufferedInputStream(proc.getInputStream());
051: } catch (Exception e) {
052: System.err.println("exception " + e);
053: }
054:
055: byte[] bytes = new byte[1024];
056: int length = 0;
057:
058: String[] percentage = new String[10];
059: int cpus = 0;
060:
061: Reader r = null;
062: try {
063: r = new BufferedReader(new InputStreamReader(stream));
064: StreamTokenizer st = new StreamTokenizer(r);
065: while (st.nextToken() != StreamTokenizer.TT_EOF) {
066: // System.out.println ("token " + st.sval + " num token " +st.nval);
067: if (st.sval != null) {
068: if (st.sval.startsWith("states")) {
069: st.nextToken();
070: st.nextToken();
071: //System.out.println ("after states token " + st.sval + " num " +st.nval);
072: percentage[cpus++] = "" + st.nval;
073: } else if (st.sval.startsWith("Mem")) {
074: break;
075: }
076: }
077: }
078: } catch (Exception e) {
079: System.err.println("got read " + e);
080: } finally {
081: if (r != null)
082: try {
083: r.close();
084: } catch (Exception e) {
085: System.err
086: .println("Got exception closing ssh input stream reader "
087: + e);
088: }
089: }
090:
091: StringBuffer buf = new StringBuffer();
092: for (int i = 0; i < cpus; i++) {
093: buf.append(percentage[i]);
094: if (i < cpus - 1)
095: buf.append(",");
096: }
097:
098: synchronized (System.out) {
099: String topTime = format.format(new Date(last));
100: Map resultsAtTime = (Map) timeToResult.get(topTime);
101:
102: if (resultsAtTime == null) {
103: timeToResult.put(topTime,
104: resultsAtTime = new HashMap());
105: // System.err.println ("timeToResult now " + timeToResult);
106: }
107:
108: for (int i = 0; i < cpus; i++) {
109: String key = (machine + "-CPU_" + i);
110: String value = "" + percentage[i];
111: resultsAtTime.put(key, value);
112: //System.err.println ("timeToResult put " + key + "->" + value);
113: }
114:
115: if (showIntermediate)
116: System.out.println(format.format(new Date(last))
117: + "," + machine + "," + buf);
118: }
119:
120: synchronized (this ) {
121: long timeTaken = System.currentTimeMillis() - last;
122: if (timeTaken < period && samples > 0) {
123: try {
124: // System.err.println ("waiting " + (period-timeTaken));
125: wait(period - timeTaken);
126: } catch (Exception e) {
127: }
128: }
129: }
130: }
131: }
132:
133: protected void startThreads(Set machines) {
134: Thread[] threads = new Thread[machines.size()];
135:
136: int i = 0;
137: Map timeToResult = new HashMap();
138: for (Iterator iter = machines.iterator(); iter.hasNext();) {
139: String machine = (String) iter.next();
140: // System.out.println ("machine " + machine);
141: threads[i] = new Thread(new TopRunner(samples, period,
142: machine, timeToResult));
143: threads[i++].start();
144: }
145:
146: i = 0;
147: for (Iterator iter = machines.iterator(); iter.hasNext();) {
148: try {
149: iter.next();
150: threads[i++].join();
151: } catch (Exception e) {
152: System.err.println("on join, got " + e);
153: }
154: }
155:
156: List keys = new ArrayList(timeToResult.keySet());
157: Collections.sort(keys);
158:
159: System.out.print("Time,");
160: // System.err.println ("keys were "+ keys);
161: List sortedMachines = null;
162: for (Iterator timeIter = keys.iterator(); timeIter.hasNext();) {
163: Object topTime = timeIter.next();
164: sortedMachines = new ArrayList(((Map) timeToResult
165: .get(topTime)).keySet());
166: Collections.sort(sortedMachines);
167:
168: for (Iterator machineCPUPairIter = sortedMachines
169: .iterator(); machineCPUPairIter.hasNext();) {
170: Object machineCPUPair = machineCPUPairIter.next();
171: System.out.print(machineCPUPair
172: + (machineCPUPairIter.hasNext() ? "," : "\n"));
173: }
174: break;
175: }
176:
177: for (Iterator timeIter = keys.iterator(); timeIter.hasNext();) {
178: Object topTime = timeIter.next();
179: Map resultsAtTime = (Map) timeToResult.get(topTime);
180: System.out.print(topTime + ",");
181: for (Iterator machineCPUPairIter = sortedMachines
182: .iterator(); machineCPUPairIter.hasNext();) {
183: Object machineCPUPair = machineCPUPairIter.next();
184: Object value = resultsAtTime.get(machineCPUPair);
185: // System.err.println ("value for "+ machineCPUPair + " was " + value);
186: System.out.print((value == null ? "-0" : value)
187: + (machineCPUPairIter.hasNext() ? "," : "\n"));
188: }
189: }
190: }
191:
192: public static void main(String[] args) {
193: if (args.length < 3) {
194: System.err
195: .println("Usage : TopRunner num_samples period (sec) machine1 machine2 ...");
196: return;
197: }
198: int samples = 1;
199: long period = 10000;
200: try {
201: samples = Integer.parseInt(args[0]);
202: } catch (Exception e) {
203: }
204: try {
205: period = ((long) Integer.parseInt(args[1])) * 1000l;
206: } catch (Exception e) {
207: }
208:
209: TopRunner runner = new TopRunner(samples, period);
210: Set machines = new HashSet();
211: for (int i = 2; i < args.length; i++)
212: machines.add(args[i]);
213: runner.startThreads(machines);
214: }
215: }
|