use of com.sohu.cache.entity.MachineStats in project cachecloud by sohutv.
the class MachineCenterImpl method collectMachineInfo.
/**
* 收集当前host的状态信息,保存到mysql;
* 这里将hostId作为参数传入,mysql中集合名为:ip:hostId
*
* @param hostId 机器id
* @param collectTime 收集时间,格式:yyyyMMddHHmm
* @param ip ip
* @return 机器的统计信息
*/
@Override
public Map<String, Object> collectMachineInfo(final long hostId, final long collectTime, final String ip) {
Map<String, Object> infoMap = new HashMap<String, Object>();
MachineStats machineStats = null;
try {
int sshPort = SSHUtil.getSshPort(ip);
machineStats = SSHUtil.getMachineInfo(ip, sshPort, ConstUtils.USERNAME, ConstUtils.PASSWORD);
machineStats.setHostId(hostId);
if (machineStats != null) {
infoMap.put(MachineConstant.Ip.getValue(), machineStats.getIp());
infoMap.put(MachineConstant.CpuUsage.getValue(), machineStats.getCpuUsage());
infoMap.put(MachineConstant.MemoryUsageRatio.getValue(), machineStats.getMemoryUsageRatio());
/**
* SSHUtil返回的内存单位为k,由于实例的内存基本存储单位都是byte,所以统一为byte
*/
if (machineStats.getMemoryFree() != null) {
infoMap.put(MachineConstant.MemoryFree.getValue(), Long.valueOf(machineStats.getMemoryFree()) * ConstUtils._1024);
} else {
infoMap.put(MachineConstant.MemoryFree.getValue(), 0);
}
infoMap.put(MachineConstant.MemoryTotal.getValue(), Long.valueOf(machineStats.getMemoryTotal()) * ConstUtils._1024);
infoMap.put(MachineConstant.Load.getValue(), machineStats.getLoad());
infoMap.put(MachineConstant.Traffic.getValue(), machineStats.getTraffic());
infoMap.put(MachineConstant.DiskUsage.getValue(), machineStats.getDiskUsageMap());
infoMap.put(ConstUtils.COLLECT_TIME, collectTime);
instanceStatsCenter.saveStandardStats(infoMap, ip, (int) hostId, ConstUtils.MACHINE);
machineStats.setMemoryFree(Long.valueOf(machineStats.getMemoryFree()) * ConstUtils._1024 + "");
machineStats.setMemoryTotal(Long.valueOf(machineStats.getMemoryTotal()) * ConstUtils._1024 + "");
machineStats.setModifyTime(new Date());
machineStatsDao.mergeMachineStats(machineStats);
logger.info("collect machine info done, host: {}, time: {}", ip, collectTime);
}
} catch (Exception e) {
logger.error("collectMachineErrorStats=>" + machineStats);
logger.error(e.getMessage(), e);
}
return infoMap;
}
use of com.sohu.cache.entity.MachineStats in project cachecloud by sohutv.
the class MachineCenterImpl method monitorMachineStats.
/**
* 监控机器的状态
*
* @param hostId 机器id
* @param ip ip
*/
@Override
public void monitorMachineStats(final long hostId, final String ip) {
Assert.isTrue(hostId > 0);
Assert.hasText(ip);
MachineStats machineStats = machineStatsDao.getMachineStatsByIp(ip);
if (machineStats == null) {
logger.warn("machine stats is null, ip: {}, time: {}", ip, new Date());
return;
}
double cpuUsage = ObjectConvert.percentToDouble(machineStats.getCpuUsage(), 0);
double memoryUsage = ObjectConvert.percentToDouble(machineStats.getMemoryUsageRatio(), 0);
double load = 0;
try {
load = Double.valueOf(machineStats.getLoad());
} catch (NumberFormatException e) {
logger.error(e.getMessage(), e);
}
double memoryThreshold = ConstUtils.MEMORY_USAGE_RATIO_THRESHOLD;
/**
* 当机器的状态超过预设的阀值时,向上汇报或者报警
*/
StringBuilder alertContent = new StringBuilder();
// cpu使用率 todo
if (cpuUsage > ConstUtils.CPU_USAGE_RATIO_THRESHOLD) {
logger.warn("cpuUsageRatio is above security line. ip: {}, cpuUsage: {}%", ip, cpuUsage);
alertContent.append("ip:").append(ip).append(",cpuUse:").append(cpuUsage);
}
// 内存使用率 todo
if (memoryUsage > memoryThreshold) {
logger.warn("memoryUsageRatio is above security line, ip: {}, memoryUsage: {}%", ip, memoryUsage);
alertContent.append("ip:").append(ip).append(",memUse:").append(memoryUsage);
}
// 负载 todo
if (load > ConstUtils.LOAD_THRESHOLD) {
logger.warn("load is above security line, ip: {}, load: {}%", ip, load);
alertContent.append("ip:").append(ip).append(",load:").append(load);
}
// 报警
if (StringUtils.isNotBlank(alertContent.toString())) {
String title = "cachecloud机器异常:";
emailComponent.sendMailToAdmin(title, alertContent.toString());
mobileAlertComponent.sendPhoneToAdmin(title + alertContent.toString());
}
}
use of com.sohu.cache.entity.MachineStats in project cachecloud by sohutv.
the class MachineCenterImpl method getAllMachineStats.
@Override
public List<MachineStats> getAllMachineStats() {
List<MachineStats> list = machineStatsDao.getAllMachineStats();
for (MachineStats ms : list) {
String ip = ms.getIp();
MachineInfo machineInfo = machineDao.getMachineInfoByIp(ip);
if (machineInfo == null || machineInfo.isOffline()) {
continue;
}
int memoryHost = instanceDao.getMemoryByHost(ip);
getMachineMemoryDetail(ms.getIp());
//获取机器申请和使用内存
long applyMem = 0;
long usedMem = 0;
List<InstanceStats> instanceStats = instanceStatsDao.getInstanceStatsByIp(ip);
for (InstanceStats instance : instanceStats) {
applyMem += instance.getMaxMemory();
usedMem += instance.getUsedMemory();
}
MachineMemInfo machineMemInfo = new MachineMemInfo();
machineMemInfo.setIp(ip);
machineMemInfo.setApplyMem(applyMem);
machineMemInfo.setUsedMem(usedMem);
ms.setMachineMemInfo(machineMemInfo);
ms.setMemoryAllocated(memoryHost);
ms.setInfo(machineInfo);
}
return list;
}
Aggregations