| 
 | 1 | +package com.sohu.cache.alert.strategy;  | 
 | 2 | + | 
 | 3 | +import java.util.List;  | 
 | 4 | +import java.util.Map;  | 
 | 5 | +import java.util.Map.Entry;  | 
 | 6 | + | 
 | 7 | +import org.apache.commons.collections.MapUtils;  | 
 | 8 | +import org.apache.commons.lang.math.NumberUtils;  | 
 | 9 | + | 
 | 10 | +import com.sohu.cache.alert.bean.AlertConfigBaseData;  | 
 | 11 | +import com.sohu.cache.entity.InstanceAlertConfig;  | 
 | 12 | +import com.sohu.cache.entity.InstanceAlertValueResult;  | 
 | 13 | +import com.sohu.cache.entity.InstanceInfo;  | 
 | 14 | +import com.sohu.cache.entity.StandardStats;  | 
 | 15 | +import com.sohu.cache.redis.enums.InstanceAlertCompareTypeEnum;  | 
 | 16 | +import com.sohu.cache.util.JsonUtil;  | 
 | 17 | + | 
 | 18 | +/**  | 
 | 19 | + * @author leifu  | 
 | 20 | + * @Date 2017年6月2日  | 
 | 21 | + * @Time 下午3:24:53  | 
 | 22 | + */  | 
 | 23 | +public abstract class AlertConfigStrategy {  | 
 | 24 | +      | 
 | 25 | +    protected final static String MB_STRING = "MB";  | 
 | 26 | +    protected final static String EMPTY = "";  | 
 | 27 | + | 
 | 28 | +    /**  | 
 | 29 | +     * 检查配置  | 
 | 30 | +     *   | 
 | 31 | +     * @param instanceAlertConfig  | 
 | 32 | +     * @param alertConfigBaseData  | 
 | 33 | +     */  | 
 | 34 | +    public abstract List<InstanceAlertValueResult> checkConfig(InstanceAlertConfig instanceAlertConfig,  | 
 | 35 | +            AlertConfigBaseData alertConfigBaseData);  | 
 | 36 | + | 
 | 37 | +    /**  | 
 | 38 | +     * 比较long类型  | 
 | 39 | +     *   | 
 | 40 | +     * @param instanceAlertConfig 报警配置  | 
 | 41 | +     * @param currentValue 当前值  | 
 | 42 | +     * @return  | 
 | 43 | +     */  | 
 | 44 | +    protected boolean isCompareLongRight(InstanceAlertConfig instanceAlertConfig, long currentValue) {  | 
 | 45 | +        long alertValue = NumberUtils.toLong(instanceAlertConfig.getAlertValue());  | 
 | 46 | +        int compareType = instanceAlertConfig.getCompareType();  | 
 | 47 | +        if (compareType == InstanceAlertCompareTypeEnum.LESS_THAN.getValue() && currentValue < alertValue) {  | 
 | 48 | +            return false;  | 
 | 49 | +        } else if (compareType == InstanceAlertCompareTypeEnum.MORE_THAN.getValue() && currentValue > alertValue) {  | 
 | 50 | +            return false;  | 
 | 51 | +        } else if (compareType == InstanceAlertCompareTypeEnum.EQUAL.getValue() && currentValue == alertValue) {  | 
 | 52 | +            return false;  | 
 | 53 | +        } else if (compareType == InstanceAlertCompareTypeEnum.NOT_EQUAL.getValue() && currentValue != alertValue) {  | 
 | 54 | +            return false;  | 
 | 55 | +        }  | 
 | 56 | +        return true;  | 
 | 57 | +    }  | 
 | 58 | + | 
 | 59 | +    /**  | 
 | 60 | +     * 比较int类型  | 
 | 61 | +     *   | 
 | 62 | +     * @param instanceAlertConfig 报警配置  | 
 | 63 | +     * @param currentValue 当前值  | 
 | 64 | +     * @return  | 
 | 65 | +     */  | 
 | 66 | +    protected boolean isCompareIntRight(InstanceAlertConfig instanceAlertConfig, int currentValue) {  | 
 | 67 | +        int alertValue = NumberUtils.toInt(instanceAlertConfig.getAlertValue());  | 
 | 68 | +        int compareType = instanceAlertConfig.getCompareType();  | 
 | 69 | +        if (compareType == InstanceAlertCompareTypeEnum.LESS_THAN.getValue() && currentValue < alertValue) {  | 
 | 70 | +            return false;  | 
 | 71 | +        } else if (compareType == InstanceAlertCompareTypeEnum.MORE_THAN.getValue() && currentValue > alertValue) {  | 
 | 72 | +            return false;  | 
 | 73 | +        } else if (compareType == InstanceAlertCompareTypeEnum.EQUAL.getValue() && currentValue == alertValue) {  | 
 | 74 | +            return false;  | 
 | 75 | +        } else if (compareType == InstanceAlertCompareTypeEnum.NOT_EQUAL.getValue() && currentValue != alertValue) {  | 
 | 76 | +            return false;  | 
 | 77 | +        }  | 
 | 78 | +        return true;  | 
 | 79 | +    }  | 
 | 80 | +      | 
 | 81 | +    /**  | 
 | 82 | +     * 比较double类型  | 
 | 83 | +     *   | 
 | 84 | +     * @param instanceAlertConfig 报警配置  | 
 | 85 | +     * @param currentValue 当前值  | 
 | 86 | +     * @return  | 
 | 87 | +     */  | 
 | 88 | +    protected boolean isCompareDoubleRight(InstanceAlertConfig instanceAlertConfig, double currentValue) {  | 
 | 89 | +        double alertValue = NumberUtils.toDouble(instanceAlertConfig.getAlertValue());  | 
 | 90 | +        int compareType = instanceAlertConfig.getCompareType();  | 
 | 91 | +        if (compareType == InstanceAlertCompareTypeEnum.LESS_THAN.getValue() && currentValue < alertValue) {  | 
 | 92 | +            return false;  | 
 | 93 | +        } else if (compareType == InstanceAlertCompareTypeEnum.MORE_THAN.getValue() && currentValue > alertValue) {  | 
 | 94 | +            return false;  | 
 | 95 | +        } else if (compareType == InstanceAlertCompareTypeEnum.EQUAL.getValue() && currentValue == alertValue) {  | 
 | 96 | +            return false;  | 
 | 97 | +        } else if (compareType == InstanceAlertCompareTypeEnum.NOT_EQUAL.getValue() && currentValue != alertValue) {  | 
 | 98 | +            return false;  | 
 | 99 | +        }  | 
 | 100 | +        return true;  | 
 | 101 | +    }  | 
 | 102 | + | 
 | 103 | +    /**  | 
 | 104 | +     * 比较字符串类型  | 
 | 105 | +     *   | 
 | 106 | +     * @param instanceAlertConfig 报警配置  | 
 | 107 | +     * @param currentValue 当期值  | 
 | 108 | +     * @return  | 
 | 109 | +     */  | 
 | 110 | +    protected boolean isCompareStringRight(InstanceAlertConfig instanceAlertConfig, String currentValue) {  | 
 | 111 | +        String alertValue = instanceAlertConfig.getAlertValue();  | 
 | 112 | +        int compareType = instanceAlertConfig.getCompareType();  | 
 | 113 | +        if (compareType == InstanceAlertCompareTypeEnum.EQUAL.getValue() && currentValue.equals(alertValue)) {  | 
 | 114 | +            return false;  | 
 | 115 | +        } else if (compareType == InstanceAlertCompareTypeEnum.NOT_EQUAL.getValue()  | 
 | 116 | +                && !currentValue.equals(alertValue)) {  | 
 | 117 | +            return false;  | 
 | 118 | +        }  | 
 | 119 | +        return true;  | 
 | 120 | +    }  | 
 | 121 | + | 
 | 122 | +    /**  | 
 | 123 | +     * 生成instance级别报警文案  | 
 | 124 | +     * @param instanceAlertConfig  | 
 | 125 | +     * @param instanceInfo  | 
 | 126 | +     * @param currentValue  | 
 | 127 | +     * @param unit  | 
 | 128 | +     * @return  | 
 | 129 | +     */  | 
 | 130 | +    protected String genInstanceAlertText(InstanceAlertConfig instanceAlertConfig, InstanceInfo instanceInfo,  | 
 | 131 | +            String currentValue, String unit) {  | 
 | 132 | +        String configKey = instanceAlertConfig.getAlertConfig();  | 
 | 133 | +        String configValue = instanceAlertConfig.getAlertValue();  | 
 | 134 | +        String compareTypeInfo = InstanceAlertCompareTypeEnum  | 
 | 135 | +                .getInstanceAlertCompareTypeEnum(instanceAlertConfig.getCompareType()).getInfo();  | 
 | 136 | +        StringBuilder alertText = new StringBuilder();  | 
 | 137 | +        alertText.append(instanceInfo.getHostPort());  | 
 | 138 | +        alertText.append(",报警项:").append(configKey);  | 
 | 139 | +        alertText.append("=").append(currentValue).append(unit).append(",");  | 
 | 140 | +        alertText.append(compareTypeInfo);  | 
 | 141 | +        alertText.append("报警阈值:").append(configValue).append(unit);  | 
 | 142 | +        return alertText.toString();  | 
 | 143 | +    }  | 
 | 144 | + | 
 | 145 | +    /**  | 
 | 146 | +     * 获取全量统计项中的内容  | 
 | 147 | +     * @param redisInfo  | 
 | 148 | +     * @param attribute  | 
 | 149 | +     * @return  | 
 | 150 | +     */  | 
 | 151 | +    protected static Object getValueFromRedisInfo(StandardStats standardStats, String attribute) {  | 
 | 152 | +        if (standardStats == null) {  | 
 | 153 | +            return null;  | 
 | 154 | +        }  | 
 | 155 | +        // 转换成Map  | 
 | 156 | +        Map<String, Object> infoMap = JsonUtil.fromJson(standardStats.getInfoJson(), Map.class);  | 
 | 157 | +        if (MapUtils.isEmpty(infoMap)) {  | 
 | 158 | +            return null;  | 
 | 159 | +        }  | 
 | 160 | +        for (Entry<String, Object> entry : infoMap.entrySet()) {  | 
 | 161 | +            Object object = entry.getValue();  | 
 | 162 | +            // 转换成Map<String, Map<String,Object>>  | 
 | 163 | +            if (!(object instanceof Map)) {  | 
 | 164 | +                continue;  | 
 | 165 | +            }  | 
 | 166 | +            Map<String, Object> sectionInfoMap = (Map<String, Object>) object;  | 
 | 167 | +            if (sectionInfoMap != null && sectionInfoMap.containsKey(attribute)) {  | 
 | 168 | +                return MapUtils.getObject(sectionInfoMap, attribute);  | 
 | 169 | +            }  | 
 | 170 | +        }  | 
 | 171 | +        return null;  | 
 | 172 | +    }  | 
 | 173 | +      | 
 | 174 | +    /**  | 
 | 175 | +     * 获取差值统计项中的内容  | 
 | 176 | +     * @param redisInfo  | 
 | 177 | +     * @param attribute  | 
 | 178 | +     * @return  | 
 | 179 | +     */  | 
 | 180 | +    protected static Object getValueFromDiffInfo(StandardStats standardStats, String attribute) {  | 
 | 181 | +        if (standardStats == null) {  | 
 | 182 | +            return null;  | 
 | 183 | +        }  | 
 | 184 | +        Map<String, Object> diffInfoMap = JsonUtil.fromJson(standardStats.getDiffJson(), Map.class);  | 
 | 185 | +        if (MapUtils.isEmpty(diffInfoMap)) {  | 
 | 186 | +            return null;  | 
 | 187 | +        }  | 
 | 188 | +        return MapUtils.getObject(diffInfoMap, attribute);  | 
 | 189 | +    }  | 
 | 190 | +      | 
 | 191 | +    /**  | 
 | 192 | +     * 获取cluster info统计项中的内容  | 
 | 193 | +     * @param redisInfo  | 
 | 194 | +     * @param attribute  | 
 | 195 | +     * @return  | 
 | 196 | +     */  | 
 | 197 | +    protected static Object getValueFromClusterInfo(StandardStats standardStats, String attribute) {  | 
 | 198 | +        if (standardStats == null) {  | 
 | 199 | +            return null;  | 
 | 200 | +        }  | 
 | 201 | +        Map<String, Object> clusterInfoMap = JsonUtil.fromJson(standardStats.getClusterInfoJson(), Map.class);  | 
 | 202 | +        if (MapUtils.isEmpty(clusterInfoMap)) {  | 
 | 203 | +            return null;  | 
 | 204 | +        }  | 
 | 205 | +        return MapUtils.getObject(clusterInfoMap, attribute);  | 
 | 206 | +    }  | 
 | 207 | +      | 
 | 208 | +    /**  | 
 | 209 | +     * 把字节变为兆  | 
 | 210 | +     * @param value  | 
 | 211 | +     * @return  | 
 | 212 | +     */  | 
 | 213 | +    protected long changeByteToMB(long value) {  | 
 | 214 | +        return value / 1024 / 1024;  | 
 | 215 | +    }  | 
 | 216 | + | 
 | 217 | +}  | 
0 commit comments