HttpURLConnection 페이지 데이터 가 져 오기

12519 단어 데이터 오 르 기
public class ZabbixParseDataServiceImpl implements ZabbixParseDataService {

    private static final CookieManager MANAGER;
    private static final String ZABBIX_FILE="";
    private static final String LOGIN_URL="http://jinrongzabbix.cnsuning.com/index.php";
    private static final String AGENT_STR = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.146 Safari/537.36";

    private static final String DATA_URL="http://jinrongzabbix.cnsuning.com/latest.php?groupid=#{GROUPID}&hostid=0";

    private static final String GROUD_ID_STR = "groupId";

    private static final Logger log = LoggerFactory.getLogger(ZabbixParseDataServiceImpl.class);
    @Autowired
    private PaymentRedisUtilService paymentRedisUtilService;

    @Autowired
    private ZabbixParseInfoDao zabbixParseInfoDao;


    static {
        MANAGER = new CookieManager();
        MANAGER.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
        CookieHandler.setDefault(MANAGER);
    }

    /**
     *   zabbix   
     * @return
     */
    @Override
   public void parseZabbixData(){
        log.info("zabbix      。");
        String resultStr= null;
        try {
            Map resultMap=getZabbixData();
            log.info("zabbix   redis   {}:"+JSONObject.toJSONString(resultMap));
            resultStr = paymentRedisUtilService.hmset(CommonConstants.REDIS_ZABBIX_KEY,resultMap);
        } catch (Exception e) {
            log.error("zabbix      redis     。",e);
        }
        log.info("zabbix   redis    :"+resultStr);
        log.info("zabbix      。");
    }

    /**
     * zabbix         
     */
    @Override
    public void parserZabbixData4DisposablePay() {
        log.info("zabbix           ");
        List> resultList = null;
        try {
            resultList = getZabbixData4PayMonitor();
            if (null != resultList && !resultList.isEmpty()) {
                zabbixParseInfoDao.insertDisposablePayInfo(resultList);
            }
        } catch (Exception e) {
            log.error("zabbix             {}",e);
        }
        log.info("zabbix           ,    {}"+resultList);
    }

    /**
     *   zabbix         
     * @return
     */
    public List> getZabbixData4PayMonitor() {
        List> monitorInfoList = new ArrayList<>();
        //         url
        login("", "");
        String url = ScmProperty.get(ZABBIX_FILE, "");
        String ip = "";

        Map> ipInfoMap = new HashMap<>();
        String dateStr = DateUtil.dateTime2String(new Date());
        Elements elements = getElements(url,".tableinfo .odd_row");
        //           
        for (int i = 0; i < elements.size(); i++) {
            Elements tds = elements.get(i).select("td");
            String ongIp = tds.get(1).select(".menu-host").text().trim();
            if (!"".equals(ongIp)) {
                ip = ongIp;
            }

            Map ipInfo = ipInfoMap.get(ip);
            if (null == ipInfo) {
                ipInfo = new HashMap<>();
                ipInfo.put("ip", ip);
                ipInfo.put("date", dateStr);
                ipInfo.put("sysName", "EPPSPGS");
                ipInfoMap.put(ip, ipInfo);
            }

            //    
            boolean continueFlag = false;
            if ("".equals(ongIp)) {
                String feild = tds.get(2).text().trim().split(" ")[0];
                if ("CPU       ".equals(feild)) {
                    Float rate = Float.parseFloat(tds.get(4).text().replace("%", ""));
                    ipInfo.put("cpuFreeRate", rate);
                    continueFlag = true;
                }

                if ("       ".equals(feild)) {
                    Float rate = Float.parseFloat(tds.get(4).text().replace("%", ""));
                    ipInfo.put("memeryUseRate", rate);
                    continueFlag = true;
                }

                if (continueFlag) {
                    continue;
                }

                String[] sps = tds.get(2).text().trim().split(" ");
                if (sps.length == 1) {
                    feild = sps[0];
                } else {
                    feild = sps[0] + " " + sps[1];
                }

                if ("  vda IO   ".equals(feild)) {
                    Float rate = Float.parseFloat(tds.get(4).text().replace("%", ""));
                    ipInfo.put("ioUseRate", rate);
                }
            }
        }

        for (Map.Entry> entry : ipInfoMap.entrySet()) {
            monitorInfoList.add(entry.getValue());
        }

        return monitorInfoList;
    }

