@Override
    public void run() {
      try {
        s_logger.debug("HostStatsCollector is running...");

        SearchCriteria<HostVO> sc = _hostDao.createSearchCriteria();
        sc.addAnd("status", SearchCriteria.Op.EQ, Status.Up.toString());
        sc.addAnd("type", SearchCriteria.Op.NEQ, Host.Type.Storage.toString());
        sc.addAnd("type", SearchCriteria.Op.NEQ, Host.Type.ConsoleProxy.toString());
        sc.addAnd("type", SearchCriteria.Op.NEQ, Host.Type.SecondaryStorage.toString());
        ConcurrentHashMap<Long, HostStats> hostStats = new ConcurrentHashMap<Long, HostStats>();
        List<HostVO> hosts = _hostDao.search(sc, null);
        for (HostVO host : hosts) {
          if (host.getId() != null) {
            HostStatsEntry stats = (HostStatsEntry) _agentMgr.getHostStatistics(host.getId());
            if (stats != null) {
              hostStats.put(host.getId(), stats);
            } else {
              s_logger.warn("Received invalid host stats for host: " + host.getId());
            }
          } else {
            s_logger.warn("Host: " + host.getId() + " does not exist, skipping host statistics");
          }
        }
        _hostStats = hostStats;
      } catch (Throwable t) {
        s_logger.error("Error trying to retrieve host stats", t);
      }
    }
 @Override
 public List<VMTemplateVO> listReadyTemplates() {
   SearchCriteria<VMTemplateVO> sc = createSearchCriteria();
   sc.addAnd("ready", SearchCriteria.Op.EQ, true);
   sc.addAnd("format", SearchCriteria.Op.NEQ, Storage.ImageFormat.ISO);
   return listIncludingRemovedBy(sc);
 }
예제 #3
0
  public void createOrUpdateIpCapacity(Long dcId, Long podId, short capacityType) {
    SearchCriteria<CapacityVO> capacitySC = _capacityDao.createSearchCriteria();

    List<CapacityVO> capacities = _capacityDao.search(capacitySC, null);
    capacitySC = _capacityDao.createSearchCriteria();
    capacitySC.addAnd("podId", SearchCriteria.Op.EQ, podId);
    capacitySC.addAnd("dataCenterId", SearchCriteria.Op.EQ, dcId);
    capacitySC.addAnd("capacityType", SearchCriteria.Op.EQ, capacityType);

    int totalIPs;
    int allocatedIPs;
    capacities = _capacityDao.search(capacitySC, null);
    if (capacityType == CapacityVO.CAPACITY_TYPE_PRIVATE_IP) {
      totalIPs = _privateIPAddressDao.countIPs(podId, dcId, false);
      allocatedIPs = _privateIPAddressDao.countIPs(podId, dcId, true);
    } else if (capacityType == CapacityVO.CAPACITY_TYPE_VIRTUAL_NETWORK_PUBLIC_IP) {
      totalIPs = _publicIPAddressDao.countIPsForNetwork(dcId, false, VlanType.VirtualNetwork);
      allocatedIPs = _publicIPAddressDao.countIPsForNetwork(dcId, true, VlanType.VirtualNetwork);
    } else {
      totalIPs = _publicIPAddressDao.countIPsForNetwork(dcId, false, VlanType.DirectAttached);
      allocatedIPs = _publicIPAddressDao.countIPsForNetwork(dcId, true, VlanType.DirectAttached);
    }

    if (capacities.size() == 0) {
      CapacityVO newPublicIPCapacity =
          new CapacityVO(null, dcId, podId, null, allocatedIPs, totalIPs, capacityType);
      _capacityDao.persist(newPublicIPCapacity);
    } else if (!(capacities.get(0).getUsedCapacity() == allocatedIPs
        && capacities.get(0).getTotalCapacity() == totalIPs)) {
      CapacityVO capacity = capacities.get(0);
      capacity.setUsedCapacity(allocatedIPs);
      capacity.setTotalCapacity(totalIPs);
      _capacityDao.update(capacity.getId(), capacity);
    }
  }
예제 #4
0
  private void createOrUpdateVlanCapacity(long dcId) {

    SearchCriteria<CapacityVO> capacitySC = _capacityDao.createSearchCriteria();

    List<CapacityVO> capacities = _capacityDao.search(capacitySC, null);
    capacitySC = _capacityDao.createSearchCriteria();
    capacitySC.addAnd("dataCenterId", SearchCriteria.Op.EQ, dcId);
    capacitySC.addAnd("capacityType", SearchCriteria.Op.EQ, Capacity.CAPACITY_TYPE_VLAN);
    capacities = _capacityDao.search(capacitySC, null);

    int totalVlans = _dcDao.countZoneVlans(dcId, false);
    int allocatedVlans = _dcDao.countZoneVlans(dcId, true);

    if (capacities.size() == 0) {
      CapacityVO newPublicIPCapacity =
          new CapacityVO(
              null, dcId, null, null, allocatedVlans, totalVlans, Capacity.CAPACITY_TYPE_VLAN);
      _capacityDao.persist(newPublicIPCapacity);
    } else if (!(capacities.get(0).getUsedCapacity() == allocatedVlans
        && capacities.get(0).getTotalCapacity() == totalVlans)) {
      CapacityVO capacity = capacities.get(0);
      capacity.setUsedCapacity(allocatedVlans);
      capacity.setTotalCapacity(totalVlans);
      _capacityDao.update(capacity.getId(), capacity);
    }
  }
 @Override
 public List<VMTemplateVO> findIsosByIdAndPath(Long domainId, Long accountId, String path) {
   SearchCriteria<VMTemplateVO> sc = createSearchCriteria();
   sc.addAnd("iso", SearchCriteria.Op.EQ, true);
   if (domainId != null) {
     sc.addAnd("domainId", SearchCriteria.Op.EQ, domainId);
   }
   if (accountId != null) {
     sc.addAnd("accountId", SearchCriteria.Op.EQ, accountId);
   }
   if (path != null) {
     sc.addAnd("path", SearchCriteria.Op.EQ, path);
   }
   return listIncludingRemovedBy(sc);
 }
