Authored by simba

Merge branch 'master' of http://git.yoho.cn/ops/monitor-service

# Conflicts:
#	monitor-service-web/src/main/resources/META-INF/spring/spring-web-context.xml
Showing 35 changed files with 1248 additions and 67 deletions
... ... @@ -82,6 +82,16 @@
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.influxdb</groupId>
<artifactId>influxdb-java</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
\ No newline at end of file
... ...
package com.monitor.common.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component("sendsmsConfig")
public class SendsmsConfig {
@Value("${sendsms.url}")
private String sendsmsUrl;// url
@Value("${sendsms.username}")
private String userName;// 用户名
@Value("${sendsms.pwd}")
private String pwd;// 密码
@Value("${sendsms.notice.productid}")
private String noticeProductid;// 产品id
public String getSendsmsUrl() {
return sendsmsUrl;
}
public String getUserName() {
return userName;
}
public String getPwd() {
return pwd;
}
public String getNoticeProductid() {
return noticeProductid;
}
}
... ...
package com.monitor.common.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
* 告警任务的短信号码 Created by yoho on 2016/5/4.
*/
@Component("snsMobileConfig")
public class SnsMobileConfig {
/********************** 支付回调手机号 **********************/
@Value("${sendsms.alarm.paycallback}")
private String payCallBack;
/********************** 支付回调手机号 ********************/
/********************* 订单异常手机号 ***********************/
@Value("${sendsms.alarm.mobile.orderexception}")
private String orderException;
/********************* 订单异常手机号 ***********************/
/******************* 用户行为异常手机号 ***************/
@Value("${sendsms.alarm.mobile.userbehaviour}")
private String userBehaviour;
/******************** 用户行为异常手机号 **************/
/******************** 数据库异常手机号 *************/
@Value("${sendsms.alarm.mobile.dbexception}")
private String dbException;
/******************* 数据库异常手机号 **************/
/****************** 服务异常告警手机号 ***************/
@Value("${sendsms.alarm.mobile.serviceexception.product}")
private String serviceExcptnProduct;
@Value("${sendsms.alarm.mobile.serviceexception.search}")
private String serviceExcptnSearch;
@Value("${sendsms.alarm.mobile.serviceexception.guang}")
private String serviceExcptnGuang;
@Value("${sendsms.alarm.mobile.serviceexception.users}")
private String serviceExcptnUsers;
@Value("${sendsms.alarm.mobile.serviceexception.order}")
private String serviceExcptnOrder;
@Value("${sendsms.alarm.mobile.serviceexception.sns}")
private String serviceExcptnSns;
@Value("${sendsms.alarm.mobile.serviceexception.promotion}")
private String serviceExcptnPromotion;
@Value("${sendsms.alarm.mobile.serviceexception.message}")
private String serviceExcptnMessage;
@Value("${sendsms.alarm.mobile.serviceexception.resources}")
private String serviceExcptnResources;
/******************** 服务异常告警手机号 ******************/
/********************Monitor模块告警手机号************************/
@Value("${sendsms.alarm.mobile.monitor.exception}")
private String monitorException;
/********************Monitor模块告警手机号************************/
/*******************Logs模块告警手机号************************/
@Value("${sendsms.alarm.mobile.logs.sns_send_failed}")
private String logsSnsSendFailed;
/*******************Logs模块告警手机号************************/
/*********************OpsManager开发程序员 手机号***************************/
@Value("${sendsms.alarm.mobile.ops_manager.developer}")
private String opsManagerDeveloper;
/*********************OpsManager开发程序员 手机号***************************/
/**
* dnspod sms
*/
@Value("${dnspod.mobile}")
private String dnspodMobile;
/**
* switch nginxConfig sms
*/
@Value("${nginx.config.switch.mobile}")
private String switchNginxConfigMobile;
/**
* jmxtrans sms
*/
@Value("${jmxtrans.mobile}")
private String jmxtransMobile;
@Value("${orderinfocheck.mobile}")
private String orderinfocheckMobile;
@Value("${base_mobile}")
private String baseMobile;
public String getOrderinfocheckMobile() {
return orderinfocheckMobile;
}
public String getPayCallBack() {
return payCallBack;
}
public String getOrderException() {
return orderException;
}
public String getUserBehaviour() {
return userBehaviour;
}
public String getDbException() {
return dbException;
}
public String getServiceExcptnProduct() {
return serviceExcptnProduct;
}
public String getServiceExcptnSearch() {
return serviceExcptnSearch;
}
public String getServiceExcptnGuang() {
return serviceExcptnGuang;
}
public String getServiceExcptnUsers() {
return serviceExcptnUsers;
}
public String getServiceExcptnOrder() {
return serviceExcptnOrder;
}
public String getServiceExcptnSns() {
return serviceExcptnSns;
}
public String getServiceExcptnPromotion() {
return serviceExcptnPromotion;
}
public String getServiceExcptnMessage() {
return serviceExcptnMessage;
}
public String getServiceExcptnResources() {
return serviceExcptnResources;
}
public String getDnspodMobile() {
return dnspodMobile;
}
public void setDnspodMobile(String dnspodMobile) {
this.dnspodMobile = dnspodMobile;
}
public String getJmxtransMobile() {
return jmxtransMobile;
}
public void setJmxtransMobile(String jmxtransMobile) {
this.jmxtransMobile = jmxtransMobile;
}
public String getSwitchNginxConfigMobile() {
return switchNginxConfigMobile;
}
public void setSwitchNginxConfigMobile(String switchNginxConfigMobile) {
this.switchNginxConfigMobile = switchNginxConfigMobile;
}
public String getOpsManagerDeveloper() {
return opsManagerDeveloper;
}
public String getMonitorException() {
return monitorException;
}
public String getLogsSnsSendFailed() {
return logsSnsSendFailed;
}
public String getBaseMobile() {
return baseMobile;
}
public void setBaseMobile(String baseMobile) {
this.baseMobile = baseMobile;
}
}
... ...
package com.monitor.common.service;
/**
* Created by zhengyouwei on 2016/5/26.
*/
public interface AlarmMsgService {
public boolean sendSms(String content, String mobile);
}
... ...
package com.monitor.common.service;
import java.util.Map;
public interface HttpRestClientService {
public String doPostStringJson(String url, String string);
public String doGet(final String uri, final Map<String, String> content);
public <T> T postForObject(String url, Object request, Class<T> responseType);
}
... ...
package com.monitor.common.service.impl;
import com.alibaba.fastjson.JSONArray;
import com.monitor.common.config.SendsmsConfig;
import com.monitor.common.config.SnsMobileConfig;
import com.monitor.common.service.AlarmMsgService;
import com.monitor.common.service.HttpRestClientService;
import com.monitor.common.util.DateFormatUtil;
import com.monitor.common.util.MD5Util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
/**
* Created by yoho on 2016/5/3.
*/
@Service("alarmMsgService")
public class AlarmMsgServiceImpl implements AlarmMsgService {
@Autowired
private HttpRestClientService httpRestClientService;
@Autowired
private SendsmsConfig sendsmsConfig;
@Autowired
private SnsMobileConfig snsMobileConfig;
private Logger logger = LoggerFactory.getLogger(AlarmMsgServiceImpl.class);
private static final int CONTENT_SUB_LENGTH = 250;
private static AtomicInteger atomicInteger = new AtomicInteger(1);
/**
* 发送短信
*
* @throws UnsupportedEncodingException
*/
@Override
public boolean sendSms(String content, String mobile) {
Integer smsid = atomicInteger.getAndIncrement();// 短信ID
Map<String, String> contentMap = new HashMap<String, String>();
contentMap.put("username", sendsmsConfig.getUserName());
contentMap.put("password", MD5Util.encryption(sendsmsConfig.getPwd()));
contentMap.put("mobile", mobile);
contentMap.put("productid", sendsmsConfig.getNoticeProductid());
boolean sendStatus = true;
// 如果短信内容不超过指定字符字符,则单条发送,如果超过,则分割多条发送
while (content.length() > CONTENT_SUB_LENGTH) {
String contentSubStr = content.substring(0, CONTENT_SUB_LENGTH);
content = content.substring(CONTENT_SUB_LENGTH);
// 发送被切割的字符串
contentMap.put("content", contentSubStr);
boolean sendSingleSmsStatus = sendSingleSms(contentMap, smsid);
if (sendSingleSmsStatus == false) {
sendStatus = false;
}
}
contentMap.put("content", content);
boolean sendSingleSmsStatus = sendSingleSms(contentMap, smsid);
if (sendSingleSmsStatus == false) {
sendStatus = false;
}
return sendStatus;
}
/**
* 发送单条短信
*
* @param smsMap
* @return
*/
private boolean sendSingleSms(Map<String, String> smsMap, Integer smsid) {
smsMap.put("content", "【有货运维】" + smsMap.get("content") + " | time:" + DateFormatUtil.getYYYY_MM_DD_HH_MM_SS() + ",id:" + smsid.intValue());
String result = httpRestClientService.doGet(sendsmsConfig.getSendsmsUrl(), smsMap);
if (result == null || !"1".equals(result.split(",")[0])) {
logger.error("发送短信失败,短信内容|" + JSONArray.toJSONString(smsMap));
smsMap.put("content", "【有货运维】发送短信告警失败!!!");
smsMap.put("mobile", snsMobileConfig.getOpsManagerDeveloper());
// 短信发送失败 发送短信给开发回来改bug
httpRestClientService.doGet(sendsmsConfig.getSendsmsUrl(), smsMap);
return false;
}
return true;
}
}
... ...
package com.monitor.common.service.impl;
import com.monitor.common.service.HttpRestClientService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import java.util.Map;
@Service("httpRestClientService")
public class HttpRestClientServiceImpl implements HttpRestClientService {
private static final Logger logger = LoggerFactory
.getLogger(HttpRestClientServiceImpl.class);
@Autowired
private RestTemplate restTemplate;
private final static String ASK = "?";
private final static String AND = "&";
@Override
public String doPostStringJson(String url, String args) {
try {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<String> entity = new HttpEntity<String>(args, headers);
String result = restTemplate.postForObject(url, entity,
String.class);
return result;
} catch (Exception e) {
logger.error("doPostStringJson failed!", e);
return null;
}
}
@Override
public String doGet(final String uri, final Map<String, String> content) {
String result = null;
try {
if (content != null) {
StringBuilder url = new StringBuilder(uri);
url.append(ASK);
for (Map.Entry<String, String> entry : content.entrySet()) {
url.append(entry.getKey() + "=" + entry.getValue() + AND);
}
result = restTemplate.getForObject(url.toString().substring(0, url.length() - 1), String.class);
} else {
result = restTemplate.getForObject(uri, String.class);
}
} catch (Exception e) {
logger.error("doGet failed!", e);
}
return result;
}
@Override
public <T> T postForObject(String url, Object request, Class<T> responseType) {
try {
return restTemplate.postForObject(url, request,
responseType);
} catch (Exception e) {
logger.error("doPostStringJson failed!url:" + url, e);
return null;
}
}
}
... ...
package com.monitor.common.util;
/**
* Created by zhengyouwei on 2016/6/21.
*/
public class ClientReportUtil {
public static String getTableName(String et,String uid){
return "client_error_" + et + "_" + uid.charAt(uid.length() - 1);
}
}
... ...
package com.monitor.common.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
/**
* 负责时间格式转换
* @author yoho
*
*/
public class DateFormatUtil {
private static final String YYYYMMDD = "yyyyMMdd";
private static final String HHMMSS = "HHmmss";
private static final String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
private static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
private static final String INFLUXDB_TIME_PATTEN = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
private static final String INFLUXDB_TIME_NANO_PATTEN = "yyyy-MM-dd'T'HH:mm:ss'Z'";
private static final Logger logger = LoggerFactory.getLogger(DateFormatUtil.class);
public static String getYYYYMMDD() {
return new SimpleDateFormat(YYYYMMDD).format(new Date());
}
public static String getHHmmss() {
return new SimpleDateFormat(HHMMSS).format(new Date());
}
public static String getYYYYMMDDByCalendar(Calendar ca) {
return new SimpleDateFormat(YYYYMMDD).format(ca.getTime());
}
public static String getYYYYMMDDHHMMSS() {
return new SimpleDateFormat(YYYYMMDDHHMMSS).format(new Date());
}
public static String getYYYY_MM_DD_HH_MM_SS() {
return new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS).format(new Date());
}
/**
* 将influxDB中的时间格式转换为格式yyyy-MM-dd HH:mm:ss
* @param date
* @return
*/
public static String displayFormat(String date) {
return displayFormatWithDateTail(date);
}
public static String displayFormatWithDateTail(String date) {
String dateWithoutTail = date.substring(0, 19) + "Z";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(INFLUXDB_TIME_NANO_PATTEN);
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
try {
Date d = simpleDateFormat.parse(dateWithoutTail);
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("Asia/Shanghai"));
calendar.setTime(d);
SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return s.format(calendar.getTime());
} catch (ParseException e) {
logger.debug("method:displayFormat args:"+date + " "+ e.toString());
return "";
}
}
/**
* 将毫秒数格式化为influxDB时间格式的字符串 yyyy-MM-dd'T'HH:mm:ss.SSS'Z'
* @param timeMsec
* @return
*/
public static String influxDBTimeFormat(String timeMsec) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(INFLUXDB_TIME_PATTEN, Locale.GERMAN);
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
try{
Date date = new Date(Long.parseLong(timeMsec));
return simpleDateFormat.format(date);
}catch (Exception e) {
logger.debug("method:displayFormat timeMsec:"+timeMsec + " "+ e.toString());
return "";
}
}
public static String influxDBTimeFormat(long timeMsec) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(INFLUXDB_TIME_PATTEN, Locale.GERMAN);
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
try{
Date date = new Date(timeMsec);
return simpleDateFormat.format(date);
}catch (Exception e) {
logger.debug("method:displayFormat timeMsec:"+timeMsec + " "+ e.toString());
return null;
}
}
public static String parseLongTo14(long time){
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(YYYYMMDDHHMMSS);
return simpleDateFormat.format(new Date(time));
}
}
... ...
package com.monitor.common.util;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
/**
* 发送短信
*
* @author yoho
*
*/
public class HttpClientUtil {
private static final Logger logger = LoggerFactory.getLogger(HttpClientUtil.class);
private final static Integer CONNECT_TIME_OUT = 10 * 1000;
private final static Integer CONNECT_REQUEST_TIME_OUT = 5 * 1000;
private final static Integer SOCKET_TIME_OUT = 30 * 1000;
private final static String ASK = "?";
private final static String AND = "&";
private final static String UTF8 = "UTF-8";
private final static String APPLICATION_JSON = "application/json; charset=UTF-8";
public static String doget(final String uri, final Map<String, String> content,final Map<String, String> header) {
String strResult = "";
RequestConfig defaultRequestConfig = RequestConfig
.custom()
.setSocketTimeout(SOCKET_TIME_OUT.intValue())
.setConnectTimeout(CONNECT_TIME_OUT.intValue())
.setConnectionRequestTimeout(CONNECT_REQUEST_TIME_OUT.intValue()).build();
CloseableHttpClient client = HttpClients.custom().setDefaultRequestConfig(defaultRequestConfig).build();
HttpGet get = null;
try {
StringBuilder url = new StringBuilder(uri);
if(content != null){
url.append(ASK);
for (Map.Entry<String, String> entry : content.entrySet()) {
url.append(entry.getKey()+"="+entry.getValue()+AND);
}
get = new HttpGet(url.toString().substring(0, url.length()-1));
}else{
get = new HttpGet(url.toString());
}
if (header != null) {
for (Map.Entry<String, String> entry : header.entrySet()) {
get.setHeader(entry.getKey(), entry.getValue());
}
}
CloseableHttpResponse response = client.execute(get);
int statusCode = response.getStatusLine().getStatusCode();
if (200 == statusCode) {
strResult = EntityUtils.toString(response.getEntity()).trim();
} else {
logger.error("|HttpClientUtil|doGet|error status|", statusCode);
}
} catch (IOException e) {
logger.error("|HttpClientUtil|doGet|", e);
} finally {
if (get != null) {
get.releaseConnection();
}
if (client != null) {
try {
client.close();
} catch (IOException e) {
logger.error("|HttpClientUtil|doGet|", e);
}
}
}
return strResult;
}
public static String doPost(final String uri, final String args, Map<String, String> header) {
String strResult = "";
RequestConfig defaultRequestConfig = RequestConfig.custom()
.setSocketTimeout(SOCKET_TIME_OUT.intValue())
.setConnectTimeout(CONNECT_TIME_OUT.intValue())
.setConnectionRequestTimeout(CONNECT_REQUEST_TIME_OUT.intValue())
.build();
CloseableHttpClient client = HttpClients.custom().setDefaultRequestConfig(defaultRequestConfig).build();
HttpPost post = new HttpPost(uri);
if (header != null) {
for (Map.Entry<String, String> entry : header.entrySet()) {
post.setHeader(entry.getKey(), entry.getValue());
}
}
StringEntity se = null;
try {
se = new StringEntity(args, UTF8);
} catch (Exception e) {
logger.error("|HttpClientUtil|doPost|", e);
}
//se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, APPLICATION_JSON));
//se.setContentType(APPLICATION_JSON);
post.setEntity(se);
CloseableHttpResponse response = null;
try {
response = client.execute(post);
int statusCode = response.getStatusLine().getStatusCode();
if (200 == statusCode) {
strResult = new String(EntityUtils.toByteArray(response.getEntity()), UTF8);
} else {
logger.error("|HttpClientUtil|doPost|error status|", statusCode);
}
} catch (IOException e) {
logger.error("|HttpClientUtil|doPost|", e);
} finally {
if (post != null) {
post.releaseConnection();
}
if (client != null) {
try {
client.close();
} catch (IOException e) {
logger.error("|HttpClientUtil|doPost|", e);
}
}
}
return strResult;
}
public static String opentsdbPut(final String uri, final String args) {
String strResult = "";
RequestConfig defaultRequestConfig = RequestConfig.custom()
.setSocketTimeout(SOCKET_TIME_OUT.intValue())
.setConnectTimeout(CONNECT_TIME_OUT.intValue())
.setConnectionRequestTimeout(CONNECT_REQUEST_TIME_OUT.intValue())
.build();
CloseableHttpClient client = HttpClients.custom().setDefaultRequestConfig(defaultRequestConfig).build();
HttpPost post = new HttpPost(uri);
StringEntity se = null;
try {
se = new StringEntity(args, UTF8);
} catch (Exception e) {
logger.error("|HttpClientUtil|opentsdbPut|", e);
}
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, APPLICATION_JSON));
se.setContentType(APPLICATION_JSON);
post.setEntity(se);
CloseableHttpResponse response = null;
try {
response = client.execute(post);
int statusCode = response.getStatusLine().getStatusCode();
if (204 != statusCode) {
logger.error("|HttpClientUtil|opentsdbPut|error status|", statusCode);
}
} catch (IOException e) {
logger.error("|HttpClientUtil|opentsdbPut|", e);
} finally {
if (post != null) {
post.releaseConnection();
}
if (client != null) {
try {
client.close();
} catch (IOException e) {
logger.error("|HttpClientUtil|opentsdbPut|", e);
}
}
}
return strResult;
}
/**
* main函数.
* @param args
* 启动参数
* @throws Exception
* Exception
*/
public static void main(String... args) throws Exception {
final String plainCreds = "yoho:yoho";
final byte[] plainCredsBytes = plainCreds.getBytes();
final byte[] base64CredsBytes = Base64.getEncoder().encode(plainCredsBytes);
final String base64Creds = new String(base64CredsBytes);
Map<String,String> map = new HashMap<String,String>();
map.put("Authorization", "Basic "+ base64Creds);
String result = doget("http://192.168.102.211:15672/api/vhosts", null, map);
System.out.println(result);
}
public static String getRemortIP(HttpServletRequest request) {
if (request.getHeader("x-forwarded-for") == null) {
return request.getRemoteAddr();
}
return request.getHeader("x-forwarded-for");
}
}
... ...
package com.monitor.common.util;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class MD5Util {
/**
*
* @param plain 明文
* @return 32位小写密文
*/
public static String encryption(String plain) {
String re_md5 = new String();
try {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(plain.getBytes());
byte b[] = md.digest();
int i;
StringBuffer buf = new StringBuffer("");
for (int offset = 0; offset < b.length; offset++) {
i = b[offset];
if (i < 0)
i += 256;
if (i < 16)
buf.append("0");
buf.append(Integer.toHexString(i));
}
re_md5 = buf.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return re_md5;
}
}
... ...
package com.monitor.common.util;
/**
* Created by yoho on 2016/6/2.
*/
public class PaginationUtil{
/**
* 根据数据量跟每个分页的数量 计算分页数
*
* @param count
* @param countOfEachPage
* @return
*/
public static int getPageCount(int count, int countOfEachPage) {
//判断是否有余数,有余数则需要增加一页
int hasReminder = count % countOfEachPage == 0 ? 0 : 1;
return count / countOfEachPage + hasReminder;
}
}
... ...
package com.monitor.common.util;
import org.influxdb.dto.QueryResult;
import org.influxdb.dto.QueryResult.Series;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
public class QueryResultUtil {
private static Logger logger = LoggerFactory.getLogger(QueryResultUtil.class);
/**
* 从查询结果中取values
* @param queryResult
* @return
*/
public static List<List<Object>> getValues(QueryResult queryResult) {
try {
Series series = getSeries(queryResult).get(0);
List<List<Object>> values = series.getValues();
return values;
} catch (Exception e) {
return null;
}
}
/**
* 从查询结果中取Series
* @param queryResult
* @return
*/
public static List<Series> getSeries(QueryResult queryResult) {
try {
List<Series> seriesList = queryResult.getResults().get(0).getSeries();
return seriesList;
} catch (Exception e) {
return null;
}
}
/**
* 从QueryResult中取出count字段
* @return
*/
public static int getCount(QueryResult queryResult) {
List<Series> series = getSeries(queryResult);
if (series == null) {
return 0;
}
return (int)(double)series.get(0).getValues().get(0).get(1);
}
}
... ...
package com.monitor.common.util;
import java.util.concurrent.atomic.AtomicInteger;
/**
* Created by zhengyouwei on 2016/6/22.
*/
public class ReportCount {
private static final AtomicInteger atomicInteger = new AtomicInteger(1);
private static int last = 0;
public static int getAndIncrement() {
return atomicInteger.getAndIncrement();
}
public static int getIncrement() {
int count = atomicInteger.get();
if (count == 0) {
return 0;
}
int increate = count - last;
last = count;
return increate;
}
}
... ...
package com.monitor.common.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Created by yoho on 2016/6/2.
*/
public class ValueFormatUtil {
private static Logger logger = LoggerFactory.getLogger(ValueFormatUtil.class);
/**
* 将String转换成int
*
* @param intStr
* @param className
* @param methodName
* @return
*/
public static int parseStringToInt(String intStr, String className, String methodName) {
try {
return Integer.valueOf(intStr);
} catch (Exception e) {
logger.error("parse page int from page string failure Class: " +
className + " method: " + methodName +
" | error:" + e.toString());
return 0;
}
}
}
... ...
... ... @@ -2,6 +2,7 @@ package com.monitor.influxdb.mapper.impl;
import com.alibaba.fastjson.JSONObject;
import com.monitor.common.contants.InfluxDBContants;
import com.monitor.common.util.DateFormatUtil;
import com.monitor.influxdb.InluxDBSingle;
import com.monitor.influxdb.mapper.IJavaApiStaticsMapper;
import com.monitor.model.domain.JavaApiStaticsModel;
... ... @@ -48,7 +49,7 @@ public class JavaApiStaticsMapper implements IJavaApiStaticsMapper {
.addField("mobj_ip", statics.getString("mobj_ip"))
.addField("mobj_id", statics.getIntValue("mobj_id"))
.addField("mobj_port", statics.getString("mobj_port"))
.addField("start", statics.getLongValue("end"))
.addField("start", statics.getLongValue("start"))
.addField("end", statics.getLongValue("end"))
.addField("is_exception", statics.getBooleanValue("is_exception"))
.time(System.currentTimeMillis() * 1000000 + random.nextInt(999999), TimeUnit.NANOSECONDS);
... ... @@ -91,9 +92,20 @@ public class JavaApiStaticsMapper implements IJavaApiStaticsMapper {
QueryResult.Series series = rel.getSeries().get(0);
Boolean is_exception = (Boolean) series.getValues().get(0).get(series.getColumns().indexOf("is_exception"));
long startTime = Math.round((Double) series.getValues().get(0).get(series.getColumns().indexOf("start")));
long endTime = Math.round((Double) series.getValues().get(0).get(series.getColumns().indexOf("end")));
long costTime = (endTime - startTime);
String exception = (String) series.getValues().get(0).get(series.getColumns().indexOf("exception"));
javaApiStaticsModel.setStartTime(DateFormatUtil.parseLongTo14(startTime));
javaApiStaticsModel.setEndTime(DateFormatUtil.parseLongTo14(endTime));
javaApiStaticsModel.setCostTime((int) costTime);
if (is_exception == true) {
javaApiStaticsModel.setStatus(0);
javaApiStaticsModel.setException(exception);
} else
javaApiStaticsModel.setStatus(1);
... ...
... ... @@ -38,10 +38,14 @@ public class JavaApiClient {
@Autowired
@Qualifier("javaapiRestTemplate")
private RestTemplate restTemplate;
//
// @Autowired
// @Qualifier("influxDBJavaApiHandler")
// private IJavaApiHadnler javaApiHadnler;
@Autowired
@Qualifier("influxDBJavaApiHandler")
private IJavaApiHadnler javaApiHadnler;
private List<IJavaApiHadnler> javaApiHadnlerList;
//TODO 需要 线程安全 处理
private Map<String, JavaApiInfo> javaApimap;
... ... @@ -56,7 +60,6 @@ public class JavaApiClient {
private long endTime;
public void initClient() {
executorService = new ThreadPoolExecutor(10, 30, 60, TimeUnit.SECONDS, new LinkedBlockingDeque<>(),
new JavaApiThreadFactory());
... ... @@ -85,7 +88,7 @@ public class JavaApiClient {
public void run() {
startTime=System.currentTimeMillis();
startTime = System.currentTimeMillis();
int tastNum = 0;
for (JavaApiInfo javaApiInfo : javaApimap.values()) {
... ... @@ -114,12 +117,14 @@ public class JavaApiClient {
//close 线程池
executorService.shutdownNow();
endTime=System.currentTimeMillis();
endTime = System.currentTimeMillis();
}
public void handleResult(JavaApiStatics rep) {
javaApiHadnler.handler(rep);
for (IJavaApiHadnler javaApiHadnler : javaApiHadnlerList) {
javaApiHadnler.handler(rep);
}
}
... ... @@ -153,7 +158,7 @@ public class JavaApiClient {
//
// mObjInfoMap.add(1, mObj2);
MObjectInfo mObj2 = new MObjectInfo();
MObjectInfo mObj2 = new MObjectInfo();
mObj2.setMoId(0);
mObj2.setMoHostIp("localhost");
mObj2.setMoTags("10080");
... ...
package com.monitor.javaserver.ctrl;
import com.monitor.javaserver.client.JavaApiClient;
import com.monitor.javaserver.status.IJavaApiStatusService;
import com.monitor.model.request.JavaApiStatusListReq;
import com.monitor.javaserver.service.IJavaApiStatusService;
import com.monitor.model.request.JavaApiStatusReq;
import com.monitor.model.response.BaseResponse;
import com.monitor.model.response.JavaApiStatusRep;
... ... @@ -12,8 +11,6 @@ import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.sql.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
... ...
... ... @@ -43,7 +43,7 @@ public class InfluxDBJavaApiHandler implements IJavaApiHadnler {
else
jsonObject.put("mobj_port", "");
jsonObject.put("start", javaApiStatics.getEndTime());
jsonObject.put("start", javaApiStatics.getStartTime());
jsonObject.put("end", javaApiStatics.getEndTime());
jsonObject.put("is_exception", javaApiStatics.isHasException());
... ...
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.concurrent.ConcurrentHashMap;
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();
}
}
... ...
package com.monitor.javaserver.status;
package com.monitor.javaserver.service;
import com.model.JavaApiInfo;
import com.monitor.model.request.JavaApiStatusListReq;
import com.monitor.model.request.JavaApiStatusReq;
import com.monitor.model.response.JavaApiStatusRep;
... ...
package com.monitor.javaserver.status.impl;
package com.monitor.javaserver.service.impl;
import com.monitor.influxdb.mapper.impl.JavaApiStaticsMapper;
import com.monitor.javaserver.status.IJavaApiStatusService;
import com.monitor.javaserver.service.IJavaApiStatusService;
import com.monitor.model.domain.JavaApiStaticsModel;
import com.monitor.model.request.JavaApiStatusReq;
import com.monitor.model.response.JavaApiStatusRep;
... ... @@ -49,6 +49,13 @@ public class JavaAppiStatusServiceImpl implements IJavaApiStatusService {
rep.setServiceId(req.getServiceId());
rep.setMObjectId(req.getMObjectId());
rep.setStatus(req.getStatus());
rep.setEndTime(req.getEndTime());
rep.setStartTime(req.getStartTime());
rep.setCostTime(req.getCostTime());
rep.setException(req.getException());
repList.add(rep);
}
... ...
... ... @@ -16,24 +16,29 @@ import javax.annotation.PostConstruct;
/**
* Created by yoho on 2016/6/21.
*/
@Component
public class InfluxComp implements Runnable {
public static final Logger DEBUG = LoggerFactory.getLogger(InfluxComp.class);
@Value("influxUrl")
private String influxUrl="http://123.206.79.151:8086";
@Value("${aws.influxdb.url}")
private String influxUrl;
@Value("influxUser")
private String influxUser="root";
@Value("${aws.influxdb.user}")
private String influxUser;
@Value("influxPwd")
private String influxPwd="root";
@Value("${aws.influxdb.pwd}")
private String influxPwd;
private InfluxDB influxDBClient;
public InfluxComp() {
@PostConstruct
public void init() {
influxDBClient = InfluxDBFactory.connect(this.influxUrl, this.influxUser, this.influxPwd);
InterVar.EXECUTOR_SERVICE.submit(this);
}
public void doService() {
... ... @@ -63,13 +68,13 @@ public class InfluxComp implements Runnable {
public QueryResult doQuery(String command) {
Query query=new Query(command,InterVar.DBNAME);
Query query = new Query(command, InterVar.DBNAME);
return influxDBClient.query(query);
}
@Override
public void run() {
doService();
}
}
... ...
... ... @@ -32,7 +32,7 @@ public interface InterVar {
String QUEUE_URL = "/api/queues";
String MOMATCHPREFIX = "*rabbitmq*";
String MOMATCHPREFIX = "(.*)rabbitmq(.*)";
String URLFORMAT = "http://{}:{}";
... ...
... ... @@ -21,6 +21,7 @@ import org.springframework.web.bind.annotation.RestController;
public class RabbitmqService {
public static final Logger DEBUG = LoggerFactory.getLogger(RabbitmqService.class);
@Autowired
InfluxComp influxComp;
@RequestMapping(value = "/overview")
... ...
... ... @@ -12,12 +12,15 @@ import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
/**
* Created by yoho on 2016/6/21.
*/
public class MoScanTask {
@EnableScheduling
@Component
public class MoScanTask {
@Autowired
IMObjectInfoService moService;
... ... @@ -25,27 +28,30 @@ public class MoScanTask {
@Autowired
ITypeInfoService typeService;
MoScanTask()
{
int i=0;
i++;
System.out.println("123");
}
//自动发现rabbit监控对象
@Scheduled(cron = "0/1 * * * * ? *")
@Scheduled(cron = "* 0/1 * * * ? ")
public void doTask() {
List<MObjectInfo> mObjectInfoList = moService.queryMObjectsInfo();
for (MObjectInfo info : mObjectInfoList) {
List<TypeInfo> typeInfosList = typeService.queryAllTypesInfo();
TypeInfo typeInfo = typeService.queryTypeInfo(info.getMoTypeId());
List<MObjectInfo> mObjectInfoList = new ArrayList<>();
if (1 == typeInfo.getTypeIsLeaf() && typeInfo.getTypeName().matches(InterVar.MOMATCHPREFIX)) {
for (TypeInfo typeInfo : typeInfosList) {
InterVar.moMaps.put(info.getMoId(), info);
if (1 == typeInfo.getTypeIsLeaf() && typeInfo.getTypeName().matches(InterVar.MOMATCHPREFIX)) {
mObjectInfoList.addAll(moService.queryMObjectsInfoByType(typeInfo.getTypeId()));
}
}
for (MObjectInfo info : mObjectInfoList) {
InterVar.moMaps.put(info.getMoId(), info);
}
}
}
... ...
... ... @@ -22,9 +22,11 @@ import static com.monitor.middleware.rabbitmq.constant.InterVar.EXECUTOR_SERVICE
/**
* 加载服务监控任务
*/
@EnableScheduling
@Component
public class MonitTask {
@Scheduled(cron = "0/1 * * * * ? *")
@Scheduled(cron = "* 0/3 * * * ? ")
public void doTask() {
for (Map.Entry<Integer, MObjectInfo> entry : InterVar.moMaps.entrySet()) {
EXECUTOR_SERVICE.submit(new OneJob(new ClusterViewJob(entry.getKey())));
... ...
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- <task:scheduler id="taskScheduler" pool-size="5"></task:scheduler>
<task:annotation-driven scheduler="taskScheduler"/>-->
<!-- <bean id="task" class="com.monitor.middleware.rabbitmq.task.MonitTask"></bean>
<task:scheduled-tasks>
<task:scheduled ref="task" method="doTask" fixed-rate="1000"/>
</task:scheduled-tasks>-->
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
<bean id="httpClientFactory" class="org.springframework.http.client.SimpleClientHttpRequestFactory">
<property name="connectTimeout" value="2000"/>
... ... @@ -25,6 +14,4 @@
<constructor-arg ref="httpClientFactory"/>
</bean>
</beans>
... ...
... ... @@ -20,6 +20,11 @@
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
</dependencies>
... ...
package com.monitor.model.domain;
import lombok.Data;
import org.apache.commons.lang.builder.ReflectionToStringBuilder;
/**
* Created by fruwei on 2016/6/22.
... ... @@ -11,15 +12,13 @@ public class JavaApiStaticsModel {
private Integer serviceType;
private Integer mObjectId;
private Integer status;
private String exception;
private String startTime;
private String endTime;
private int costTime;
@Override
public String toString() {
return "JavaApiStaticsModel{" +
"mObjectId=" + mObjectId +
", serviceId=" + serviceId +
", serviceType=" + serviceType +
", status=" + status +
'}';
return ReflectionToStringBuilder.toString(this);
}
}
... ...
... ... @@ -12,6 +12,9 @@ public class JavaApiStatusRep {
private Integer serviceType;
private Integer mObjectId;
private Integer status;
private String exception;
private String startTime;
private String endTime;
private int costTime;
}
... ...
... ... @@ -16,9 +16,11 @@
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task.xsd">
<context:property-placeholder location="classpath:/test/*.properties" />
<context:property-placeholder location="classpath:/product/*.properties" />
<context:component-scan base-package="com.monitor" />
<!-- 打开aop 注解 -->
<!-- 打开aop 注解 -->
<aop:aspectj-autoproxy proxy-target-class="true"/>
<!-- Enables the Spring Task @Scheduled programming model -->
... ...
... ... @@ -6,4 +6,8 @@ influxdb.user=root;root
influxdb.pwd=root;root
influxdb.connect.timeout=15;15
influxdb.read.timeout=40;40
influxdb.write.timeout=20;20
\ No newline at end of file
influxdb.write.timeout=20;20
aws.influxdb.url=http://123.206.79.151:8086
aws.influxdb.user=root
aws.influxdb.pwd=root
\ No newline at end of file
... ...
# ******************** send sms common configs ********************
sendsms.url=http://www.ztsms.cn/sendSms.do
sendsms.username=youhuo
sendsms.pwd=I8vX4MtK
sendsms.notice.productid=333333
base_mobile=15905144483,18751986615,18652008443
#------------------------------------------支付回调手机号------------------------------------------------
sendsms.alarm.paycallback=15905144483,18751986615,18652008443
#------------------------------------------支付回调手机号------------------------------------------------
#------------------------------------------订单异常手机号------------------------------------------------
sendsms.alarm.mobile.orderexception=15905144483,13585196262,18652008443
#------------------------------------------订单异常手机号------------------------------------------------
#------------------------------------------用户行为异常手机号------------------------------------------------
sendsms.alarm.mobile.userbehaviour=15905144483,18001582955
#------------------------------------------用户行为异常手机号------------------------------------------------
#------------------------------------------数据库异常手机号------------------------------------------------
sendsms.alarm.mobile.dbexception=15905144483,18652008443,18751986615,13811102093
#------------------------------------------数据库异常手机号------------------------------------------------
#-------------------------------------------服务异常告警手机号----------------------------------------------------
sendsms.alarm.mobile.serviceexception.product=15905144483,18652925653,13451938602,18751986615,18652008443
sendsms.alarm.mobile.serviceexception.search=15905144483,18551640215,18652925653,18751986615,18652008443
sendsms.alarm.mobile.serviceexception.guang=15905144483,18001582955,18652925653,18751986615,18652008443,18551607734,15651035157,13951882433
sendsms.alarm.mobile.serviceexception.users=15905144483,18001582955,18652925653,18751986615,18652008443,13951882433,15651035157,18551607734
sendsms.alarm.mobile.serviceexception.order=15905144483,15950561359,13951634768,18652925653,18751986615,18652008443
sendsms.alarm.mobile.serviceexception.sns=15905144483,13451938602,18652925653,18751986615,18652008443
sendsms.alarm.mobile.serviceexception.promotion=15905144483,18652925653,13451938602,18751986615,18652008443
sendsms.alarm.mobile.serviceexception.message=15905144483,18001582955,18652925653,18751986615,18652008443
sendsms.alarm.mobile.serviceexception.resources=15905144483,15950561359,18652925653,18751986615,18652008443
#-------------服务异常告警手机号-------------------
#---------------------Monitor异常数据告警手机号------------------
sendsms.alarm.mobile.monitor.exception=15905144483,18751986615
#---------------------Monitor异常数据告警手机号------------------
#-----------------------------Logs告警模块---------------------------
#短信发送失败量超过阀值 告警手机号
sendsms.alarm.mobile.logs.sns_send_failed=15905144483,18751986615,18652008443,18001582955
#-----------------------------Logs告警模块--------------------------
#-----------------------------监控系统开发人员--------------------------
sendsms.alarm.mobile.ops_manager.developer=13914772123,15150551036
#-----------------------------监控系统开发人员--------------------------
dnspod.mobile=15905144483,18652008443,18751986615
jmxtrans.mobile=15905144483,18751986615
nginx.config.switch.mobile=15905144483,18652008443,18751986615
#订单校验失败
orderinfocheck.mobile=15905144483,18652008443,15950561359,13902496867,18511020499
... ...
package com.monitor;
import com.monitor.common.config.SnsMobileConfig;
import com.monitor.common.service.AlarmMsgService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.PropertySource;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* Created by fruwei on 2016/6/22.
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
"classpath*:META-INF/spring/spring*.xml"})
@PropertySource({"classpath:jdbc.properties",
"classpath:*.properties"})
public class MessageTest {
@Autowired
public AlarmMsgService alarmMsgService;
@Autowired
private SnsMobileConfig snsMobileConfig;
@Test
public void testSendMsg(){
alarmMsgService.sendSms("test", snsMobileConfig.getOpsManagerDeveloper());
}
}
... ...