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.io.File;
009: import java.io.FileInputStream;
010: import java.util.Date;
011: import java.util.HashSet;
012: import java.util.Set;
013: import java.util.Map;
014: import java.io.StreamTokenizer;
015:
016: /**
017: * Parses hosts file - only will ask hosts that have an acme service
018: *
019: * Generates table like :
020: *
021: * Time, sv023-CPU_0,sv023-CPU_1,sv023-CPU_2
022: * 18:27:09, 0.0, 1.0, 0.0
023: * 18:27:14, -0, -0, -0
024: *
025: * (-0 indicates no top data at that time at that host)
026: * Numbers represent percentage CPU usage.
027: *
028: * Arguments : number of samples, seconds between samples, and host file to get hosts from
029: *
030: * Typical Usage :
031: * java -classpath albbn.jar org.cougaar.logistics.plugin.utils.TopRunnerFromHosts 2 5 $CIP/operator/s1-hosts.xml > results.csv
032: *
033: * (Here it asks for 2 samples, 5 seconds apart, reading from the s1-hosts.xml file.)
034: */
035: public class TopRunnerFromHosts extends TopRunner {
036: public TopRunnerFromHosts(int samples, long period, String machine,
037: Map timeToResult) {
038: super (samples, period, machine, timeToResult);
039: }
040:
041: protected Set getHosts(File hostsFile) {
042: Set hostSet = new HashSet();
043:
044: try {
045: Reader r = new BufferedReader(new InputStreamReader(
046: new FileInputStream(hostsFile)));
047: StreamTokenizer st = new StreamTokenizer(r);
048: String host = "";
049: boolean gotName = false;
050: boolean gotService = false;
051: boolean hostIsNext = true;
052: while (st.nextToken() != StreamTokenizer.TT_EOF) {
053: String token = st.sval;
054: if (token != null) {
055: //System.err.println ("token " + token);
056: if (token.startsWith("name")) {
057: gotName = true;
058: gotService = false;
059: hostIsNext = true;
060: // System.err.println ("name " + gotName + " service " + gotService);
061: } else if (gotName && hostIsNext) {
062: host = token;
063: hostIsNext = false;
064: // System.err.println ("host is " + host);
065: }
066: if (token.startsWith("service")) {
067: gotService = true;
068: // System.err.println ("name " + gotName + " service " + gotService);
069: }
070: if (gotService && token.equals("acme")) {
071: hostSet.add(host);
072: gotName = false;
073: gotService = false;
074: // System.err.println ("name " + gotName + " service " + gotService);
075: }
076: }
077: }
078: System.err.println("machines " + hostSet);
079: } catch (Exception e) {
080: System.err.println("exception " + e);
081: e.printStackTrace();
082: }
083:
084: return hostSet;
085: }
086:
087: public static void main(String[] args) {
088: if (args.length < 3) {
089: System.err
090: .println("Usage : TopRunnerFromHosts num_samples period (sec) hosts.xml");
091: return;
092: }
093:
094: int samples = 1;
095: long period = 10000;
096: try {
097: samples = Integer.parseInt(args[0]);
098: } catch (Exception e) {
099: }
100: try {
101: period = ((long) Integer.parseInt(args[1])) * 1000l;
102: } catch (Exception e) {
103: }
104:
105: File hosts = new File(args[2]);
106: if (!hosts.exists()) {
107: System.err.println("can't find file " + hosts);
108: } else {
109: TopRunnerFromHosts runner = new TopRunnerFromHosts(samples,
110: period, "", null);
111: // parse the xml file and give the result to startThreads
112: runner.startThreads(runner.getHosts(hosts));
113: }
114: }
115: }
|