use of com.sohu.cache.entity.MachineStats in project new-cloud by xie-summer.
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, new HashMap<String, Object>(0), 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 new-cloud by xie-summer.
the class SSHUtilTest method testMachineStats.
@Test
public void testMachineStats() throws SSHException {
String ip = "127.0.0.1";
int port = 22;
String userName = "cachecloud-open";
String password = "cachecloud-open";
MachineStats machineStats = SSHUtil.getMachineInfo(ip, port, userName, password);
logger.info("ip {} machineStats: {}", machineStats);
}
use of com.sohu.cache.entity.MachineStats in project new-cloud by xie-summer.
the class SSHUtil method getMachineInfo.
/**
* Get HostPerformanceEntity[cpuUsage, memUsage, load] by ssh.<br>
* 方法返回前已经释放了所有资源,调用方不需要关心
*
* @param ip
* @param userName
* @param password
* @throws Exception
* @since 1.0.0
*/
public static MachineStats getMachineInfo(String ip, int port, String userName, String password) throws SSHException {
if (StringUtil.isBlank(ip)) {
try {
throw new IllegalParamException("Param ip is empty!");
} catch (IllegalParamException e) {
throw new SSHException(e.getMessage(), e);
}
}
port = IntegerUtil.defaultIfSmallerThan0(port, ConstUtils.SSH_PORT_DEFAULT);
final MachineStats systemPerformanceEntity = new MachineStats();
systemPerformanceEntity.setIp(ip);
sshTemplate.execute(ip, port, userName, password, new SSHCallback() {
@Override
public Result call(SSHSession session) {
// 解析top命令
session.executeCommand(COMMAND_TOP, new DefaultLineProcessor() {
@Override
public void process(String line, int lineNum) throws Exception {
if (lineNum > 5) {
return;
}
if (1 == lineNum) {
// 第一行,通常是这样:
// top - 19:58:52 up 416 days, 30 min, 1 user, load average:
// 0.00, 0.00, 0.00
int loadAverageIndex = line.indexOf(LOAD_AVERAGE_STRING);
String loadAverages = line.substring(loadAverageIndex).replace(LOAD_AVERAGE_STRING, EMPTY_STRING);
String[] loadAverageArray = loadAverages.split(",");
if (3 == loadAverageArray.length) {
systemPerformanceEntity.setLoad(StringUtil.trimToEmpty(loadAverageArray[0]));
}
} else if (3 == lineNum) {
// 第三行通常是这样:
// , 0.0% sy, 0.0% ni, 100.0% id, 0.0% wa,
// 0.0% hi, 0.0% si
// redhat:%Cpu(s): 0.0 us
// centos7:Cpu(s): 0.0% us
double cpuUs = getUsCpu(line);
systemPerformanceEntity.setCpuUsage(String.valueOf(cpuUs));
}
}
});
// 解析memory
session.executeCommand(COMMAND_MEM, new LineProcessor() {
private String totalMem;
private String freeMem;
private String buffersMem;
private String cachedMem;
@Override
public void process(String line, int lineNum) throws Exception {
if (line.contains(MEM_TOTAL)) {
totalMem = matchMemLineNumber(line).trim();
} else if (line.contains(MEM_FREE)) {
freeMem = matchMemLineNumber(line).trim();
} else if (line.contains(MEM_BUFFERS)) {
buffersMem = matchMemLineNumber(line).trim();
} else if (line.contains(MEM_CACHED)) {
cachedMem = matchMemLineNumber(line).trim();
}
}
@Override
public void finish() {
if (!StringUtil.isBlank(totalMem, freeMem, buffersMem)) {
Long totalMemLong = NumberUtils.toLong(totalMem);
Long freeMemLong = NumberUtils.toLong(freeMem);
Long buffersMemLong = NumberUtils.toLong(buffersMem);
Long cachedMemLong = NumberUtils.toLong(cachedMem);
Long usedMemFree = freeMemLong + buffersMemLong + cachedMemLong;
Double memoryUsage = 1 - (NumberUtils.toDouble(usedMemFree.toString()) / NumberUtils.toDouble(totalMemLong.toString()) / 1.0);
systemPerformanceEntity.setMemoryTotal(String.valueOf(totalMemLong));
systemPerformanceEntity.setMemoryFree(String.valueOf(usedMemFree));
DecimalFormat df = new DecimalFormat("0.00");
systemPerformanceEntity.setMemoryUsageRatio(df.format(memoryUsage * 100));
}
}
});
// 统计磁盘使用状况
/**
* 内容通常是这样: Filesystem 容量 已用 可用 已用% 挂载点 /dev/xvda2 5.8G 3.2G 2.4G
* 57% / /dev/xvda1 99M 8.0M 86M 9% /boot none 769M 0 769M 0%
* /dev/shm /dev/xvda7 68G 7.1G 57G 12% /home /dev/xvda6 2.0G 36M
* 1.8G 2% /tmp /dev/xvda5 2.0G 199M 1.7G 11% /var
*/
session.executeCommand(COMMAND_DF_LH, new LineProcessor() {
private Map<String, String> diskUsageMap = new HashMap<String, String>();
@Override
public void process(String line, int lineNum) throws Exception {
if (lineNum == 1) {
return;
}
line = line.replaceAll(" {1,}", WORD_SEPARATOR);
String[] lineArray = line.split(WORD_SEPARATOR);
if (6 == lineArray.length) {
String diskUsage = lineArray[4];
String mountedOn = lineArray[5];
diskUsageMap.put(mountedOn, diskUsage);
}
}
@Override
public void finish() {
systemPerformanceEntity.setDiskUsageMap(diskUsageMap);
}
});
return null;
}
});
// 统计当前网络流量 @TODO
Double traffic = 0.0;
systemPerformanceEntity.setTraffic(traffic.toString());
return systemPerformanceEntity;
}
use of com.sohu.cache.entity.MachineStats in project new-cloud by xie-summer.
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