Authored by jack

连续三次触发阈值 告警

@@ -2,6 +2,7 @@ package com.monitor.zabbix.constants; @@ -2,6 +2,7 @@ package com.monitor.zabbix.constants;
2 2
3 import com.fasterxml.jackson.databind.DeserializationFeature; 3 import com.fasterxml.jackson.databind.DeserializationFeature;
4 import com.fasterxml.jackson.databind.ObjectMapper; 4 import com.fasterxml.jackson.databind.ObjectMapper;
  5 +import com.monitor.zabbix.model.ErrorAlarm;
5 import com.monitor.zabbix.model.ZabbixItemInfo; 6 import com.monitor.zabbix.model.ZabbixItemInfo;
6 7
7 import java.util.HashMap; 8 import java.util.HashMap;
@@ -9,6 +10,7 @@ import java.util.List; @@ -9,6 +10,7 @@ import java.util.List;
9 import java.util.Map; 10 import java.util.Map;
10 import java.util.concurrent.ConcurrentHashMap; 11 import java.util.concurrent.ConcurrentHashMap;
11 import java.util.concurrent.CopyOnWriteArrayList; 12 import java.util.concurrent.CopyOnWriteArrayList;
  13 +import java.util.concurrent.atomic.AtomicInteger;
12 14
13 /** 15 /**
14 * Created by yoho on 2016/10/11. 16 * Created by yoho on 2016/10/11.
@@ -38,4 +40,6 @@ public interface Constants { @@ -38,4 +40,6 @@ public interface Constants {
38 Map<Integer, String> ZABBIXALARMIDMPA = new ConcurrentHashMap<>(); 40 Map<Integer, String> ZABBIXALARMIDMPA = new ConcurrentHashMap<>();
39 41
40 Map<Integer, CopyOnWriteArrayList<ZabbixItemInfo>> ZABBIXALARMITEMMAP = new ConcurrentHashMap<>(); 42 Map<Integer, CopyOnWriteArrayList<ZabbixItemInfo>> ZABBIXALARMITEMMAP = new ConcurrentHashMap<>();
  43 +
  44 + Map<String, ErrorAlarm> ERRORMAP = new ConcurrentHashMap<>();
41 } 45 }
  1 +package com.monitor.zabbix.model;
  2 +
  3 +import lombok.Data;
  4 +
  5 +/**
  6 + * Created by yoho on 2016/11/3.
  7 + */
  8 +@Data
  9 +public class ErrorAlarm {
  10 +
  11 + Long time;
  12 +
  13 + Integer count = 1;
  14 +
  15 +}