예제 #6
0
  @Override
  public List<AccountVO> findRecentlyDeletedAccounts(
      Long maxAccountId, Date earliestRemovedDate, Filter filter) {
    if (earliestRemovedDate == null) {
      return null;
    }
    SearchCriteria<AccountVO> sc = createSearchCriteria();
    if (maxAccountId != null) {
      sc.addAnd("id", SearchCriteria.Op.LTEQ, maxAccountId);
    }
    sc.addAnd("removed", SearchCriteria.Op.NNULL);
    sc.addAnd("removed", SearchCriteria.Op.GTEQ, earliestRemovedDate);

    return listIncludingRemovedBy(sc, filter);
  }
  public int removeByAccountId(long accountId) {
    SearchCriteria<ConditionVO> sc = createSearchCriteria();

    sc.addAnd("accountId", SearchCriteria.Op.EQ, accountId);

    return remove(sc);
  }
 @Override
 public List<VMTemplateVO> listByHypervisorType(List<HypervisorType> hyperTypes) {
   SearchCriteria<VMTemplateVO> sc = createSearchCriteria();
   hyperTypes.add(HypervisorType.None);
   sc.addAnd("hypervisorType", SearchCriteria.Op.IN, hyperTypes.toArray());
   return listBy(sc);
 }
  @Override
  public AlertVO getLastAlert(short type, long dataCenterId, Long podId) {
    Filter searchFilter =
        new Filter(AlertVO.class, "createdDate", Boolean.FALSE, Long.valueOf(0), Long.valueOf(1));
    SearchCriteria<AlertVO> sc = createSearchCriteria();

    sc.addAnd("type", SearchCriteria.Op.EQ, Short.valueOf(type));
    sc.addAnd("dataCenterId", SearchCriteria.Op.EQ, Long.valueOf(dataCenterId));
    if (podId != null) {
      sc.addAnd("podId", SearchCriteria.Op.EQ, podId);
    }

    List<AlertVO> alerts = listBy(sc, searchFilter);
    if ((alerts != null) && !alerts.isEmpty()) {
      return alerts.get(0);
    }
    return null;
  }
  @Override
  public List<AsyncJobVO> getResetJobs(long msid) {
    SearchCriteria<AsyncJobVO> sc = pendingAsyncJobSearch.create();
    sc.setParameters("status", JobInfo.Status.IN_PROGRESS);

    // construct query: (job_executing_msid=msid OR (job_executing_msid IS NULL AND
    // job_init_msid=msid))
    SearchCriteria<AsyncJobVO> msQuery = createSearchCriteria();
    msQuery.addOr("executingMsid", SearchCriteria.Op.EQ, msid);
    SearchCriteria<AsyncJobVO> initMsQuery = createSearchCriteria();
    initMsQuery.addAnd("executingMsid", SearchCriteria.Op.NULL);
    initMsQuery.addAnd("initMsid", SearchCriteria.Op.EQ, msid);
    msQuery.addOr("initMsid", SearchCriteria.Op.SC, initMsQuery);

    sc.addAnd("executingMsid", SearchCriteria.Op.SC, msQuery);

    Filter filter = new Filter(AsyncJobVO.class, "created", true, null, null);
    return listIncludingRemovedBy(sc, filter);
  }
 @Override
 public SecurityGroupRuleVO findByProtoPortsAndAllowedGroupId(
     long securityGroupId, String proto, int startPort, int endPort, Long allowedGroupId) {
   SearchCriteria<SecurityGroupRuleVO> sc = protoPortsAndSecurityGroupIdSearch.create();
   sc.addAnd("securityGroupId", SearchCriteria.Op.EQ, securityGroupId);
   sc.setParameters("proto", proto);
   sc.setParameters("startPort", startPort);
   sc.setParameters("endPort", endPort);
   sc.setParameters("allowedNetworkId", allowedGroupId);
   return findOneIncludingRemovedBy(sc);
 }
예제 #12
0
  @Override
  public List<AccountVO> findNewAccounts(Long minAccountId, Filter filter) {
    if (minAccountId == null) {
      return null;
    }

    SearchCriteria<AccountVO> sc = createSearchCriteria();
    sc.addAnd("id", SearchCriteria.Op.GT, minAccountId);

    return listIncludingRemovedBy(sc, filter);
  }
