|
|
package com.monitor.javaserver.handle.impl;
|
|
|
|
|
|
import com.model.JavaApiInfo;
|
|
|
import com.model.MObjectInfo;
|
|
|
import com.monitor.common.config.SnsMobileConfig;
|
|
|
import com.monitor.common.service.AlarmMsgService;
|
|
|
import com.monitor.javaserver.common.JavaApiStatics;
|
|
|
import com.monitor.javaserver.handle.IJavaApiHadnler;
|
|
|
import org.apache.commons.collections.map.MultiKeyMap;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
import org.springframework.util.MultiValueMap;
|
|
|
|
|
|
import java.util.Map;
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
|
import java.util.concurrent.ConcurrentMap;
|
|
|
import java.util.concurrent.atomic.AtomicInteger;
|
|
|
|
|
|
/**
|
|
|
* Created by fruwei on 2016/6/22.
|
|
|
*/
|
|
|
@Component("msgJavaApiHandler")
|
|
|
public class MsgJavaApiHandler implements IJavaApiHadnler {
|
|
|
|
|
|
Logger log = LoggerFactory.getLogger(InfluxDBJavaApiHandler.class);
|
|
|
|
|
|
@Autowired
|
|
|
public AlarmMsgService alarmMsgService;
|
|
|
@Autowired
|
|
|
private SnsMobileConfig snsMobileConfig;
|
|
|
|
|
|
private ConcurrentHashMap<String, AtomicInteger> mapStatics = new ConcurrentHashMap<String, AtomicInteger>();
|
|
|
|
|
|
|
|
|
@Override
|
|
|
public void handler(JavaApiStatics javaApiStatics) {
|
|
|
String key = buildKey(javaApiStatics);
|
|
|
//无错
|
|
|
if (javaApiStatics.isHasException() == false) {
|
|
|
if (mapStatics.containsKey(key)) {
|
|
|
mapStatics.remove(key);
|
|
|
}
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
int num = 0;
|
|
|
if (mapStatics.containsKey(key)) {
|
|
|
AtomicInteger errNums = mapStatics.get(key);
|
|
|
num = errNums.incrementAndGet();
|
|
|
} else {
|
|
|
mapStatics.put(key, new AtomicInteger(1));
|
|
|
num = 1;
|
|
|
}
|
|
|
|
|
|
JavaApiInfo javaApiInfo = javaApiStatics.getJavaApiInfo();
|
|
|
if (javaApiInfo.getApiWarnTrigger() > 0 &&
|
|
|
num >= javaApiInfo.getApiWarnTrigger() &&
|
|
|
javaApiInfo.getApiToggle() == 1) {
|
|
|
//告警
|
|
|
String msg = buildErrMsg(num, javaApiStatics);
|
|
|
log.info("send error msg : {} .", msg);
|
|
|
alarmMsgService.sendSms(msg, snsMobileConfig.getOpsManagerDeveloper());
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
public String buildKey(JavaApiStatics javaApiStatics) {
|
|
|
String key;
|
|
|
key = javaApiStatics.getJavaApiInfo().getServiceId() + "_" + javaApiStatics.getMObjectInfo().getMoId();
|
|
|
return key;
|
|
|
}
|
|
|
|
|
|
|
|
|
public String buildErrMsg(int num, JavaApiStatics javaApiStatics) {
|
|
|
JavaApiInfo javaApiInfo = javaApiStatics.getJavaApiInfo();
|
|
|
MObjectInfo mObjectInfo = javaApiStatics.getMObjectInfo();
|
|
|
|
|
|
StringBuilder msgBuilder = new StringBuilder();
|
|
|
msgBuilder.append("Java API Error ")
|
|
|
.append("最近出错次数:" + num).append(" , ")
|
|
|
.append("NAME:" + javaApiInfo.getApiName()).append(" , ")
|
|
|
.append("URL:" + javaApiInfo.getApiUrl()).append(" , ")
|
|
|
.append("IP:" + mObjectInfo.getMoHostIp()).append(" , ")
|
|
|
.append("TYPE:" + javaApiInfo.getServiceType()).append(" , ")
|
|
|
.append(".");
|
|
|
|
|
|
return msgBuilder.toString();
|
|
|
}
|
|
|
|
|
|
} |
...
|
...
|
|