在实际工作中主要用了三个方法。这个是我工作近二周总结出来了,欢迎参考欢迎讨论。不过还是希望如果对你有帮助,还望评论一下,让我知道~~~这也是我的动力
1:验证路径是否存在
boolean flag=true;
try
{
// register with STAF
STAFHandle handle = new STAFHandle("TestProcess");
System.out.println("STAF handle: " + handle.getHandle());
// test the dir
String reqTestTuxdir = "cd " +dir;
// System.out.println("the reqTestdir is : " + reqTestTuxdir);
String request = "START SHELL COMMAND " + STAFUtil.wrapData(reqTestTuxdir) ;
String service="PROCESS";
// unchange
service = "FS";
request = "LIST DIRECTORY "+dir+"";
String result = handle.submit(bejname, service, request);
STAFMarshallingContext mc = STAFMarshallingContext.unmarshall(
result);
List entryList = (List)mc.getRootObject();
if(entryList.size()>0){
System.out.println("machine "+bejname+" the dir : "+dir+" ----passed. " );
} else{
System.out.println("machine "+bejname+" the dir : "+dir+" ----failed. " );
flag=false;
}
handle.unRegister();
}
catch(STAFException e)
{
System.out.println("machine "+bejname+" the dir : "+dir+" ----failed. " );
flag=false;
}
return flag;
测试路径dir 是否存在,可以用fs服务查看dir下是否有内容。(或者到路径的上一层去测试,是否含有此dir)
2:验证一个版本是否正确
try{
STAFHandle handle = new STAFHandle("TestProcess");
// System.out.println("test_version STAF handle: " + handle.getHandle());
String service="PROCESS";
// test the version
String test_version=p.getCheckcommand();
String hostdir=p.getEnv();
// String request= "START SHELL COMMAND "+STAFUtil.wrapData(test_version)+" env PATH="+hostdir+"/bin:$PATH"+
// " env LD_LIBRARY_PATH="+hostdir+"lib:$LD_LIBRARY_PATH"+
// " wait returnstdout stderrtostdout";
String request= "START SHELL COMMAND "+test_version+" env PATH="+hostdir+"/bin:$PATH"+
" env LD_LIBRARY_PATH="+hostdir+"/lib:$LD_LIBRARY_PATH"+
" wait returnstdout stderrtostdout";
//System.out.println("request is : " +request);
String result = handle.submit(bejname, service, request);
STAFMarshallingContext mc = STAFMarshallingContext.unmarshall(result);
Map processCompletionMap = (Map)mc.getRootObject();
// String processRC = (String)processCompletionMap.get("rc");
/* if (!processRC.equals("0"))
{
System.out.println("ERROR: Process RC is " + processRC + " instead of 0.");
System.exit(1);
}*/
List returnedFileList = (List)processCompletionMap.get("fileList");
Map stdoutMap = (Map)returnedFileList.get(0);
String stdoutRC = (String)stdoutMap.get("rc");
if (!stdoutRC.equals("0"))
{
System.out.println("ERROR retrieving process Stdout data. RC=" + stdoutRC);
System.exit(1);
}
String stdoutData = (String)stdoutMap.get("data");
//System.out.println("\n the returned data is :\n");
//System.out.println(stdoutData);
if(stdoutData.indexOf(p.getPro_version())>=0){
System.out.println("the "+p.getPro_name()+" version of machine "+bejname+" is correct. " );
p.setEnv("passed");
}else{
System.out.println("the "+p.getPro_name()+" version of machine "+bejname+" is wrong. " );
p.setEnv("failed");
}
}
catch(STAFException e)
{
System.out.println("Error registering with STAF");
}
return p;
使用submit2方法,对返回结果进行解析抽取,得要需要的数据。此case返回版本值。
3:查看对方机器的CPU,MEMORY状态。执行命令后,将命令的输出重定向到另一文件中,再用另一个服务fs,把文件copy过来。这样就可以进行解析了。
try {
STAFHandle handle = new STAFHandle("TestProcess");
System.out.println("STAF handle: " + handle.getHandle());
String request =null;
//unchange
/* request="start shell command top -b -n 1 wait stdout /testarea/jingt1732/staf_tj/top.txt";
System.out.println("request: "+request);
STAFResult result = handle.submit2(bejname,"process",request);
if (result.Ok != result.rc) {
System.out.println("Error starting the process top, RC: } " + result.rc);
}*/
String topfile="/testarea/jingt/workspace/staf_exa/top.txt";
request="COPY FILE /testarea/jingt1732/staf_tj/top.txt TOFILE "+topfile;
STAFResult result = handle.submit2(bejname, "fs",request );
if (result.Ok != result.rc) {
System.out.println("Error coping file, RC: " + result.rc);
}
else{
System.out.println("copy ok ");
FileReader reader = new FileReader(topfile);
BufferedReader br = new BufferedReader(reader);
String str = null;
String[] strArray = null;
while((str = br.readLine()) != null) {
int m = 0;
if (str.indexOf("Cpu") != -1) {
strArray = str.split(",");
for (String tmp : strArray) {
if (tmp.trim().length() == 0)
continue;
if (++m == 4) {
tmp=tmp.substring(0,tmp.length()-2);
System.out.println("the cpu idel"+tmp);
machine.setCPU(tmp);
}
}
}
if (str.indexOf("Mem") != -1) {
System.out.println("the information of Menmory is : "+str);
machine.setMemory(str);
}
}
br.close();
}
} catch (Exception e) {
e.printStackTrace();
}