Authored by simba

update

package com.monitor.common.util;
import org.apache.commons.lang.StringUtils;
import redis.clients.jedis.Jedis;
import java.util.HashMap;
import java.util.Map;
public class RedisInfo {
public static Map<String,Object> getRedisInfo(String host,int port){
Jedis client = new Jedis(host, port);
String redisInfo=client.info();
if(StringUtils.isBlank(redisInfo)){
return null;
}
Map<String,Object> result=new HashMap<String, Object>();
String[] arr = redisInfo.split("\r\n");
for (String str : arr) {
if (str.startsWith("#")||str.startsWith("\r\n")) {
continue;
}
String[] a = str.split(":");
if(a.length==2){
result.put(a[0], a[1]);
}
}
return result;
}
public static void main(String args[]){
System.out.println(getRedisInfo("192.168.102.222",6379));
}
}
\ No newline at end of file
... ...
package com.monitor.common.util;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class SSHRedis {
/**
* 远程 执行命令并返回结果调用过程 是同步的(执行完才会返回)
* @param host 主机名
* @param user 用户名
* @param psw 密码
* @param port 端口
* @param command 命令
* @return
*/
public static String exec(String host,String user,String psw,int port,String command){
String result="";
Session session =null;
ChannelExec openChannel =null;
try {
JSch jsch=new JSch();
session = jsch.getSession(user, host, port);
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.setPassword(psw);
session.connect();
openChannel = (ChannelExec) session.openChannel("exec");
openChannel.setCommand(command);
int exitStatus = openChannel.getExitStatus();
System.out.println(exitStatus);
openChannel.connect();
InputStream in = openChannel.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String buf = null;
while ((buf = reader.readLine()) != null) {
result+= new String(buf.getBytes("gbk"),"UTF-8")+" <br>\r\n";
}
} catch (JSchException | IOException e) {
result+=e.getMessage();
}finally{
if(openChannel!=null&&!openChannel.isClosed()){
openChannel.disconnect();
}
if(session!=null&&session.isConnected()){
session.disconnect();
}
}
return result;
}
public static void main(String args[]){
String exec = exec("192.168.102.162", "root", "123456", 22, "sleep 20;cd /usr/bin;redis-cli -h 192.168.102.222 -p 6379 info;");
System.out.println(exec);
}
}
\ No newline at end of file
... ... @@ -120,8 +120,8 @@ public class ZkMapper extends InfluxDBQuery implements IZkMapper {
for (ZkInfo param : paramList) {
int cloudType = param.getCloudType();
String hostIp = param.getHostIp();
sb.append("SELECT hostIp,cloudType,isLive,time FROM " + InfluxDBContants.ZOOKEEPER_ALARM+" where time > now() - 1h ");
sb.append(" and hostIp='" + hostIp+"'");
sb.append("SELECT hostIp,cloudType,isLive,time FROM " + InfluxDBContants.ZOOKEEPER_ALARM+" where");
sb.append(" hostIp='" + hostIp+"'");
sb.append(" and cloudType=" + cloudType);
sb.append(" order by time desc limit 1;");
}
... ...
package com.monitor.middleware.redis.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.model.RedisInfo;
import com.monitor.common.util.HttpRestClient;
import com.monitor.common.util.SSHRedis;
import com.monitor.common.util.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 java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class RedisMonitorServiceImpl implements IRedisMonitorService {
... ... @@ -29,23 +30,65 @@ public class RedisMonitorServiceImpl implements IRedisMonitorService {
@Override
public void redisMonitor() {
}
public String getRedisInfo(String redisInfo, String info) {
String[] arry = redisInfo.split("<br>");
for (int i = 0; i < arry.length; i++) {
String[] b = arry[i].replace("\r\n", "").split(":");
for (int j = 0; j < b.length; j++) {
if (b[j].equals(info)) {
return b[1].trim();
/**********************************************************************
*1、处理twemproxy
***********************************************************************/
List<com.model.RedisInfo> proxyList=redisMonitorMapper.selectRedisMonitorByLevel(1);
if(CollectionUtils.isEmpty(proxyList)){
return;
}
StringBuffer paramMonitor=null;
Map<String,List<String>> tMap=new HashMap<String,List<String>>();
List<String> ipList=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);
}
//查看代理下的redis
JSONObject alpha=response.getJSONObject("alpha");
ipList=new ArrayList<String>();
for (Map.Entry<String, Object> entry : alpha.entrySet()) {
String key=entry.getKey();
if(key.indexOf(":")>1){
ipList.add(key);
}
}
tMap.put(obj.getHostIp(),ipList);
}
obj.setParamMonitor(paramMonitor.toString());
redisMonitorMapper.updateByPrimaryKey(obj);
}
/**********************************************************************
*2、处理Redis
***********************************************************************/
if(tMap.size()==0){
return;
}
Map<String,Object> rMap=new HashMap<String,Object>();
for (Map.Entry<String, List<String>> entry : tMap.entrySet()) {
String key = entry.getKey().toString();
List<String> rlist = entry.getValue();
if(!CollectionUtils.isEmpty(rlist)){
for(String ipStr:rlist){
String[] ipConfig=ipStr.split(":");
Map<String,Object> result=null;
if(ipConfig.length==2){
result=RedisInfo.getRedisInfo(ipConfig[0],Integer.valueOf(ipConfig[1]));
rMap.put(ipConfig[0],result);
}
}
}
}
return null;
}
}
}
... ...
... ... @@ -60,14 +60,15 @@ public class ZkMonitorServiceImpl implements IZkMonitorService {
private int checkConnection1(String ip){
RetryPolicy retryPolicy=new RetryOneTime(1000);
CuratorFramework client =CuratorFrameworkFactory.newClient(ip+":2181", 5*1000, 5*1000, retryPolicy);
try {
RetryPolicy retryPolicy=new RetryOneTime(1000);
CuratorFramework client = CuratorFrameworkFactory.newClient(ip+":2181", retryPolicy);
client.start();
List<String> children = client.getChildren().forPath("/");
client.close();
} catch (Exception e) {
return 0;
}finally {
client.close();
}
return 1;
}
... ...