|
|
@@ -2,6 +2,7 @@ package com.usthe.manager.service.impl;
|
|
|
|
|
|
import com.usthe.common.entity.job.Configmap;
|
|
|
import com.usthe.common.entity.job.Job;
|
|
|
+import com.usthe.common.entity.message.CollectRep;
|
|
|
import com.usthe.common.util.CommonConstants;
|
|
|
import com.usthe.common.util.SnowFlakeIdGenerator;
|
|
|
import com.usthe.manager.dao.MonitorDao;
|
|
|
@@ -32,6 +33,8 @@ import java.util.stream.Collectors;
|
|
|
@Slf4j
|
|
|
public class MonitorServiceImpl implements MonitorService {
|
|
|
|
|
|
+ private static final Long MONITOR_ID_TMP = 1000000000L;
|
|
|
+
|
|
|
@Autowired
|
|
|
private AppService appService;
|
|
|
|
|
|
@@ -47,7 +50,19 @@ public class MonitorServiceImpl implements MonitorService {
|
|
|
@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);
|
|
|
+ CollectRep collectRep = jobScheduling.addSyncCollectJob(appDefine);
|
|
|
+ // 判断探测结果 失败则抛出探测异常
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -95,12 +110,57 @@ public class MonitorServiceImpl implements MonitorService {
|
|
|
|
|
|
@Override
|
|
|
public void modifyMonitor(Monitor monitor, List<Param> params) throws RuntimeException {
|
|
|
-
|
|
|
+ long monitorId = monitor.getId();
|
|
|
+ // 查判断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()) || !preMonitor.getHost().equals(monitor.getHost())) {
|
|
|
+ // 监控的 类型和host不能修改
|
|
|
+ throw new IllegalArgumentException("Can not modify monitor's app or host");
|
|
|
+ }
|
|
|
+ // 构造采集任务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 -> {
|
|
|
+ param.setMonitorId(monitorId);
|
|
|
+ param.setGmtCreate(null);
|
|
|
+ param.setGmtUpdate(null);
|
|
|
+ return new Configmap(param.getField(), param.getValue(), param.getType());
|
|
|
+ }).collect(Collectors.toList());
|
|
|
+ appDefine.setConfigmap(configmaps);
|
|
|
+ // 更新采集任务
|
|
|
+ jobScheduling.updateAsyncCollectJob(appDefine);
|
|
|
+ // 下发更新成功后刷库
|
|
|
+ try {
|
|
|
+ monitor.setJobId(preMonitor.getJobId());
|
|
|
+ monitor.setStatus(preMonitor.getStatus());
|
|
|
+ monitor.setGmtCreate(null);
|
|
|
+ monitor.setGmtUpdate(null);
|
|
|
+ monitorDao.save(monitor);
|
|
|
+ paramDao.saveAll(params);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error(e.getMessage(), e);
|
|
|
+ throw new MonitorDatabaseException(e.getMessage());
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
public void deleteMonitor(long id) throws RuntimeException {
|
|
|
-
|
|
|
+ Optional<Monitor> monitorOptional = monitorDao.findById(id);
|
|
|
+ if (monitorOptional.isPresent()) {
|
|
|
+ Monitor monitor = monitorOptional.get();
|
|
|
+ monitorDao.deleteById(id);
|
|
|
+ paramDao.deleteParamsByMonitorId(id);
|
|
|
+ jobScheduling.cancelAsyncCollectJob(monitor.getJobId());
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
@Override
|