Search in sources :

Example 1 with Configmap

use of com.usthe.common.entity.job.Configmap in project hertzbeat by dromara.

the class MonitorServiceImpl method detectMonitor.

@Override
@Transactional(readOnly = true)
public void detectMonitor(Monitor monitor, List<Param> params) throws MonitorDetectException {
    Long monitorId = monitor.getId();
    if (monitorId == null || monitorId == 0) {
        monitorId = MONITOR_ID_TMP;
    }
    Job appDefine = appService.getAppDefine(monitor.getApp());
    appDefine.setMonitorId(monitorId);
    appDefine.setCyclic(false);
    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);
    // To detect availability, you only need to collect the set of availability indicators with a priority of 0.
    // 探测可用性只需要采集优先级为0的可用性指标集合
    List<Metrics> availableMetrics = appDefine.getMetrics().stream().filter(item -> item.getPriority() == 0).collect(Collectors.toList());
    appDefine.setMetrics(availableMetrics);
    List<CollectRep.MetricsData> collectRep = collectJobService.collectSyncJobData(appDefine);
    // 判断探测结果 失败则抛出探测异常
    if (collectRep == null || collectRep.isEmpty()) {
        throw new MonitorDetectException("No collector response");
    }
    if (collectRep.get(0).getCode() != CollectRep.Code.SUCCESS) {
        throw new MonitorDetectException(collectRep.get(0).getMsg());
    }
}
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) Metrics(com.usthe.common.entity.job.Metrics) MonitorDetectException(com.usthe.manager.support.exception.MonitorDetectException) Configmap(com.usthe.common.entity.job.Configmap) Job(com.usthe.common.entity.job.Job) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with Configmap

use of com.usthe.common.entity.job.Configmap in project hertzbeat by dromara.

the class WheelTimerTask method initJobMetrics.

/**
 * Initialize job fill information
 * 初始化job填充信息
 *
 * @param job job
 */
private void initJobMetrics(Job job) {
    // 将监控实际参数值对采集字段进行替换
    List<Configmap> config = job.getConfigmap();
    Map<String, Configmap> configmap = config.stream().peek(item -> {
        // 对加密串进行解密
        if (item.getType() == CommonConstants.PARAM_TYPE_PASSWORD && item.getValue() != null) {
            String decodeValue = AesUtil.aesDecode(String.valueOf(item.getValue()));
            if (decodeValue == null) {
                log.error("Aes Decode value {} error.", item.getValue());
            }
            item.setValue(decodeValue);
        } else if (item.getValue() != null && item.getValue() instanceof String) {
            item.setValue(((String) item.getValue()).trim());
        }
    }).collect(Collectors.toMap(Configmap::getKey, item -> item));
    List<Metrics> metrics = job.getMetrics();
    List<Metrics> metricsTmp = new ArrayList<>(metrics.size());
    for (Metrics metric : metrics) {
        JsonElement jsonElement = GSON.toJsonTree(metric);
        jsonElement = replaceSpecialValue(jsonElement, configmap);
        metric = GSON.fromJson(jsonElement, Metrics.class);
        metricsTmp.add(metric);
    }
    job.setMetrics(metricsTmp);
}
Also used : JsonObject(com.google.gson.JsonObject) CommonConstants(com.usthe.common.util.CommonConstants) Iterator(java.util.Iterator) GsonUtil(com.usthe.common.util.GsonUtil) Configmap(com.usthe.common.entity.job.Configmap) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) JsonElement(com.google.gson.JsonElement) JsonArray(com.google.gson.JsonArray) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) SpringContextHolder(com.usthe.collector.util.SpringContextHolder) Job(com.usthe.common.entity.job.Job) Gson(com.google.gson.Gson) Map(java.util.Map) MetricsTaskDispatch(com.usthe.collector.dispatch.MetricsTaskDispatch) AesUtil(com.usthe.common.util.AesUtil) JsonPrimitive(com.google.gson.JsonPrimitive) Metrics(com.usthe.common.entity.job.Metrics) Metrics(com.usthe.common.entity.job.Metrics) Configmap(com.usthe.common.entity.job.Configmap) JsonElement(com.google.gson.JsonElement) ArrayList(java.util.ArrayList)

Example 3 with Configmap

use of com.usthe.common.entity.job.Configmap in project hertzbeat by dromara.

the class WheelTimerTask method replaceSpecialValue.

/**
 * json parameter replacement       json参数替换
 *
 * @param jsonElement json
 * @param configmap   parameter map   参数map
 * @return json
 */
