Search in sources :

Example 1 with RouterHealthCheckResultVO

use of com.cloud.network.dao.RouterHealthCheckResultVO in project cloudstack by apache.

the class VirtualNetworkApplianceManagerImpl method updateRouterHealthCheckResult.

private void updateRouterHealthCheckResult(final long routerId, String checkName, String checkType, boolean checkResult, String checkMessage) {
    boolean newHealthCheckEntry = false;
    RouterHealthCheckResultVO connectivityVO = routerHealthCheckResultDao.getRouterHealthCheckResult(routerId, checkName, checkType);
    if (connectivityVO == null) {
        connectivityVO = new RouterHealthCheckResultVO(routerId, checkName, checkType);
        newHealthCheckEntry = true;
    }
    connectivityVO.setCheckResult(checkResult);
    connectivityVO.setLastUpdateTime(new Date());
    if (StringUtils.isNotEmpty(checkMessage)) {
        connectivityVO.setCheckDetails(checkMessage.getBytes(com.cloud.utils.StringUtils.getPreferredCharset()));
    }
    if (newHealthCheckEntry) {
        routerHealthCheckResultDao.persist(connectivityVO);
    } else {
        routerHealthCheckResultDao.update(connectivityVO.getId(), connectivityVO);
    }
}
Also used : RouterHealthCheckResultVO(com.cloud.network.dao.RouterHealthCheckResultVO) Date(java.util.Date)

Example 2 with RouterHealthCheckResultVO

use of com.cloud.network.dao.RouterHealthCheckResultVO in project cloudstack by apache.

the class VirtualNetworkApplianceManagerImpl method getHealthChecksFromDb.

private Map<String, Map<String, RouterHealthCheckResultVO>> getHealthChecksFromDb(long routerId) {
    List<RouterHealthCheckResultVO> healthChecksList = routerHealthCheckResultDao.getHealthCheckResults(routerId);
    Map<String, Map<String, RouterHealthCheckResultVO>> healthCheckResults = new HashMap<>();
    if (healthChecksList.isEmpty()) {
        return healthCheckResults;
    }
    for (RouterHealthCheckResultVO healthCheck : healthChecksList) {
        if (!healthCheckResults.containsKey(healthCheck.getCheckType())) {
            healthCheckResults.put(healthCheck.getCheckType(), new HashMap<>());
        }
        healthCheckResults.get(healthCheck.getCheckType()).put(healthCheck.getCheckName(), healthCheck);
    }
    return healthCheckResults;
}
Also used : HashMap(java.util.HashMap) RouterHealthCheckResultVO(com.cloud.network.dao.RouterHealthCheckResultVO) Map(java.util.Map) HashMap(java.util.HashMap)

Example 3 with RouterHealthCheckResultVO

use of com.cloud.network.dao.RouterHealthCheckResultVO in project cloudstack by apache.

the class QueryManagerImpl method searchForRoutersInternal.

