/*
   *
   * 从内存和数据库表里获取每个IP的端口信息,存入端口配置表里
   */
  public void fromLastToOraspaceconfig() throws Exception {
    List list = new ArrayList();
    List list1 = new ArrayList();
    List shareList = new ArrayList();
    Hashtable oraspacehash = new Hashtable();
    Session session = null;
    Vector configV = new Vector();
    IpTranslation tranfer = new IpTranslation();
    try {
      // 从ORASPACCE配置表里获取列表
      rs = conn.executeQuery("select * from system_oraspaceconf order by ipaddress");
      while (rs.next()) list1.add(loadFromRS(rs));
      if (list1 != null && list1.size() > 0) {
        for (int i = 0; i < list1.size(); i++) {
          Oraspaceconfig oraspaceconfig = (Oraspaceconfig) list1.get(i);
          // IP:表空间名称

          oraspacehash.put(
              oraspaceconfig.getIpaddress() + ":" + oraspaceconfig.getSpacename(), oraspaceconfig);
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    try {

      // 从内存中得到所有ORASPACCE采集信息
      Hashtable sharedata = ShareData.getOraspacedata();

      // 从数据库得到监视ORASPACCE列表
      DBDao dbdao = new DBDao();
      DBTypeDao typedao = new DBTypeDao();
      DBTypeVo typevo = typedao.findByDbtype("oracle");
      shareList = dbdao.getDbByTypeMonFlag(typevo.getId(), 1);
      if (shareList != null && shareList.size() > 0) {
        for (int i = 0; i < shareList.size(); i++) {
          //					OraclePartsDao oracleDao=null;
          try {
            DBVo dbmonitorlist = (DBVo) shareList.get(i);
            //						 oracleDao=new OraclePartsDao();
            //						List<OracleEntity> oracles=oracleDao.findOracleParts(dbmonitorlist.getId());
            //						for(OracleEntity ora:oracles){
            //
            //						}
            if (sharedata.get(dbmonitorlist.getIpAddress() + ":" + dbmonitorlist.getId()) != null) {
              Vector tableinfo_v =
                  (Vector)
                      sharedata.get(dbmonitorlist.getIpAddress() + ":" + dbmonitorlist.getId());
              if (tableinfo_v == null) continue;
              Hashtable spaces = new Hashtable();
              spaces.put("ip", dbmonitorlist.getIpAddress() + ":" + dbmonitorlist.getId());
              spaces.put("tableinfo_v", tableinfo_v);
              list.add(spaces);
            }
          } catch (Exception e) {
            e.printStackTrace();
          } finally {
            //						if(oracleDao!=null)
            //						  oracleDao.close();
          }
        }
      }
      // 判断采集到的ORASPACCE信息是否已经在ORASPACCE配置表里已经存在,若不存在则加入
      if (list != null && list.size() > 0) {
        for (int i = 0; i < list.size(); i++) {
          Hashtable spaces = (Hashtable) list.get(i);
          if (spaces != null && spaces.size() > 0) {
            String ip = (String) spaces.get("ip");
            Vector tableinfo_v = (Vector) spaces.get("tableinfo_v");
            if (tableinfo_v != null && tableinfo_v.size() > 0) {
              Oraspaceconfig oraspaceconfig = null;
              for (int k = 0; k < tableinfo_v.size(); k++) {
                Hashtable return_value = (Hashtable) tableinfo_v.get(k);
                String spacename = (String) return_value.get("tablespace");
                String[] iparr = ip.split(":");
                String tip = tranfer.formIpToHex(iparr[0]);
                if (!oraspacehash.containsKey(tip + ":" + iparr[1] + ":" + spacename)) {
                  oraspaceconfig = new Oraspaceconfig();
                  oraspaceconfig.setSpacename(spacename);
                  oraspaceconfig.setBak("");
                  oraspaceconfig.setIpaddress(tip + ":" + iparr[1]);
                  oraspaceconfig.setLinkuse("");
                  oraspaceconfig.setAlarmvalue(90);
                  oraspaceconfig.setSms(new Integer(0)); // 0:不告警 1:告警,默认的情况是不发送短信
                  oraspaceconfig.setReportflag(new Integer(0)); // 0:不存在于报表 1:存在于报表,默认的情况是不存在于报表
                  conn = new DBManager();
                  save(oraspaceconfig);
                  // configV.add(oraspaceconfig);
                  oraspacehash.put(tip + ":" + iparr[1] + ":" + spacename, oraspaceconfig);
                }
              }
            }
          }
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    // TODO Auto-generated method stub
  }
  public void collect_data(String id, Hashtable gatherHash) {

    Hashtable data_ht = new Hashtable();
    String jvm_utilization = "0";
    try {
      ServerStream serverstream = new ServerStream();
      Hashtable returnVal = new Hashtable();
      String ipaddress = "";
      Tomcat node = new Tomcat();
      TomcatDao dao = new TomcatDao();
      try {
        node = (Tomcat) dao.findByID(id);
      } catch (Exception e) {
      } finally {
        dao.close();
      }
      try {
        com.afunms.polling.node.Tomcat tc = new com.afunms.polling.node.Tomcat();
        BeanUtils.copyProperties(tc, node);
        ipaddress = tc.getIpAddress();
        com.afunms.polling.node.Tomcat tnode =
            (com.afunms.polling.node.Tomcat) PollingEngine.getInstance().getTomcatByIP(ipaddress);
        Calendar date = Calendar.getInstance();
        Date cc = date.getTime();
        String tempsenddate = sdf.format(cc);
        // 初始化Tomcat服务的状态
        tnode.setLastTime(tempsenddate);
        tnode.setAlarm(false);
        tnode.getAlarmMessage().clear();
        tnode.setStatus(0);

        StringBuffer tmp = new StringBuffer();
        tmp.append(tc.getIpAddress());
        tmp.append(",");
        tmp.append(tc.getPort());
        tmp.append(",");
        tmp.append(tc.getUser());
        tmp.append(" , ");
        tmp.append(tc.getPassword());
        returnVal.put(String.valueOf(0), tmp.toString());
      } catch (Exception e) {
        e.printStackTrace();
      }

      String liststr = serverstream.validServer(returnVal);
      if ("".equals(liststr)) {

        try {
          com.afunms.polling.node.Tomcat tc = new com.afunms.polling.node.Tomcat();
          BeanUtils.copyProperties(tc, node);
          // if (data_ht==null){
          // 需要增加邮件服务所在的服务器是否能连通
          Host host = (Host) PollingEngine.getInstance().getNodeByIP(node.getIpAddress());
          Vector ipPingData = (Vector) ShareData.getPingdata().get(node.getIpAddress());
          if (ipPingData != null) {
            Pingcollectdata pingdata = (Pingcollectdata) ipPingData.get(0);
            Calendar tempCal = (Calendar) pingdata.getCollecttime();
            Date cc = tempCal.getTime();
            String _time = sdf.format(cc);
            String lastTime = _time;
            String pingvalue = pingdata.getThevalue();
            if (pingvalue == null || pingvalue.trim().length() == 0) pingvalue = "0";
            double pvalue = new Double(pingvalue);
            if (pvalue == 0) {
              // 主机服务器连接不上***********************************************
              com.afunms.polling.node.Tomcat tnode =
                  (com.afunms.polling.node.Tomcat)
                      PollingEngine.getInstance().getTomcatByIP(ipaddress);
              tnode.setAlarm(true);
              tnode.setStatus(1);
              List alarmList = tnode.getAlarmMessage();
              if (alarmList == null) alarmList = new ArrayList();
              tnode.getAlarmMessage().add("TOMCAT服务停止");
              String sysLocation = "";
              try {
                SmscontentDao eventdao = new SmscontentDao();
                String eventdesc =
                    "TOMCAT服务("
                        + tnode.getAlias()
                        + " IP:"
                        + tnode.getAdminIp()
                        + ")"
                        + "的TOMCAT服务停止";
                eventdao.createEventWithReasion(
                    "poll",
                    tnode.getId() + "",
                    tnode.getAdminIp() + "(" + tnode.getAdminIp() + ")",
                    eventdesc,
                    3,
                    "tomcat",
                    "ping",
                    "所在的服务器连接不上");
              } catch (Exception e) {
                e.printStackTrace();
              }
            } else {
              com.afunms.polling.node.Tomcat tnode =
                  (com.afunms.polling.node.Tomcat)
                      PollingEngine.getInstance().getTomcatByIP(ipaddress);
              tnode.setAlarm(true);
              tnode.setStatus(3);
              List alarmList = tnode.getAlarmMessage();
              if (alarmList == null) alarmList = new ArrayList();
              tnode.getAlarmMessage().add("TOMCAT服务停止");
              Pingcollectdata hostdata = null;
              hostdata = new Pingcollectdata();
              hostdata.setIpaddress(ipaddress);
              Calendar date = Calendar.getInstance();
              hostdata.setCollecttime(date);
              hostdata.setCategory("TomcatPing");
              hostdata.setEntity("Utilization");
              hostdata.setSubentity("ConnectUtilization");
              hostdata.setRestype("dynamic");
              hostdata.setUnit("%");
              hostdata.setThevalue("0");
              TomcatDao tomcatdao = new TomcatDao();
              try {
                tomcatdao.createHostData(hostdata);
              } catch (Exception e) {
                e.printStackTrace();
              } finally {
                tomcatdao.close();
              }
            }

          } else {
            com.afunms.polling.node.Tomcat tnode =
                (com.afunms.polling.node.Tomcat)
                    PollingEngine.getInstance().getTomcatByIP(ipaddress);
            tnode.setAlarm(true);
            tnode.setStatus(3);
            List alarmList = tnode.getAlarmMessage();
            if (alarmList == null) alarmList = new ArrayList();
            tnode.getAlarmMessage().add("TOMCAT服务停止");
            Pingcollectdata hostdata = null;
            hostdata = new Pingcollectdata();
            hostdata.setIpaddress(ipaddress);
            Calendar date = Calendar.getInstance();
            hostdata.setCollecttime(date);
            hostdata.setCategory("TomcatPing");
            hostdata.setEntity("Utilization");
            hostdata.setSubentity("ConnectUtilization");
            hostdata.setRestype("dynamic");
            hostdata.setUnit("%");
            hostdata.setThevalue("0");
            TomcatDao tomcatdao = new TomcatDao();
            try {
              tomcatdao.createHostData(hostdata);
            } catch (Exception e) {
              e.printStackTrace();
            } finally {
              tomcatdao.close();
            }
          }
          // }
          createSMS("tomcat", tc);
        } catch (Exception e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
      } else {
        Pingcollectdata hostdata = null;
        hostdata = new Pingcollectdata();
        hostdata.setIpaddress(ipaddress);
        Calendar date = Calendar.getInstance();
        hostdata.setCollecttime(date);
        hostdata.setCategory("TomcatPing");
        hostdata.setEntity("Utilization");
        hostdata.setSubentity("ConnectUtilization");
        hostdata.setRestype("dynamic");
        hostdata.setUnit("%");
        hostdata.setThevalue("100");
        TomcatDao tomcatdao = new TomcatDao();
        try {
          tomcatdao.createHostData(hostdata);
        } catch (Exception e) {
          e.printStackTrace();
        } finally {
          tomcatdao.close();
        }
      }
      String[] pos_s = liststr.split(",");
      for (int list_i = 0; list_i < pos_s.length - 1; list_i++) {
        String tmps = returnVal.get(pos_s[list_i]).toString();
        String[] serverinfo = tmps.split(",");
        serverstream.foundData(serverinfo[0], serverinfo[1], serverinfo[2], serverinfo[3]);
        data_ht = serverstream.data_ht;
        TomcatDao tomcatdao = new TomcatDao();
        Hashtable sendeddata = ShareData.getSendeddata();

        try {
          Calendar time = Calendar.getInstance();
          String lasttime = data_ht.get("mon_time").toString();
          String nexttime = data_ht.get("nexttime").toString();

          String server = data_ht.get("server").toString();
          String jvm = data_ht.get("jvm").toString();

          String ip = data_ht.get("ip").toString();
          String port = data_ht.get("port").toString();
          String portsum1 = data_ht.get("portsum1").toString();
          String portsum2 = data_ht.get("portsum2").toString();
          String portdetail1 = data_ht.get("portdetail1").toString();
          String portdetail2 = data_ht.get("portdetail2").toString();
          if (data_ht != null) {
            int jvm_memoryuiltillize = 0;

            String[] temjvm = jvm.split(",");
            double freememory = Double.parseDouble(temjvm[0].trim());
            double totalmemory = (double) Double.parseDouble(temjvm[1].trim());
            double maxmemory = (double) Double.parseDouble(temjvm[2].trim());

            jvm_memoryuiltillize = (int) Math.rint((totalmemory - freememory) * 100 / totalmemory);
            jvm_utilization = String.valueOf(jvm_memoryuiltillize);

            Pingcollectdata hostdata = null;
            hostdata = new Pingcollectdata();
            hostdata.setIpaddress(ip);
            Calendar date = Calendar.getInstance();
            hostdata.setCollecttime(date);
            hostdata.setCategory("tomcat_jvm");
            hostdata.setEntity("Utilization");
            hostdata.setSubentity("jvm_utilization");
            hostdata.setRestype("dynamic");
            hostdata.setUnit("%");
            hostdata.setThevalue(jvm_utilization);
            try {
              tomcatdao.createHostData(hostdata);

              if (sendeddata.containsKey("tomcat" + ":" + ip))
                sendeddata.remove("tomcat" + ":" + ip);
            } catch (Exception e) {
              e.printStackTrace();
            }
          }

        } catch (Exception e) {
          e.printStackTrace();
        } finally {
          tomcatdao.close();
        }
      }
      try {
        data_ht.put("jvm_utilization", jvm_utilization);
        ShareData.setTomcatdata(node.getIpAddress(), data_ht);
      } catch (Exception ex) {
        ex.printStackTrace();
      }
      data_ht = null;
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
    }
  }
  /** @author nielin modify at 2010-01-08 */
  public int writeXml(int flag) {
    int done = 0;
    // 需要做分布式判断
    String runmodel = PollingEngine.getCollectwebflag();
    try {
      I_HostLastCollectData hostlastmanager = new HostLastCollectDataManager();
      Hashtable ipAllData = (Hashtable) ShareData.getSharedata().get(ipaddress);

      Vector vector = new Vector();
      String[] netInterfaceItem = {
        "index",
        "ifDescr",
        "ifSpeed",
        "ifAdminStatus",
        "ifOperStatus",
        "OutBandwidthUtilHdx",
        "InBandwidthUtilHdx"
      };
      try {
        if ("0".equals(runmodel)) {
          // 采集与访问是集成模式
          vector = hostlastmanager.getInterface_share(ipaddress, netInterfaceItem, "index", "", "");
        } else {
          // 采集与访问是分离模式
          vector = hostlastmanager.getInterface(ipaddress, netInterfaceItem, "index", "", "");
        }
      } catch (Exception e) {
        e.printStackTrace();
        return done;
      }
      try {
        _fis =
            new FileInputStream(
                ResourceCenter.getInstance().getSysPath()
                    + "panel/model/"
                    + oid
                    + "_"
                    + imageType
                    + ".jsp");
      } catch (Exception e) {
        e.printStackTrace();
        _fis.close();
        return done;
      }
      _fis.skip(headBytes.getBytes().length);
      _builder = new SAXBuilder();
      _doc = _builder.build(_fis);
      _root = _doc.getRootElement();
      _nodes = _root.getChild("nodes");

      List list = _nodes.getChildren();

      PortconfigDao dao = new PortconfigDao();

      try {

        for (int i = 0; i < list.size(); i++) {
          Element eleNode = (Element) list.get(i);
          if (eleNode.getChildText("index") == null) break;
          int index = Integer.valueOf(eleNode.getChildText("index")).intValue();
          String x = eleNode.getChildText("x");
          String y = eleNode.getChildText("y");
          String img = eleNode.getChildText("img");
          String direction = eleNode.getChildText("direction");
          String ifname = "";
          String OutBandwidthUtilHdx = "0";
          String InBandwidthUtilHdx = "0";
          String portuse = "";
          if (vector != null && vector.size() > 0) {
            for (int m = 0; m < vector.size(); m++) {
              String[] strs = (String[]) vector.get(m);
              String _ifname = strs[1];
              String _index = strs[0];
              String opstatus = strs[4];
              OutBandwidthUtilHdx = strs[5];
              InBandwidthUtilHdx = strs[6];

              Portconfig portconfig = null;
              try {
                portconfig = dao.getPanelByipandindex(ipaddress, index + "");
              } catch (Exception e) {

              }
              if (portconfig != null
                  && portconfig.getLinkuse() != null
                  && portconfig.getLinkuse().trim().length() > 0) {
                portuse = portconfig.getLinkuse();
              }

              // SysLogger.info(ipaddress+"====ifOperStatus====="+strs[4]);
              if (Integer.parseInt(_index) == index) {
                ifname = _ifname;
                if ("down".equalsIgnoreCase(opstatus)) {
                  // DOWN
                  if ("1".equalsIgnoreCase(direction)) {
                    // 向上的端口
                    img = "image/up_down_gray.gif";
                  } else {
                    // 向下的端口
                    img = "image/down_down_gray.gif";
                  }
                } else {
                  // UP
                  if ("1".equalsIgnoreCase(direction)) {
                    // 向上的端口
                    img = "image/up_up_green.gif";
                  } else {
                    // 向下的端口
                    img = "image/down_up_green.gif";
                  }
                }
                break;
              }
            }
          }
          addNode(
              index + "",
              InBandwidthUtilHdx,
              OutBandwidthUtilHdx,
              img,
              ipaddress,
              ifname,
              portuse,
              x,
              y);
        }

      } catch (Exception e) {
        e.printStackTrace();
      } finally {
        dao.close();
      }

      Format format = Format.getCompactFormat();
      format.setEncoding("GB2312");
      format.setIndent("	");
      serializer = new XMLOutputter(format);
      // SysLogger.info("path==="+fullPath);
      fos = new FileOutputStream(fullPath);
      fos.write(headBytes.getBytes());
      serializer.output(doc, fos);
      fos.close();
      done = 1;
    } catch (Exception e) {
      done = 0;
      e.printStackTrace();
      SysLogger.error("Error in XmlOperator.close()", e);
    }
    return done;
  }