`
eagletony
  • 浏览: 40720 次
  • 来自: ...
社区版块
存档分类
最新评论

java 通过Runtime 读取shell

阅读更多
package realtimeMonitor;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class ResolveJSONForShell {
		public static String[] resolveJSONForSwapSpace() throws JSONException, IOException{
		    String shellvalue = executeShell("192.168.8.202","host_swap_space","");
			
			// String json = "{'returncode': 0, 'response':  [['6010872', '"+randomNum()+"', '"+randomNum()+"']], 'time': 1289024723}";
			  JSONObject jsonObj = new JSONObject(shellvalue);
			  JSONArray array= jsonObj.getJSONArray("response");
			  
			  System.out.println(array);
			  System.out.println("used:"+(array.getJSONArray(0).get(1)));
			  System.out.println("free:"+(array.getJSONArray(0).get(2)));
			  String[] swapSpaceString={(String) array.getJSONArray(0).get(1),(String)array.getJSONArray(0).get(2)};
			  return swapSpaceString;
		}
		/*public static String resolveJSONForCPUPerformance(){
			String jsonar ="[6]";
			return jsonar.substring(1,2);
		}*/
		public static String resolveJSONForCPUUtilization() throws IOException{
		//	String jsonar ="["+randomNum()+"]";
			String shellvalue = executeShell("192.168.8.202","host_cpu_performance","cpu_utilization");
			
			return shellvalue.substring(1,2);
		}
		public static Map resolveJSONForDiskSpace() throws JSONException, IOException{
			 String shellvalue = executeShell("192.168.8.202","host_disk_space","");
			List<String[]> diList=new ArrayList<String[]>();
			List<Double[]> dataList=new ArrayList<Double[]>();
			List<String> groupList=new ArrayList<String>();
			
			/*String jsonDs="{'returncode': 0, 'response':" +
					" [['/dev/sda6', '"+randomNum()+"', '"+randomNum()+"', '"+randomNum()+"', '6', '/'], " +
					"['none', '"+randomNum()+"', '"+randomNum()+"', '"+randomNum()+"', '1', '/dev']," +
					" ['none', '"+randomNum()+"', '"+randomNum()+"', '"+randomNum()+"', '1', '/dev/shm']," +
					" ['none', '"+randomNum()+"', '"+randomNum()+"', '"+randomNum()+"', '1', '/var/run']," +
					" ['none', '"+randomNum()+"', '"+randomNum()+"', '"+randomNum()+"', '0', '/var/lock']," +
					" ['none', '"+randomNum()+"', '"+randomNum()+"', '"+randomNum()+"', '0', '/lib/init/rw']], 'time:': 1289271097}";*/
			JSONObject jsonObj = new JSONObject(shellvalue);
			JSONArray array= jsonObj.getJSONArray("response");
			
			for(int i=0;i<array.length();i++){				
				String[] arrays={(String) array.getJSONArray(i).get(1),
						((String) array.getJSONArray(i).get(2)),
						((String) array.getJSONArray(i).get(3)),
						((String) array.getJSONArray(i).get(5))};
				diList.add(arrays);
			}
			
			for(String[] a: diList){
				
				Double[] data={Double.parseDouble(a[0]),Double.parseDouble(a[1]),Double.parseDouble(a[2])};
				dataList.add(data);
				groupList.add(a[3]);
			}
			
			double[][] dataArg=new double[3][dataList.size()];
			
			for(int i=0;i<dataList.size();i++){
				
				System.out.println((dataList.get(i))[0]);
				System.out.println((dataList.get(i))[1]);
				System.out.println((dataList.get(i))[2]);
				
				dataArg[0][i]=(dataList.get(i))[0];
				dataArg[1][i]=(dataList.get(i))[1];
				dataArg[2][i]=(dataList.get(i))[2];
			}
			List rowList=new ArrayList();
			rowList.add("all");
			rowList.add("used");
			rowList.add("free");
			
			Map map=new HashMap();
			map.put("dataArg",dataArg);
			map.put("groupList", groupList);
			map.put("rowList", rowList);
		
			return map;
		}
		
		public static String resolveJSONForMemoryPerformance(){
			return null;
		}
		
		public static String resolveJSONForMemoryUsed() throws IOException{
			String shellvalue = executeShell("192.168.8.202","host_memory_performance","memory_used");
		//	String jsonmu="["+randomNum()+"]";
			return shellvalue.substring(1, 2);
		}
		
		
		
		public static void  main(String arg[]) throws JSONException, IOException{
			resolveJSONForDiskSpace();
			
		}

		 public static String executeShell(String ip,String var1,String var2) throws IOException {
             StringBuffer stringBuffer = new StringBuffer();
             String line="";
              try {
            	  
            	 String filePath = Thread.currentThread().getContextClassLoader().getResource("").toURI().getPath();
            	 filePath =filePath.replace("WebRoot/WEB-INF/classes/", "");
                 System.out.println("filePath :"+filePath );      
            	 Process pid = null;
                    //      String[] cmd = {"/bin/bash", "-c", "/home/qm/shell_test/test.sh 192.168.8.202 get_oracle_cpu"};
                            String[] cmd = {"/bin/bash","-c","/home/qm/shell_test/test.sh "+ip+" "+var1+" "+var2}; //192.168.8.202 get_oracle_cpu"};
                                    pid = Runtime.getRuntime().exec(cmd);
                                    if (pid != null) {
                                    //      stringBuffer.append(pid.exitValue());
                            //              bufferedReader = new BufferedReader(new InputStreamReader(pid.getInputStream()), 1024;)
                                            InputStreamReader ir=new InputStreamReader(pid.getInputStream());
                                            LineNumberReader input = new LineNumberReader (ir);
                                           
                                            while ((line = input.readLine ()) != null)
                                            System.out.println(line);
                                    //      System.out.println(stringBuffer);
                                    } 
                    } catch (Exception ioe) {
                            stringBuffer.append(ioe.getMessage());
                    }
              return line;
     } 
   
		private static int randomNum()   
	    {      
	        System.out.println((Math.random()*10000+50000));         
	        return (int)(Math.random()*10000+50000);   
	    }
			 

}

分享到:
评论
发表评论

文章已被作者锁定,不允许评论。

相关推荐

Global site tag (gtag.js) - Google Analytics