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



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章