예제 #13
0
  @Override
  public List<AccountVO> findActiveAccounts(Long maxAccountId, Filter filter) {
    if (maxAccountId == null) {
      return null;
    }

    SearchCriteria<AccountVO> sc = createSearchCriteria();
    sc.addAnd("id", SearchCriteria.Op.LTEQ, maxAccountId);

    return listBy(sc, filter);
  }
예제 #14
0
  @Override
  public List<UserVmVO> listBy(long routerId, State... states) {
    SearchCriteria sc = RouterStateSearch.create();
    SearchCriteria ssc = createSearchCriteria();

    sc.setParameters("router", routerId);
    for (State state : states) {
      ssc.addOr("state", SearchCriteria.Op.EQ, state.toString());
    }
    sc.addAnd("state", SearchCriteria.Op.SC, ssc);
    return listBy(sc);
  }
예제 #15
0
  public void init() {
    BaseCmd.setComponents(new ApiResponseHelper());
    BaseListCmd.configure();

    _systemAccount = _accountMgr.getSystemAccount();
    _systemUser = _accountMgr.getSystemUser();
    _dispatcher = ApiDispatcher.getInstance();

    Integer apiPort = null; // api port, null by default
    ComponentLocator locator = ComponentLocator.getLocator(ManagementServer.Name);
    ConfigurationDao configDao = locator.getDao(ConfigurationDao.class);
    SearchCriteria<ConfigurationVO> sc = configDao.createSearchCriteria();
    sc.addAnd("name", SearchCriteria.Op.EQ, "integration.api.port");
    List<ConfigurationVO> values = configDao.search(sc, null);
    if ((values != null) && (values.size() > 0)) {
      ConfigurationVO apiPortConfig = values.get(0);
      if (apiPortConfig.getValue() != null) {
        apiPort = Integer.parseInt(apiPortConfig.getValue());
      }
    }

    Set<Class<?>> cmdClasses =
        ReflectUtil.getClassesWithAnnotation(
            APICommand.class, new String[] {"org.apache.cloudstack.api", "com.cloud.api"});

    for (Class<?> cmdClass : cmdClasses) {
      String apiName = cmdClass.getAnnotation(APICommand.class).name();
      if (_apiNameCmdClassMap.containsKey(apiName)) {
        s_logger.error("API Cmd class " + cmdClass.getName() + " has non-unique apiname" + apiName);
        continue;
      }
      _apiNameCmdClassMap.put(apiName, cmdClass);
    }

    encodeApiResponse = Boolean.valueOf(configDao.getValue(Config.EncodeApiResponse.key()));
    String jsonType = configDao.getValue(Config.JavaScriptDefaultContentType.key());
    if (jsonType != null) {
      jsonContentType = jsonType;
    }

    if (apiPort != null) {
      ListenerThread listenerThread = new ListenerThread(this, apiPort);
      listenerThread.start();
    }
  }
예제 #16
0
  @Override
  public boolean start() {
    Integer apiPort = null; // api port, null by default
    SearchCriteria<ConfigurationVO> sc = _configDao.createSearchCriteria();
    sc.addAnd("name", SearchCriteria.Op.EQ, Config.IntegrationAPIPort.key());
    List<ConfigurationVO> values = _configDao.search(sc, null);
    if ((values != null) && (values.size() > 0)) {
      ConfigurationVO apiPortConfig = values.get(0);
      if (apiPortConfig.getValue() != null) {
        apiPort = Integer.parseInt(apiPortConfig.getValue());
      }
    }

    Map<String, String> configs = _configDao.getConfiguration();
    String strSnapshotLimit = configs.get(Config.ConcurrentSnapshotsThresholdPerHost.key());
    if (strSnapshotLimit != null) {
      Long snapshotLimit = NumbersUtil.parseLong(strSnapshotLimit, 1L);
      if (snapshotLimit.longValue() <= 0) {
        s_logger.debug(
            "Global config parameter "
                + Config.ConcurrentSnapshotsThresholdPerHost.toString()
                + " is less or equal 0; defaulting to unlimited");
      } else {
        _dispatcher.setCreateSnapshotQueueSizeLimit(snapshotLimit);
      }
    }

    Set<Class<?>> cmdClasses = new HashSet<Class<?>>();
    for (PluggableService pluggableService : _pluggableServices) {
      cmdClasses.addAll(pluggableService.getCommands());
      if (s_logger.isDebugEnabled()) {
        s_logger.debug("Discovered plugin " + pluggableService.getClass().getSimpleName());
      }
    }

    for (Class<?> cmdClass : cmdClasses) {
      APICommand at = cmdClass.getAnnotation(APICommand.class);
      if (at == null) {
        throw new CloudRuntimeException(
            String.format(
                "%s is claimed as a API command, but it doesn't have @APICommand annotation",
                cmdClass.getName()));
      }
      String apiName = at.name();
      if (_apiNameCmdClassMap.containsKey(apiName)) {
        s_logger.error("API Cmd class " + cmdClass.getName() + " has non-unique apiname" + apiName);
        continue;
      }
      _apiNameCmdClassMap.put(apiName, cmdClass);
    }

    encodeApiResponse = Boolean.valueOf(_configDao.getValue(Config.EncodeApiResponse.key()));
    String jsonType = _configDao.getValue(Config.JavaScriptDefaultContentType.key());
    if (jsonType != null) {
      jsonContentType = jsonType;
    }

    if (apiPort != null) {
      ListenerThread listenerThread = new ListenerThread(this, apiPort);
      listenerThread.start();
    }

    return true;
  }
