Search in sources :

Example 6 with Monitor

use of com.usthe.common.entity.manager.Monitor in project hertzbeat by dromara.

the class MonitorServiceImpl method modifyMonitor.

@Override
@Transactional(rollbackFor = Exception.class)
public void modifyMonitor(Monitor monitor, List<Param> params) throws RuntimeException {
    long monitorId = monitor.getId();
    // Check to determine whether the monitor corresponding to the monitor id exists
    // 查判断monitorId对应的此监控是否存在
    Optional<Monitor> queryOption = monitorDao.findById(monitorId);
    if (!queryOption.isPresent()) {
        throw new IllegalArgumentException("The Monitor " + monitorId + " not exists");
    }
    Monitor preMonitor = queryOption.get();
    if (!preMonitor.getApp().equals(monitor.getApp())) {
        // 监控的类型不能修改
        throw new IllegalArgumentException("Can not modify monitor's app type");
    }
    // Auto Update Default Tags: monitorName
    List<Tag> tags = monitor.getTags();
    if (tags == null) {
        tags = new LinkedList<>();
        monitor.setTags(tags);
    }
    for (Tag tag : tags) {
        if (CommonConstants.TAG_MONITOR_NAME.equals(tag.getName())) {
            tag.setValue(monitor.getName());
        }
    }
    // Construct the collection task Job entity
    // 构造采集任务Job实体
    Job appDefine = appService.getAppDefine(monitor.getApp());
    appDefine.setId(preMonitor.getJobId());
    appDefine.setMonitorId(monitorId);
    appDefine.setInterval(monitor.getIntervals());
    appDefine.setCyclic(true);
    appDefine.setTimestamp(System.currentTimeMillis());
    List<Configmap> configmaps = params.stream().map(param -> new Configmap(param.getField(), param.getValue(), param.getType())).collect(Collectors.toList());
    appDefine.setConfigmap(configmaps);
    // 下发更新成功后刷库
    try {
        monitor.setJobId(preMonitor.getJobId());
        monitor.setStatus(preMonitor.getStatus());
        monitorDao.save(monitor);
        paramDao.saveAll(params);
        // Update the collection task after the storage is completed
        // 入库完成后更新采集任务
        collectJobService.updateAsyncCollectJob(appDefine);
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        throw new MonitorDatabaseException(e.getMessage());
    }
}
Also used : java.util(java.util) CommonConstants(com.usthe.common.util.CommonConstants) MonitorDao(com.usthe.manager.dao.MonitorDao) ParamDefine(com.usthe.common.entity.manager.ParamDefine) MonitorDatabaseException(com.usthe.manager.support.exception.MonitorDatabaseException) Configmap(com.usthe.common.entity.job.Configmap) Autowired(org.springframework.beans.factory.annotation.Autowired) Param(com.usthe.common.entity.manager.Param) IntervalExpressionUtil(com.usthe.common.util.IntervalExpressionUtil) MonitorService(com.usthe.manager.service.MonitorService) Job(com.usthe.common.entity.job.Job) AppCount(com.usthe.manager.pojo.dto.AppCount) Tag(com.usthe.common.entity.manager.Tag) Service(org.springframework.stereotype.Service) MonitorDto(com.usthe.manager.pojo.dto.MonitorDto) CollectRep(com.usthe.common.entity.message.CollectRep) ParamDao(com.usthe.manager.dao.ParamDao) Monitor(com.usthe.common.entity.manager.Monitor) PageRequest(org.springframework.data.domain.PageRequest) Page(org.springframework.data.domain.Page) Collectors(java.util.stream.Collectors) IpDomainUtil(com.usthe.common.util.IpDomainUtil) Slf4j(lombok.extern.slf4j.Slf4j) AlertDefineBindDao(com.usthe.alert.dao.AlertDefineBindDao) SnowFlakeIdGenerator(com.usthe.common.util.SnowFlakeIdGenerator) Specification(org.springframework.data.jpa.domain.Specification) AppService(com.usthe.manager.service.AppService) AesUtil(com.usthe.common.util.AesUtil) CollectJobService(com.usthe.collector.dispatch.entrance.internal.CollectJobService) MonitorDetectException(com.usthe.manager.support.exception.MonitorDetectException) Metrics(com.usthe.common.entity.job.Metrics) Transactional(org.springframework.transaction.annotation.Transactional) Configmap(com.usthe.common.entity.job.Configmap) MonitorDatabaseException(com.usthe.manager.support.exception.MonitorDatabaseException) MonitorDetectException(com.usthe.manager.support.exception.MonitorDetectException) Monitor(com.usthe.common.entity.manager.Monitor) MonitorDatabaseException(com.usthe.manager.support.exception.MonitorDatabaseException) Tag(com.usthe.common.entity.manager.Tag) Job(com.usthe.common.entity.job.Job) Transactional(org.springframework.transaction.annotation.Transactional)

Example 7 with Monitor

use of com.usthe.common.entity.manager.Monitor in project hertzbeat by dromara.

the class MonitorServiceImpl method getAllAppMonitorsCount.

@Override
public List<AppCount> getAllAppMonitorsCount() {
    List<AppCount> appCounts = monitorDao.findAppsStatusCount();
    if (appCounts == null) {
        return null;
    }
    // Statistical category information, calculate the number of corresponding states for each monitor
    // 统计类别信息,计算每个监控分别对应状态的数量
    Map<String, AppCount> appCountMap = new HashMap<>(appCounts.size());
    for (AppCount item : appCounts) {
        AppCount appCount = appCountMap.getOrDefault(item.getApp(), new AppCount());
        appCount.setApp(item.getApp());
        switch(item.getStatus()) {
            case CommonConstants.AVAILABLE_CODE:
                appCount.setAvailableSize(appCount.getAvailableSize() + item.getSize());
                break;
            case CommonConstants.UN_AVAILABLE_CODE:
                appCount.setUnAvailableSize(appCount.getUnAvailableSize() + item.getSize());
                break;
            case CommonConstants.UN_MANAGE_CODE:
                appCount.setUnManageSize(appCount.getUnManageSize() + item.getSize());
                break;
            case CommonConstants.UN_REACHABLE_CODE:
                appCount.setUnReachableSize(appCount.getUnReachableSize() + item.getSize());
                break;
            default:
                break;
        }
        appCountMap.put(item.getApp(), appCount);
    }
    // 遍历统计得到的map,转换成List<App Count>结果集
    return appCountMap.values().stream().peek(item -> {
        item.setSize(item.getAvailableSize() + item.getUnManageSize() + item.getUnReachableSize() + item.getUnAvailableSize());
        Job job = appService.getAppDefine(item.getApp());
        if (job != null) {
            item.setCategory(job.getCategory());
        }
    }).collect(Collectors.toList());
}
Also used : java.util(java.util) CommonConstants(com.usthe.common.util.CommonConstants) MonitorDao(com.usthe.manager.dao.MonitorDao) ParamDefine(com.usthe.common.entity.manager.ParamDefine) MonitorDatabaseException(com.usthe.manager.support.exception.MonitorDatabaseException) Configmap(com.usthe.common.entity.job.Configmap) Autowired(org.springframework.beans.factory.annotation.Autowired) Param(com.usthe.common.entity.manager.Param) IntervalExpressionUtil(com.usthe.common.util.IntervalExpressionUtil) MonitorService(com.usthe.manager.service.MonitorService) Job(com.usthe.common.entity.job.Job) AppCount(com.usthe.manager.pojo.dto.AppCount) Tag(com.usthe.common.entity.manager.Tag) Service(org.springframework.stereotype.Service) MonitorDto(com.usthe.manager.pojo.dto.MonitorDto) CollectRep(com.usthe.common.entity.message.CollectRep) ParamDao(com.usthe.manager.dao.ParamDao) Monitor(com.usthe.common.entity.manager.Monitor) PageRequest(org.springframework.data.domain.PageRequest) Page(org.springframework.data.domain.Page) Collectors(java.util.stream.Collectors) IpDomainUtil(com.usthe.common.util.IpDomainUtil) Slf4j(lombok.extern.slf4j.Slf4j) AlertDefineBindDao(com.usthe.alert.dao.AlertDefineBindDao) SnowFlakeIdGenerator(com.usthe.common.util.SnowFlakeIdGenerator) Specification(org.springframework.data.jpa.domain.Specification) AppService(com.usthe.manager.service.AppService) AesUtil(com.usthe.common.util.AesUtil) CollectJobService(com.usthe.collector.dispatch.entrance.internal.CollectJobService) MonitorDetectException(com.usthe.manager.support.exception.MonitorDetectException) Metrics(com.usthe.common.entity.job.Metrics) Transactional(org.springframework.transaction.annotation.Transactional) AppCount(com.usthe.manager.pojo.dto.AppCount) Job(com.usthe.common.entity.job.Job)

