Search in sources :

Example 16 with MachineStats

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());
    }
}
Also used : MachineStats(com.sohu.cache.entity.MachineStats) Date(java.util.Date)

Aggregations

MachineStats (com.sohu.cache.entity.MachineStats)16 InstanceStats (com.sohu.cache.entity.InstanceStats)6 HashMap (java.util.HashMap)6 MachineInfo (com.sohu.cache.entity.MachineInfo)4 MachineMemInfo (com.sohu.cache.entity.MachineMemInfo)4 SSHException (com.sohu.cache.exception.SSHException)4 Date (java.util.Date)4 AppDesc (com.sohu.cache.entity.AppDesc)2 InstanceInfo (com.sohu.cache.entity.InstanceInfo)2 IllegalParamException (com.sohu.cache.exception.IllegalParamException)2 DefaultLineProcessor (com.sohu.cache.ssh.SSHTemplate.DefaultLineProcessor)2 LineProcessor (com.sohu.cache.ssh.SSHTemplate.LineProcessor)2 Result (com.sohu.cache.ssh.SSHTemplate.Result)2 SSHCallback (com.sohu.cache.ssh.SSHTemplate.SSHCallback)2 SSHSession (com.sohu.cache.ssh.SSHTemplate.SSHSession)2 IOException (java.io.IOException)2 DecimalFormat (java.text.DecimalFormat)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 Test (org.junit.Test)2