예제 #17
0
  // TODO: Get rid of this case once we've determined that the capacity listeners above have all the
  // changes
  // create capacity entries if none exist for this server
  private void createCapacityEntry(StartupCommand startup, HostVO server) {
    SearchCriteria<CapacityVO> capacitySC = _capacityDao.createSearchCriteria();
    capacitySC.addAnd("hostOrPoolId", SearchCriteria.Op.EQ, server.getId());
    capacitySC.addAnd("dataCenterId", SearchCriteria.Op.EQ, server.getDataCenterId());
    capacitySC.addAnd("podId", SearchCriteria.Op.EQ, server.getPodId());

    if (startup instanceof StartupRoutingCommand) {
      SearchCriteria<CapacityVO> capacityCPU = _capacityDao.createSearchCriteria();
      capacityCPU.addAnd("hostOrPoolId", SearchCriteria.Op.EQ, server.getId());
      capacityCPU.addAnd("dataCenterId", SearchCriteria.Op.EQ, server.getDataCenterId());
      capacityCPU.addAnd("podId", SearchCriteria.Op.EQ, server.getPodId());
      capacityCPU.addAnd("capacityType", SearchCriteria.Op.EQ, CapacityVO.CAPACITY_TYPE_CPU);
      List<CapacityVO> capacityVOCpus = _capacityDao.search(capacitySC, null);
      Float cpuovercommitratio =
          Float.parseFloat(
              _clusterDetailsDao
                  .findDetail(server.getClusterId(), "cpuOvercommitRatio")
                  .getValue());
      Float memoryOvercommitRatio =
          Float.parseFloat(
              _clusterDetailsDao
                  .findDetail(server.getClusterId(), "memoryOvercommitRatio")
                  .getValue());

      if (capacityVOCpus != null && !capacityVOCpus.isEmpty()) {
        CapacityVO CapacityVOCpu = capacityVOCpus.get(0);
        long newTotalCpu =
            (long)
                (server.getCpus().longValue() * server.getSpeed().longValue() * cpuovercommitratio);
        if ((CapacityVOCpu.getTotalCapacity() <= newTotalCpu)
            || ((CapacityVOCpu.getUsedCapacity() + CapacityVOCpu.getReservedCapacity())
                <= newTotalCpu)) {
          CapacityVOCpu.setTotalCapacity(newTotalCpu);
        } else if ((CapacityVOCpu.getUsedCapacity() + CapacityVOCpu.getReservedCapacity()
                > newTotalCpu)
            && (CapacityVOCpu.getUsedCapacity() < newTotalCpu)) {
          CapacityVOCpu.setReservedCapacity(0);
          CapacityVOCpu.setTotalCapacity(newTotalCpu);
        } else {
          s_logger.debug(
              "What? new cpu is :"
                  + newTotalCpu
                  + ", old one is "
                  + CapacityVOCpu.getUsedCapacity()
                  + ","
                  + CapacityVOCpu.getReservedCapacity()
                  + ","
                  + CapacityVOCpu.getTotalCapacity());
        }
        _capacityDao.update(CapacityVOCpu.getId(), CapacityVOCpu);
      } else {
        CapacityVO capacity =
            new CapacityVO(
                server.getId(),
                server.getDataCenterId(),
                server.getPodId(),
                server.getClusterId(),
                0L,
                (long) (server.getCpus().longValue() * server.getSpeed().longValue()),
                CapacityVO.CAPACITY_TYPE_CPU);
        _capacityDao.persist(capacity);
      }

      SearchCriteria<CapacityVO> capacityMem = _capacityDao.createSearchCriteria();
      capacityMem.addAnd("hostOrPoolId", SearchCriteria.Op.EQ, server.getId());
      capacityMem.addAnd("dataCenterId", SearchCriteria.Op.EQ, server.getDataCenterId());
      capacityMem.addAnd("podId", SearchCriteria.Op.EQ, server.getPodId());
      capacityMem.addAnd("capacityType", SearchCriteria.Op.EQ, CapacityVO.CAPACITY_TYPE_MEMORY);
      List<CapacityVO> capacityVOMems = _capacityDao.search(capacityMem, null);

      if (capacityVOMems != null && !capacityVOMems.isEmpty()) {
        CapacityVO CapacityVOMem = capacityVOMems.get(0);
        long newTotalMem = (long) ((server.getTotalMemory()) * memoryOvercommitRatio);
        if (CapacityVOMem.getTotalCapacity() <= newTotalMem
            || (CapacityVOMem.getUsedCapacity() + CapacityVOMem.getReservedCapacity()
                <= newTotalMem)) {
          CapacityVOMem.setTotalCapacity(newTotalMem);
        } else if (CapacityVOMem.getUsedCapacity() + CapacityVOMem.getReservedCapacity()
                > newTotalMem
            && CapacityVOMem.getUsedCapacity() < newTotalMem) {
          CapacityVOMem.setReservedCapacity(0);
          CapacityVOMem.setTotalCapacity(newTotalMem);
        } else {
          s_logger.debug(
              "What? new cpu is :"
                  + newTotalMem
                  + ", old one is "
                  + CapacityVOMem.getUsedCapacity()
                  + ","
                  + CapacityVOMem.getReservedCapacity()
                  + ","
                  + CapacityVOMem.getTotalCapacity());
        }
        _capacityDao.update(CapacityVOMem.getId(), CapacityVOMem);
      } else {
        CapacityVO capacity =
            new CapacityVO(
                server.getId(),
                server.getDataCenterId(),
                server.getPodId(),
                server.getClusterId(),
                0L,
                server.getTotalMemory(),
                CapacityVO.CAPACITY_TYPE_MEMORY);
        _capacityDao.persist(capacity);
      }
    }
  }
  @Override
  public List<SecurityGroupRulesVO> searchForSecurityGroupRules(ListSecurityGroupsCmd cmd)
      throws PermissionDeniedException, InvalidParameterValueException {
    Account caller = UserContext.current().getCaller();
    Long domainId = cmd.getDomainId();
    String accountName = cmd.getAccountName();
    Long instanceId = cmd.getVirtualMachineId();
    String securityGroup = cmd.getSecurityGroupName();
    Long id = cmd.getId();
    Long accountId = null;

    if (instanceId != null) {
      UserVmVO userVM = _userVMDao.findById(instanceId);
      if (userVM == null) {
        throw new InvalidParameterValueException(
            "Unable to list network groups for virtual machine instance "
                + instanceId
                + "; instance not found.");
      }
      _accountMgr.checkAccess(caller, null, userVM);
      return listSecurityGroupRulesByVM(instanceId.longValue());
    }

    if (_accountMgr.isAdmin(caller.getType())) {
      if (domainId != null) {
        Domain domain = _domainMgr.getDomain(domainId);
        if (domain == null) {
          throw new InvalidParameterValueException("Unable to find domain by id " + domainId);
        }
        _accountMgr.checkAccess(caller, domain);
        if (accountName != null) {
          Account account = _accountMgr.getActiveAccountByName(accountName, domainId);
          if (account == null) {
            throw new InvalidParameterValueException(
                "Unable to find account " + accountName + " in domain " + domainId);
          }
          _accountMgr.checkAccess(caller, null, account);
          accountId = account.getId();
        }
      }
    } else {
      // regular user can see only his own security groups
      accountId = caller.getId();
    }

    List<SecurityGroupRulesVO> securityRulesList = new ArrayList<SecurityGroupRulesVO>();
    Filter searchFilter =
        new Filter(SecurityGroupVO.class, "id", true, cmd.getStartIndex(), cmd.getPageSizeVal());
    Object keyword = cmd.getKeyword();

    SearchBuilder<SecurityGroupVO> sb = _securityGroupDao.createSearchBuilder();
    sb.and("id", sb.entity().getId(), SearchCriteria.Op.EQ);
    sb.and("accountId", sb.entity().getAccountId(), SearchCriteria.Op.EQ);
    sb.and("name", sb.entity().getName(), SearchCriteria.Op.EQ);
    sb.and("domainId", sb.entity().getDomainId(), SearchCriteria.Op.EQ);

    // only do a recursive domain search if the search is not limited by account or instance
    if ((accountId == null)
        && (instanceId == null)
        && (caller.getType() == Account.ACCOUNT_TYPE_DOMAIN_ADMIN)) {
      SearchBuilder<DomainVO> domainSearch = _domainDao.createSearchBuilder();
      domainSearch.and("path", domainSearch.entity().getPath(), SearchCriteria.Op.LIKE);
      sb.join(
          "domainSearch",
          domainSearch,
          sb.entity().getDomainId(),
          domainSearch.entity().getId(),
          JoinBuilder.JoinType.INNER);
    }

    SearchCriteria<SecurityGroupVO> sc = sb.create();

    if (id != null) {
      sc.setParameters("id", id);
    }

    if (securityGroup != null) {
      sc.setParameters("name", securityGroup);
    }

    if (accountId != null) {
      sc.setParameters("accountId", accountId);
    }

    // only do a recursive domain search if the search is not limited by account or instance
    if ((accountId == null)
        && (instanceId == null)
        && (caller.getType() == Account.ACCOUNT_TYPE_DOMAIN_ADMIN)) {
      DomainVO domain = _domainDao.findById(caller.getDomainId());
      sc.setJoinParameters("domainSearch", "path", domain.getPath() + "%");
    }

    if (keyword != null) {
      SearchCriteria<SecurityGroupRulesVO> ssc = _securityGroupRulesDao.createSearchCriteria();
      ssc.addOr("name", SearchCriteria.Op.LIKE, "%" + keyword + "%");
      ssc.addOr("description", SearchCriteria.Op.LIKE, "%" + keyword + "%");
      sc.addAnd("name", SearchCriteria.Op.SC, ssc);
    }

    List<SecurityGroupVO> securityGroups = _securityGroupDao.search(sc, searchFilter);
    for (SecurityGroupVO group : securityGroups) {
      securityRulesList.addAll(_securityGroupRulesDao.listSecurityRulesByGroupId(group.getId()));
    }

    return securityRulesList;
  }
    @Override
    public void run() {
      try {
        SearchCriteria<HostVO> sc = _hostDao.createSearchCriteria();
        sc.addAnd("status", SearchCriteria.Op.EQ, Status.Up.toString());
        sc.addAnd("type", SearchCriteria.Op.EQ, Host.Type.Storage.toString());

        ConcurrentHashMap<Long, StorageStats> storageStats =
            new ConcurrentHashMap<Long, StorageStats>();
        List<HostVO> hosts = _hostDao.search(sc, null);
        for (HostVO host : hosts) {
          GetStorageStatsCommand command = new GetStorageStatsCommand(host.getGuid());
          Answer answer = _agentMgr.easySend(host.getId(), command);
          if (answer != null && answer.getResult()) {
            storageStats.put(host.getId(), (StorageStats) answer);
          }
        }

        sc = _hostDao.createSearchCriteria();
        sc.addAnd("status", SearchCriteria.Op.EQ, Status.Up.toString());
        sc.addAnd("type", SearchCriteria.Op.EQ, Host.Type.SecondaryStorage.toString());

        hosts = _hostDao.search(sc, null);
        for (HostVO host : hosts) {
          GetStorageStatsCommand command = new GetStorageStatsCommand(host.getGuid());
          Answer answer = _agentMgr.easySend(host.getId(), command);
          if (answer != null && answer.getResult()) {
            storageStats.put(host.getId(), (StorageStats) answer);
          }
        }
        _storageStats = storageStats;

        ConcurrentHashMap<Long, StorageStats> storagePoolStats =
            new ConcurrentHashMap<Long, StorageStats>();

        List<StoragePoolVO> storagePools = _storagePoolDao.listAll();
        for (StoragePoolVO pool : storagePools) {
          GetStorageStatsCommand command =
              new GetStorageStatsCommand(pool.getUuid(), pool.getPoolType(), pool.getPath());
          Answer answer = _storageManager.sendToPool(pool, command);
          if (answer != null && answer.getResult()) {
            storagePoolStats.put(pool.getId(), (StorageStats) answer);
          }
        }
        _storagePoolStats = storagePoolStats;

        // a list to store the new capacity entries that will be committed once everything is
        // calculated
        List<CapacityVO> newCapacities = new ArrayList<CapacityVO>();

        // Updating the storage entries and creating new ones if they dont exist.
        Transaction txn = Transaction.open(Transaction.CLOUD_DB);
        try {
          if (s_logger.isTraceEnabled()) {
            s_logger.trace("recalculating system storage capacity");
          }
          txn.start();
          for (Long hostId : storageStats.keySet()) {
            StorageStats stats = storageStats.get(hostId);
            short capacityType = -1;
            HostVO host = _hostDao.findById(hostId);
            host.setTotalSize(stats.getCapacityBytes());
            _hostDao.update(host.getId(), host);

            SearchCriteria<CapacityVO> capacitySC = _capacityDao.createSearchCriteria();
            capacitySC.addAnd("hostOrPoolId", SearchCriteria.Op.EQ, hostId);
            capacitySC.addAnd("dataCenterId", SearchCriteria.Op.EQ, host.getDataCenterId());

            if (Host.Type.SecondaryStorage.equals(host.getType())) {
              capacityType = CapacityVO.CAPACITY_TYPE_SECONDARY_STORAGE;
            } else if (Host.Type.Storage.equals(host.getType())) {
              capacityType = CapacityVO.CAPACITY_TYPE_STORAGE;
            }
            if (-1 != capacityType) {
              capacitySC.addAnd("capacityType", SearchCriteria.Op.EQ, capacityType);
              List<CapacityVO> capacities = _capacityDao.search(capacitySC, null);
              if (capacities.size() == 0) { // Create a new one
                CapacityVO capacity =
                    new CapacityVO(
                        host.getId(),
                        host.getDataCenterId(),
                        host.getPodId(),
                        stats.getByteUsed(),
                        stats.getCapacityBytes(),
                        capacityType);
                _capacityDao.persist(capacity);
              } else { // Update if it already exists.
                CapacityVO capacity = capacities.get(0);
                capacity.setUsedCapacity(stats.getByteUsed());
                capacity.setTotalCapacity(stats.getCapacityBytes());
                _capacityDao.update(capacity.getId(), capacity);
              }
            }
          } // End of for
          txn.commit();
        } catch (Exception ex) {
          txn.rollback();
          s_logger.error("Unable to start transaction for storage capacity update");
        } finally {
          txn.close();
        }

        for (Long poolId : storagePoolStats.keySet()) {
          StorageStats stats = storagePoolStats.get(poolId);
          StoragePoolVO pool = _storagePoolDao.findById(poolId);

          if (pool == null) {
            continue;
          }

          pool.setCapacityBytes(stats.getCapacityBytes());
          long available = stats.getCapacityBytes() - stats.getByteUsed();
          if (available < 0) {
            available = 0;
          }
          pool.setAvailableBytes(available);
          _storagePoolDao.update(pool.getId(), pool);

          _storageManager.createCapacityEntry(pool, 0L);
        }
      } catch (Throwable t) {
        s_logger.error("Error trying to retrieve storage stats", t);
      }
    }
    @Override
    public void run() {
      try {
        s_logger.debug("VmStatsCollector is running...");

        SearchCriteria<HostVO> sc = _hostDao.createSearchCriteria();
        sc.addAnd("status", SearchCriteria.Op.EQ, Status.Up.toString());
        sc.addAnd("type", SearchCriteria.Op.NEQ, Host.Type.Storage.toString());
        sc.addAnd("type", SearchCriteria.Op.NEQ, Host.Type.ConsoleProxy.toString());
        sc.addAnd("type", SearchCriteria.Op.NEQ, Host.Type.SecondaryStorage.toString());
        List<HostVO> hosts = _hostDao.search(sc, null);

        for (HostVO host : hosts) {
          List<UserVmVO> vms = _userVmDao.listRunningByHostId(host.getId());
          List<Long> vmIds = new ArrayList<Long>();

          for (UserVmVO vm : vms) {
            vmIds.add(vm.getId());
          }

          try {
            HashMap<Long, VmStatsEntry> vmStatsById =
                _userVmMgr.getVirtualMachineStatistics(host.getId(), host.getName(), vmIds);

            if (vmStatsById != null) {
              VmStatsEntry statsInMemory = null;

              Set<Long> vmIdSet = vmStatsById.keySet();
              for (Long vmId : vmIdSet) {
                VmStatsEntry statsForCurrentIteration = vmStatsById.get(vmId);
                statsInMemory = (VmStatsEntry) _VmStats.get(vmId);

                if (statsInMemory == null) {
                  // no stats exist for this vm, directly persist
                  _VmStats.put(vmId, statsForCurrentIteration);
                } else {
                  // update each field
                  statsInMemory.setCPUUtilization(statsForCurrentIteration.getCPUUtilization());
                  statsInMemory.setNumCPUs(statsForCurrentIteration.getNumCPUs());
                  statsInMemory.setNetworkReadKBs(
                      statsInMemory.getNetworkReadKBs()
                          + statsForCurrentIteration.getNetworkReadKBs());
                  statsInMemory.setNetworkWriteKBs(
                      statsInMemory.getNetworkWriteKBs()
                          + statsForCurrentIteration.getNetworkWriteKBs());

                  _VmStats.put(vmId, statsInMemory);
                }
              }
            }

          } catch (Exception e) {
            s_logger.debug("Failed to get VM stats for host with ID: " + host.getId());
            continue;
          }
        }

      } catch (Throwable t) {
        s_logger.error("Error trying to retrieve VM stats", t);
      }
    }
  private void checkStatusOfCurrentlyExecutingSnapshots() {
    SearchCriteria<SnapshotScheduleVO> sc = _snapshotScheduleDao.createSearchCriteria();
    sc.addAnd("asyncJobId", SearchCriteria.Op.NNULL);
    List<SnapshotScheduleVO> snapshotSchedules = _snapshotScheduleDao.search(sc, null);
    for (SnapshotScheduleVO snapshotSchedule : snapshotSchedules) {
      Long asyncJobId = snapshotSchedule.getAsyncJobId();
      AsyncJobVO asyncJob = _asyncJobDao.findById(asyncJobId);
      switch (asyncJob.getStatus()) {
        case AsyncJobResult.STATUS_SUCCEEDED:
          // The snapshot has been successfully backed up.
          // The snapshot state has also been cleaned up.
          // We can schedule the next job for this snapshot.
          // Remove the existing entry in the snapshot_schedule table.
          scheduleNextSnapshotJob(snapshotSchedule);
          break;
        case AsyncJobResult.STATUS_FAILED:
          // Check the snapshot status.
          Long snapshotId = snapshotSchedule.getSnapshotId();
          if (snapshotId == null) {
            // createSnapshotAsync exited, successfully or unsuccessfully,
            // even before creating a snapshot record
            // No cleanup needs to be done.
            // Schedule the next snapshot.
            scheduleNextSnapshotJob(snapshotSchedule);
          } else {
            SnapshotVO snapshot = _snapshotDao.findById(snapshotId);
            if (snapshot == null || snapshot.getRemoved() != null) {
              // This snapshot has been deleted successfully from the primary storage
              // Again no cleanup needs to be done.
              // Schedule the next snapshot.
              // There's very little probability that the code reaches this point.
              // The snapshotId is a foreign key for the snapshot_schedule table
              // set to ON DELETE CASCADE. So if the snapshot entry is deleted, the
              // snapshot_schedule entry will be too.
              // But what if it has only been marked as removed?
              scheduleNextSnapshotJob(snapshotSchedule);
            } else {
              // The management server executing this snapshot job appears to have crashed
              // while creating the snapshot on primary storage/or backing it up.
              // We have no idea whether the snapshot was successfully taken on the primary or not.
              // Schedule the next snapshot job.
              // The ValidatePreviousSnapshotCommand will take appropriate action on this snapshot
              // If the snapshot was taken successfully on primary, it will retry backing it up.
              // and cleanup the previous snapshot
              // Set the userId to that of system.
              // _snapshotManager.validateSnapshot(1L, snapshot);
              // In all cases, schedule the next snapshot job
              scheduleNextSnapshotJob(snapshotSchedule);
            }
          }

          break;
        case AsyncJobResult.STATUS_IN_PROGRESS:
          // There is no way of knowing from here whether
          // 1) Another management server is processing this snapshot job
          // 2) The management server has crashed and this snapshot is lying
          // around in an inconsistent state.
          // Hopefully, this can be resolved at the backend when the current snapshot gets executed.
          // But if it remains in this state, the current snapshot will not get executed.
          // And it will remain in stasis.
          break;
      }
    }
  }
  @Override
  public List<LoadBalancerVO> searchForLoadBalancers(ListLoadBalancerRulesCmd cmd) {
    Account caller = UserContext.current().getCaller();
    Long ipId = cmd.getPublicIpId();
    Long zoneId = cmd.getZoneId();
    String path = null;

    Pair<String, Long> accountDomainPair =
        _accountMgr.finalizeAccountDomainForList(caller, cmd.getAccountName(), cmd.getDomainId());
    String accountName = accountDomainPair.first();
    Long domainId = accountDomainPair.second();

    if (caller.getType() == Account.ACCOUNT_TYPE_DOMAIN_ADMIN
        || caller.getType() == Account.ACCOUNT_TYPE_RESOURCE_DOMAIN_ADMIN) {
      Domain domain = _domainMgr.getDomain(caller.getDomainId());
      path = domain.getPath();
    }

    Filter searchFilter =
        new Filter(LoadBalancerVO.class, "id", true, cmd.getStartIndex(), cmd.getPageSizeVal());

    Object id = cmd.getId();
    Object name = cmd.getLoadBalancerRuleName();
    Object keyword = cmd.getKeyword();
    Object instanceId = cmd.getVirtualMachineId();

    SearchBuilder<LoadBalancerVO> sb = _lbDao.createSearchBuilder();
    sb.and("id", sb.entity().getId(), SearchCriteria.Op.EQ);
    sb.and("name", sb.entity().getName(), SearchCriteria.Op.LIKE);
    sb.and("sourceIpAddress", sb.entity().getSourceIpAddressId(), SearchCriteria.Op.EQ);
    sb.and("accountId", sb.entity().getAccountId(), SearchCriteria.Op.EQ);
    sb.and("domainId", sb.entity().getDomainId(), SearchCriteria.Op.EQ);

    if (instanceId != null) {
      SearchBuilder<LoadBalancerVMMapVO> lbVMSearch = _lb2VmMapDao.createSearchBuilder();
      lbVMSearch.and("instanceId", lbVMSearch.entity().getInstanceId(), SearchCriteria.Op.EQ);
      sb.join(
          "lbVMSearch",
          lbVMSearch,
          sb.entity().getId(),
          lbVMSearch.entity().getLoadBalancerId(),
          JoinBuilder.JoinType.INNER);
    }

    if (path != null) {
      // for domain admin we should show only subdomains information
      SearchBuilder<DomainVO> domainSearch = _domainDao.createSearchBuilder();
      domainSearch.and("path", domainSearch.entity().getPath(), SearchCriteria.Op.LIKE);
      sb.join(
          "domainSearch",
          domainSearch,
          sb.entity().getDomainId(),
          domainSearch.entity().getId(),
          JoinBuilder.JoinType.INNER);
    }

    if (zoneId != null) {
      SearchBuilder<IPAddressVO> ipSearch = _ipAddressDao.createSearchBuilder();
      ipSearch.and("zoneId", ipSearch.entity().getDataCenterId(), SearchCriteria.Op.EQ);
      sb.join(
          "ipSearch",
          ipSearch,
          sb.entity().getSourceIpAddressId(),
          ipSearch.entity().getId(),
          JoinBuilder.JoinType.INNER);
    }

    SearchCriteria<LoadBalancerVO> sc = sb.create();
    if (keyword != null) {
      SearchCriteria<LoadBalancerVO> ssc = _lbDao.createSearchCriteria();
      ssc.addOr("name", SearchCriteria.Op.LIKE, "%" + keyword + "%");
      ssc.addOr("description", SearchCriteria.Op.LIKE, "%" + keyword + "%");

      sc.addAnd("name", SearchCriteria.Op.SC, ssc);
    }

    if (name != null) {
      sc.setParameters("name", "%" + name + "%");
    }

    if (id != null) {
      sc.setParameters("id", id);
    }

    if (ipId != null) {
      sc.setParameters("sourceIpAddress", ipId);
    }

    if (instanceId != null) {
      sc.setJoinParameters("lbVMSearch", "instanceId", instanceId);
    }

    if (domainId != null) {
      sc.setParameters("domainId", domainId);
      if (accountName != null) {
        Account account = _accountMgr.getActiveAccountByName(accountName, domainId);
        sc.setParameters("accountId", account.getId());
      }
    }

    if (path != null) {
      sc.setJoinParameters("domainSearch", "path", path + "%");
    }

    if (zoneId != null) {
      sc.setJoinParameters("ipSearch", "zoneId", zoneId);
    }

    return _lbDao.search(sc, searchFilter);
  }
예제 #23
0
 @Override
 public List<AccountVO> findAccountsLike(String accountName) {
   SearchCriteria<AccountVO> sc = createSearchCriteria();
   sc.addAnd("accountName", SearchCriteria.Op.LIKE, "%" + accountName + "%");
   return listBy(sc);
 }