Search in sources :

Example 6 with MachineStats

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

Example 7 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)

Example 8 with MachineStats

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;
}
Also used : MachineInfo(com.sohu.cache.entity.MachineInfo) InstanceStats(com.sohu.cache.entity.InstanceStats) MachineStats(com.sohu.cache.entity.MachineStats) MachineMemInfo(com.sohu.cache.entity.MachineMemInfo)

Aggregations

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