@@ -16,10 +16,7 @@ import com.monitor.zabbix.impl.PointBuilder; @@ -16,10 +16,7 @@ import com.monitor.zabbix.impl.PointBuilder;
16 import com.monitor.zabbix.mapper.ZabbixHistoryMapper; 16 import com.monitor.zabbix.mapper.ZabbixHistoryMapper;
17 import com.monitor.zabbix.mapper.ZabbixHostMapper; 17 import com.monitor.zabbix.mapper.ZabbixHostMapper;
18 import com.monitor.zabbix.mapper.ZabbixItemMapper; 18 import com.monitor.zabbix.mapper.ZabbixItemMapper;
19 -import com.monitor.zabbix.model.ZabbixDHistoryInfo;  
20 -import com.monitor.zabbix.model.ZabbixHostInfo;  
21 -import com.monitor.zabbix.model.ZabbixItemInfo;  
22 -import com.monitor.zabbix.model.ZabbixUHistoryInfo; 19 +import com.monitor.zabbix.model.*;
23 import org.apache.commons.lang.StringUtils; 20 import org.apache.commons.lang.StringUtils;
24 import org.joda.time.DateTime; 21 import org.joda.time.DateTime;
25 import org.slf4j.Logger; 22 import org.slf4j.Logger;
@@ -44,30 +41,30 @@ import java.util.concurrent.ThreadFactory; @@ -44,30 +41,30 @@ import java.util.concurrent.ThreadFactory;
44 @EnableScheduling 41 @EnableScheduling
45 public class ZabbixAlarm { 42 public class ZabbixAlarm {
46 43
47 - private static String ALARMTEMPLATE = "时间:%s,ip:%s,标签:%s,CPU使用率:%.2f%%,可用内存/总内存(MB):%s,输入带宽:%.2fMbps,输出带宽:%.2fMbps"; 44 + private static String ALARMTEMPLATE = "时间:%s,IP:%s,标签:%s CPU使用率:%.2f%%,可用内存/总内存(MB):%s,输入/输出带宽(Mbps):%.2f / %.2f";
48 45
49 public static final Logger DEBUG = LoggerFactory.getLogger(ZabbixAlarm.class); 46 public static final Logger DEBUG = LoggerFactory.getLogger(ZabbixAlarm.class);
50 47
51 @Autowired 48 @Autowired
52 - ZabbixHostMapper hostMapper; 49 + private ZabbixHostMapper hostMapper;
53 50
54 @Autowired 51 @Autowired
55 - ZabbixItemMapper itemMapper; 52 + private ZabbixItemMapper itemMapper;
56 53
57 @Autowired 54 @Autowired
58 - ZabbixHistoryMapper historyMapper; 55 + private ZabbixHistoryMapper historyMapper;
59 56
60 @Value("${zabbix_cpu_alarm}") 57 @Value("${zabbix_cpu_alarm}")
61 - Double zabbixCpuAlarm; 58 + private Double zabbixCpuAlarm;
62 59
63 @Value("${zabbix_mem_alarm}") 60 @Value("${zabbix_mem_alarm}")
64 - Double zabbixMemAlarm; 61 + private Double zabbixMemAlarm;
65 62
66 @Value("${zabbix_net_alarm}") 63 @Value("${zabbix_net_alarm}")
67 - Double zabbixNetAlarm; 64 + private Double zabbixNetAlarm;
68 65
69 @Autowired 66 @Autowired
70 - IHostInfoService iHostInfoService; 67 + private IHostInfoService iHostInfoService;
71 68
72 @Autowired 69 @Autowired
73 private AlarmMsgService alarmMsgService; 70 private AlarmMsgService alarmMsgService;
@@ -75,7 +72,6 @@ public class ZabbixAlarm { @@ -75,7 +72,6 @@ public class ZabbixAlarm {
75 @Autowired 72 @Autowired
76 private SnsMobileConfig snsMobileConfig; 73 private SnsMobileConfig snsMobileConfig;
77 74
78 -  
79 private final class ZabbixTask implements Runnable { 75 private final class ZabbixTask implements Runnable {
80 List<Integer> hostIdList; 76 List<Integer> hostIdList;
81 77
@@ -275,15 +271,75 @@ public class ZabbixAlarm { @@ -275,15 +271,75 @@ public class ZabbixAlarm {
275 271
276 //|| zabbixMemAlarm > memPer 272 //|| zabbixMemAlarm > memPer
277 273
278 - if (zabbixCpuAlarm > idleCpu || zabbixNetAlarm < inNet || zabbixNetAlarm < outNet) { 274 + if ((zabbixCpuAlarm > idleCpu || zabbixNetAlarm < inNet || zabbixNetAlarm < outNet) && queryErrorMap(ip)) {
  275 +
  276 + String nowTime = DateTime.now().toString("yyyy-MM-dd HH:mm:ss");
279 277
280 - String nowString = DateTime.now().toString("yyyy-MM-dd HH:mm:ss"); 278 + String alarmInfo = String.format(ALARMTEMPLATE, nowTime, ip, tags, 100 - idleCpu, String.valueOf(avMem) + " / " + String.valueOf(toMem), inNet, outNet);
281 279
282 - String alarmInfo = String.format(ALARMTEMPLATE, nowString, ip, tags, 100 - idleCpu, String.valueOf(avMem) + " / " + String.valueOf(toMem), inNet, outNet); 280 + DEBUG.info("3m 3times ,send alarm vm info {}", alarmInfo);
283 281
284 - DEBUG.info("Alarm vm info {}", alarmInfo); 282 + Constants.ERRORMAP.remove(ip);
285 283
286 alarmMsgService.sendSms("服务器性能告警", alarmInfo, snsMobileConfig.getBaseMobile()); 284 alarmMsgService.sendSms("服务器性能告警", alarmInfo, snsMobileConfig.getBaseMobile());
287 } 285 }
288 } 286 }
  287 +
  288 +
  289 + /**
  290 + * 查询最近告警记录,未满三次的不发送告警短信,满三次发送告警短信
  291 + * @param ip
  292 + * @return
  293 + */
  294 + private boolean queryErrorMap(String ip) {
  295 +
  296 + Long nowTimeStamp = System.currentTimeMillis();
  297 +
  298 + ErrorAlarm errorAlarm = Constants.ERRORMAP.get(ip);
  299 +
  300 + //最近没有超过告警阈值,新建alarm记录
  301 + if (null == errorAlarm) {
  302 +
  303 + errorAlarm = new ErrorAlarm();
  304 +
  305 + errorAlarm.setCount(1);
  306 +
  307 + errorAlarm.setTime(nowTimeStamp);
  308 +
  309 + Constants.ERRORMAP.put(ip, errorAlarm);
  310 +
  311 + return false;
  312 +
  313 + } else {
  314 + Long lastTime = errorAlarm.getTime();
  315 +
  316 + //间隔一分钟,考虑zabbix上报延迟,放长到90s,未连续,则清除记录,不发送
  317 + if ((90 * 1000) < (nowTimeStamp - lastTime)) {
  318 +
  319 + Constants.ERRORMAP.remove(ip);
  320 +
  321 + return false;
  322 +
  323 + } else {
  324 +
  325 + Integer lastCount = errorAlarm.getCount();
  326 +
  327 + //连续3次 告警
  328 + if (2 <= lastCount) {
  329 +
  330 + Constants.ERRORMAP.remove(ip);
  331 +
  332 + return true;
  333 + }
  334 +
  335 + //未三次,更新错误时间与连续的次数
  336 + errorAlarm.setTime(nowTimeStamp);
  337 +
  338 + errorAlarm.setCount(lastCount++);
  339 +
  340 + return false;
  341 + }
  342 + }
  343 + }
289 } 344 }
  345 +