    /**
     *   zabbix   
     * @return
     */
    public  Map getZabbixData(){
        //    
        login("", "");
        //       
        String select = ".tableinfo .odd_row";
        //        
        Map resultMap=new HashMap<>();
        //      
        Map> systemsMap=new HashMap<>();
        JSONObject zabbixJson;
        try {
            //  scm     
            zabbixJson=JSONObject.parseObject(ScmProperty.get(ZABBIX_FILE,""));
        } catch (Exception e) {
            log.error("Scm  zabbix.properties     ZABBIX  ",e);
            //    new     
            zabbixJson=new JSONObject();
        }
        //  scm            
        for(Map.Entry zabbixParam: zabbixJson.entrySet()){
            JSONObject fieldJson=(JSONObject)zabbixParam.getValue();
            //     url
            String url=DATA_URL.replace("#{GROUPID}",fieldJson.getString(GROUD_ID_STR));
            //     
            String applicationName=zabbixParam.getKey().trim();
            if(null==applicationName || applicationName.split("_").length<2){
                continue;
            }
            //             ,    
            String[] applicationNameArr=applicationName.split("_");
            //               redis key
            String systemNameUpper=applicationNameArr[0].toUpperCase();
            //         
            String applicationNameUpper=applicationNameArr[1].toUpperCase();
            if(!systemsMap.containsKey(systemNameUpper)){
                systemsMap.put(systemNameUpper,new HashMap());
            }
            systemsMap.get(systemNameUpper).put(applicationNameUpper,selectDocment(getElements(url,select),fieldJson));
        }
        //       
        for(Map.Entry> systemMap:systemsMap.entrySet()){
            resultMap.put(systemMap.getKey(),JSONObject.toJSONString(systemMap.getValue()));
        }
        return resultMap;
    }

    /**
     *   zabbix
     * @param userKey
     * @param pwdKey
     */
    private void login(String userKey,String pwdKey){
        String loginUrl="";
        try {
            loginUrl=LOGIN_URL+"?request=&name="+ScmProperty.get(ZABBIX_FILE, userKey)+"&password="+ScmProperty.get(ZABBIX_FILE, pwdKey)+"&enter=Sign+in";
        } catch (Exception e) {
            log.error("Scm  zabbix.properties          。",e);
        }
        try {
            requestData(loginUrl,false,new Task() {
                @Override
                public String doAction(HttpURLConnection conn) {
                    return conn.getHeaderField("Location");
                }
            });
        } catch (Exception e) {
            log.error("zabbix      。",e);
        }
    }

    //         Elements
    private Elements getElements(String url, String select){
        try {
            String requestStr=requestData(url,true,new Task() {
                @Override
                public String doAction(HttpURLConnection conn) throws IOException {
                    StringBuilder builder = new StringBuilder();
                    BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(),"UTF-8"));
                    String line;
                    while ((line = reader.readLine()) != null){
                        builder.append(line);
                    }
                    reader.close();
                    return builder.toString();
                }
            });
            Document doc = Jsoup.parse(requestStr);
            return doc.select(select);
        } catch (IOException e) {
            log.error("zabbix      。",e);
        }
        return new Elements();
    }

    //      
    private  T requestData(String url,boolean idDoOutput,Task task) throws IOException{
            URL serverUrl = new URL(url);
            HttpURLConnection conn = (HttpURLConnection) serverUrl.openConnection();
            // http   ,      true
            if(idDoOutput){
                conn.setDoOutput(true);
            }
            conn.setRequestMethod("GET");
            conn.setUseCaches(false);
            conn.addRequestProperty("Accept-Charset", "UTF-8;");
            conn.addRequestProperty("User-Agent", AGENT_STR);
            T t = task.doAction(conn);
            conn.connect();
            return t;
    }

    private interface Task {
        /**
         * @param conn
         * @return
         */
        T doAction(HttpURLConnection conn) throws IOException ;
    }
    /**
     * @Description:           
     * @return List     
     * @author yangjing
     * @date 2017-2-25   4:33:29
     */
    private Map selectDocment(Elements elements,JSONObject fieldJson){
        //    
        Map applicationMap= new HashMap<>();
        try {
            Map ipMap=new HashMap<>();
            applicationMap.put(GROUD_ID_STR,fieldJson.getString(GROUD_ID_STR));
            applicationMap.put("dataMap",ipMap);
            String ip = null;
            for (int i = 0; i < elements.size(); i++) {
                Elements tds=elements.get(i).select("td");
                //   3   ip
                if(tds.size()==3){
                    ip = tds.get(1).text().trim();
                    setIpMap(ipMap,ip);
                }
                //   7        
                if(tds.size()==7){
                    String item = tds.get(2).text().trim();
                    String value = tds.get(4).text().trim();
                    setField(fieldJson,item,ipMap,ip,value);
                }
            }
        } catch (Exception e) {
            log.error("zabbix           。groupId:"+fieldJson.getString(GROUD_ID_STR),e);
        }
        return applicationMap;
    }

    public void setIpMap(Map ipMap, String ip){
        if(!ipMap.containsKey(ip)){
            ipMap.put(ip,new PaymentMonitorDto());
        }
    }

    public void setField(JSONObject fieldJson,String item,Map ipMap, String ip,String value){
        if(fieldJson.containsKey(item)){
            setIpMap(ipMap,ip);
            setField(ipMap.get(ip),fieldJson.getString(item),value);
        }
    }

    private void setField(PaymentMonitorDto param,String field,String value){
        switch (field){
            case "server_cpuRate":param.setServer_cpuRate(value);
                break;
            case "server_IO":param.setServer_IO(value);
                break;
            case "server_diskRate":param.setServer_diskRate(value);
                break;
            case "server_memoryRate":param.setServer_memoryRate(value);
                break;
            case "app_connections":param.setApp_connections(value);
                break;
            case "app_cpuRate":param.setApp_cpuRate(value);
                break;
            case "app_memoryRate": param.setApp_memoryRate(value);
                break;
            default: break;
        }
    }
}

좋은 웹페이지 즐겨찾기