STAF 读取命令执行结果的返回值

在实际工作中主要用了三个方法。这个是我工作近二周总结出来了,欢迎参考欢迎讨论。不过还是希望如果对你有帮助,还望评论一下,让我知道~~~这也是我的动力

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(); 
	    	} 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值