From e51c9dbcac5383b4de7d79223cc82fb48b9aa8c6 Mon Sep 17 00:00:00 2001 From: bayuzhen Date: Tue, 28 May 2024 18:37:41 +0800 Subject: [PATCH] =?UTF-8?q?=E8=BF=81=E7=A7=BBcloudTower=E7=9B=91=E6=8E=A7?= =?UTF-8?q?=E5=91=8A=E8=AD=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bocloud.cms.service/pom.xml | 6 +++++ .../cms/service/AnalysisServiceImpl.java | 26 ++++++++++++++----- .../bocloud/cms/service/HostServiceImpl.java | 7 ++--- .../service/core/AnalysisHandleRunable.java | 2 +- 4 files changed, 30 insertions(+), 11 deletions(-) diff --git a/bocloud.cms.service/pom.xml b/bocloud.cms.service/pom.xml index 68b992c..c6aca01 100644 --- a/bocloud.cms.service/pom.xml +++ b/bocloud.cms.service/pom.xml @@ -74,6 +74,12 @@ snakeyaml 1.25 + + com.bocloud + bocloud.rpc + 5.6.0-HBCL-RELEASE + compile + \ No newline at end of file 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 c7dc5e6..7ae3894 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 @@ -20,6 +20,7 @@ import com.bocloud.common.utils.MapTools; import com.bocloud.ims.entity.CloudServer; import com.bocloud.ims.entity.Server; import com.bocloud.ims.enums.CloudProvider; +import com.bocloud.rpc.interfaces.ims.ImsRpcService; import com.google.common.collect.Lists; import com.google.common.util.concurrent.ThreadFactoryBuilder; import org.apache.commons.lang3.StringUtils; @@ -95,6 +96,8 @@ public class AnalysisServiceImpl implements AnalysisService { private AnalysisRuleRepository analysisRuleRepository; @Autowired private AmqpTemplate amqpTemplate; + @Autowired + private ImsRpcService imsRpcService; private static Map vmVcenterMetricMap = new HashMap<>(8); private static Map hostVcenterMetricMap = new HashMap<>(4); @@ -132,7 +135,7 @@ public class AnalysisServiceImpl implements AnalysisService { 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"); + hostCLoudTowerMetricMap.put(MEM_USAGE_AVERAGE, "bocloud_cloudtower_host_host_memory_usage_percent"); } @Override @@ -163,12 +166,12 @@ public class AnalysisServiceImpl implements AnalysisService { return snapshot; } - public AnalysisSnapshot hostAnalysis(JSONArray params, String type, Date start, Date end, String flag, String vendorType) { + public AnalysisSnapshot hostAnalysis(JSONArray params, String type, Date start, Date end, String flag, String vendorType, Long vendorId) { AnalysisSnapshot snapshot = new AnalysisSnapshot(); List tags = this.queryHostTagsFromPrometheus(start, end, flag, vendorType, params); Map paramMap = convertParamMap(params); //列表数据 - List list = hostAnalysisList(paramMap, start, end, tags, vendorType); + List list = hostAnalysisList(paramMap, start, end, tags, vendorType, vendorId); JSONObject result = new JSONObject(); result.put("list", list); List charts = tops(paramMap, type, start, end, new HashSet<>(), tags, vendorType); @@ -641,7 +644,7 @@ public class AnalysisServiceImpl implements AnalysisService { return list; } - private List hostAnalysisList(Map paramMap, Date start, Date end, List tags, String vendorType) { + private List hostAnalysisList(Map paramMap, Date start, Date end, List tags, String vendorType, Long vendorId) { if (ListTool.isEmpty(tags)) { return new ArrayList<>(); } @@ -649,9 +652,18 @@ public class AnalysisServiceImpl implements AnalysisService { List hostParams = Lists.newArrayList(); String key = vendorType.equals(OPENSTACK.name()) ? "id" : vendorType.equals(VMWARE.name()) ? "name" : "hostname"; hostParams.add(new Param(MapTools.simpleMap(key, JSONObject.toJSONString(tags).substring(1, JSONObject.toJSONString(tags).length() - 1)), Sign.IN)); - BocloudResult serverResult = this.imsServerService.list(new Pager(1, Integer.MAX_VALUE, hostParams, null)); - - List serverList = ((JSONObject) serverResult.getData()).getJSONArray(Common.ROWS).toJavaList(Server.class); + BocloudResult serverResult = new BocloudResult(); + List serverList = new ArrayList<>(); + if (CLOUDTOWER.name().equals(vendorType)) { + JSONArray serverArray = JSONArray.parseArray(JSONObject.toJSONString(imsRpcService.getServerByName(tags, vendorId))); + for (Object o : serverArray) { + Server server = JSONObject.parseObject(o.toString(), Server.class); + serverList.add(server); + } + } else { + serverResult = this.imsServerService.list(new Pager(1, Integer.MAX_VALUE, hostParams, null)); + serverList = ((JSONObject) serverResult.getData()).getJSONArray(Common.ROWS).toJavaList(Server.class); + } List list = new ArrayList<>(serverList.size()); for (Server server : serverList) { JSONObject jsonObject = new JSONObject(); 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 97603d4..6570604 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 @@ -246,6 +246,7 @@ public class HostServiceImpl implements HostService, InitializingBean { // 查询主机cpu平均使用情况 Double cpuUsage = prometheusService .simpleQuery("vsphere_host_cpu_usage_average{cpu='instance-total',source='" + name + "'}"); + cpuUsed = cpuUsed != null ? cpuUsed : serverConfig.getCpuUsed(); result.put("cpuUsed", cpuUsed != null ? cpuUsed : ""); result.put("cpuUsage", cpuUsage != null ? cpuUsage : ""); if (null != serverConfig) { @@ -258,10 +259,10 @@ public class HostServiceImpl implements HostService, InitializingBean { } else if ("mem".equals(type)) { Double memUsed = prometheusService .simpleQuery("round(vsphere_host_mem_consumed_average{source='" + name + "'}/ 1024 / 1024,0.01)"); - result.put("memUsed", memUsed != null ? memUsed : ""); + result.put("memUsed", memUsed != null ? memUsed : serverConfig.getMemUsed()); result.put("memTotalCapacity", serverConfig != null ? serverConfig.getMemoryCapacity() : ""); - if (null != serverConfig && null != memUsed) { - result.put("memFree", Double.parseDouble(format.format(serverConfig.getMemoryCapacity() - memUsed))); + if (null != serverConfig && result.containsKey("memUsed")) { + result.put("memFree", Double.parseDouble(format.format(serverConfig.getMemoryCapacity() - Double.valueOf(result.get("memUsed").toString())))); } } else if ("disk".equals(type)) { if (serverConfig != null) { 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 e63136f..2e19de6 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 @@ -84,7 +84,7 @@ public class AnalysisHandleRunable implements Runnable { snapshot = monitorAnalysisService.vmAnalysis(JSONObject.parseArray(analysaisRule.getRules()), type, start, end, flag, vendor.getType(), token); break; case "serverCapacityExpansion": - snapshot = monitorAnalysisService.hostAnalysis(JSONObject.parseArray(analysaisRule.getRules()), type, start, end, flag, vendor.getType()); + snapshot = monitorAnalysisService.hostAnalysis(JSONObject.parseArray(analysaisRule.getRules()), type, start, end, flag, vendor.getType(), vendor.getId()); break; default: logger.error("未知的类型");