Example 8 with Monitor

use of com.usthe.common.entity.manager.Monitor in project hertzbeat by dromara.

the class MonitorServiceImpl method addMonitor.

@Override
@Transactional(rollbackFor = Exception.class)
public void addMonitor(Monitor monitor, List<Param> params) throws RuntimeException {
    // Apply for monitor id         申请 monitor id
    long monitorId = SnowFlakeIdGenerator.generateId();
    // Init Set Default Tags: monitorId monitorName app
    List<Tag> tags = monitor.getTags();
    if (tags == null) {
        tags = new LinkedList<>();
        monitor.setTags(tags);
    }
    tags.add(Tag.builder().name(CommonConstants.TAG_MONITOR_ID).value(String.valueOf(monitorId)).type((byte) 0).build());
    tags.add(Tag.builder().name(CommonConstants.TAG_MONITOR_NAME).value(String.valueOf(monitor.getName())).type((byte) 0).build());
    // Construct the collection task Job entity     构造采集任务Job实体
    Job appDefine = appService.getAppDefine(monitor.getApp());
    appDefine.setMonitorId(monitorId);
    appDefine.setInterval(monitor.getIntervals());
    appDefine.setCyclic(true);
    appDefine.setTimestamp(System.currentTimeMillis());
    List<Configmap> configmaps = params.stream().map(param -> {
        param.setMonitorId(monitorId);
        return new Configmap(param.getField(), param.getValue(), param.getType());
    }).collect(Collectors.toList());
    appDefine.setConfigmap(configmaps);
    // Send the collection task to get the job ID
    // 下发采集任务得到jobId
    long jobId = collectJobService.addAsyncCollectJob(appDefine);
    // 下发成功后刷库
    try {
        monitor.setId(monitorId);
        monitor.setJobId(jobId);
        monitor.setStatus(CommonConstants.AVAILABLE_CODE);
        monitorDao.save(monitor);
        paramDao.saveAll(params);
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        // Repository brushing abnormally cancels the previously delivered task
        // 刷库异常取消之前的下发任务
        collectJobService.cancelAsyncCollectJob(jobId);
        throw new MonitorDatabaseException(e.getMessage());
    }
}
Also used : java.util(java.util) CommonConstants(com.usthe.common.util.CommonConstants) MonitorDao(com.usthe.manager.dao.MonitorDao) ParamDefine(com.usthe.common.entity.manager.ParamDefine) MonitorDatabaseException(com.usthe.manager.support.exception.MonitorDatabaseException) Configmap(com.usthe.common.entity.job.Configmap) Autowired(org.springframework.beans.factory.annotation.Autowired) Param(com.usthe.common.entity.manager.Param) IntervalExpressionUtil(com.usthe.common.util.IntervalExpressionUtil) MonitorService(com.usthe.manager.service.MonitorService) Job(com.usthe.common.entity.job.Job) AppCount(com.usthe.manager.pojo.dto.AppCount) Tag(com.usthe.common.entity.manager.Tag) Service(org.springframework.stereotype.Service) MonitorDto(com.usthe.manager.pojo.dto.MonitorDto) CollectRep(com.usthe.common.entity.message.CollectRep) ParamDao(com.usthe.manager.dao.ParamDao) Monitor(com.usthe.common.entity.manager.Monitor) PageRequest(org.springframework.data.domain.PageRequest) Page(org.springframework.data.domain.Page) Collectors(java.util.stream.Collectors) IpDomainUtil(com.usthe.common.util.IpDomainUtil) Slf4j(lombok.extern.slf4j.Slf4j) AlertDefineBindDao(com.usthe.alert.dao.AlertDefineBindDao) SnowFlakeIdGenerator(com.usthe.common.util.SnowFlakeIdGenerator) Specification(org.springframework.data.jpa.domain.Specification) AppService(com.usthe.manager.service.AppService) AesUtil(com.usthe.common.util.AesUtil) CollectJobService(com.usthe.collector.dispatch.entrance.internal.CollectJobService) MonitorDetectException(com.usthe.manager.support.exception.MonitorDetectException) Metrics(com.usthe.common.entity.job.Metrics) Transactional(org.springframework.transaction.annotation.Transactional) Configmap(com.usthe.common.entity.job.Configmap) MonitorDatabaseException(com.usthe.manager.support.exception.MonitorDatabaseException) Tag(com.usthe.common.entity.manager.Tag) Job(com.usthe.common.entity.job.Job) MonitorDatabaseException(com.usthe.manager.support.exception.MonitorDatabaseException) MonitorDetectException(com.usthe.manager.support.exception.MonitorDetectException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 9 with Monitor

use of com.usthe.common.entity.manager.Monitor in project hertzbeat by dromara.

the class MonitorServiceImpl method enableManageMonitors.

@Override
public void enableManageMonitors(HashSet<Long> ids) {
    // Update monitoring status Add corresponding monitoring periodic task
    // 更新监控状态 新增对应的监控周期性任务
    List<Monitor> unManagedMonitors = monitorDao.findMonitorsByIdIn(ids).stream().filter(monitor -> monitor.getStatus() == CommonConstants.UN_MANAGE_CODE && monitor.getJobId() != null).peek(monitor -> monitor.setStatus(CommonConstants.AVAILABLE_CODE)).collect(Collectors.toList());
    if (!unManagedMonitors.isEmpty()) {
        monitorDao.saveAll(unManagedMonitors);
        for (Monitor monitor : unManagedMonitors) {
            // Construct the collection task Job entity
            // 构造采集任务Job实体
            Job appDefine = appService.getAppDefine(monitor.getApp());
            appDefine.setMonitorId(monitor.getId());
            appDefine.setInterval(monitor.getIntervals());
            appDefine.setCyclic(true);
            appDefine.setTimestamp(System.currentTimeMillis());
            List<Param> params = paramDao.findParamsByMonitorId(monitor.getId());
            List<Configmap> configmaps = params.stream().map(param -> new Configmap(param.getField(), param.getValue(), param.getType())).collect(Collectors.toList());
            appDefine.setConfigmap(configmaps);
            // Issue collection tasks       下发采集任务
            collectJobService.addAsyncCollectJob(appDefine);
        }
    }
}
Also used : java.util(java.util) CommonConstants(com.usthe.common.util.CommonConstants) MonitorDao(com.usthe.manager.dao.MonitorDao) ParamDefine(com.usthe.common.entity.manager.ParamDefine) MonitorDatabaseException(com.usthe.manager.support.exception.MonitorDatabaseException) Configmap(com.usthe.common.entity.job.Configmap) Autowired(org.springframework.beans.factory.annotation.Autowired) Param(com.usthe.common.entity.manager.Param) IntervalExpressionUtil(com.usthe.common.util.IntervalExpressionUtil) MonitorService(com.usthe.manager.service.MonitorService) Job(com.usthe.common.entity.job.Job) AppCount(com.usthe.manager.pojo.dto.AppCount) Tag(com.usthe.common.entity.manager.Tag) Service(org.springframework.stereotype.Service) MonitorDto(com.usthe.manager.pojo.dto.MonitorDto) CollectRep(com.usthe.common.entity.message.CollectRep) ParamDao(com.usthe.manager.dao.ParamDao) Monitor(com.usthe.common.entity.manager.Monitor) PageRequest(org.springframework.data.domain.PageRequest) Page(org.springframework.data.domain.Page) Collectors(java.util.stream.Collectors) IpDomainUtil(com.usthe.common.util.IpDomainUtil) Slf4j(lombok.extern.slf4j.Slf4j) AlertDefineBindDao(com.usthe.alert.dao.AlertDefineBindDao) SnowFlakeIdGenerator(com.usthe.common.util.SnowFlakeIdGenerator) Specification(org.springframework.data.jpa.domain.Specification) AppService(com.usthe.manager.service.AppService) AesUtil(com.usthe.common.util.AesUtil) CollectJobService(com.usthe.collector.dispatch.entrance.internal.CollectJobService) MonitorDetectException(com.usthe.manager.support.exception.MonitorDetectException) Metrics(com.usthe.common.entity.job.Metrics) Transactional(org.springframework.transaction.annotation.Transactional) Monitor(com.usthe.common.entity.manager.Monitor) Configmap(com.usthe.common.entity.job.Configmap) Param(com.usthe.common.entity.manager.Param) Job(com.usthe.common.entity.job.Job)

Example 10 with Monitor

use of com.usthe.common.entity.manager.Monitor in project hertzbeat by dromara.

the class MonitorServiceImpl method getMonitorDto.

@Override
@Transactional(readOnly = true)
public MonitorDto getMonitorDto(long id) throws RuntimeException {
    Optional<Monitor> monitorOptional = monitorDao.findById(id);
    if (monitorOptional.isPresent()) {
        Monitor monitor = monitorOptional.get();
        MonitorDto monitorDto = new MonitorDto();
        monitorDto.setMonitor(monitor);
        List<Param> params = paramDao.findParamsByMonitorId(id);
        monitorDto.setParams(params);
        Job job = appService.getAppDefine(monitor.getApp());
        List<String> metrics = job.getMetrics().stream().map(Metrics::getName).collect(Collectors.toList());
        monitorDto.setMetrics(metrics);
        return monitorDto;
    } else {
        return null;
    }
}
Also used : MonitorDto(com.usthe.manager.pojo.dto.MonitorDto) Monitor(com.usthe.common.entity.manager.Monitor) Param(com.usthe.common.entity.manager.Param) Job(com.usthe.common.entity.job.Job) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

Monitor (com.usthe.common.entity.manager.Monitor)13 Transactional (org.springframework.transaction.annotation.Transactional)9 Job (com.usthe.common.entity.job.Job)8 Param (com.usthe.common.entity.manager.Param)8 Autowired (org.springframework.beans.factory.annotation.Autowired)8 CollectJobService (com.usthe.collector.dispatch.entrance.internal.CollectJobService)7 Configmap (com.usthe.common.entity.job.Configmap)7 MonitorDao (com.usthe.manager.dao.MonitorDao)7 ParamDao (com.usthe.manager.dao.ParamDao)7 MonitorDto (com.usthe.manager.pojo.dto.MonitorDto)7 MonitorService (com.usthe.manager.service.MonitorService)7 Collectors (java.util.stream.Collectors)7 Slf4j (lombok.extern.slf4j.Slf4j)7 Page (org.springframework.data.domain.Page)7 PageRequest (org.springframework.data.domain.PageRequest)7 Specification (org.springframework.data.jpa.domain.Specification)7 Service (org.springframework.stereotype.Service)7 AlertDefineBindDao (com.usthe.alert.dao.AlertDefineBindDao)6 Metrics (com.usthe.common.entity.job.Metrics)6 ParamDefine (com.usthe.common.entity.manager.ParamDefine)6