Compare commits

..

2 Commits

Author SHA1 Message Date
bayuzhen e51c9dbcac 迁移cloudTower监控告警 2024-05-28 18:37:41 +08:00
bayuzhen 7383eaaec2 迁移cloudTower监控 2024-05-28 15:09:39 +08:00
14 changed files with 541 additions and 12 deletions

View File

@ -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;
}

View File

@ -208,4 +208,12 @@ public enum MonitorResourceType {
* smartX
*/
MONITOR_SMARTX_VM,
/**
* CLOUDTOWER
*/
MONITOR_CLOUDTOWER_VM,
/**
* CLOUDTOWER宿
*/
MONITOR_CLOUDTOWER_HOST,
}

View File

@ -149,6 +149,8 @@ public enum CloudProvider {
/**
* SMARTX
*/
SMARTX;
SMARTX,
CLOUDTOWER;
}

View File

@ -74,6 +74,12 @@
<artifactId>snakeyaml</artifactId>
<version>1.25</version>
</dependency>
<dependency>
<groupId>com.bocloud</groupId>
<artifactId>bocloud.rpc</artifactId>
<version>5.6.0-HBCL-RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,6 @@
package com.bocloud.cms.intf;
import com.bocloud.cms.model.ServerModel;
public interface CloudTowerHostService extends MonitoringAble<ServerModel>{
}

View File

@ -0,0 +1,7 @@
package com.bocloud.cms.intf;
import com.bocloud.ims.entity.CloudServer;
public interface CloudTowerVmService extends MonitoringAble<CloudServer>{
}

View File

@ -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<String, String> vmVcenterMetricMap = new HashMap<>(8);
private static Map<String, String> hostVcenterMetricMap = new HashMap<>(4);
@ -103,6 +106,9 @@ public class AnalysisServiceImpl implements AnalysisService {
private static Map<String, String> vmManageOneMetricMap = new HashMap<>(4);
private static Map<String, String> hostManageOneMetricMap = new HashMap<>(4);
private static Map<String, String> vmTianyiMetricMap = new HashMap<>(4);
private static Map<String, String> vmCloudTowerMetricMap = new HashMap<>(4);
private static Map<String, String> hostCLoudTowerMetricMap = new HashMap<>(4);
static {
@ -126,6 +132,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_host_host_memory_usage_percent");
}
@Override
@ -156,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<String> tags = this.queryHostTagsFromPrometheus(start, end, flag, vendorType, params);
Map<String, JSONObject> paramMap = convertParamMap(params);
//列表数据
List<JSONObject> list = hostAnalysisList(paramMap, start, end, tags, vendorType);
List<JSONObject> list = hostAnalysisList(paramMap, start, end, tags, vendorType, vendorId);
JSONObject result = new JSONObject();
result.put("list", list);
List<JSONObject> charts = tops(paramMap, type, start, end, new HashSet<>(), tags, vendorType);
@ -271,6 +281,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 +381,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 +448,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 +521,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 +621,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<PrometheusResult> 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<PrometheusResult> memUsageAverageResult = prometheusService.query(memUsageAverageFilter, time);
convertCloudServer(memUsageAverageResult, tempList, CLOUDTOWER.name(), "memUsage");
});
break;
default:
throw new IllegalStateException("Unexpected value: " + vendorType);
}
@ -575,7 +644,7 @@ public class AnalysisServiceImpl implements AnalysisService {
return list;
}
private List<JSONObject> hostAnalysisList(Map<String, JSONObject> paramMap, Date start, Date end, List<String> tags, String vendorType) {
private List<JSONObject> hostAnalysisList(Map<String, JSONObject> paramMap, Date start, Date end, List<String> tags, String vendorType, Long vendorId) {
if (ListTool.isEmpty(tags)) {
return new ArrayList<>();
}
@ -583,9 +652,18 @@ public class AnalysisServiceImpl implements AnalysisService {
List<Param> 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<Server> serverList = ((JSONObject) serverResult.getData()).getJSONArray(Common.ROWS).toJavaList(Server.class);
BocloudResult serverResult = new BocloudResult();
List<Server> 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<JSONObject> list = new ArrayList<>(serverList.size());
for (Server server : serverList) {
JSONObject jsonObject = new JSONObject();
@ -648,6 +726,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<PrometheusResult> 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<PrometheusResult> memUsageAverageResult = prometheusService.query(memUsageAverageFilter, time);
convertServer(memUsageAverageResult, list, CLOUDTOWER.name(), "memUsage");
});
break;
default:
throw new IllegalStateException("Unexpected value: " + vendorType);
}
@ -696,6 +788,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 +836,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 +1003,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<String, String> 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<String, String> 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 +1082,7 @@ public class AnalysisServiceImpl implements AnalysisService {
private void convertCloudServer(List<PrometheusResult> results, List<JSONObject> 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 +1159,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);
}

View File

@ -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<ServerModel> listTargetObjects(List<String> targetIds, MonitorResourceType resourceType) {
List<Integer> 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<Param> 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<String, String> getPrometheusExprParam(ServerModel targetObject, Metric metric) {
Map<String, String> 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<Param> 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<ServerModel> 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());
}
}

View File

@ -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<CloudServer> listTargetObjects(List<String> targetIds, MonitorResourceType resourceType) {
List<Integer> 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<Param> 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<String, String> getPrometheusExprParam(CloudServer targetObject, Metric metric) {
Map<String, String> 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<Param> 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<CloudServer> 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());
}
}

View File

@ -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<ServerModel> list) {
List<String> 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<String> target = list.stream().map(m -> String.valueOf(m.getId())).collect(Collectors.toList());
List<ResourceAlarmStatus> 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<PrometheusResult> 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<PrometheusResult> 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> server = imsServerService.detail(id);
Assert.isTrue(server.isSuccess(), " 未查询到该主机相关信息");
HashMap<String, Object> 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());
@ -218,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) {
@ -230,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) {

View File

@ -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"));

View File

@ -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<Param> 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<Server> list = data.getJSONArray(Common.ROWS).toJavaList(Server.class);
List<File> files = new ArrayList<>();
for (Server server : list) {
Map<String, String> 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
*

View File

@ -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<CloudServerModel> list, RequestContext context) {
List<String> 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<PrometheusResult> cpuList = prometheusService.query("round(bocloud_cloudtower_vm_elf_vm_cpu_overall_usage_percent{instanceId=~'" + instanceId + "'},0.01)");
List<PrometheusResult> memoryList = prometheusService.query("round(bocloud_cloudtower_vm_elf_vm_memory_usage_percent{instanceId=~'" + instanceId + "'},0.01)");
List<String> target = list.stream().map(m -> String.valueOf(m.getId())).collect(Collectors.toList());
List<ResourceAlarmStatus> 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<CloudServerModel> list) {
List<CloudServerModel> vmwareVms = list.stream().filter(s -> s.getVendorType().equalsIgnoreCase(CloudProvider.VMWARE.name())).collect(Collectors.toList());

View File

@ -71,6 +71,7 @@ public class AnalysisHandleRunable implements Runnable {
break;
case MANAGEONE:
case TIANYI:
case CLOUDTOWER:
flag = String.valueOf(vendorId);
break;
default:
@ -83,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("未知的类型");