예제 #1
0
  public void run() {
    if (DiscoverEngine.getInstance().getStopStatus() == 1) return;
    snmp = SnmpUtil.getInstance();
    SysLogger.info("开始分析设备" + node.getIpAddress() + "的地址转发表");
    Set shieldList = DiscoverResource.getInstance().getShieldSet();
    List netshieldList = DiscoverResource.getInstance().getNetshieldList();
    List netincludeList = DiscoverResource.getInstance().getNetincludeList();
    IfEntity ifEntity = null;
    IfEntity endifEntity = null;

    // 生成线程池
    ThreadPool threadPool = new ThreadPool(addressList.size());

    for (int i = 0; i < addressList.size(); i++) {
      // ###################################
      // 若不需要服务器,则不进行交换机的ARP表采集
      // if(1==1)continue;
      // ###################################
      try {
        // node.updateCount(2);
        IpAddress ipAddr = (IpAddress) addressList.get(i);
        threadPool.runTask(NetMediThread.createTask(ipAddr, node));
        // SysLogger.info("在"+node.getIpAddress()+"的地址转发表发现IP "+ipAddr.getIpAddress()+",开始分析");
      } catch (Exception ex) {
        ex.printStackTrace();
      }
    } // end_for
    // 关闭线程池并等待所有任务完成
    threadPool.join();
    threadPool.close();
    threadPool = null;

    DiscoverEngine.getInstance().addDiscoverdcount();
    snmp = null;
    setCompleted(true);
  } // end_run
예제 #2
0
public class Host extends Node {
  private int hasDetected;
  private boolean isDiscovered;
  private boolean linkAnalysed;
  private int ipTotal;
  private String alias;
  private String switchIds;
  private int completSwitchs;
  private boolean isRouter;
  private List<String> vlanCommunities;
  private String bridgeAddress;

  private static final int PER_THREAD_IP = DiscoverResource.getInstance().getPerThreadIps();

  public Host() {
    setDiscovered(false);
    setNetMask("255.255.255.0");
    hasDetected = 0;
    ipTotal = -1;
    alias = null;
  }

  public void completeOneSwitch() {
    completSwitchs++;
  }

  public boolean allSwitchesFound() {
    if (switchIds == null) return true;
    String[] ids = switchIds.split(",");
    if (completSwitchs == ids.length) return true;
    else return false;
  }

  public void setRouter(boolean is) {
    isRouter = is;
  }

  public boolean isRouter() {
    return isRouter;
  }

  public void addSwitchId(int id) {
    if (switchIds == null) switchIds = "" + id;
    else switchIds += "," + id;
    SysLogger.info(ipAddress + "加入一交换,id=" + id);
  }

  public String getSwitchIds() {
    return switchIds;
  }

  public boolean isDiscovered() {
    return isDiscovered;
  }

  public void setDiscovered(boolean isDiscovered) {
    this.isDiscovered = isDiscovered;
  }

  public synchronized void updateCount(int detectType) {
    if (detectType == 1) doIPNetProbe();
    else hasDetected++;

    if (hasDetected == ipTotal) setDiscovered(true);
  }

  /** 发现与该设备相连的所有设备 */
  public void doDiscover() {
    // 增加CDP协议发现设备
    if (this.category == 1 || this.category == 2 || this.category == 3) {
      if (this.sysOid.indexOf("1.3.6.1.4.1.9") >= 0) doCiscoCDPProbe(); // 用CDP发现网络设备
    }

    // 增加STP协议发现设备

    if (this.category == 3) {
      // SysLogger.info("&&& 用ipnetmedial开始发现"+this.ipAddress);
      doIPNetProbe(); // 二层交换机
      // SysLogger.info("&&& 用ipnetmedial开始发现"+this.ipAddress);
    } else {
      // SysLogger.info("&&& 用iprouter开始发现"+this.ipAddress);
      doIPRouterProbe(); // 路由或路由交换机(三层交换机)
    }
  }

  /** 对CDP进行探测 */
  private void doCiscoCDPProbe() {
    if (DiscoverEngine.getInstance().getStopStatus() == 1) return;
    DiscoverEngine.getInstance().addThread(new CiscoCDPProbeThread(this));
  }
  /** 对IP_Rouer_Table进行探测 */
  private void doIPRouterProbe() {
    if (DiscoverEngine.getInstance().getStopStatus() == 1) return;
    DiscoverEngine.getInstance().addThread(new IPRouterProbeThread(this));
  }

  /** 对IP_Net_To_Media进行探测 */
  private void doIPNetProbe() {
    if (DiscoverEngine.getInstance().getStopStatus() == 1) return;
    List ipNetTable =
        SnmpUtil.getInstance().getIpNetToMediaTable(this.getIpAddress(), this.getCommunity());

    if (ipNetTable == null || ipNetTable.size() == 0) // important
    {
      setDiscovered(true);
      return;
    }
    ipTotal = ipNetTable.size();
    int threadTotal = 0; // 线程总数
    SysLogger.info("PER_THREAD_IP===" + PER_THREAD_IP);
    if (ipTotal % PER_THREAD_IP == 0) // 每个线程对N个ip进行探测
    threadTotal = ipTotal / PER_THREAD_IP;
    else threadTotal = ipTotal / PER_THREAD_IP + 1;

    IPNetProbeThread probeThread = null;
    for (int i = 0; i < threadTotal; i++) {
      if (i == threadTotal - 1)
        probeThread = new IPNetProbeThread(this, ipNetTable.subList(i * PER_THREAD_IP, ipTotal));
      else
        probeThread =
            new IPNetProbeThread(
                this, ipNetTable.subList(i * PER_THREAD_IP, (i + 1) * PER_THREAD_IP));
      DiscoverEngine.getInstance().addThread(probeThread);
    }
  }

  /** 按接口索引找到接口 */
  public IfEntity getIfEntityByIndex(String ifIndex) {
    if (ifEntityList == null || ifEntityList.size() == 0) return null;

    IfEntity ifEntity = null;
    for (int i = 0; i < ifEntityList.size(); i++) {
      IfEntity obj = (IfEntity) ifEntityList.get(i);
      if (obj.getIndex().equals(ifIndex)) {
        ifEntity = obj;
        break;
      }
    }
    if (ifEntity == null) SysLogger.info(ipAddress + "中没有索引为" + ifIndex + "的接口");

    return ifEntity;
  }

  /** 按端口找到一个接口 */
  public IfEntity getIfEntityByPort(String port) {
    if (port == null) return null;
    if (ifEntityList == null || ifEntityList.size() == 0) return null;

    IfEntity ifEntity = null;
    for (int i = 0; i < ifEntityList.size(); i++) {
      IfEntity obj = (IfEntity) ifEntityList.get(i);
      if (obj.getPort().equals(port)) {
        ifEntity = obj;
        break;
      }
    }
    if (ifEntity == null) SysLogger.info(ipAddress + "中没有端口为" + port + "的接口");
    return ifEntity;
  }

  /** 按描述找到一个接口 */
  public IfEntity getIfEntityByDesc(String desc) {
    if (desc == null) return null;
    if (ifEntityList == null || ifEntityList.size() == 0) return null;

    IfEntity ifEntity = null;
    for (int i = 0; i < ifEntityList.size(); i++) {
      IfEntity obj = (IfEntity) ifEntityList.get(i);
      if (obj.getDescr().equals(desc)) {
        ifEntity = obj;
        break;
      }
    }
    if (ifEntity == null) SysLogger.info(ipAddress + "中没有端口描述为" + desc + "的接口");
    return ifEntity;
  }
  /** 按IP找到接口 */
  public IfEntity getIfEntityByIP(String ip) {
    if (ifEntityList == null || ifEntityList.size() == 0) return null;

    IfEntity ifEntity = null;
    for (int i = 0; i < ifEntityList.size(); i++) {
      IfEntity obj = (IfEntity) ifEntityList.get(i);
      if (obj.getIpList() != null) {
        if (obj.getIpList().split(",").length > 0) {
          int flag = 0;
          String IPS[] = obj.getIpList().split(",");
          for (int k = 0; k < IPS.length; k++) {
            // SysLogger.info(this.getIpAddress()+"含有接口地址"+IPS[k]+"===="+ip);
            if (IPS[k].equalsIgnoreCase(ip)) {
              ifEntity = obj;
              flag = 1;
              break;
            }
          }
          if (flag == 1) break;
        } else {
          // SysLogger.info(this.getIpAddress()+"含有接口地址"+obj.getIpList()+"====="+ip);
          if (obj.getIpList().equalsIgnoreCase(ip)) {
            ifEntity = obj;
            break;
          }
        }
      }
    }
    return ifEntity;
  }

  /** 找到该设备的管理地址 */
  /*
      public String getAdminIp()
      {
      	if(ifEntityList==null||ifEntityList.size()==0)
      		return null;

      	IfEntity ifEntity = null;
      	for(int i=0;i<ifEntityList.size();i++)
      	{
      		IfEntity obj = (IfEntity)ifEntityList.get(i);
      		if(obj.getType() == 24){
      			//为LOOPBACK地址
      			if(obj.getIpAddress().indexOf("127.0")<0){
      				ifEntity = obj;
      				return obj.getIpAddress();
      			}
      		}
      	}
          if(ifEntity == null)
  		   SysLogger.info(ipAddress + "对应的设备中没有管理地址");
  		return null;
      }
  */
  public String getAlias() {
    return alias;
  }

  public void setAlias(String alias) {
    this.alias = alias;
  }

  public boolean isLinkAnalysed() {
    return linkAnalysed;
  }

  public void setLinkAnalysed(boolean linkAnalysed) {
    this.linkAnalysed = linkAnalysed;
  }

  public void setVlanCommunities(List<String> vlanCommunities) {
    this.vlanCommunities = vlanCommunities;
  }

  public String getBridgeAddress() {
    return bridgeAddress;
  }

  public void setBridgeAddress(String bridgeAddress) {
    this.bridgeAddress = bridgeAddress;
  }
}