java中JSON数据的解析
2013年09月03日
⁄ 综合
⁄ 共 16901字 ⁄ 字号
小 中 大
- package com.sinoufc.base.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 com.sinoufc.base.org.json.JSONArray;
- import com.sinoufc.base.org.json.JSONException;
- import com.sinoufc.base.org.json.JSONObject;
- import com.sinoufc.base.permission.baseinfo.dao.BaseInfoDao;
- import com.sinoufc.framework.cache.Global;
- import com.sinoufc.framework.exception.SystemException;
-
- public class ResolveJSONForShell {
- private BaseInfoDao baseInfoDao=(BaseInfoDao) Global.getSpringBean("baseInfoDao");
-
- public static List<String[]> resolveJSONForAllUsers(String ipaddress,String realtimePath) throws IOException{
- String shellvalue = executeShell(ipaddress,"host_all_users","",realtimePath);
- String ss="{'returncode': 0, 'response': [['qm', 'pts/0', '08:41', '38:09m', '2:16m', '0.58s', '-bash '],"
- +"['qm', 'pts/1', '10:07', '0.00s', '0.68s', '0.10s', 'java Shell 192. '],"
- +"['qm', 'pts/2', '11:02', '3:43', '0.20s', '0.20s', '-bash '], "
- +"['qm', 'pts/3', '13:33', '49:54m', '1:21m', '0.02s', 'sshd: qm [priv] ']], 'time:': 1289890011}";
- String nullStr="{'returncode': 0, 'response': [['', '', '', '', '', '', '']]}";
- if((null==shellvalue)||shellvalue.equals("1")||shellvalue.equals("")){
- shellvalue=nullStr;
- }
- List<String[]> userlist=new ArrayList<String[]>();
- try{
- JSONObject jsonObj = new JSONObject(shellvalue);
- JSONArray array= jsonObj.getJSONArray("response");
- for(int i=0;i<array.length();i++){
- String[] alluserStr={(String)array.getJSONArray(i).get(0),(String) array.getJSONArray(i).get(1),
- (String)array.getJSONArray(i).get(2),(String)array.getJSONArray(i).get(3),
- (String)array.getJSONArray(i).get(4),(String)array.getJSONArray(i).get(5),
- (String)array.getJSONArray(i).get(6)};
- userlist.add(alluserStr);
- }
- }catch(JSONException je){
- je.printStackTrace();
- throw new SystemException("连接不上主机或者没有数据!");
- }
- return userlist;
- }
-
- public static List<String[]> resolveJSONForDiskSpaceWithTab(String ipaddress,String realtimePath)throws JSONException, IOException{
- String shellvalue = executeShell(ipaddress,"host_disk_space","",realtimePath);
- String nullStr="{'returncode': 0, 'response': [['', '', '', '', '', '']], "
- + "'time:': 1289891593}";
- if((null==shellvalue)||shellvalue.equals("1")||shellvalue.equals("")){
- shellvalue=nullStr;
- }
- List<String[]> diskSpacelist=new ArrayList<String[]>();
- try{
- JSONObject jsonObj = new JSONObject(shellvalue);
- JSONArray array= jsonObj.getJSONArray("response");
-
- for(int i=0;i<array.length();i++){
- String[] alluserStr={ (String)array.getJSONArray(i).get(0),(String) array.getJSONArray(i).get(1),
- (String)array.getJSONArray(i).get(2),(String)array.getJSONArray(i).get(3),
- (String)array.getJSONArray(i).get(4),(String)array.getJSONArray(i).get(5)};
- diskSpacelist.add(alluserStr);
- }
-
- }catch(JSONException je){
- je.printStackTrace();
- throw new SystemException("连接不上主机或者没有数据!");
- }
- return diskSpacelist;
- }
-
- public static List<String[]> resolveJSONForProcessesSnapshot(String ipaddress,String realtimePath)throws JSONException, IOException{
- String shellvalue = executeShell(ipaddress,"host_processes_snapshot","",realtimePath);
-
- String nullStr="{'returncode': 0, 'response': [['', '', '', '', '', '', '', '', '', '', '']], 'time:': 1289891863}";
- if((null==shellvalue)||shellvalue.equals("1")||shellvalue.equals("")){
- shellvalue=nullStr;
- }
- List<String[]> processlist=new ArrayList<String[]>();
- try{
- JSONObject jsonObj = new JSONObject(shellvalue);
- JSONArray array= jsonObj.getJSONArray("response");
-
- for(int i=0;i<array.length();i++){
- String[] processStr={(String) array.getJSONArray(i).get(0),(String) array.getJSONArray(i).get(1),
- (String)array.getJSONArray(i).get(2),(String)array.getJSONArray(i).get(3),
- (String)array.getJSONArray(i).get(4),(String)array.getJSONArray(i).get(5),
- (String)array.getJSONArray(i).get(6), (String)array.getJSONArray(i).get(7),
- (String)array.getJSONArray(i).get(8), (String)array.getJSONArray(i).get(9),
- (String)array.getJSONArray(i).get(10)};
- processlist.add(processStr);
- }
- }catch(JSONException je){
- je.printStackTrace();
- throw new SystemException("连接不上主机或者没有数据!");
- }
- return processlist;
-
- }
-
- public static String[] resolveJSONForSwapSpace(String ipaddress,String realtimePath) throws JSONException, IOException{
- String shellvalue = executeShell(ipaddress,"host_swap_space","",realtimePath);
- System.out.println("----------shellvalue:"+shellvalue);
- String nullStr = "{'returncode': 0, 'response': [['', '', '']], 'time:': 1289381310}";
- if((null==shellvalue)||shellvalue.equals("1")||shellvalue.equals("")){
- shellvalue=nullStr;
- }
- String[] swapSpaceString = new String[3];
- try{
- 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)));
- swapSpaceString[0]=(String) array.getJSONArray(0).get(1);
- swapSpaceString[1]=(String)array.getJSONArray(0).get(2);
- swapSpaceString[2]=(String)array.getJSONArray(0).get(0);
- }catch(JSONException je){
- je.printStackTrace();
- throw new SystemException("连接不上主机或者没有数据!");
- }
- return swapSpaceString;
- }
- public static Map<String,String> resolveJSONForCPUPerformance(String ipaddress,String realtimePath) throws IOException, JSONException{
- Map<String,String> map =new HashMap<String,String>();
-
- String shellvalue = executeShell(ipaddress,"host_cpu_performance","",realtimePath);
- String nullStr="{'returncode': 0, 'response': [['', '', '', '']], 'time:': 1289878737}";
- if((null==shellvalue)||shellvalue.equals("1")||shellvalue.equals("")){
- shellvalue=nullStr;
- }
- try{
- JSONObject jsonObj = new JSONObject(shellvalue);
- JSONArray array= jsonObj.getJSONArray("response");
- map.put("usertime", (String) array.getJSONArray(0).get(0));
- map.put("systemtime", (String) array.getJSONArray(0).get(1));
- map.put("wio", (String) array.getJSONArray(0).get(2));
- map.put("freetime", (String) array.getJSONArray(0).get(3));
- }catch(JSONException je){
- je.printStackTrace();
- throw new SystemException("连接不上主机或者没有数据!");
- }
- return map;
- }
- public static String resolveJSONForCPUUtilization(String ipaddress,String realtimePath) throws IOException{
-
- String shellvalue = executeShell(ipaddress,"host_cpu_performance","cpu_utilization",realtimePath);
- if((null==shellvalue)||shellvalue.equals("1")||shellvalue.equals("")){
- shellvalue="[ ]";
- }
- System.out.println("resolveJSONForCPUUtilization:"+shellvalue);
- return shellvalue.substring(1,shellvalue.length()-1);
- }
- public static Map resolveJSONForDiskSpace(String ipaddress,String realtimePath) throws JSONException, IOException{
- String shellvalue = executeShell(ipaddress,"host_disk_space","",realtimePath);
- Map map=new HashMap();
- try{
- List<String[]> diList=new ArrayList<String[]>();
- List<Double[]> dataList=new ArrayList<Double[]>();
- List<String> groupList=new ArrayList<String>();
-
- 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])/1024,Double.parseDouble(a[1])/1024,Double.parseDouble(a[2])/1024};
- 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.put("dataArg",dataArg);
- map.put("groupList", groupList);
- map.put("rowList", rowList);
- }catch(Exception e){
- e.printStackTrace();
- throw new SystemException("连接不上主机或者没有数据!");
- }
- return map;
- }
-
-
-
- public static String[] resolveJSONForMemoryPerformance(String ipaddress,String realtimePath) throws IOException, JSONException{
- String shellvalue = executeShell(ipaddress,"host_memory_performance","",realtimePath);
- String nullStr = "{'returncode': 0, 'response': [['', '', '']], 'time:': 1289891288}";
- if((null==shellvalue)||shellvalue.equals("1")||shellvalue.equals("")){
- shellvalue=nullStr;
- }
- String[] memoryString=new String[3];
- try{
- JSONObject jsonObj = new JSONObject(shellvalue);
- JSONArray array= jsonObj.getJSONArray("response");
- memoryString[0]=(String)array.getJSONArray(0).get(0);
- memoryString[1]=(String) array.getJSONArray(0).get(1);
- memoryString[2]=(String)array.getJSONArray(0).get(2);
- }catch(JSONException je){
- je.printStackTrace();
- throw new SystemException("连接不上主机或者没有数据!");
- }
- return memoryString;
- }
-
- public static String[] resolveJSONForMemoryUsed(String ipaddress,String realtimePath) throws IOException, JSONException{
- String shellvalue = executeShell(ipaddress,"host_memory_performance","",realtimePath);
- String[] memoryUsedStr=new String[2];
- String nullStr = "{'returncode': 0, 'response': [['', '', '']], 'time:': 1289891288}";
- if((null==shellvalue)||shellvalue.equals("1")||shellvalue.equals("")){
- shellvalue=nullStr;
- }
- try{
- JSONObject jsonObj = new JSONObject(shellvalue);
- JSONArray array= jsonObj.getJSONArray("response");
- memoryUsedStr[0]=(String)array.getJSONArray(0).get(0);
- memoryUsedStr[1]=(String) array.getJSONArray(0).get(1);
- System.out.println("shellvalue"+shellvalue);
- }catch(JSONException je){
- je.printStackTrace();
- throw new SystemException("连接不上主机或者没有数据!");
- }
- return memoryUsedStr;
- }
-
-
-
-
-
-
- public static String executeShell(String ip,String var1,String var2,String realtimePath) throws IOException {
- StringBuffer stringBuffer = new StringBuffer();
- String line="";
- InputStreamReader ir=null;
- LineNumberReader input=null;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- try {
- Process pid = null;
-
-
-
-
- String filePath = Thread.currentThread().getContextClassLoader().getResource("").toURI().getPath();
- filePath =filePath.replace("WEB-INF/classes/", "");
- String[] cmd = {"/bin/bash","-c",filePath+"shell/test.sh "+ip+" "+var1+" "+var2};
-
- System.out.println("****************"+cmd[0]+cmd[1]+cmd[2]);
- pid = Runtime.getRuntime().exec(cmd);
- if (pid != null) {
- ir=new InputStreamReader(pid.getInputStream());
- input = new LineNumberReader (ir);
-
- line = input.readLine();
- System.out.println(line);
- }
- } catch (Exception ioe) {
- stringBuffer.append(ioe.getMessage());
- }finally{
- ir.close();
- input.close();
- }
- return line;
- }
-
- private static int randomNum()
- {
- System.out.println((Math.random()*10000+50000));
- return (int)(Math.random()*10000+50000);
- }
-
-
-
-
- }