private JsonElement replaceSpecialValue(JsonElement jsonElement, Map<String, Configmap> configmap) {
    if (jsonElement.isJsonObject()) {
        JsonObject jsonObject = jsonElement.getAsJsonObject();
        Iterator<Map.Entry<String, JsonElement>> iterator = jsonObject.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, JsonElement> entry = iterator.next();
            JsonElement element = entry.getValue();
            String key = entry.getKey();
            // 替换KEY-VALUE情况的属性 比如http headers params
            if (key != null && key.startsWith("^_^") && key.endsWith("^_^")) {
                key = key.replaceAll("\\^_\\^", "");
                Configmap param = configmap.get(key);
                if (param != null && param.getType() == (byte) 3) {
                    String jsonValue = (String) param.getValue();
                    Map<String, String> map = GsonUtil.fromJson(jsonValue, Map.class);
                    if (map != null) {
                        map.forEach((name, value) -> {
                            if (name != null && !"".equals(name.trim())) {
                                jsonObject.addProperty(name, value);
                            }
                        });
                    }
                }
                iterator.remove();
                continue;
            }
            // 替换正常的VALUE值
            if (element.isJsonPrimitive()) {
                // Check if there are special characters Replace
                // 判断是否含有特殊字符 替换
                String value = element.getAsString();
                if (value.startsWith("^_^") && value.endsWith("^_^")) {
                    value = value.replaceAll("\\^_\\^", "");
                    Configmap param = configmap.get(value);
                    if (param != null) {
                        value = (String) param.getValue();
                        jsonObject.addProperty(entry.getKey(), value);
                    } else {
                        iterator.remove();
                    }
                }
            } else {
                jsonObject.add(entry.getKey(), replaceSpecialValue(entry.getValue(), configmap));
            }
        }
    } else if (jsonElement.isJsonArray()) {
        JsonArray jsonArray = jsonElement.getAsJsonArray();
        Iterator<JsonElement> iterator = jsonArray.iterator();
        int index = 0;
        while (iterator.hasNext()) {
            JsonElement element = iterator.next();
            if (element.isJsonPrimitive()) {
                // Check if there are special characters Replace
                // 判断是否含有特殊字符 替换
                String value = element.getAsString();
                if (value.startsWith("^_^") && value.endsWith("^_^")) {
                    value = value.replaceAll("\\^_\\^", "");
                    Configmap param = configmap.get(value);
                    if (param != null) {
                        value = (String) param.getValue();
                        jsonArray.set(index, new JsonPrimitive(value));
                    } else {
                        iterator.remove();
                    }
                }
            } else {
                jsonArray.set(index, replaceSpecialValue(element, configmap));
            }
            index++;
        }
    }
    return jsonElement;
}
Also used : JsonArray(com.google.gson.JsonArray) Configmap(com.usthe.common.entity.job.Configmap) JsonPrimitive(com.google.gson.JsonPrimitive) JsonElement(com.google.gson.JsonElement) Iterator(java.util.Iterator) JsonObject(com.google.gson.JsonObject) Map(java.util.Map)

Example 4 with Configmap

use of com.usthe.common.entity.job.Configmap 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 5 with Configmap

use of com.usthe.common.entity.job.Configmap 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)

Aggregations

Configmap (com.usthe.common.entity.job.Configmap)7 Job (com.usthe.common.entity.job.Job)6 Collectors (java.util.stream.Collectors)6 Slf4j (lombok.extern.slf4j.Slf4j)6 CollectJobService (com.usthe.collector.dispatch.entrance.internal.CollectJobService)5 Metrics (com.usthe.common.entity.job.Metrics)5 Monitor (com.usthe.common.entity.manager.Monitor)5 Param (com.usthe.common.entity.manager.Param)5 AesUtil (com.usthe.common.util.AesUtil)5 CommonConstants (com.usthe.common.util.CommonConstants)5 MonitorDao (com.usthe.manager.dao.MonitorDao)5 ParamDao (com.usthe.manager.dao.ParamDao)5 Autowired (org.springframework.beans.factory.annotation.Autowired)5 Service (org.springframework.stereotype.Service)5 AlertDefineBindDao (com.usthe.alert.dao.AlertDefineBindDao)4 ParamDefine (com.usthe.common.entity.manager.ParamDefine)4 Tag (com.usthe.common.entity.manager.Tag)4 CollectRep (com.usthe.common.entity.message.CollectRep)4 IntervalExpressionUtil (com.usthe.common.util.IntervalExpressionUtil)4 IpDomainUtil (com.usthe.common.util.IpDomainUtil)4