private Pair<List<DomainRouterJoinVO>, Integer> searchForRoutersInternal(BaseListProjectAndAccountResourcesCmd cmd, Long id, String name, String state, Long zoneId, Long podId, Long clusterId, Long hostId, String keyword, Long networkId, Long vpcId, Boolean forVpc, String role, String version, Boolean isHealthCheckFailed) {
    Account caller = CallContext.current().getCallingAccount();
    List<Long> permittedAccounts = new ArrayList<Long>();
    Ternary<Long, Boolean, ListProjectResourcesCriteria> domainIdRecursiveListProject = new Ternary<Long, Boolean, ListProjectResourcesCriteria>(cmd.getDomainId(), cmd.isRecursive(), null);
    _accountMgr.buildACLSearchParameters(caller, id, cmd.getAccountName(), cmd.getProjectId(), permittedAccounts, domainIdRecursiveListProject, cmd.listAll(), false);
    Long domainId = domainIdRecursiveListProject.first();
    Boolean isRecursive = domainIdRecursiveListProject.second();
    ListProjectResourcesCriteria listProjectResourcesCriteria = domainIdRecursiveListProject.third();
    Filter searchFilter = new Filter(DomainRouterJoinVO.class, "id", true, cmd.getStartIndex(), cmd.getPageSizeVal());
    SearchBuilder<DomainRouterJoinVO> sb = _routerJoinDao.createSearchBuilder();
    // select distinct
    sb.select(null, Func.DISTINCT, sb.entity().getId());
    // ids to get
    // number of
    // records with
    // pagination
    _accountMgr.buildACLViewSearchBuilder(sb, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
    sb.and("name", sb.entity().getInstanceName(), SearchCriteria.Op.LIKE);
    sb.and("id", sb.entity().getId(), SearchCriteria.Op.EQ);
    sb.and("accountId", sb.entity().getAccountId(), SearchCriteria.Op.IN);
    sb.and("state", sb.entity().getState(), SearchCriteria.Op.EQ);
    sb.and("dataCenterId", sb.entity().getDataCenterId(), SearchCriteria.Op.EQ);
    sb.and("podId", sb.entity().getPodId(), SearchCriteria.Op.EQ);
    sb.and("clusterId", sb.entity().getClusterId(), SearchCriteria.Op.EQ);
    sb.and("hostId", sb.entity().getHostId(), SearchCriteria.Op.EQ);
    sb.and("vpcId", sb.entity().getVpcId(), SearchCriteria.Op.EQ);
    sb.and("role", sb.entity().getRole(), SearchCriteria.Op.EQ);
    sb.and("version", sb.entity().getTemplateVersion(), SearchCriteria.Op.LIKE);
    if (forVpc != null) {
        if (forVpc) {
            sb.and("forVpc", sb.entity().getVpcId(), SearchCriteria.Op.NNULL);
        } else {
            sb.and("forVpc", sb.entity().getVpcId(), SearchCriteria.Op.NULL);
        }
    }
    if (networkId != null) {
        sb.and("networkId", sb.entity().getNetworkId(), SearchCriteria.Op.EQ);
    }
    List<Long> routersWithFailures = null;
    if (isHealthCheckFailed != null) {
        GenericSearchBuilder<RouterHealthCheckResultVO, Long> routerHealthCheckResultSearch = routerHealthCheckResultDao.createSearchBuilder(Long.class);
        routerHealthCheckResultSearch.and("checkResult", routerHealthCheckResultSearch.entity().getCheckResult(), SearchCriteria.Op.EQ);
        routerHealthCheckResultSearch.selectFields(routerHealthCheckResultSearch.entity().getRouterId());
        routerHealthCheckResultSearch.done();
        SearchCriteria<Long> ssc = routerHealthCheckResultSearch.create();
        ssc.setParameters("checkResult", false);
        routersWithFailures = routerHealthCheckResultDao.customSearch(ssc, null);
        if (routersWithFailures != null && !routersWithFailures.isEmpty()) {
            if (isHealthCheckFailed) {
                sb.and("routerId", sb.entity().getId(), SearchCriteria.Op.IN);
            } else {
                sb.and("routerId", sb.entity().getId(), SearchCriteria.Op.NIN);
            }
        } else if (isHealthCheckFailed) {
            return new Pair<List<DomainRouterJoinVO>, Integer>(Collections.emptyList(), 0);
        }
    }
    SearchCriteria<DomainRouterJoinVO> sc = sb.create();
    _accountMgr.buildACLViewSearchCriteria(sc, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
    if (keyword != null) {
        SearchCriteria<DomainRouterJoinVO> ssc = _routerJoinDao.createSearchCriteria();
        ssc.addOr("name", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        ssc.addOr("instanceName", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        ssc.addOr("state", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        ssc.addOr("networkName", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        ssc.addOr("vpcName", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        ssc.addOr("redundantState", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        sc.addAnd("instanceName", SearchCriteria.Op.SC, ssc);
    }
    if (name != null) {
        sc.setParameters("name", "%" + name + "%");
    }
    if (id != null) {
        sc.setParameters("id", id);
    }
    if (state != null) {
        sc.setParameters("state", state);
    }
    if (zoneId != null) {
        sc.setParameters("dataCenterId", zoneId);
    }
    if (podId != null) {
        sc.setParameters("podId", podId);
    }
    if (clusterId != null) {
        sc.setParameters("clusterId", clusterId);
    }
    if (hostId != null) {
        sc.setParameters("hostId", hostId);
    }
    if (networkId != null) {
        sc.setParameters("networkId", networkId);
    }
    if (vpcId != null) {
        sc.setParameters("vpcId", vpcId);
    }
    if (role != null) {
        sc.setParameters("role", role);
    }
    if (version != null) {
        sc.setParameters("version", "Cloudstack Release " + version + "%");
    }
    if (routersWithFailures != null && !routersWithFailures.isEmpty()) {
        sc.setParameters("routerId", routersWithFailures.toArray(new Object[routersWithFailures.size()]));
    }
    // search VR details by ids
    Pair<List<DomainRouterJoinVO>, Integer> uniqueVrPair = _routerJoinDao.searchAndCount(sc, searchFilter);
    Integer count = uniqueVrPair.second();
    if (count.intValue() == 0) {
        // empty result
        return uniqueVrPair;
    }
    List<DomainRouterJoinVO> uniqueVrs = uniqueVrPair.first();
    Long[] vrIds = new Long[uniqueVrs.size()];
    int i = 0;
    for (DomainRouterJoinVO v : uniqueVrs) {
        vrIds[i++] = v.getId();
    }
    List<DomainRouterJoinVO> vrs = _routerJoinDao.searchByIds(vrIds);
    return new Pair<List<DomainRouterJoinVO>, Integer>(vrs, count);
}
Also used : Account(com.cloud.user.Account) Ternary(com.cloud.utils.Ternary) ArrayList(java.util.ArrayList) DomainRouterJoinVO(com.cloud.api.query.vo.DomainRouterJoinVO) ListProjectResourcesCriteria(com.cloud.projects.Project.ListProjectResourcesCriteria) TemplateFilter(com.cloud.template.VirtualMachineTemplate.TemplateFilter) Filter(com.cloud.utils.db.Filter) ArrayList(java.util.ArrayList) List(java.util.List) RouterHealthCheckResultVO(com.cloud.network.dao.RouterHealthCheckResultVO) Pair(com.cloud.utils.Pair)

Example 4 with RouterHealthCheckResultVO

use of com.cloud.network.dao.RouterHealthCheckResultVO in project cloudstack by apache.

the class VirtualNetworkApplianceManagerImpl method parseHealthCheckVOFromJson.

private RouterHealthCheckResultVO parseHealthCheckVOFromJson(final long routerId, final String checkName, final String checkType, final Map<String, String> checkData, final Map<String, Map<String, RouterHealthCheckResultVO>> checksInDb) {
    boolean success = Boolean.parseBoolean(checkData.get("success"));
    Date lastUpdate = new Date(Long.parseLong(checkData.get("lastUpdate")));
    double lastRunDuration = Double.parseDouble(checkData.get("lastRunDuration"));
    String message = checkData.get("message");
    final RouterHealthCheckResultVO hcVo;
    boolean newEntry = false;
    if (checksInDb.containsKey(checkType) && checksInDb.get(checkType).containsKey(checkName)) {
        hcVo = checksInDb.get(checkType).get(checkName);
    } else {
        hcVo = new RouterHealthCheckResultVO(routerId, checkName, checkType);
        newEntry = true;
    }
    hcVo.setCheckResult(success);
    hcVo.setLastUpdateTime(lastUpdate);
    if (StringUtils.isNotEmpty(message)) {
        hcVo.setCheckDetails(message.getBytes(com.cloud.utils.StringUtils.getPreferredCharset()));
    }
    if (newEntry) {
        routerHealthCheckResultDao.persist(hcVo);
    } else {
        routerHealthCheckResultDao.update(hcVo.getId(), hcVo);
    }
    s_logger.info("Found health check " + hcVo + " which took running duration (ms) " + lastRunDuration);
    return hcVo;
}
Also used : RouterHealthCheckResultVO(com.cloud.network.dao.RouterHealthCheckResultVO) Date(java.util.Date)

Example 5 with RouterHealthCheckResultVO

use of com.cloud.network.dao.RouterHealthCheckResultVO in project cloudstack by apache.

the class VirtualNetworkApplianceManagerImpl method parseHealthCheckResults.

/**
 * @param checksJson JSON expected is
 *                   {
 *                      checkType1: {
 *                          checkName1: {
 *                              success: true/false,
 *                              lastUpdate: date string,
 *                              lastRunDuration: ms spent on test,
 *                              message: detailed message from check execution
 *                          },
 *                          checkType2: .....
 *                      },
 *                      checkType2: ......
 *                   }
 * @return converts the above JSON into list of RouterHealthCheckResult.
 */
private List<RouterHealthCheckResult> parseHealthCheckResults(final Map<String, Map<String, Map<String, String>>> checksJson, final long routerId) {
    final Map<String, Map<String, RouterHealthCheckResultVO>> checksInDb = getHealthChecksFromDb(routerId);
    List<RouterHealthCheckResult> healthChecks = new ArrayList<>();
    final String lastRunKey = "lastRun";
    for (String checkType : checksJson.keySet()) {
        if (checksJson.get(checkType).containsKey(lastRunKey)) {
            // Log last run of this check type run info
            Map<String, String> lastRun = checksJson.get(checkType).get(lastRunKey);
            s_logger.info("Found check types executed on VR " + checkType + ", start: " + lastRun.get("start") + ", end: " + lastRun.get("end") + ", duration: " + lastRun.get("duration"));
        }
        for (String checkName : checksJson.get(checkType).keySet()) {
            if (lastRunKey.equals(checkName)) {
                continue;
            }
            try {
                final RouterHealthCheckResultVO hcVo = parseHealthCheckVOFromJson(routerId, checkName, checkType, checksJson.get(checkType).get(checkName), checksInDb);
                healthChecks.add(hcVo);
            } catch (Exception ex) {
                s_logger.error("Skipping health check: Exception while parsing check result data for router id " + routerId + ", check type: " + checkType + ", check name: " + checkName + ":" + ex.getLocalizedMessage(), ex);
            }
        }
    }
    return healthChecks;
}
Also used : ArrayList(java.util.ArrayList) RouterHealthCheckResultVO(com.cloud.network.dao.RouterHealthCheckResultVO) Map(java.util.Map) HashMap(java.util.HashMap) ConnectionException(com.cloud.exception.ConnectionException) AgentUnavailableException(com.cloud.exception.AgentUnavailableException) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) JsonSyntaxException(com.google.gson.JsonSyntaxException) OperationTimedoutException(com.cloud.exception.OperationTimedoutException) InsufficientCapacityException(com.cloud.exception.InsufficientCapacityException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ParseException(java.text.ParseException) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) ConfigurationException(javax.naming.ConfigurationException) RouterHealthCheckResult(com.cloud.network.RouterHealthCheckResult)

Aggregations

RouterHealthCheckResultVO (com.cloud.network.dao.RouterHealthCheckResultVO)5 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 DomainRouterJoinVO (com.cloud.api.query.vo.DomainRouterJoinVO)1 AgentUnavailableException (com.cloud.exception.AgentUnavailableException)1 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)1 ConnectionException (com.cloud.exception.ConnectionException)1 InsufficientCapacityException (com.cloud.exception.InsufficientCapacityException)1 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)1 OperationTimedoutException (com.cloud.exception.OperationTimedoutException)1 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)1 RouterHealthCheckResult (com.cloud.network.RouterHealthCheckResult)1 ListProjectResourcesCriteria (com.cloud.projects.Project.ListProjectResourcesCriteria)1 TemplateFilter (com.cloud.template.VirtualMachineTemplate.TemplateFilter)1 Account (com.cloud.user.Account)1 Pair (com.cloud.utils.Pair)1 Ternary (com.cloud.utils.Ternary)1 Filter (com.cloud.utils.db.Filter)1