--- projects/cms/source/host/java/DecodeCPU_TXT.java 2000/12/07 18:26:19 1.1 +++ projects/cms/source/host/java/DecodeCPU_TXT.java 2001/01/22 04:31:18 1.9 @@ -5,59 +5,68 @@ class DecodeCPU_TXT { XMLFormatter packet; - public DecodeCPU_TXT(String _file){ - - // open the file and read in all the relevant details - try { - BufferedReader in = new BufferedReader(new FileReader(_file)); - String line = new String(); - - // get rid of the first output (it doesn't contain cpu info) - for ( int i=0; i < 7; i++ ){ - line = in.readLine(); - System.out.println("Throwing away line: "+line); - } - // first line has the format: load averages: 0.30, 0.27, 0.29 16:58:10 - // tokenize it using spaces as delimiters? - StringTokenizer tok = new StringTokenizer(line,":",false); - tok.nextToken(); // load averages: - String load1 = tok.nextToken(" ,"); // "0.30" - String load2 = tok.nextToken(); // "0.27" - String load3 = tok.nextToken(); // "0.29" - String sTime = tok.nextToken(); // "16:58:10" + public DecodeCPU_TXT(){ + HashMap data; + try { + String[] cmd = {"statgrab.pl"}; + Process proc = Runtime.getRuntime().exec(cmd); + BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream())); + + data = new HashMap(); + + // Messy, but it'll keep going until no more data :/ + try { + String line = new String(); + while(true) { + line = in.readLine(); + int split = line.indexOf(' '); + data.put(line.substring(0, split), line.substring(split+1)); + } + } + catch (Exception e) {} + + String load1 = (String) data.get("packet.load.load1"); + String load5 = (String) data.get("packet.load.load5"); + String load15 = (String) data.get("packet.load.load15"); - // get the next line - line = in.readLine(); // 632 processes: 591 sleeping, 10 zombie, 30 stopped, 1 on cpu - tok = new StringTokenizer(line," ",false); - String totalProcesses = tok.nextToken(); // "632" - String sleeping = tok.nextToken(": ,"); // "591" - String zombie = tok.nextToken(); // "10" - String stopped = tok.nextToken(","); // "30" + String totalProcesses = (String) data.get("packet.processes.total"); + String sleeping = (String) data.get("packet.processes.sleeping"); + String zombie = (String) data.get("packet.processes.zombie"); + String stopped = (String) data.get("packet.processes.stopped"); + String onCPU = (String) data.get("packet.processes.cpu"); - // get the next line - line = in.readLine(); // CPU states: 71.1% idle, 1.8% user, 4.8% kernel, 22.2% iowait, 0.0% swap - tok = new StringTokenizer(line,":%,",false); - String idle = tok.nextToken(); // "71.1" - String user = tok.nextToken(",%"); // " 1.8" - String kernel = tok.nextToken(); // " 4.8" - String iowait = tok.nextToken(); // " 22.2" - String swap = tok.nextToken(); // " 0.0" + String idle = (String) data.get("packet.cpu.idle"); + String user = (String) data.get("packet.cpu.user"); + String kernel = (String) data.get("packet.cpu.kernel"); + String iowait = (String) data.get("packet.cpu.iowait"); + String swap = (String) data.get("packet.cpu.swap"); - line = in.readLine(); // Memory: 4096M real, 2380M free, 1237M swap in use, 9774M swap free - tok = new StringTokenizer(line,": ,",false); - tok.nextToken(); // Memory: - String real = tok.nextToken(); // "4096M" - String free = tok.nextToken(", "); // "2380M" - String swapInUse = tok.nextToken(); // "1237M" - String swapFree = tok.nextToken(", "); // "9774M" - - // done now make it into an xml packet ;) - - packet = new XMLFormatter("core"); + String real = (String) data.get("packet.memory.real"); + String free = (String) data.get("packet.memory.free"); + String swapInUse = (String) data.get("packet.memory.swap_in_use"); + String swapFree = (String) data.get("packet.memory.swap_free"); + + String osname = (String) data.get("packet.os.name"); + String osrelease = (String) data.get("packet.os.release"); + String osplatform = (String) data.get("packet.os.platform"); + String ossysname = (String) data.get("packet.os.sysname"); + String osversion = (String) data.get("packet.os.version"); + + String usercount = (String) data.get("packet.users.count"); + String userlist = (String) data.get("packet.users.list"); + + packet = new XMLFormatter(); + packet.addNest("os"); + packet.addElement("name",osname); + packet.addElement("release",osrelease); + packet.addElement("platform",osplatform); + packet.addElement("sysname",ossysname); + packet.addElement("version",osversion); + packet.closeNest(); packet.addNest("load"); - packet.addElement("1",load1); - packet.addElement("2",load2); - packet.addElement("3",load3); + packet.addElement("load1",load1); + packet.addElement("load5",load5); + packet.addElement("load15",load15); packet.closeNest(); packet.addNest("processes"); packet.addElement("total",totalProcesses); @@ -73,25 +82,32 @@ class DecodeCPU_TXT { packet.addElement("swap",swap); packet.closeNest(); packet.addNest("memory"); - packet.addElement("real",real); + packet.addElement("total",real); packet.addElement("free",free); - packet.addElement("swapinuse",swapInUse); - packet.addElement("swapfree",swapFree); packet.closeNest(); + packet.addNest("swap"); + packet.addElement("inuse",swapInUse); + packet.addElement("free",swapFree); + packet.closeNest(); + packet.addNest("users"); + packet.addElement("count",usercount); + //packet.addElement("list",userlist); + packet.closeNest(); - } - catch ( IOException e ){ - // probably couldn't find the file - } - - + in.close(); + + } + catch(Exception e) { + System.out.println("ERROR IN DECODE: "+e); + e.printStackTrace(); + } + } public String getItems(){ // return the xml return packet.returnXML(); - } -} // class \ No newline at end of file +} // class