Authored by zhengyouwei

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

# Conflicts:
#	monitor-service-web/src/main/resources/test/timed_task_corn.properties
Showing 17 changed files with 145 additions and 85 deletions
... ... @@ -10,18 +10,21 @@ import com.monitor.influxdb.mapper.MonitorAlarmMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* Created by yoho on 2016/5/3.
*/
@Service("alarmMsgService")
public class AlarmMsgServiceImpl implements AlarmMsgService {
public static final Logger DEBUG = LoggerFactory.getLogger(AlarmMsgService.class);
@Autowired
private HttpRestClientService httpRestClientService;
... ... @@ -39,6 +42,11 @@ public class AlarmMsgServiceImpl implements AlarmMsgService {
private static final int CONTENT_SUB_LENGTH = 250;
public static final String DEFAULT_SENT_LOCK="sent_lock";
private Map<String, Long> msgSentLogMap = new HashMap<>();
/**
* 发送短信
*
... ... @@ -61,6 +69,13 @@ public class AlarmMsgServiceImpl implements AlarmMsgService {
*/
@Override
public boolean sendSms(String type, String content, String alarmInfo, String mobile) {
//无需发送
if (!checkSentOrNo(content, mobile)) {
return true;
}
Map<String, String> contentMap = new HashMap<String, String>();
contentMap.put("OperID", sendsmsConfig.getUserName());
contentMap.put("OperPass", sendsmsConfig.getPwd());
... ... @@ -91,8 +106,8 @@ public class AlarmMsgServiceImpl implements AlarmMsgService {
try {
monitorAlarmMapper.insertAlarmMsg(InfluxDBContants.AWS, type, content, alarmInfo, sendStatus ? "successed" : "failed");
}catch (Exception e){
logger.error("insert sms into influxdb failed",e);
} catch (Exception e) {
logger.error("insert sms into influxdb failed", e);
}
return sendStatus;
... ... @@ -110,7 +125,7 @@ public class AlarmMsgServiceImpl implements AlarmMsgService {
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]))) {
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());
... ... @@ -125,4 +140,60 @@ public class AlarmMsgServiceImpl implements AlarmMsgService {
}
}
private boolean checkSentOrNo(String content, String mobile) {
String msgKey = content + "@" + mobile;
synchronized (DEFAULT_SENT_LOCK) {
//没有发送记录
if (!msgSentLogMap.containsKey(msgKey)) {
msgSentLogMap.put(msgKey, System.currentTimeMillis());
return true;
} else {
Long sentTime = msgSentLogMap.get(msgKey);
Long sentAgainTime = System.currentTimeMillis();
//距离发次发送已超过5分钟
if (5 * 60 * 1000 <= (sentAgainTime - sentTime)) {
msgSentLogMap.put(msgKey, System.currentTimeMillis());
return true;
}
}
return false;
}
}
/**
* 清理垃圾的短信发送记录
*/
@Scheduled(fixedRate = 5 * 60 * 1000)
public void clearMsgSentLog() {
synchronized (DEFAULT_SENT_LOCK)
{
for (Map.Entry<String, Long> entry : msgSentLogMap.entrySet()) {
String sentKey = entry.getKey();
Long sentTime = msgSentLogMap.get(sentKey);
Long sentAgainTime = System.currentTimeMillis();
if (5 * 60 * 1000 <= (sentAgainTime - sentTime)) {
msgSentLogMap.remove(sentKey);
}
}
}
}
}
... ...
... ... @@ -146,6 +146,7 @@ public class MemcachedCtrl {
temp.setCpu(temp.getCpu() + "\r\n" + vo.getCpu());
temp.setMem(temp.getMem() + "\r\n" + vo.getMem());
temp.setNet(temp.getNet() + "\r\n" + vo.getNet());
temp.setDiff(temp.getDiff()+"\r\n"+vo.getDiff());
}
return temp;
}
... ... @@ -171,7 +172,7 @@ public class MemcachedCtrl {
}
vo.setUpTime(info.getUpTime());
vo.setDiff(info.getDiff()+"ms");
vo.setCurItems(info.getCurItems());
vo.setMaxMemory(info.getMaxMemory());
vo.setCurConnections(info.getCurConnections());
... ... @@ -180,6 +181,7 @@ public class MemcachedCtrl {
vo.setUrl(key);
vo.setName(MemConstants.MEMCACHED_NAME_MAP.get(key));
//增加twemproxy的内存、cpu、网卡
ZabbixTaskService zabbixTaskService = SpringContextUtils.getBeanByClass(ZabbixTaskService.class);
... ...
... ... @@ -27,4 +27,6 @@ public class MemcachedInfo {
private Long oldSet; //前一次监控的set数
private Long diff;
}
... ...
... ... @@ -32,4 +32,7 @@ public class MemcachedVo {
private String mem = "- / -";
private String net = "- / -";
//get、set操作时间差
private String diff;
}
... ...
... ... @@ -12,7 +12,9 @@ import com.monitor.middleware.memcached.constant.MemConstants;
import com.monitor.middleware.memcached.model.MemcachedInfo;
import com.monitor.zabbix.comp.InfluxdbComp;
import net.rubyeye.xmemcached.MemcachedClient;
import net.rubyeye.xmemcached.MemcachedClientBuilder;
import net.rubyeye.xmemcached.XMemcachedClient;
import net.rubyeye.xmemcached.XMemcachedClientBuilder;
import net.rubyeye.xmemcached.exception.MemcachedException;
import net.rubyeye.xmemcached.utils.AddrUtil;
import org.apache.commons.lang.StringUtils;
... ... @@ -181,6 +183,19 @@ public class MemcachedMonitorImpl {
info.setMaxMemory(new DecimalFormat("0.00").format(limit_maxbytes * 1.0 / 1024 / 1024 / 1024) + " G");
info.setUpTime(uptime / (60 * 60 * 24) + " Days");
info.setUseMemory(new DecimalFormat("0.00").format(bytes * 1.0 / 1024 / 1024 / 1024) + " G");
MemcachedClientBuilder builder = new XMemcachedClientBuilder(AddrUtil.getAddresses(url));
try {
MemcachedClient client = builder.build();
long setTime = System.currentTimeMillis();
client.set("hello",3,"Hello");
client.get("hello");
long diff = System.currentTimeMillis()-setTime;
info.setDiff(diff);
client.shutdown();
} catch (Exception e) {
LOGGER.error("memcached get set occurs Exception"+e.getMessage());
}
MemConstants.MEMCACHED_INFO_MAP.put(url, info);
LOGGER.info("MemConstants.MEMCACHED_INFO_MAP all key is {}", MemConstants.MEMCACHED_INFO_MAP.keySet());
}
... ...
... ... @@ -101,7 +101,7 @@ public class RabbitmqCtrl {
DEBUG.info("Query the queues of mo {}", request.getMoId());
BaseResponse<PageResponse<QueueInfo>> baseResponse = new BaseResponse();
BaseResponse baseResponse = new BaseResponse();
QueueView oneView = InterVar.queueViewMaps.get(Integer.parseInt(request.getMoId()));
... ... @@ -123,7 +123,7 @@ public class RabbitmqCtrl {
}
}
List<QueueInfo> selectedList = new ArrayList<>();
/*List<QueueInfo> selectedList = new ArrayList<>();
int start = (request.getCurrentPage() - 1) * request.getPageSize();
... ... @@ -134,9 +134,9 @@ public class RabbitmqCtrl {
for (int i = 0; i < realCount; i++) {
selectedList.add(queuesList.get(start + i));
}
}*/
PageResponse<QueueInfo> response = new PageResponse<>();
/*PageResponse<QueueInfo> response = new PageResponse<>();
response.setCurrentPage(request.getCurrentPage());
... ... @@ -144,11 +144,11 @@ public class RabbitmqCtrl {
response.setPageSize(request.getPageSize());
response.setTotalPage(queuesList.size() / request.getPageSize() + 1);
response.setTotalPage(queuesList.size() / request.getPageSize() + 1);*/
response.setRows(selectedList);
/* response.setRows(selectedList);*/
baseResponse.setData(response);
baseResponse.setData(queuesList);
}
return baseResponse;
... ...
... ... @@ -2,6 +2,7 @@ package com.monitor.middleware.redis.constants;
import com.monitor.middleware.redis.model.RedisInfo;
import com.monitor.middleware.redis.model.TwemproxyInfo;
import redis.clients.jedis.JedisPool;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
... ... @@ -16,4 +17,6 @@ public interface Constants {
//key: "ip:port"
Map<String, TwemproxyInfo> TWEMPROXY_INFO_MAP = new ConcurrentHashMap<>();
/* Map<String, JedisPool> JEDIS_POOL_MAP = new ConcurrentHashMap<>();*/
}
... ...
... ... @@ -61,19 +61,6 @@ public class RedisCtrl {
DEBUG.error("Failed to query twemproxy info... error {}", e);
}
Collections.sort(twemproxyVOList, new Comparator<TwemproxyVo>() {
@Override
public int compare(TwemproxyVo o1, TwemproxyVo o2) {
if (o1.getNetwork().compareTo(o2.getNetwork()) > 0) {
return 1;
} else if (o1.getNetwork().compareTo(o2.getNetwork()) < 0) {
return -1;
}
return 0;
}
});
response.setData(twemproxyVOList);
return response;
... ...
... ... @@ -28,6 +28,7 @@ import java.util.concurrent.atomic.AtomicLong;
public class RedisMonitorImpl {
public static final ExecutorService EXECUTOR_SERVICE = Executors.newFixedThreadPool(10);
@Autowired
private AlarmMsgService alarmMsgService;
... ... @@ -40,10 +41,8 @@ public class RedisMonitorImpl {
@Autowired
private IMObjectInfoService mobjectService;
private Map<Integer, List<MObjectInfo>> serversMap;
@Getter
private AtomicLong update=new AtomicLong();
private AtomicLong update = new AtomicLong();
@Scheduled(cron="${cron_task_redis_monitor}")
public void monitor() {
... ... @@ -52,10 +51,11 @@ public class RedisMonitorImpl {
Constants.TWEMPROXY_INFO_MAP.clear();
//遍历所有的redis / twemproxy
serversMap = getServersMap();
Map<Integer, List<MObjectInfo>> serversMap = getServersMap();
for (Map.Entry<Integer, List<MObjectInfo>> entry : serversMap.entrySet()) {
//监控对象 类型
String typeName = typeService.queryTypeInfo(entry.getKey()).getTypeName();
List<MObjectInfo> redisList = new ArrayList<>();
... ... @@ -87,7 +87,7 @@ public class RedisMonitorImpl {
}
public Map<Integer, List<MObjectInfo>> getServersMap() {
private Map<Integer, List<MObjectInfo>> getServersMap() {
Map<Integer, List<MObjectInfo>> serversMap = new HashMap<>();
... ... @@ -174,6 +174,4 @@ public class RedisMonitorImpl {
return twemproxyTask;
}
}
... ...
package com.monitor.middleware.redis.task;
import com.monitor.middleware.redis.service.IRedisMonitorHandleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class RedisMonitorTask {
// @Autowired
// private IRedisMonitorHandleService redisMonitorService;
//
// @Scheduled(cron="${cron_task_redis_monitor}")
// public void redisMonitor(){
// redisMonitorService.redisMonitor();
// }
}
... ... @@ -321,7 +321,7 @@ public class RedisTask extends Task {
//增加twemproxy的内存、cpu、网卡
Map<String, String> vmMap = doQueryZabbix(redisInfo.getIp());
DEBUG.info("query vm_info {}", vmMap);
DEBUG.debug("query vm_info {}", vmMap);
if (null != vmMap) {
String cpu = vmMap.containsKey(SystemCpuEnum.CPU_UTIL_USER.name()) ? vmMap.get(SystemCpuEnum.CPU_UTIL_USER.name()) : "-";
... ... @@ -345,7 +345,7 @@ public class RedisTask extends Task {
if (0 != tot) {
percent = String.valueOf(avaLong * 100 / totLong);
}
}
redisInfo.setMem(percent + "%" + "(" + avaLong + "/" + totLong + ")");
... ... @@ -365,38 +365,35 @@ public class RedisTask extends Task {
if (StringUtils.equals("master", info.getRole())) {
if (client.isConnected()) {
long startSet = System.currentTimeMillis();
long startSet = System.currentTimeMillis();
DEBUG.info("Start to set key {} value {} time {}", testKey, testValue, startSet);
//写主 读主
client.set(testKey.getBytes(), testValue.getBytes());
DEBUG.debug("Start to set key {} value {} time {}", testKey, testValue, startSet);
//写主 读主
client.set(testKey.getBytes(), testValue.getBytes());
long endSet = System.currentTimeMillis();
long endSet = System.currentTimeMillis();
DEBUG.info("End to set key {} value {} time {}", testKey, testValue, endSet);
DEBUG.debug("End to set key {} value {} time {}", testKey, testValue, endSet);
Thread.sleep(3000);
Thread.sleep(1000);
long startGet = System.currentTimeMillis();
long startGet = System.currentTimeMillis();
DEBUG.info("Start to get key {} value {} time {}", testKey, testValue, startGet);
DEBUG.debug("Start to get key {} value {} time {}", testKey, testValue, startGet);
String testResult = new String(client.get(testKey.getBytes()));
String testResult = new String(client.get(testKey.getBytes()));
long endGet = System.currentTimeMillis();
long endGet = System.currentTimeMillis();
DEBUG.info("End to get key {} value {} time {}", testKey, testValue, endGet);
DEBUG.debug("End to get key {} value {} time {}", testKey, testValue, endGet);
if (StringUtils.equals(testValue, testResult)) {
return true;
}
if (StringUtils.equals(testValue, testResult)) {
DEBUG.info("test value {} is not equal result {} end set time {} ,end get time {}", testValue, testResult, endSet, endGet);
return true;
}
DEBUG.info("test value {} is not equal result {} end set time {} ,end get time {}", testValue, testResult, endSet, endGet);
} else {
//写主 读从
String masterInfo = info.getMasters();
... ... @@ -417,12 +414,16 @@ public class RedisTask extends Task {
masterClient.close();
Thread.sleep(3000);
Thread.sleep(1000);
if (client.isConnected()) {
//从读
String testResult = new String(client.get(testKey.getBytes()));
//从读,不对,增加一次
String testResult = new String(client.get(testKey.getBytes()));
if (StringUtils.equals(testValue, testResult)) {
return true;
} else {
Thread.sleep(2000);
testResult = new String(client.get(testKey.getBytes()));
if (StringUtils.equals(testValue, testResult)) {
return true;
}
... ... @@ -434,6 +435,4 @@ public class RedisTask extends Task {
return false;
}
}
... ...
... ... @@ -131,7 +131,7 @@ public class TwemproxyTask extends Task {
//增加twemproxy的内存、cpu、网卡
Map<String, String> vmMap = doQueryZabbix(this.getIp());
DEBUG.info("Query vm_info {}", vmMap);
DEBUG.debug("Query vm_info {}", vmMap);
if (null != vmMap) {
String cpu = vmMap.containsKey(SystemCpuEnum.CPU_UTIL_USER.name()) ? vmMap.get(SystemCpuEnum.CPU_UTIL_USER.name()) : "-";
... ...
... ... @@ -9,7 +9,7 @@
debug:当此属性设置为true时,将打印出logback内部日志信息,实时查看logback运行状态。默认值为false。 -->
<configuration scan="true" scanPeriod="60 seconds" debug="false">
<!-- 日志最大的历史 7天 -->
<property name="maxHistory" value="2"/>
<property name="maxHistory" value="1"/>
<!-- 日志最大的文件大小 10MB-->
<property name="maxFileSize" value="100MB"/>
... ...
... ... @@ -29,4 +29,4 @@ cron_task_zabbix_scan=0 0 0 * * ?
cron_task_javaservice_alarm=0 0 0 * * ?
cron_task_memcached=0 0 0 * * ?
\ No newline at end of file
cron_task_memcached=0 0 0 * * ?
... ...
... ... @@ -181,7 +181,7 @@ public class ZabbixAlarm {
List<ZabbixItemInfo> itemInfoList = itemMapper.queryItemInfo(idList);
DEBUG.info("Found id to items info {}", itemInfoList);
DEBUG.debug("Found id to items info {}", itemInfoList);
for (ZabbixItemInfo itemInfo : itemInfoList) {
CopyOnWriteArrayList<ZabbixItemInfo> itemInfos = Constants.ZABBIXALARMITEMMAP.get(itemInfo.getHostId());
... ...
... ... @@ -87,7 +87,7 @@ public class ZabbixIpTask implements Callable {
}
List<ZabbixHostInfo> hostInfoList = hostMapper.queryHostInfo(ipList);
DEBUG.info("Found host ip to id info {}", hostInfoList);
DEBUG.debug("Found host ip to id info {}", hostInfoList);
if (null == hostInfoList) {
return;
... ... @@ -134,7 +134,7 @@ public class ZabbixIpTask implements Callable {
List<ZabbixItemInfo> itemInfoList = itemMapper.queryItemInfo(idList);
DEBUG.info("Found id to items info {}", itemInfoList);
DEBUG.debug("Found id to items info {}", itemInfoList);
if (null == itemInfoList) {
return;
... ...
... ... @@ -123,7 +123,7 @@ public class ZabbixTaskService {
QueryResult result = influxdbComp.doQuery(new Query(command, "zabbix"));
DEBUG.info("influxdb result {}", result);
DEBUG.debug("influxdb result {}", result);
if (null == result || null == result.getResults()) {
return null;
... ...