package org.jbits.javashell.os;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.jbits.javashell.ShellException;
import org.jbits.javashell.ShellPipeCollector;
import org.jbits.javashell.ShellProcess;
public class OS {
private static final String HOSTNAME = "hostname";
private static final String UNAME = "uname -a";
private static final String LSOF = "lsof";
private static final String WHOAMI = "whoami";
private ShellProcess p;
private ShellPipeCollector c;
private List<String> output = null;
private float totalTimeMilliseconds = Float.NaN;
private OS(String command, String ... args ) {
this.p = new ShellProcess(command);
c = new ShellPipeCollector();
p.addOutputListener(c);
}
private void run() {
try {
long start = System.nanoTime();
int ret = p.execute();
totalTimeMilliseconds = (float)((System.nanoTime()-start)/1000000.0);
String cc = c.toString();
output = new ArrayList<String>();
for (String s:cc.split("\\n")) {
this.output.add(s);
}
if (ret!=0) {
throw new ShellException(p);
}
} catch (IOException e) {
throw new RuntimeException(e);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
private List<String> getOutput() {
if (this.output!=null) {
return this.output;
} else {
run();
return this.output;
}
}
public static OS singleCommand(String command, String... args) {
return new OS(command,args);
}
public static OS hostname() {
return singleCommand(HOSTNAME).replaceAllOutput("\\n", "");
}
public static OS uname() {
return singleCommand(UNAME);
}
public static OS lsof() {
return singleCommand(LSOF);
}
public static OS whoami() {
return singleCommand(WHOAMI);
}
public HeadedRecordset recordset() {
return new HeadedRecordset(getOutput());
}
public List<String> getOutputList() {
return this.output;
}
public void toStdOut() {
for (String s:getOutput()) {
System.out.println(s);
}
}
public OS skipLine() {
List<String> buf = getOutput();
if (buf.size()>0) {
buf.remove(0);
}
return this;
}
public OS grepOutput(String str) {
List<String> out = new ArrayList<String>();
for (String s:getOutput()) {
if (s.contains(str)) {
out.add(s);
}
}
this.output=out;
return this;
}
public OS replaceAllOutput(String pattern, String replaceWith) {
List<String> out = new ArrayList<String>();
for (String s:getOutput()) {
out.add(s.replaceAll(pattern, replaceWith));
}
this.output=out;
return this;
}
public int lineCount() {
return getOutput().size();
}
public float totalTimeMilliseconds() {
if (Float.isNaN(totalTimeMilliseconds)) {
run();
}
return totalTimeMilliseconds;
}
public String toString() {
StringBuilder buf = new StringBuilder();
Iterator<String> it = getOutput().iterator();
while (it.hasNext()) {
String s = it.next();
buf.append(s);
if (it.hasNext()) {
buf.append('\n');
}
}
return buf.toString();
}
public String stringValue() {
StringBuilder buf = new StringBuilder();
for (String s:getOutput()) {
buf.append(s);
}
return buf.toString();
}
public static class CpuStat {
public float user;
public float nice;
public float system;
public float iowait;
public float steal;
public float idle;
}
private static final String SAR_CPU = "sar -u 1";
private static final String AVERAGE_LINE="Average:";
private static final String CPU_USER="%user";
private static final String CPU_NICE="%nice";
private static final String CPU_SYSTEM="%system";
private static final String CPU_IOWAIT="%iowait";
private static final String CPU_STEAL="%steal";
private static final String CPU_IDLE="%idle";
public static CpuStat cpuStat() {
HeadedRecordset recordset = new HeadedRecordset(
singleCommand(SAR_CPU).
skipLine().
skipLine().
replaceAllOutput("PM", " ").
replaceAllOutput("AM", " ").
getOutput()
);
recordset.skipToLine(AVERAGE_LINE);
if (recordset.hasLine(AVERAGE_LINE)) {
CpuStat ret = new OS.CpuStat();
ret.idle=Float.parseFloat(recordset.getString(CPU_IDLE))/100.0F;
ret.iowait=Float.parseFloat(recordset.getString(CPU_IOWAIT))/100.0F;
ret.nice=Float.parseFloat(recordset.getString(CPU_NICE))/100.0F;
ret.steal=Float.parseFloat(recordset.getString(CPU_STEAL))/100.0F;
ret.system=Float.parseFloat(recordset.getString(CPU_SYSTEM))/100.0F;
ret.user=Float.parseFloat(recordset.getString(CPU_USER))/100.0F;
return ret;
} else {
return null;
}
}
}