Authored by simba

update

... ... @@ -200,7 +200,7 @@ public class HttpClientUtil {
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);
String result = doget("http://192.168.102.211:15672/api/vhosts", null, map);
System.out.println(result);
}
... ...
package com.monitor.common.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.client.RestTemplate;
import java.util.Map;
public class HttpRestClient {
private static final Logger logger = LoggerFactory
.getLogger(HttpRestClient.class);
private RestTemplate restTemplate;
public <T> T defaultPost(String uri, Object request, Class<T> responseType) {
try {
return restTemplate.postForObject(uri, request,responseType);
} catch (Exception e) {
logger.error("postForObject failed!url:"+uri, e);
return null;
}
}
public <T> T defaultGet(String uri, Class<T> responseType,Map<String, ?> map) {
try {
StringBuilder url = new StringBuilder(uri);
if(map != null){
url.append("?");
for (Map.Entry<String, ?> entry : map.entrySet()) {
url.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
}
return restTemplate.getForObject(url.toString().substring(0, url.length()-1), responseType);
}
return restTemplate.getForObject( uri, responseType);
} catch (Exception e) {
logger.error("getForObject failed!url: "+uri, e);
return null;
}
}
public <T> T defaultGet(String uri, Class<T> responseType) {
try {
return restTemplate.getForObject(uri, responseType);
} catch (Exception e) {
logger.error("getForObject failed!url: "+uri, e);
return null;
}
}
public void setRestTemplate(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
}
... ...
... ... @@ -38,4 +38,8 @@
</property>
</bean>
<bean id="httpRestClient" class="com.monitor.common.util.HttpRestClient">
<property name="restTemplate" ref="restTemplate"/>
</bean>
</beans>
\ No newline at end of file
... ...
package com.monitor.influxdb.mapper.impl;
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import org.apache.commons.lang.StringUtils;
import org.influxdb.dto.Point;
import org.influxdb.dto.QueryResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.monitor.common.contants.InfluxDBContants;
import com.monitor.common.util.QueryResultUtil;
import com.monitor.influxdb.InfluxDBModel;
... ... @@ -18,6 +8,15 @@ import com.monitor.influxdb.InluxDBSingle;
import com.monitor.influxdb.mapper.IRedisMapper;
import com.monitor.influxdb.model.RedisInfo;
import com.monitor.model.domain.PageBean;
import org.apache.commons.lang.StringUtils;
import org.influxdb.dto.Point;
import org.influxdb.dto.QueryResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;
@Component
public class RedisMapper extends InfluxDBQuery implements IRedisMapper {
... ... @@ -36,7 +35,7 @@ public class RedisMapper extends InfluxDBQuery implements IRedisMapper {
.addField("role", redis.getRole())
.addField("connected_clients", redis.getConnected_clients())
.addField("is_slave", redis.getIs_slave())
.addField("used_memory_peak_human", redis.getUsed_memory_peak_human())
// .addField("used_memory_peak_human", redis.getUsed_memory_peak_human())
.time(System.currentTimeMillis() * 1000000 + random.nextInt(999999), TimeUnit.NANOSECONDS)
.build();
inluxDBSingle.getInfluxDBByName(InfluxDBContants.ALARM).getInfluxDB()
... ...
package com.monitor.middleware.redis.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.monitor.common.util.HttpRestClient;
import com.monitor.common.util.SSHRedis;
import com.monitor.influxdb.mapper.IRedisMapper;
import com.monitor.influxdb.model.RedisInfo;
import com.monitor.middleware.redis.service.IRedisMonitorService;
import com.monitor.mysql.mapper.RedisMonitorMapper;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import com.monitor.common.util.SSHRedis;
import com.monitor.influxdb.mapper.IRedisMapper;
import com.monitor.influxdb.model.RedisInfo;
import com.monitor.middleware.redis.service.IRedisMonitorService;
import java.util.List;
@Service
public class RedisMonitorServiceImpl implements IRedisMonitorService {
... ... @@ -19,9 +24,48 @@ public class RedisMonitorServiceImpl implements IRedisMonitorService {
@Autowired
IRedisMapper redisMapper;
@Autowired
RedisMonitorMapper redisMonitorMapper;
@Autowired
HttpRestClient httpRestClient;
@Override
public void redisMonitor() {
/**********************************************************************
*1、处理twemproxy
***********************************************************************/
List<com.model.RedisInfo> proxyList=redisMonitorMapper.selectRedisMonitorByLevel(1);
if(CollectionUtils.isEmpty(proxyList)){
return;
}
StringBuffer paramMonitor=null;
for(com.model.RedisInfo obj:proxyList){
paramMonitor=new StringBuffer();
//JSONObject response=httpRestClient.defaultPost(obj.getHostIp()+obj.getParamMonitor(), null, JSONObject.class);
JSONObject response=httpRestClient.defaultPost("http://192.168.102.222:22222", null, JSONObject.class);
if(null != response){
int total_connections=(Integer)response.get("total_connections");
int curr_connections=(Integer)response.get("curr_connections");
if(total_connections>0){
paramMonitor.append("总连接数:"+total_connections);
}
if (total_connections > 0) {
paramMonitor.append("当前接数:"+curr_connections);
}
}
obj.setParamMonitor(paramMonitor.toString());
redisMonitorMapper.updateByPrimaryKey(obj);
}
/**********************************************************************
*2、处理Redis
***********************************************************************/
List<com.model.RedisInfo> redisList=redisMonitorMapper.selectRedisMonitorByLevel(2);
int isSlave=0;
int isRun=0;
String redisInfo = SSHRedis.exec("192.168.102.162", "root", "123456", 22,"sleep 20;cd /usr/bin;redis-cli -h 192.168.102.222 -p 6379 info;");
... ...