diff --git a/bocloud.cms.common/src/main/java/com/bocloud/cms/common/config/StorageConfig.java b/bocloud.cms.common/src/main/java/com/bocloud/cms/common/config/StorageConfig.java index c93071d..176d89f 100644 --- a/bocloud.cms.common/src/main/java/com/bocloud/cms/common/config/StorageConfig.java +++ b/bocloud.cms.common/src/main/java/com/bocloud/cms/common/config/StorageConfig.java @@ -86,5 +86,7 @@ public class StorageConfig { @Value("${upload.storage.path:/home/cmp}/prometheus/xsky") private File xskyDir; + @Value("${upload.storage.path:/home/cmp}/prometheus/cloudTower") + private File cloudTowerDir; } \ No newline at end of file diff --git a/bocloud.cms.entity/src/main/java/com/bocloud/cms/enums/MonitorResourceType.java b/bocloud.cms.entity/src/main/java/com/bocloud/cms/enums/MonitorResourceType.java index a61bb6e..89d408c 100644 --- a/bocloud.cms.entity/src/main/java/com/bocloud/cms/enums/MonitorResourceType.java +++ b/bocloud.cms.entity/src/main/java/com/bocloud/cms/enums/MonitorResourceType.java @@ -208,4 +208,12 @@ public enum MonitorResourceType { * smartX云主机 */ MONITOR_SMARTX_VM, + /** + * CLOUDTOWER云主机 + */ + MONITOR_CLOUDTOWER_VM, + /** + * CLOUDTOWER宿主机 + */ + MONITOR_CLOUDTOWER_HOST, } diff --git a/bocloud.cms.entity/src/main/java/com/bocloud/ims/enums/CloudProvider.java b/bocloud.cms.entity/src/main/java/com/bocloud/ims/enums/CloudProvider.java index a7ee914..6df1fb5 100644 --- a/bocloud.cms.entity/src/main/java/com/bocloud/ims/enums/CloudProvider.java +++ b/bocloud.cms.entity/src/main/java/com/bocloud/ims/enums/CloudProvider.java @@ -149,6 +149,8 @@ public enum CloudProvider { /** * SMARTX */ - SMARTX; + SMARTX, + + CLOUDTOWER; } diff --git a/bocloud.cms.service/src/main/java/com/bocloud/cms/intf/CloudTowerHostService.java b/bocloud.cms.service/src/main/java/com/bocloud/cms/intf/CloudTowerHostService.java new file mode 100644 index 0000000..e34b17d --- /dev/null +++ b/bocloud.cms.service/src/main/java/com/bocloud/cms/intf/CloudTowerHostService.java @@ -0,0 +1,6 @@ +package com.bocloud.cms.intf; + +import com.bocloud.cms.model.ServerModel; + +public interface CloudTowerHostService extends MonitoringAble{ +} diff --git a/bocloud.cms.service/src/main/java/com/bocloud/cms/intf/CloudTowerVmService.java b/bocloud.cms.service/src/main/java/com/bocloud/cms/intf/CloudTowerVmService.java new file mode 100644 index 0000000..fc05c30 --- /dev/null +++ b/bocloud.cms.service/src/main/java/com/bocloud/cms/intf/CloudTowerVmService.java @@ -0,0 +1,7 @@ +package com.bocloud.cms.intf; + + +import com.bocloud.ims.entity.CloudServer; + +public interface CloudTowerVmService extends MonitoringAble{ +} diff --git a/bocloud.cms.service/src/main/java/com/bocloud/cms/service/AnalysisServiceImpl.java b/bocloud.cms.service/src/main/java/com/bocloud/cms/service/AnalysisServiceImpl.java index d76b90f..c7dc5e6 100644 --- a/bocloud.cms.service/src/main/java/com/bocloud/cms/service/AnalysisServiceImpl.java +++ b/bocloud.cms.service/src/main/java/com/bocloud/cms/service/AnalysisServiceImpl.java @@ -103,6 +103,9 @@ public class AnalysisServiceImpl implements AnalysisService { private static Map vmManageOneMetricMap = new HashMap<>(4); private static Map hostManageOneMetricMap = new HashMap<>(4); private static Map vmTianyiMetricMap = new HashMap<>(4); + private static Map vmCloudTowerMetricMap = new HashMap<>(4); + private static Map hostCLoudTowerMetricMap = new HashMap<>(4); + static { @@ -126,6 +129,10 @@ public class AnalysisServiceImpl implements AnalysisService { vmTianyiMetricMap.put(CPU_USAGE_AVERAGE, "round(tianyi_agt_ecs_cpu_usage{} or tianyi_sys_ecs_cpu_util{},0.01)"); vmTianyiMetricMap.put(MEM_USAGE_AVERAGE, "round(tianyi_agt_ecs_mem_usedPercent{} or tianyi_sys_ecs_mem_util{},0.01)"); vmTianyiMetricMap.put(DISK_USED_PERCENT, "round(tianyi_agt_ecs_disk_usedPercent{},0.01)"); + vmCloudTowerMetricMap.put(CPU_USAGE_AVERAGE, "bocloud_cloudtower_vm_elf_vm_cpu_overall_usage_percent"); + vmCloudTowerMetricMap.put(MEM_USAGE_AVERAGE, "bocloud_cloudtower_vm_elf_vm_memory_usage_percent"); + hostCLoudTowerMetricMap.put(CPU_USAGE_AVERAGE, "bocloud_cloudtower_host_host_cpu_overall_usage_percent"); + hostCLoudTowerMetricMap.put(MEM_USAGE_AVERAGE, "bocloud_cloudtower_vm_elf_vm_cpu_overall_usage_percent"); } @Override @@ -271,6 +278,20 @@ public class AnalysisServiceImpl implements AnalysisService { charts.add(vmLineChart(time, "云主机内存利用率Bottom", expr8, "%", TIANYI.name())); charts.add(vmLineChart(time, "云主机磁盘利用率Bottom", expr81, "%", TIANYI.name())); break; + case CLOUDTOWER: + String expr12 = "sort_desc(bottomk(10,round(sum by (id,name) (" + MessageFormat.format(template, + paramMap.get(CPU_USAGE_AVERAGE).getString(VALUE_TYPE), + "bocloud_cloudtower_vm_elf_vm_cpu_overall_usage_percent{instanceId=~'" + vmText + "'}", duration, + paramMap.get(CPU_USAGE_AVERAGE).getString(OPERATOR), + paramMap.get(CPU_USAGE_AVERAGE).getString(Common.VALUE)) + "),0.01)))"; + String expr13 = "sort_desc(bottomk(10,round(sum by (id,name) (" + MessageFormat.format(template, + paramMap.get(MEM_USAGE_AVERAGE).getString(VALUE_TYPE), + "bocloud_cloudtower_vm_elf_vm_memory_usage_percent{instanceId=~'" + vmText + "'}", duration, + paramMap.get(MEM_USAGE_AVERAGE).getString(OPERATOR), + paramMap.get(MEM_USAGE_AVERAGE).getString(Common.VALUE)) + "),0.01)))"; + charts.add(vmLineChart(time, "云主机CPU利用率Bottom", expr12, "%", CLOUDTOWER.name())); + charts.add(vmLineChart(time, "云主机内存利用率Bottom", expr13, "%", CLOUDTOWER.name())); + break; default: throw new IllegalStateException("Unexpected value: " + vendorType); } @@ -357,6 +378,20 @@ public class AnalysisServiceImpl implements AnalysisService { charts.add(vmLineChart(time, "云主机内存利用率Top", expr9, "%", TIANYI.name())); charts.add(vmLineChart(time, "云主机磁盘利用率Top", expr91, "%", TIANYI.name())); break; + case CLOUDTOWER: + String expr13 = "sort(topk(10,round(sum by (id,name) (" + MessageFormat.format(template, + paramMap.get(CPU_USAGE_AVERAGE).getString(VALUE_TYPE), + "bocloud_cloudtower_vm_elf_vm_cpu_overall_usage_percent{instanceId=~'" + vmText + "'}", duration, + paramMap.get(CPU_USAGE_AVERAGE).getString(OPERATOR), + paramMap.get(CPU_USAGE_AVERAGE).getString(Common.VALUE)) + "),0.01)))"; + String expr14 = "sort(topk(10,round(sum by (id,name) (" + MessageFormat.format(template, + paramMap.get(MEM_USAGE_AVERAGE).getString(VALUE_TYPE), + "bocloud_cloudtower_vm_elf_vm_memory_usage_percent{instanceId=~'" + vmText + "'}", duration, + paramMap.get(MEM_USAGE_AVERAGE).getString(OPERATOR), + paramMap.get(MEM_USAGE_AVERAGE).getString(Common.VALUE)) + "),0.01)))"; + charts.add(vmLineChart(time, "云主机CPU利用率Top", expr13, "%", CLOUDTOWER.name())); + charts.add(vmLineChart(time, "云主机内存利用率Top", expr14, "%", CLOUDTOWER.name())); + break; default: throw new IllegalStateException("Unexpected value: " + vendorType); } @@ -410,6 +445,20 @@ public class AnalysisServiceImpl implements AnalysisService { charts.add(hostLineChart(time, "宿主机CPU利用率Top", expr6, MANAGEONE.name(), "%")); charts.add(hostLineChart(time, "宿主机内存利用率Top", expr7, MANAGEONE.name(), "%")); break; + case CLOUDTOWER: + String expr11 = "sort_desc(bottomk(10,round(sum by (id,name) (" + MessageFormat.format(template, + paramMap.get(CPU_USAGE_AVERAGE).getString(VALUE_TYPE), + "bocloud_cloudtower_host_host_cpu_overall_usage_percent{name=~'" + hostText + "'}", duration, + paramMap.get(CPU_USAGE_AVERAGE).getString(OPERATOR), + paramMap.get(CPU_USAGE_AVERAGE).getString(Common.VALUE)) + "),0.01)))"; + String expr12 = "sort_desc(bottomk(10,round(sum by (id,name) (" + MessageFormat.format(template, + paramMap.get(MEM_USAGE_AVERAGE).getString(VALUE_TYPE), + "bocloud_cloudtower_host_host_memory_usage_percent{name=~'" + hostText + "'}", duration, + paramMap.get(MEM_USAGE_AVERAGE).getString(OPERATOR), + paramMap.get(MEM_USAGE_AVERAGE).getString(Common.VALUE)) + "),0.01)))"; + charts.add(vmLineChart(time, "宿主机CPU利用率Bottom", expr11, "%", CLOUDTOWER.name())); + charts.add(vmLineChart(time, "宿云主机内存利用率Bottom", expr12, "%", CLOUDTOWER.name())); + break; default: throw new IllegalStateException("Unexpected value: " + vendorType); } @@ -469,6 +518,9 @@ public class AnalysisServiceImpl implements AnalysisService { String ip = cloudServer.getPrivateIps() != null ? StringUtils.join(JSONArray.parseArray(cloudServer.getPrivateIps(), String.class).toArray(), ",") : ""; jsonObject.put("ip", ip); break; + case CLOUDTOWER: + jsonObject.put("ip", cloudServer.getManagerIp()); + break; default: throw new IllegalStateException("Unexpected value: " + vendorType); } @@ -566,6 +618,20 @@ public class AnalysisServiceImpl implements AnalysisService { convertCloudServer(diskUsedPercentResult, tempList, TIANYI.name(), "diskUsedPercent"); }); break; + case CLOUDTOWER: + Optional.ofNullable(paramMap.get(CPU_USAGE_AVERAGE)).ifPresent(jsonObject -> { + String cpuUsageAverageFilter = "round(" + MessageFormat.format(template, jsonObject.getString(VALUE_TYPE), + "bocloud_cloudtower_vm_elf_vm_cpu_overall_usage_percent{instanceId=~'" + tag + "'}", duration) + ",0.01)"; + List cpuUsageAverageResult = prometheusService.query(cpuUsageAverageFilter, time); + convertCloudServer(cpuUsageAverageResult, tempList, CLOUDTOWER.name(), "cpuUsage"); + }); + Optional.ofNullable(paramMap.get(MEM_USAGE_AVERAGE)).ifPresent(jsonObject -> { + String memUsageAverageFilter = "round(" + MessageFormat.format(template, jsonObject.getString(VALUE_TYPE), + "bocloud_cloudtower_vm_elf_vm_memory_usage_percent{instanceId=~'" + tag + "'}", duration) + ",0.01)"; + List memUsageAverageResult = prometheusService.query(memUsageAverageFilter, time); + convertCloudServer(memUsageAverageResult, tempList, CLOUDTOWER.name(), "memUsage"); + }); + break; default: throw new IllegalStateException("Unexpected value: " + vendorType); } @@ -648,6 +714,20 @@ public class AnalysisServiceImpl implements AnalysisService { convertServer(memUsageAverageResult, list, MANAGEONE.name(), "memUsage"); }); break; + case CLOUDTOWER: + Optional.ofNullable(paramMap.get(CPU_USAGE_AVERAGE)).ifPresent(jsonObject -> { + String cpuUsageAverageFilter = "round(" + MessageFormat.format(template, paramMap.get(CPU_USAGE_AVERAGE).getString(VALUE_TYPE), + "bocloud_cloudtower_host_host_cpu_overall_usage_percent{name=~'" + tag + "'}", duration) + ",0.01)"; + List cpuUsageAverageResult = prometheusService.query(cpuUsageAverageFilter, time); + convertServer(cpuUsageAverageResult, list, CLOUDTOWER.name(), "cpuUsage"); + }); + Optional.ofNullable(paramMap.get(MEM_USAGE_AVERAGE)).ifPresent(jsonObject -> { + String memUsageAverageFilter = "round(" + MessageFormat.format(template, paramMap.get(MEM_USAGE_AVERAGE).getString(VALUE_TYPE), + "bocloud_cloudtower_host_host_memory_usage_percent{name=~'" + tag + "'}", duration) + ",0.01)"; + List memUsageAverageResult = prometheusService.query(memUsageAverageFilter, time); + convertServer(memUsageAverageResult, list, CLOUDTOWER.name(), "memUsage"); + }); + break; default: throw new IllegalStateException("Unexpected value: " + vendorType); } @@ -696,6 +776,13 @@ public class AnalysisServiceImpl implements AnalysisService { tags.add(String.valueOf(prometheusResult.getMetric().get("instance_id"))); } break; + case CLOUDTOWER: + expr = getPerformanceExpr(params, start, end, flag, CLOUDTOWER, "vm"); + prometheusResultList = prometheusService.query(expr, String.valueOf(end.getTime() / 1000L)); + for (PrometheusResult prometheusResult : prometheusResultList) { + tags.add(String.valueOf(prometheusResult.getMetric().get("instanceId"))); + } + break; default: throw new IllegalStateException("Unexpected value: " + vendorType); } @@ -737,6 +824,13 @@ public class AnalysisServiceImpl implements AnalysisService { tags.add(String.valueOf(prometheusResult.getMetric().get("id"))); } break; + case CLOUDTOWER: + expr = getPerformanceExpr(params, start, end, flag, CLOUDTOWER, "host"); + prometheusResultList = prometheusService.query(expr, String.valueOf(end.getTime() / 1000L)); + for (PrometheusResult prometheusResult : prometheusResultList) { + tags.add(String.valueOf(prometheusResult.getMetric().get("name"))); + } + break; default: throw new IllegalStateException("Unexpected value: " + vendorType); } @@ -897,6 +991,49 @@ public class AnalysisServiceImpl implements AnalysisService { } } break; + case CLOUDTOWER: + if ("vm".equals(type) && vmCloudTowerMetricMap.containsKey(param.getString(METRIC))) { + String metric = vmCloudTowerMetricMap.get(param.getString(METRIC)); + Map map = new HashMap<>(2); + map.put("vendorId", vendorTags); + if (StringUtils.isBlank(expr)) { + expr = MessageFormat.format(template, param.getString(VALUE_TYPE), + PrometheusUtils.putParams(metric, map), duration, param.getString(OPERATOR), + param.getString(Common.VALUE)); + } else { + expr = expr + MessageFormat.format(template, param.getString(VALUE_TYPE), + vmCloudTowerMetricMap.get(param.getString(METRIC)), duration, + param.getString(OPERATOR), param.getString(Common.VALUE)); + } + // 因为cloudTower云平台没有disk平均使用率的监控信息 所以需要特殊处理 + if (i == params.size() - 1) { + result.append(" sum by (instanceId)(").append(expr).append(")"); + break; + } else { + result.append(" sum by (instanceId)(").append(expr).append(") ").append(param.getString("relation")); + } + } else if ("host".equals(type) && hostCLoudTowerMetricMap.containsKey(param.getString(METRIC))) { + String metric = hostCLoudTowerMetricMap.get(param.getString(METRIC)); + Map map = new HashMap<>(2); + map.put("vendorId", vendorTags); + if (StringUtils.isBlank(expr)) { + expr = MessageFormat.format(template, param.getString(VALUE_TYPE), + PrometheusUtils.putParams(metric, map), duration, param.getString(OPERATOR), + param.getString(Common.VALUE)); + } else { + expr = expr + MessageFormat.format(template, param.getString(VALUE_TYPE), + hostCLoudTowerMetricMap.get(param.getString(METRIC)), duration, + param.getString(OPERATOR), param.getString(Common.VALUE)); + } + if (i == params.size() - 1) { + result.append(" sum by (name)(").append(expr).append(")"); + } else { + result.append(" sum by (name)(").append(expr).append(") ").append(param.getString("relation")); + } + } else { + result = new StringBuilder(result.substring(0, result.length() - 3)); + } + break; default: throw new IllegalStateException("Unexpected value: " + vendorTags); } @@ -933,6 +1070,7 @@ public class AnalysisServiceImpl implements AnalysisService { private void convertCloudServer(List results, List list, String vendorType, String key) { switch (CloudProvider.valueOf(vendorType)) { case OPENSTACK: + case CLOUDTOWER: for (PrometheusResult result : results) { for (JSONObject jsonObject : list) { if (String.valueOf(jsonObject.get("instanceId")) @@ -1009,6 +1147,16 @@ public class AnalysisServiceImpl implements AnalysisService { } } break; + case CLOUDTOWER: + for (PrometheusResult result : results) { + for (JSONObject jsonObject : list) { + if (String.valueOf(jsonObject.get("name")) + .equals(String.valueOf(result.getMetric().get("name")))) { + jsonObject.put(key, result.getValue()[1]); + } + } + } + break; default: throw new IllegalStateException("Unexpected value: " + vendorType); } diff --git a/bocloud.cms.service/src/main/java/com/bocloud/cms/service/CloudTowerHostServiceImpl.java b/bocloud.cms.service/src/main/java/com/bocloud/cms/service/CloudTowerHostServiceImpl.java new file mode 100644 index 0000000..2afa93d --- /dev/null +++ b/bocloud.cms.service/src/main/java/com/bocloud/cms/service/CloudTowerHostServiceImpl.java @@ -0,0 +1,106 @@ +package com.bocloud.cms.service; + +import com.alibaba.fastjson.JSONObject; +import com.bocloud.cms.entity.Alarm; +import com.bocloud.cms.entity.Metric; +import com.bocloud.cms.entity.Rule; +import com.bocloud.cms.entity.RuleGroup; +import com.bocloud.cms.enums.MonitorResourceType; +import com.bocloud.cms.intf.CloudTowerHostService; +import com.bocloud.cms.intf.PrometheusService; +import com.bocloud.cms.intf.ResourceAlarmStatusService; +import com.bocloud.cms.model.PrometheusAlarm; +import com.bocloud.cms.model.ServerModel; +import com.bocloud.cms.repository.ResourceAlarmStatusRepository; +import com.bocloud.cms.service.internal.ImsServerService; +import com.bocloud.common.model.BocloudResult; +import com.bocloud.common.model.Pager; +import com.bocloud.common.model.Param; +import com.bocloud.common.model.Sign; +import com.bocloud.common.utils.Common; +import com.bocloud.common.utils.MapTools; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.InitializingBean; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.util.Assert; + +import java.util.*; + +@Service +public class CloudTowerHostServiceImpl implements CloudTowerHostService, InitializingBean { + private static final Logger logger = LoggerFactory.getLogger(CloudTowerHostServiceImpl.class); + + @Autowired + CloudTowerHostService cloudTowerHostService; + /** + * 查询宿主机列表时使用 + */ + @Autowired + private ImsServerService imsServerService; + @Autowired + private PrometheusService prometheusService; + @Autowired + private ResourceAlarmStatusRepository alarmStatusRepository; + @Autowired + private ResourceAlarmStatusService resourceAlarmStatusService; + + @Override + public void afterPropertiesSet() throws Exception { + MonitoringAbleRegistry.register(MonitorResourceType.MONITOR_CLOUDTOWER_HOST, this); + } + @Override + public List listTargetObjects(List targetIds, MonitorResourceType resourceType) { + List targetList = new ArrayList<>(); + Optional.ofNullable(targetIds).orElse(new ArrayList<>()).forEach(targetId -> targetList.add(Integer.valueOf(targetId))); + Param param = new Param(MapTools.simpleMap("id", JSONObject.toJSONString(targetList).substring(1, JSONObject.toJSONString(targetList).length() - 1)), Sign.IN); + + List params = Collections.singletonList(param); + BocloudResult result = imsServerService.list(new Pager(1, Integer.MAX_VALUE, params, null)); + Assert.isTrue(result.isSuccess(), result.getMessage()); + JSONObject data = (JSONObject) result.getData(); + return data.getJSONArray(Common.ROWS).toJavaList(ServerModel.class); + } + + @Override + public Map getPrometheusExprParam(ServerModel targetObject, Metric metric) { + Map params = new HashMap<>(16, 0.75F); + params.put("name", targetObject.getName()); + params.put("vendorId",targetObject.getVendorId().toString()); + return params; + } + + @Override + public ServerModel parseObjectFromAlert(PrometheusAlarm.PrometheusAlert alert, RuleGroup ruleGroup, Rule rule, Metric metric) { + String instanceId = String.valueOf(alert.getLabels().get("instanceId")); + // 通过主机名称去查找对应的主机信息 + List params = Collections.singletonList(new Param(MapTools.simpleMap("name", alert.getLabels().get("name")), Sign.EQ)); + BocloudResult result = imsServerService.list(new Pager(1, 1, params, null)); + Assert.isTrue(result.isSuccess(), result.getMessage()); + JSONObject data = (JSONObject) result.getData(); + List list = data.getJSONArray(Common.ROWS).toJavaList(ServerModel.class); + Assert.isTrue(list != null && !list.isEmpty(), "cloudTowerHost not found,instanceId:" + instanceId); + return list.get(0); + } + + @Override + public void destroy(ServerModel object, String catalog) throws Exception { + + } + + @Override + public void wrapAlarm(Alarm alarm, ServerModel object) { + alarm.setTarget(object.getId()); + alarm.setVendorId(object.getVendorId()); + alarm.setOwnerId(object.getCreatorId()); + String targetName = object.getManageIp() == null ? object.getName() : object.getName() + "(" + object.getManageIp() + ")"; + alarm.setTargetName(targetName); + } + + @Override + public boolean alarmEnable(RuleGroup ruleGroup, ServerModel object) { + return resourceAlarmStatusService.alarmEnable(ruleGroup.getResourceType(), String.valueOf(object.getId()), ruleGroup.getTenantId()); + } + +} diff --git a/bocloud.cms.service/src/main/java/com/bocloud/cms/service/CloudTowerVmServiceImpl.java b/bocloud.cms.service/src/main/java/com/bocloud/cms/service/CloudTowerVmServiceImpl.java new file mode 100644 index 0000000..0ad64f5 --- /dev/null +++ b/bocloud.cms.service/src/main/java/com/bocloud/cms/service/CloudTowerVmServiceImpl.java @@ -0,0 +1,100 @@ +package com.bocloud.cms.service; + +import com.alibaba.fastjson.JSONObject; +import com.bocloud.cms.entity.Alarm; +import com.bocloud.cms.entity.Metric; +import com.bocloud.cms.entity.Rule; +import com.bocloud.cms.entity.RuleGroup; +import com.bocloud.cms.enums.MonitorResourceType; +import com.bocloud.cms.intf.AlarmService; +import com.bocloud.cms.intf.CloudTowerVmService; +import com.bocloud.cms.intf.ResourceAlarmStatusService; +import com.bocloud.cms.model.PrometheusAlarm; +import com.bocloud.cms.service.internal.ImsCloudServerService; +import com.bocloud.common.model.BocloudResult; +import com.bocloud.common.model.Pager; +import com.bocloud.common.model.Param; +import com.bocloud.common.model.Sign; +import com.bocloud.common.utils.Common; +import com.bocloud.common.utils.MapTools; +import com.bocloud.ims.entity.CloudServer; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.InitializingBean; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.util.Assert; + +import java.util.*; + +@Service +public class CloudTowerVmServiceImpl implements CloudTowerVmService, InitializingBean { + private static final Logger logger = LoggerFactory.getLogger(CloudTowerVmServiceImpl.class); + + @Autowired + CloudTowerVmService cloudTowerVmService; + @Autowired + private ImsCloudServerService imsCloudServerService; + @Autowired + private ResourceAlarmStatusService resourceAlarmStatusService; + @Autowired + private AlarmService alarmService; + @Override + public void afterPropertiesSet() throws Exception { + MonitoringAbleRegistry.register(MonitorResourceType.MONITOR_CLOUDTOWER_VM, this); + } + @Override + public List listTargetObjects(List targetIds, MonitorResourceType resourceType) { + List targetList = new ArrayList<>(); + Optional.ofNullable(targetIds).orElse(new ArrayList<>()).forEach(targetId -> targetList.add(Integer.valueOf(targetId))); + Param param = new Param(MapTools.simpleMap("id", JSONObject.toJSONString(targetList).substring(1, JSONObject.toJSONString(targetList).length() - 1)), Sign.IN); + + List params = Collections.singletonList(param); + BocloudResult result = imsCloudServerService.list(new Pager(1, Integer.MAX_VALUE, params, null), null); + Assert.isTrue(result.isSuccess(), result.getMessage()); + JSONObject data = (JSONObject) result.getData(); + return data.getJSONArray(Common.ROWS).toJavaList(CloudServer.class); + } + + @Override + public Map getPrometheusExprParam(CloudServer targetObject, Metric metric) { + Map params = new HashMap<>(16, 0.75F); + params.put("instanceId", targetObject.getInstanceId()); + params.put("vendorId",targetObject.getVendorId().toString()); + return params; + } + + @Override + public CloudServer parseObjectFromAlert(PrometheusAlarm.PrometheusAlert alert, RuleGroup ruleGroup, Rule rule, Metric metric) { + String instanceId = String.valueOf(alert.getLabels().get("instanceId")); + // 通过vmName去查找对应的云主机信息 + List params = Collections.singletonList(new Param(MapTools.simpleMap("instanceId", instanceId), Sign.EQ)); + BocloudResult result = imsCloudServerService.list(new Pager(1, 1, params, null), null); + Assert.isTrue(result.isSuccess(), result.getMessage()); + JSONObject data = (JSONObject) result.getData(); + List list = data.getJSONArray(Common.ROWS).toJavaList(CloudServer.class); + Assert.isTrue(list != null && !list.isEmpty(), "cloudTowerVm not found,instanceId:" + instanceId); + return list.get(0); + } + + @Override + public void destroy(CloudServer cloudServer, String catalog) throws Exception { + logger.info("destroy cloudTower vm : " + cloudServer.getId()); + alarmService.solveByCloudServer(cloudServer.getId(),catalog,1L,"系统解决"); + } + + @Override + public void wrapAlarm(Alarm alarm, CloudServer object) { + alarm.setTarget(object.getId()); + alarm.setVendorId(object.getVendorId()); + alarm.setOwnerId(object.getCreatorId()); + String targetName = object.getManagerIp() == null ? object.getName() : object.getName() + "(" + object.getManagerIp() + ")"; + alarm.setTargetName(targetName); + } + + @Override + public boolean alarmEnable(RuleGroup ruleGroup, CloudServer object) { + return resourceAlarmStatusService.alarmEnable(ruleGroup.getResourceType(), String.valueOf(object.getId()), ruleGroup.getTenantId()); + } + +} diff --git a/bocloud.cms.service/src/main/java/com/bocloud/cms/service/HostServiceImpl.java b/bocloud.cms.service/src/main/java/com/bocloud/cms/service/HostServiceImpl.java index 5a398c7..97603d4 100644 --- a/bocloud.cms.service/src/main/java/com/bocloud/cms/service/HostServiceImpl.java +++ b/bocloud.cms.service/src/main/java/com/bocloud/cms/service/HostServiceImpl.java @@ -117,6 +117,7 @@ public class HostServiceImpl implements HostService, InitializingBean { handleOpenStackList(context, list); handleManageOneList(context, list); handleH3cList(context, list); + handleCloudTower(context, list); String ips = StringUtils.join(ipList.toArray(), "|"); //主机通断状态 @@ -148,6 +149,33 @@ public class HostServiceImpl implements HostService, InitializingBean { return result; } + private void handleCloudTower(RequestContext context, List list) { + List names = list.stream().filter(s -> s.getVendorType().equalsIgnoreCase(CloudProvider.CLOUDTOWER.name())) + .map(s -> String.valueOf(s.getName())).collect(Collectors.toList()); + if (ListTool.isEmpty(names)) { + return; + } + List target = list.stream().map(m -> String.valueOf(m.getId())).collect(Collectors.toList()); + List alarmStatuses = this.alarmStatusRepository.list(MonitorResourceType.MONITOR_CLOUDTOWER_HOST.name(), target, context); + + for (ServerModel server : list) { + String cpuExpr = "bocloud_cloudtower_host_host_cpu_overall_usage_percent{name='" +server.getName()+"'}"; + List cpu = prometheusService.query(cpuExpr); + for (PrometheusResult cpuUsage : cpu) { + server.setCpuUsage(String.valueOf(cpuUsage.getValue()[1])); + } + String memoryExpr = "bocloud_cloudtower_host_host_memory_usage_percent{name='" +server.getName() +"'}"; + List memory = prometheusService.query(memoryExpr); + for (PrometheusResult memUsage : memory) { + server.setMemUsage(String.valueOf(memUsage.getValue()[1])); + } + //告警启用状态,默认开启 + boolean alarmEnable = !Optional.ofNullable(alarmStatuses).orElse(new ArrayList<>()).stream() + .filter(a -> a.getResourceId().equals(server.getId().toString())) + .findAny().isPresent(); + server.setAlarmEnable(alarmEnable); + } + } @Override public BocloudResult hostOverview(Long id, String type) { @@ -155,7 +183,7 @@ public class HostServiceImpl implements HostService, InitializingBean { BocloudResult server = imsServerService.detail(id); Assert.isTrue(server.isSuccess(), " 未查询到该主机相关信息"); HashMap result = new HashMap<>(16, 0.75F); - if (type.equals(CloudProvider.VMWARE.name())) { + if (type.equals(CloudProvider.VMWARE.name()) || type.equals(CloudProvider.CLOUDTOWER.name())) { result.put("name", server.getData().getName()); result.put("powerState", server.getData().getPowerState()); result.put("ip", server.getData().getManageIp()); diff --git a/bocloud.cms.service/src/main/java/com/bocloud/cms/service/TopServiceImpl.java b/bocloud.cms.service/src/main/java/com/bocloud/cms/service/TopServiceImpl.java index 9306177..fe42989 100644 --- a/bocloud.cms.service/src/main/java/com/bocloud/cms/service/TopServiceImpl.java +++ b/bocloud.cms.service/src/main/java/com/bocloud/cms/service/TopServiceImpl.java @@ -77,6 +77,8 @@ public class TopServiceImpl implements TopService { return hostLineChart("宿主机CPU利用率", "round(sort_desc(topk(" + limit + ",bocloud_fusioncloud_host_cpuUsage{vendorId='" + vendorId + "'})),0.01)"); case SMARTX: return hostLineChart("宿主机CPU利用率", "round(sort_desc(topk(" + limit + ",smartx_host_cpu_overall_usage_percent{vendorId='" + vendorId + "'})),0.01)"); + case CLOUDTOWER: + return hostLineChart("宿主机CPU利用率", "round(sort_desc(topk(" + limit + ",bocloud_cloudtower_host_host_cpu_overall_usage_percent{vendorId='" + vendorId + "'})),0.01)"); default: return new LineChartResult(null, new Values[]{new Values("宿主机cpu利用率", null)}, null); } @@ -104,6 +106,8 @@ public class TopServiceImpl implements TopService { return hostLineChart("宿主机内存利用率", "round(sort_desc(topk(" + limit + ",bocloud_fusioncloud_host_memoryUsage{vendorId='" + vendorId + "'})),0.01)"); case SMARTX: return hostLineChart("宿主机内存利用率", "round(sort_desc(topk(" + limit + ",smartx_host_memory_usage_percent{vendorId='" + vendorId + "'})),0.01)"); + case CLOUDTOWER: + return hostLineChart("宿主机内存利用率", "round(sort_desc(topk(" + limit + ",bocloud_cloudtower_host_host_memory_usage_percent{vendorId='" + vendorId + "'})),0.01)"); default: return new LineChartResult(null, new Values[]{new Values("宿主机内存利用率", null)}, null); } @@ -138,6 +142,9 @@ public class TopServiceImpl implements TopService { case SMARTX: expr = "round(sort(topk(" + limit * 3 + ",smartx_elf_vm_cpu_overall_usage_percent{vendorId='" + vendor.getId() + "'})),0.01)"; break; + case CLOUDTOWER: + expr = "round(sort(topk(" + limit * 3 + ",bocloud_cloudtower_vm_elf_vm_cpu_overall_usage_percent{vendorId='" + vendor.getId() + "'})),0.01)"; + break; default: break; } @@ -174,6 +181,9 @@ public class TopServiceImpl implements TopService { case SMARTX: expr = "round(sort(topk(" + limit * 3 + ",smartx_elf_vm_memory_usage_percent{vendorId='" + vendor.getId() + "'})),0.01)"; break; + case CLOUDTOWER: + expr = "round(sort(topk(" + limit * 3 + ",bocloud_cloudtower_vm_elf_vm_memory_usage_percent{vendorId='" + vendor.getId() + "'})),0.01)"; + break; default: break; } @@ -289,6 +299,11 @@ public class TopServiceImpl implements TopService { vmName = String.valueOf(result.getMetric().get("vm")); String instanceId = String.valueOf(result.getMetric().get("vm")); paramList.add(new Param(MapTools.simpleMap("instanceId", instanceId), Sign.EQ)); + } else if (result.getMetric().containsKey("vendorId") && CloudProvider.CLOUDTOWER == vendorType) { + //cloudTower 使用vm查询 + vmName = String.valueOf(result.getMetric().get("name")); + String instanceId = String.valueOf(result.getMetric().get("instanceId")); + paramList.add(new Param(MapTools.simpleMap("instanceId", instanceId), Sign.EQ)); } else { //manageone 使用instanceId查询 vmName = String.valueOf(result.getMetric().get("name")); diff --git a/bocloud.cms.service/src/main/java/com/bocloud/cms/service/VendorServiceImpl.java b/bocloud.cms.service/src/main/java/com/bocloud/cms/service/VendorServiceImpl.java index d4b7091..da2381b 100644 --- a/bocloud.cms.service/src/main/java/com/bocloud/cms/service/VendorServiceImpl.java +++ b/bocloud.cms.service/src/main/java/com/bocloud/cms/service/VendorServiceImpl.java @@ -86,6 +86,9 @@ public class VendorServiceImpl implements VendorService { case XSKY: openXsky(vendor); break; + case CLOUDTOWER: + openCloudTower(vendor); + break; default: BocloudResult result = imsCloudVendorService.openMonitoring(idsList); Assert.isTrue(result.isSuccess(), result.getMessage()); @@ -263,6 +266,47 @@ public class VendorServiceImpl implements VendorService { } } + /** + * 开启cloudTower监控 + * + * @param vendor + * @return + * @throws Exception + */ + private void openCloudTower(CloudVendor vendor) throws Exception { + List params = new ArrayList<>(); + params.add(new Param(MapTools.simpleMap("vendorId", vendor.getId()), Sign.EQ)); + params.add(new Param(MapTools.simpleMap("exporterInstalled", true), Sign.EQ)); + BocloudResult serversResult = imsServerService.list(new Pager(1, Integer.MAX_VALUE, false, params, null)); + JSONObject data = (JSONObject) serversResult.getData(); + List list = data.getJSONArray(Common.ROWS).toJavaList(Server.class); + List files = new ArrayList<>(); + for (Server server : list) { + Map labels = new HashMap<>(16, 0.75F); + labels.put("id", String.valueOf(server.getId())); + labels.put("name", server.getName()); + labels.put("vendor_id", String.valueOf(vendor.getId())); + labels.put("instanceId", server.getUuid()); + JSONObject config = new JSONObject(); + config.put("targets", Arrays.asList(server.getManageIp() + ":" + port)); + config.put("labels", labels); + JSONArray array = new JSONArray(); + array.add(config); + File file = new File(storageConfig.getCloudTowerDir(), "server_" + server.getId() + ".json"); + FileUtils.write(file, array.toJSONString(), "UTF-8"); + files.add(file); + } + BocloudResult result = imsCloudVendorService.openMonitoring(Collections.singletonList(vendor.getId())); + if (result.isFailed()) { + files.forEach(file -> { + if (file.exists() && !file.delete()) { + logger.error("file delete failed, path:{} " + file.getPath()); + } + }); + throw new RuntimeException(result.getMessage()); + } + } + /** * 开启xsky监控 * diff --git a/bocloud.cms.service/src/main/java/com/bocloud/cms/service/VmServiceImpl.java b/bocloud.cms.service/src/main/java/com/bocloud/cms/service/VmServiceImpl.java index e6a8f6b..a8f55a6 100644 --- a/bocloud.cms.service/src/main/java/com/bocloud/cms/service/VmServiceImpl.java +++ b/bocloud.cms.service/src/main/java/com/bocloud/cms/service/VmServiceImpl.java @@ -98,6 +98,7 @@ public class VmServiceImpl implements VmService, InitializingBean { handleHmcList(list, context); handleTencentList(list, context); handleSmartxList(list, context); + handleCloudTower(list, context); data.put(Common.ROWS, list); return result; } @@ -384,6 +385,48 @@ public class VmServiceImpl implements VmService, InitializingBean { MonitoringAbleRegistry.register(MonitorResourceType.MONITOR_VCENTER_VM, this); } + private void handleCloudTower(List list, RequestContext context) { + List instanceIds = list.stream() + .filter(s -> s.getVendorType().equalsIgnoreCase(CloudProvider.CLOUDTOWER.name())) + .map(CloudServer::getInstanceId).collect(Collectors.toList()); + if (ListTool.isEmpty(instanceIds)) { + return; + } + + String instanceId = StringUtils.join(instanceIds.toArray(), "|"); + List cpuList = prometheusService.query("round(bocloud_cloudtower_vm_elf_vm_cpu_overall_usage_percent{instanceId=~'" + instanceId + "'},0.01)"); + List memoryList = prometheusService.query("round(bocloud_cloudtower_vm_elf_vm_memory_usage_percent{instanceId=~'" + instanceId + "'},0.01)"); + + List target = list.stream().map(m -> String.valueOf(m.getId())).collect(Collectors.toList()); + List alarmStatuses = this.alarmStatusRepository.list(MonitorResourceType.MONITOR_CLOUDTOWER_VM.name(), target, context); + + for (CloudServerModel cloudServer : list) { + cloudServer.setMemTotal(String.valueOf(cloudServer.getMemory())); + //CPU利用率 + for (PrometheusResult cpuUsage : cpuList) { + if (cpuUsage.getMetric().get("instanceId").equals(cloudServer.getInstanceId())) { + cloudServer.setCpuUsage(String.valueOf(cpuUsage.getValue()[1])); + break; + } + } + //内存利用率 + for (PrometheusResult memoryUsage : memoryList) { + if (memoryUsage.getMetric().get("instanceId").equals(cloudServer.getInstanceId())) { + cloudServer.setMemUsage(String.valueOf(memoryUsage.getValue()[1])); + break; + } + } + + //告警启用状态,默认开启 + if ( alarmStatuses.size()>0 ){ + for (ResourceAlarmStatus alarmStatus : alarmStatuses) { + if (alarmStatus.getResourceId().equals(cloudServer.getId().toString())){ + cloudServer.setAlarmEnable(alarmStatus.getEnable()); + } + } + } + } + } private void handleVmwareList(RequestContext context, List list) { List vmwareVms = list.stream().filter(s -> s.getVendorType().equalsIgnoreCase(CloudProvider.VMWARE.name())).collect(Collectors.toList()); diff --git a/bocloud.cms.service/src/main/java/com/bocloud/cms/service/core/AnalysisHandleRunable.java b/bocloud.cms.service/src/main/java/com/bocloud/cms/service/core/AnalysisHandleRunable.java index 1e29484..e63136f 100644 --- a/bocloud.cms.service/src/main/java/com/bocloud/cms/service/core/AnalysisHandleRunable.java +++ b/bocloud.cms.service/src/main/java/com/bocloud/cms/service/core/AnalysisHandleRunable.java @@ -71,6 +71,7 @@ public class AnalysisHandleRunable implements Runnable { break; case MANAGEONE: case TIANYI: + case CLOUDTOWER: flag = String.valueOf(vendorId); break; default: