Search in sources :

Example 11 with Monitor

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

the class MonitorServiceImpl method deleteMonitors.

@Override
@Transactional(rollbackFor = Exception.class)
public void deleteMonitors(Set<Long> ids) throws RuntimeException {
    List<Monitor> monitors = monitorDao.findMonitorsByIdIn(ids);
    if (monitors != null) {
        monitorDao.deleteAll(monitors);
        paramDao.deleteParamsByMonitorIdIn(ids);
        alertDefineBindDao.deleteAlertDefineMonitorBindsByMonitorIdIn(monitors.stream().map(Monitor::getId).collect(Collectors.toList()));
        for (Monitor monitor : monitors) {
            collectJobService.cancelAsyncCollectJob(monitor.getJobId());
        }
    }
}
Also used : Monitor(com.usthe.common.entity.manager.Monitor) Transactional(org.springframework.transaction.annotation.Transactional)

Example 12 with Monitor

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

the class DbAlertStoreHandlerImpl method store.

@Override
public void store(Alert alert) {
    // todo Using the cache does not directly manipulate the library    使用缓存不直接操作库
    Map<String, String> tags = alert.getTags();
    String monitorIdStr = tags.get(CommonConstants.TAG_MONITOR_ID);
    if (monitorIdStr == null) {
        log.error("alert tags monitorId is null.");
        return;
    }
    long monitorId = Long.parseLong(monitorIdStr);
    Monitor monitor = monitorService.getMonitor(monitorId);
    if (monitor == null) {
        log.warn("Dispatch alarm the monitorId: {} not existed, ignored.", monitorId);
        return;
    }
    if (monitor.getTags() != null) {
        monitor.getTags().forEach(item -> {
            tags.put(item.getName(), item.getValue());
        });
    }
    if (!tags.containsKey(CommonConstants.TAG_MONITOR_NAME)) {
        tags.put(CommonConstants.TAG_MONITOR_NAME, monitor.getName());
    }
    if (monitor.getStatus() == CommonConstants.UN_MANAGE_CODE) {
        // 当监控未管理时  忽略静默其告警信息
        return;
    }
    if (monitor.getStatus() == CommonConstants.AVAILABLE_CODE) {
        if (CommonConstants.AVAILABLE.equals(alert.getTarget())) {
            // Availability Alarm Need to change the monitoring status to unavailable
            // 可用性告警 需变更监控状态为不可用
            monitorService.updateMonitorStatus(monitor.getId(), CommonConstants.UN_AVAILABLE_CODE);
        } else if (CommonConstants.REACHABLE.equals(alert.getTarget())) {
            // Reachability alarm The monitoring status needs to be changed to unreachable
            // 可达性告警 需变更监控状态为不可达
            monitorService.updateMonitorStatus(monitor.getId(), CommonConstants.UN_REACHABLE_CODE);
        }
    } else {
        // 若是恢复告警 需对监控状态进行恢复
        if (alert.getStatus() == CommonConstants.ALERT_STATUS_CODE_RESTORED) {
            monitorService.updateMonitorStatus(monitorId, CommonConstants.AVAILABLE_CODE);
        }
    }
    // Alarm drop library  告警落库
    alertService.addAlert(alert);
}
Also used : Monitor(com.usthe.common.entity.manager.Monitor)

Example 13 with Monitor

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

the class JobSchedulerInit method run.

@Override
public void run(String... args) throws Exception {
    // 读取数据库已经添加应用 构造采集任务
    List<Monitor> monitors = monitorDao.findMonitorsByStatusNotInAndAndJobIdNotNull(Arrays.asList((byte) 0, (byte) 4));
    for (Monitor monitor : monitors) {
        try {
            // 构造采集任务Job实体
            Job appDefine = appService.getAppDefine(monitor.getApp());
            // todo 这里暂时是深拷贝处理
            appDefine = GsonUtil.fromJson(GsonUtil.toJson(appDefine), Job.class);
            appDefine.setId(monitor.getJobId());
            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);
            // 下发采集任务
            collectJobService.addAsyncCollectJob(appDefine);
        } catch (Exception e) {
            log.error("init monitor job: {} error,continue next monitor", monitor, e);
        }
    }
}
Also used : ParamDao(com.usthe.manager.dao.ParamDao) Order(org.springframework.core.annotation.Order) Arrays(java.util.Arrays) MonitorDao(com.usthe.manager.dao.MonitorDao) GsonUtil(com.usthe.common.util.GsonUtil) Configmap(com.usthe.common.entity.job.Configmap) Monitor(com.usthe.common.entity.manager.Monitor) Autowired(org.springframework.beans.factory.annotation.Autowired) Param(com.usthe.common.entity.manager.Param) Collectors(java.util.stream.Collectors) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) Job(com.usthe.common.entity.job.Job) Service(org.springframework.stereotype.Service) CommandLineRunner(org.springframework.boot.CommandLineRunner) CollectJobService(com.usthe.collector.dispatch.entrance.internal.CollectJobService) 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)

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