|
|
package com.monitor.zabbix.service;
|
|
|
|
|
|
/**
|
|
|
* Created by yoho on 2016/11/2.
|
|
|
*/
|
|
|
|
|
|
import com.monitor.common.config.SnsMobileConfig;
|
|
|
import com.monitor.common.service.AlarmMsgService;
|
|
|
import com.monitor.zabbix.constants.Constants;
|
|
|
import com.monitor.zabbix.enums.NetIfEnum;
|
|
|
import com.monitor.zabbix.enums.SystemCpuEnum;
|
|
|
import com.monitor.zabbix.enums.VmMemoryEnum;
|
|
|
import com.monitor.zabbix.impl.PointBuilder;
|
|
|
import com.monitor.zabbix.mapper.ZabbixHistoryMapper;
|
|
|
import com.monitor.zabbix.mapper.ZabbixHostMapper;
|
|
|
import com.monitor.zabbix.mapper.ZabbixItemMapper;
|
|
|
import com.monitor.zabbix.model.ZabbixDHistoryInfo;
|
|
|
import com.monitor.zabbix.model.ZabbixHostInfo;
|
|
|
import com.monitor.zabbix.model.ZabbixItemInfo;
|
|
|
import com.monitor.zabbix.model.ZabbixUHistoryInfo;
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
import org.joda.time.DateTime;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.scheduling.annotation.EnableScheduling;
|
|
|
import org.springframework.scheduling.annotation.Scheduled;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
import java.util.concurrent.ExecutorService;
|
|
|
import java.util.concurrent.Executors;
|
|
|
import java.util.concurrent.ThreadFactory;
|
|
|
|
|
|
/**
|
|
|
* 全网监控
|
|
|
*/
|
|
|
@Service
|
|
|
@EnableScheduling
|
|
|
public class ZabbixAlarm {
|
|
|
|
|
|
private static String ALARMTEMPLATE = "时间:%s,ip:%s,CPU使用率:%s,可用内存/总内存(MB):%s,输入带宽:%.2fMbps,输出带宽:%.2fMbps";
|
|
|
|
|
|
public static final Logger DEBUG = LoggerFactory.getLogger(ZabbixAlarm.class);
|
|
|
|
|
|
@Autowired
|
|
|
ZabbixHostMapper hostMapper;
|
|
|
|
|
|
@Autowired
|
|
|
ZabbixItemMapper itemMapper;
|
|
|
|
|
|
@Autowired
|
|
|
ZabbixHistoryMapper historyMapper;
|
|
|
|
|
|
@Value("${zabbix_cpu_alarm}")
|
|
|
Double zabbixCpuAlarm;
|
|
|
|
|
|
@Value("${zabbix_mem_alarm}")
|
|
|
Double zabbixMemAlarm;
|
|
|
|
|
|
@Value("${zabbix_net_alarm}")
|
|
|
Double zabbixNetAlarm;
|
|
|
|
|
|
@Autowired
|
|
|
private AlarmMsgService alarmMsgService;
|
|
|
|
|
|
@Autowired
|
|
|
private SnsMobileConfig snsMobileConfig;
|
|
|
|
|
|
|
|
|
private final class ZabbixTask implements Runnable {
|
|
|
List<Integer> hostIdList;
|
|
|
|
|
|
public ZabbixTask(List<Integer> idList) {
|
|
|
|
|
|
this.hostIdList = idList;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void run() {
|
|
|
if (null == hostIdList || 0 == hostIdList.size()) {
|
|
|
return;
|
|
|
}
|
|
|
queryIdToItem(hostIdList);
|
|
|
|
|
|
for (Integer hostId : hostIdList) {
|
|
|
|
|
|
queryHistory(hostId);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
@Scheduled(fixedRate = 60 * 1000)
|
|
|
public void dispatchAlarm() {
|
|
|
|
|
|
queryAllHostInfo();
|
|
|
|
|
|
List<Integer> allIdList = new ArrayList<>();
|
|
|
|
|
|
allIdList.addAll(Constants.ZABBIXALARMIDMPA.keySet());
|
|
|
|
|
|
int executorSize = allIdList.size() / 100 + 1;
|
|
|
|
|
|
ExecutorService executor = Executors.newFixedThreadPool(executorSize, new ThreadFactory() {
|
|
|
@Override
|
|
|
public Thread newThread(Runnable r) {
|
|
|
Thread thread = new Thread(r);
|
|
|
thread.setName("zabbix-alarm-executor");
|
|
|
thread.setDaemon(true);
|
|
|
return thread;
|
|
|
}
|
|
|
});
|
|
|
|
|
|
int start = 0;
|
|
|
|
|
|
int end = 0;
|
|
|
|
|
|
for (int i = 0; i < executorSize; i++) {
|
|
|
|
|
|
if (end >= allIdList.size() - 1) {
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
start = end;
|
|
|
|
|
|
end += (i + 1) * 100;
|
|
|
|
|
|
end = end >= allIdList.size() ? (allIdList.size() - 1) : end;
|
|
|
|
|
|
List<Integer> taskList = allIdList.subList(start, end + 1);
|
|
|
//提交监控任务
|
|
|
executor.submit(new ZabbixTask(taskList));
|
|
|
}
|
|
|
|
|
|
executor.shutdown();
|
|
|
}
|
|
|
|
|
|
//查询所有host信息
|
|
|
private void queryAllHostInfo() {
|
|
|
List<ZabbixHostInfo> zabbixHostInfoList = hostMapper.queryAllHostInfo();
|
|
|
|
|
|
for (ZabbixHostInfo zabbixHostInfo : zabbixHostInfoList) {
|
|
|
|
|
|
Constants.ZABBIXALARMIDMPA.put(zabbixHostInfo.getHostId(), zabbixHostInfo.getIp());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 根据id查询item,转存内存map中,供后续使用
|
|
|
*/
|
|
|
private void queryIdToItem(List<Integer> idList) {
|
|
|
List<Integer> queryIdList = new ArrayList<>();
|
|
|
|
|
|
for (Integer id : idList) {
|
|
|
|
|
|
if (Constants.ZABBIXALARMIDMPA.containsKey(id)) {
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
queryIdList.add(id);
|
|
|
}
|
|
|
|
|
|
if (idList.isEmpty()) {
|
|
|
DEBUG.info("Not found new hostId in {}, no need to query item....", idList);
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
List<ZabbixItemInfo> itemInfoList = itemMapper.queryItemInfo(idList);
|
|
|
|
|
|
DEBUG.info("Found id to items info {}", itemInfoList);
|
|
|
|
|
|
for (ZabbixItemInfo itemInfo : itemInfoList) {
|
|
|
List<ZabbixItemInfo> itemInfos = Constants.ZABBIXALARMITEMMAP.get(itemInfo.getHostId());
|
|
|
|
|
|
if (null == itemInfos) {
|
|
|
itemInfos = new ArrayList<>();
|
|
|
}
|
|
|
|
|
|
itemInfos.add(itemInfo);
|
|
|
|
|
|
Constants.ZABBIXALARMITEMMAP.put(itemInfo.getHostId(), itemInfos);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 根据ip查询 item的历史记录
|
|
|
*/
|
|
|
private void queryHistory(Integer hostId) {
|
|
|
|
|
|
List<ZabbixItemInfo> itemInfos = Constants.ZABBIXALARMITEMMAP.get(hostId);
|
|
|
|
|
|
if (null == itemInfos) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
List<Object> historyList = new ArrayList<>();
|
|
|
|
|
|
for (ZabbixItemInfo itemInfo : itemInfos) {
|
|
|
if (null == itemInfo) {
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
//system.cpu在另外一个库中,调用另一个接口
|
|
|
if (StringUtils.startsWith(itemInfo.getKeyName(), "system.cpu")) {
|
|
|
|
|
|
ZabbixDHistoryInfo dHistoryInfo = historyMapper.queryDHistoryInfo(itemInfo.getItemId());
|
|
|
|
|
|
if (null != dHistoryInfo) {
|
|
|
historyList.add(dHistoryInfo);
|
|
|
}
|
|
|
} else {
|
|
|
ZabbixUHistoryInfo uHistoryInfo = historyMapper.queryUHistoryInfo(itemInfo.getItemId());
|
|
|
|
|
|
if (null != uHistoryInfo) {
|
|
|
historyList.add(uHistoryInfo);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
checkAlarm(hostId, itemInfos, historyList);
|
|
|
}
|
|
|
|
|
|
private void checkAlarm(Integer hostId, List<ZabbixItemInfo> itemInfos, List<Object> historyList) {
|
|
|
|
|
|
String ip = Constants.ZABBIXALARMIDMPA.get(hostId);
|
|
|
|
|
|
if (StringUtils.isBlank(ip) || StringUtils.equals("127.0.0.1", ip)) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
Double idleCpu = PointBuilder.findValueByKey(SystemCpuEnum.CPU_UTIL_IDLE.key(), itemInfos, historyList);
|
|
|
|
|
|
idleCpu = (0 == idleCpu) ? 100 : idleCpu;
|
|
|
|
|
|
Double avMem = PointBuilder.findValueByKey(VmMemoryEnum.MEMORY_SIZE_AVAILABLE.key(), itemInfos, historyList);
|
|
|
|
|
|
Double toMem = PointBuilder.findValueByKey(VmMemoryEnum.MEMORY_SIZE_TOTAL.key(), itemInfos, historyList);
|
|
|
|
|
|
Double memPer = (0 == toMem) ? 1 : (avMem / toMem);
|
|
|
|
|
|
Double inNet = PointBuilder.findValueByKey(NetIfEnum.NET_IF_IN.key(), itemInfos, historyList);
|
|
|
|
|
|
Double outNet = PointBuilder.findValueByKey(NetIfEnum.NET_IF_OUT.key(), itemInfos, historyList);
|
|
|
|
|
|
if (zabbixCpuAlarm > idleCpu || zabbixMemAlarm > memPer || zabbixNetAlarm < inNet || zabbixNetAlarm < outNet) {
|
|
|
|
|
|
String nowString = DateTime.now().toString("yyyy-MM-dd HH:mm:ss");
|
|
|
|
|
|
String alarmInfo = String.format(ALARMTEMPLATE, nowString, ip, String.valueOf(100 - idleCpu), String.valueOf(avMem) + " / " + String.valueOf(toMem), inNet, outNet);
|
|
|
|
|
|
DEBUG.info("Alarm vm info {}", alarmInfo);
|
|
|
|
|
|
alarmMsgService.sendSms("服务器性能告警", alarmInfo, snsMobileConfig.getBaseMobile());
|
|
|
}
|
|
|
}
|
|
|
} |
...
|
...
|
|