Authored by jack-xue

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

Showing 18 changed files with 259 additions and 81 deletions
... ... @@ -18,6 +18,15 @@ public class SendsmsConfig {
@Value("${sendsms.notice.productid}")
private String noticeProductid;// 产品id
@Value("${qcloud_sms_key}")
private String qcloudSmsKey;
@Value("${qcloud_sms_url}")
private String qcloudSmsUrl;
@Value("${qcloud_voice_url}")
private String qcloudVoiceUrl;
public String getSendsmsUrl() {
return sendsmsUrl;
}
... ... @@ -33,4 +42,16 @@ public class SendsmsConfig {
public String getNoticeProductid() {
return noticeProductid;
}
public String getQcloudSmsUrl() {
return qcloudSmsUrl;
}
public String getQcloudSmsKey() {
return qcloudSmsKey;
}
public String getQcloudVoiceUrl() {
return qcloudVoiceUrl;
}
}
... ...
package com.monitor.common.service.impl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.monitor.common.config.SendsmsConfig;
import com.monitor.common.config.SnsMobileConfig;
import com.monitor.common.util.MD5Util;
import com.monitor.influxdb.contants.InfluxDBContants;
import com.monitor.common.service.AlarmMsgService;
import com.monitor.common.service.HttpRestClientService;
import com.monitor.influxdb.mapper.MonitorAlarmMapper;
import com.monitor.model.domain.QcloudSms;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
... ... @@ -36,9 +39,6 @@ public class AlarmMsgServiceImpl implements AlarmMsgService {
private SendsmsConfig sendsmsConfig;
@Autowired
private SnsMobileConfig snsMobileConfig;
@Autowired
private MonitorAlarmMapper monitorAlarmMapper;
private Logger logger = LoggerFactory.getLogger(AlarmMsgServiceImpl.class);
... ... @@ -78,41 +78,18 @@ public class AlarmMsgServiceImpl implements AlarmMsgService {
return true;
}
logger.warn("Send msg type {} content {} alarmInfo {} moniole {}...", type, content, alarmInfo, mobile);
Map<String, String> contentMap = new HashMap<String, String>();
contentMap.put("OperID", sendsmsConfig.getUserName());
contentMap.put("OperPass", sendsmsConfig.getPwd());
contentMap.put("DesMobile", mobile);
contentMap.put("ContentType", 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);
//
// if (sendSingleSmsStatus == false) {
// sendStatus = false;
// }
// }
logger.info("Send msg type {} content {} alarmInfo {} moniole {}...", type, content, alarmInfo, mobile);
if (StringUtils.isBlank(content)) {
return false;
}
contentMap.put("Content", content);
boolean sendSingleSmsStatus = sendSingleSms(contentMap);
if (sendSingleSmsStatus == false) {
String result = sendSingleSms(content, mobile);
boolean sendStatus = true;
if (result == null) {
sendStatus = false;
}
try {
monitorAlarmMapper.insertAlarmMsg(InfluxDBContants.AWS, type, content, alarmInfo, sendStatus ? "successed" : "failed");
monitorAlarmMapper.insertAlarmMsg(InfluxDBContants.AWS, type, content, alarmInfo, sendStatus ? "successed" : "faild", result);
} catch (Exception e) {
logger.error("insert sms into influxdb failed", e);
}
... ... @@ -121,29 +98,30 @@ public class AlarmMsgServiceImpl implements AlarmMsgService {
}
/**
* 发送单条短信
* 发送短信
*
* @param smsMap
* @return
*/
private boolean sendSingleSms(Map<String, String> smsMap) {
private String sendSingleSms(String content, String mobile) {
try {
smsMap.put("Content", URLEncoder.encode("【有货运维】" + smsMap.get("Content"), "UTF-8"));
String result = httpRestClientService.get(sendsmsConfig.getSendsmsUrl(), smsMap, String.class);
if (result == null || !("01".equals(result.split(",")[0]) || "00".equals(result.split(",")[0]) || "03".equals(result.split(",")[0]))) {
logger.error("发送短信失败,短信内容|" + JSONArray.toJSONString(smsMap));
smsMap.put("Content", URLEncoder.encode("【有货运维】发送短信告警失败!!!", "UTF-8"));
smsMap.put("DesMobile", snsMobileConfig.getBaseMobile());
// 短信发送失败 发送短信给开发回来改bug
httpRestClientService.get(sendsmsConfig.getSendsmsUrl(), smsMap, String.class);
return false;
QcloudSms qcloudSms = new QcloudSms();
List<QcloudSms.Tel> telList = new ArrayList<>();
String[] mobiles = mobile.split(",");
QcloudSms.Tel tel;
for (String mob : mobiles) {
tel = new QcloudSms.Tel();
tel.setPhone(mob);
telList.add(tel);
}
return true;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return false;
qcloudSms.setTel(telList);
qcloudSms.setMsg("故障:"+content);
qcloudSms.setSig(MD5Util.encryption(sendsmsConfig.getQcloudSmsKey() + mobile));
String result = httpRestClientService.doPostStringJson(sendsmsConfig.getQcloudSmsUrl(), JSON.toJSONString(qcloudSms));
return result;
} catch (Exception e) {
logger.error("send sms failed", e);
return null;
}
}
... ...
package com.monitor.common.service.impl;
import com.alibaba.fastjson.JSON;
import com.monitor.common.config.SendsmsConfig;
import com.monitor.common.config.SnsMobileConfig;
import com.monitor.common.service.HttpRestClientService;
import com.monitor.common.service.VoiceMsgService;
import com.monitor.common.util.MD5Util;
... ... @@ -27,6 +29,9 @@ public class VoiceMsgServiceImpl implements VoiceMsgService {
@Autowired
private HttpRestClientService httpRestClientService;
@Autowired
private SendsmsConfig sendsmsConfig;
public final Logger DEBUG = LoggerFactory.getLogger(getClass());
@Override
... ... @@ -42,10 +47,10 @@ public class VoiceMsgServiceImpl implements VoiceMsgService {
VoiceSms.Tel tel = new VoiceSms.Tel();
tel.setNationcode("86");
tel.setPhone(str);
voiceSms.setSig(MD5Util.encryption("6e56f948f6f1c0a1bc359e23f7acc140" + str));
voiceSms.setSig(MD5Util.encryption(sendsmsConfig.getQcloudSmsKey() + str));
voiceSms.setTel(tel);
DEBUG.info("send voice msg,content:{}", JSON.toJSONString(voiceSms));
String result = httpRestClientService.doPostStringJson("https://yun.tim.qq.com/v3/tlsvoicesvr/sendvoice?sdkappid=1400021400&random=123", JSON.toJSONString(voiceSms));
String result = httpRestClientService.doPostStringJson(sendsmsConfig.getQcloudVoiceUrl(), JSON.toJSONString(voiceSms));
DEBUG.info("send voice msg,result:{}", result);
}
... ... @@ -67,13 +72,13 @@ public class VoiceMsgServiceImpl implements VoiceMsgService {
String[] mobiles = mobile.split(",");
for (String str : mobiles) {
VoiceSmsNotice.Tel tel = new VoiceSmsNotice.Tel();
tel.setNationcode("86");
tel.setPhone(str);
voiceSms.setSig(MD5Util.encryption("6e56f948f6f1c0a1bc359e23f7acc140" + str));
voiceSms.setSig(MD5Util.encryption(sendsmsConfig.getQcloudSmsKey() + str));
voiceSms.setTel(tel);
DEBUG.info("send voice notice msg,content:{}", JSON.toJSONString(voiceSms));
String result = httpRestClientService.doPostStringJson("https://yun.tim.qq.com/v3/tlsvoicesvr/sendvoiceprompt?sdkappid=1400021400&random=123", JSON.toJSONString(voiceSms));
String result = httpRestClientService.doPostStringJson(sendsmsConfig.getQcloudVoiceUrl(), JSON.toJSONString(voiceSms));
DEBUG.info("send voice notice msg,result:{}", result);
monitorAlarmMapper.insertAlarmMsg(InfluxDBContants.AWS, "voice", "请及时处理故障:紧急故障", "null", result!=null ? "successed" : "faild", result);
}
} catch (Exception e) {
... ...
... ... @@ -31,7 +31,6 @@
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<constructor-arg value="UTF-8" />
</bean>
<bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter" />
<bean class="org.springframework.http.converter.FormHttpMessageConverter" />
... ...
... ... @@ -9,7 +9,7 @@ public interface MonitorAlarmMapper {
* @param influxDBName
* @param snsContent
*/
void insertAlarmMsg(String influxDBName, String alarmType, String snsContent, String alarmInfo, String sendStatus);
void insertAlarmMsg(String influxDBName, String alarmType, String snsContent, String alarmInfo, String sendStatus,String result);
/**
* 查询type的tag-value
... ...
package com.monitor.influxdb.mapper;
import com.monitor.model.domain.QcloudSmsUpBody;
import com.monitor.model.request.SmsUp;
import com.monitor.model.domain.QcloudSmsUp;
import com.monitor.model.domain.QcloudVoiceUpBody;
import org.influxdb.dto.QueryResult;
/**
... ... @@ -9,7 +9,7 @@ import org.influxdb.dto.QueryResult;
*/
public interface SmsUpMapper {
void insert(QcloudSmsUpBody qcloudSmsUpBody);
void insert(QcloudVoiceUpBody qcloudSmsUpBody);
void insert(QcloudSmsUp qcloudSmsUp);
QueryResult select(int min);
}
... ...
... ... @@ -26,7 +26,7 @@ public class MonitorAlarmMapperImpl extends InfluxDBQuery implements MonitorAlar
* @param alarmInfo
*/
@Override
public void insertAlarmMsg(String influxDBName, String alarmType, String snsContent, String alarmInfo, String sendStatus) {
public void insertAlarmMsg(String influxDBName, String alarmType, String snsContent, String alarmInfo, String sendStatus,String result) {
BatchPoints batchPoints = BatchPoints
.database(InfluxDBContants.YOMO_MONITOR).tag("event", "alarm").retentionPolicy("default")
.build();
... ... @@ -34,6 +34,7 @@ public class MonitorAlarmMapperImpl extends InfluxDBQuery implements MonitorAlar
.tag("type", alarmType)
.tag("send_status", sendStatus)
.addField("sms_content", snsContent)
.addField("result", result)
.addField("alarm_info", alarmInfo).build();
batchPoints.point(point);
inluxDBSingle.getInfluxDBByName(influxDBName).getInfluxDB()
... ...
... ... @@ -3,8 +3,8 @@ package com.monitor.influxdb.mapper.impl;
import com.monitor.influxdb.InfluxDBQuery;
import com.monitor.influxdb.contants.InfluxDBContants;
import com.monitor.influxdb.mapper.SmsUpMapper;
import com.monitor.model.domain.QcloudSmsUpBody;
import com.monitor.model.request.SmsUp;
import com.monitor.model.domain.QcloudSmsUp;
import com.monitor.model.domain.QcloudVoiceUpBody;
import org.apache.commons.lang.StringUtils;
import org.influxdb.dto.BatchPoints;
import org.influxdb.dto.Point;
... ... @@ -18,11 +18,11 @@ import org.springframework.stereotype.Component;
public class SmsUpMapperImpl extends InfluxDBQuery implements SmsUpMapper {
@Override
public void insert(QcloudSmsUpBody qcloudSmsUpBody) {
public void insert(QcloudVoiceUpBody qcloudSmsUpBody) {
BatchPoints batchPoints = BatchPoints
.database(InfluxDBContants.YOMO_MONITOR).retentionPolicy("default")
.build();
Point point = Point.measurement("sms_up")
Point point = Point.measurement("voice_up")
.addField("result", qcloudSmsUpBody.getResult())
.addField("callid", StringUtils.isBlank(qcloudSmsUpBody.getCallid()) ? " " : qcloudSmsUpBody.getCallid())
.addField("mobile", StringUtils.isBlank(qcloudSmsUpBody.getMobile())?" ":qcloudSmsUpBody.getMobile())
... ... @@ -37,9 +37,26 @@ public class SmsUpMapperImpl extends InfluxDBQuery implements SmsUpMapper {
}
@Override
public void insert(QcloudSmsUp qcloudSmsUp) {
BatchPoints batchPoints = BatchPoints
.database(InfluxDBContants.YOMO_MONITOR).retentionPolicy("default")
.build();
Point point = Point.measurement("sms_up")
.addField("sign", StringUtils.isBlank(qcloudSmsUp.getSign()) ? " " : qcloudSmsUp.getSign())
.addField("text", StringUtils.isBlank(qcloudSmsUp.getText()) ? " " : qcloudSmsUp.getText())
.addField("mobile", StringUtils.isBlank(qcloudSmsUp.getMobile())?" ":qcloudSmsUp.getMobile())
.addField("time", StringUtils.isBlank(qcloudSmsUp.getTime())?" ":qcloudSmsUp.getTime())
.addField("time", StringUtils.isBlank(qcloudSmsUp.getNationcode())?" ":qcloudSmsUp.getNationcode())
.addField("extend", StringUtils.isBlank(qcloudSmsUp.getExtend())?" ":qcloudSmsUp.getExtend()).build();
batchPoints.point(point);
inluxDBSingle.getInfluxDBByName(InfluxDBContants.AWS).getInfluxDB()
.write(batchPoints);
}
@Override
public QueryResult select(int min) {
String command = "SELECT count(nationcode) FROM sms_up where time > now() - "+ min+"m and result = '0'";
String command = "SELECT count(text) FROM sms_up where time > now() - "+ min+"m";
return query(InfluxDBContants.AWS, command, InfluxDBContants.YOMO_MONITOR);
... ...
package com.monitor.model.domain;
import lombok.Data;
import java.util.List;
/**
* Created by zhengyouwei on 2016/12/19.
*/
@Data
public class QcloudSms {
private List<Tel> tel;
private String type="0";
private String sig ;
private String ext;
private String msg;
private String extend;
public static class Tel{
private String nationcode="86";
private String phone;
public String getNationcode() {
return nationcode;
}
public void setNationcode(String nationcode) {
this.nationcode = nationcode;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
}
... ...
... ... @@ -8,7 +8,17 @@ import lombok.Data;
@Data
public class QcloudSmsUp {
private QcloudSmsUpBody voiceprompt_callback;
private String nationcode;
private String mobile;
private String text;
private String sign;
private String time;
private String extend;
}
... ...
package com.monitor.model.domain;
import lombok.Data;
/**
* Created by zhengyouwei on 2016/12/16.
*/
@Data
public class QcloudVoiceUp {
private QcloudVoiceUpBody voiceprompt_callback;
}
... ...
... ... @@ -6,7 +6,7 @@ import lombok.Data;
* Created by zhengyouwei on 2016/12/16.
*/
@Data
public class QcloudSmsUpBody {
public class QcloudVoiceUpBody {
private String result;
private String callid;
... ...
... ... @@ -19,7 +19,7 @@ public class VoiceSmsNotice {
private String ext;
public static class Tel{
private String nationcode;
private String nationcode="86";
private String phone;
... ...
... ... @@ -3,7 +3,7 @@ package com.monitor.other.smsUp;
import com.alibaba.fastjson.JSON;
import com.monitor.influxdb.mapper.SmsUpMapper;
import com.monitor.model.domain.QcloudSmsUp;
import com.monitor.model.request.SmsUp;
import com.monitor.model.domain.QcloudVoiceUp;
import com.monitor.model.response.BaseResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
... ... @@ -25,16 +25,32 @@ public class SmsUpctrl {
private SmsUpMapper smsUpMapper;
/**
* 删除
* 语音上行
* @return
*/
@RequestMapping("/smsup")
@RequestMapping("/voiceUp")
@ResponseBody
public BaseResponse smsup(@RequestBody QcloudSmsUp qcloudSmsUp) {
logger.info("smsup req :{}", JSON.toJSONString(qcloudSmsUp));
public BaseResponse voiceUp(@RequestBody QcloudVoiceUp qcloudSmsUp) {
logger.info("voiceUp req :{}", JSON.toJSONString(qcloudSmsUp));
try {
smsUpMapper.insert(qcloudSmsUp.getVoiceprompt_callback());
}catch (Exception e){
logger.error("voiceUp",e);
}
return new BaseResponse();
}
/**
* 短信上行
* @return
*/
@RequestMapping("/smsUp")
@ResponseBody
public BaseResponse smsUp(@RequestBody QcloudSmsUp qcloudSmsUp) {
logger.info("smsup req :{}", JSON.toJSONString(qcloudSmsUp));
try {
smsUpMapper.insert(qcloudSmsUp);
}catch (Exception e){
logger.error("smsup",e);
}
return new BaseResponse();
... ...
... ... @@ -49,24 +49,25 @@ public class AlarmSendVoiceTask {
DEBUG.info("alarmSendVoice task sms count = {}", count);
if (count > 5) {//大于无
//todo 查询是否需要语音通知
if (count > 5) {//大于5
boolean needVoice = true;
// QueryResult queryResult1 = smsUpMapper.select(30);
// int voiceCount = QueryResultUtil.getCount(queryResult1);
QueryResult queryResult1 = smsUpMapper.select(30);
int voiceCount = QueryResultUtil.getCount(queryResult1);
// DEBUG.info("alarmSendVoice task sms voiceCount = {}", voiceCount);
DEBUG.info("alarmSendVoice task sms voiceCount = {}", voiceCount);
// if (voiceCount > 0) {
if (voiceCount > 0) {
needVoice = false;
}else {
if (lastTime == 0) {
lastTime = now;
} else {
if (now - lastTime < 10 * 60 * 1000) {//十分钟内不再发送
if (now - lastTime < 10 * 60 * 1000) {//十分钟内不再发送
needVoice = false;
}
}
// }
}
if (needVoice) {
lastTime = now;
... ...
package com.monitor.other.test;
import com.model.DBWorkJob;
import com.monitor.common.service.AlarmMsgService;
import com.monitor.common.service.VoiceMsgService;
import com.monitor.model.response.BaseResponse;
import com.monitor.other.worksystem.service.DBService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* Created by zhengyouwei on 2016/10/11.
*
* 数据库工单
*
*/
@Controller
@RequestMapping("/testCtrl")
public class TestCtrl {
Logger logger = LoggerFactory.getLogger(getClass());
@Autowired
private AlarmMsgService alarmMsgService;
@Autowired
private VoiceMsgService voiceMsgService;
@RequestMapping("/testSms")
@ResponseBody
public BaseResponse testSms() {
try {
alarmMsgService.sendSms("test","这是一条测试短信","15905144483");
}catch (Exception e){
logger.error("testSms ~",e);
return new BaseResponse<>();
}
return new BaseResponse();
}
@RequestMapping("/testVoice")
@ResponseBody
public BaseResponse testVoice() {
try {
voiceMsgService.sendVoiceAlarm("15905144483");
}catch (Exception e){
logger.error("testSms ~",e);
return new BaseResponse<>();
}
return new BaseResponse();
}
}
... ...
... ... @@ -4,6 +4,10 @@ sendsms.username=yohoyw
sendsms.pwd=NCftHmJ9
sendsms.notice.productid=8
qcloud_voice_url=https://yun.tim.qq.com/v3/tlsvoicesvr/sendvoiceprompt?sdkappid=1400021400&random=123
qcloud_sms_url=https://yun.tim.qq.com/v3/tlssmssvr/sendmultisms2?sdkappid=1400021400&random=124
qcloud_sms_key=6e56f948f6f1c0a1bc359e23f7acc140
base_mobile=18751986615,18652008443,18502542319,15150509659,13515100825,18252034289
swaith_mobile=15905144483,18751986615
... ...
... ... @@ -3,6 +3,10 @@ sendsms.url=http://221.179.180.158:9009/HttpQuickProcess_utf-8/submitMessageAll
#sendsms.username=yoho
#sendsms.pwd=E
qcloud_voice_url=https://yun.tim.qq.com/v3/tlsvoicesvr/sendvoiceprompt?sdkappid=1400021400&random=123
qcloud_sms_url=https://yun.tim.qq.com/v3/tlssmssvr/sendmultisms2?sdkappid=1400021400&random=124
qcloud_sms_key=6e56f948f6f1
sendsms.username=yohoyw
sendsms.pwd=NCftHmJ9
... ...