Authored by FengRuwei

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

package com.monitor.influxdb.mapper.impl;
import com.monitor.common.contants.InfluxDBContants;
import com.monitor.influxdb.InfluxDBModel;
import com.monitor.influxdb.InfluxDBQuery;
import com.monitor.influxdb.InluxDBSingle;
import com.monitor.influxdb.mapper.IZkMapper;
import com.monitor.influxdb.model.ZkInfo;
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;
... ... @@ -30,7 +33,6 @@ public class ZkMapper extends InfluxDBQuery implements IZkMapper {
public void insert(ZkInfo zkInfo) {
Point point = Point.measurement(InfluxDBContants.ZOOKEEPER_ALARM)
.addField("id", zkInfo.getId())
.addField("hostIp", zkInfo.getHostIp())
.addField("isLive", zkInfo.getIsLive())
.time(System.currentTimeMillis() * 1000000 + random.nextInt(999999), TimeUnit.NANOSECONDS)
... ... @@ -42,12 +44,64 @@ public class ZkMapper extends InfluxDBQuery implements IZkMapper {
@Override
public int selectCountByCodition(PageBean page) {
String command = "SELECT COUNT(id) FROM "+InfluxDBContants.ZOOKEEPER_ALARM;
StringBuffer command=new StringBuffer();
command.append("SELECT COUNT(hostIp) FROM " + InfluxDBContants.ZOOKEEPER_ALARM) ;
String hostIp=(String)page.getParams().get("hostIP");
int flag=0;
if(StringUtils.isNotBlank(hostIp)){
flag=1;
command.append("where hostIp='"+hostIp+"'");
}
String time=(String)page.getParams().get("time");
if(StringUtils.isNotBlank(hostIp)){
if(flag==1){
command.append(" and time>'"+time+"'");
}else{
command.append(" where time>'"+time+"'");
}
}
String isLive=(String)page.getParams().get("isLive");
if(StringUtils.isNotBlank(hostIp)){
if(flag==1){
command.append(" and time="+isLive);
}else{
command.append("where and time="+isLive);
}
}
InfluxDBModel influxDBModel= inluxDBSingle.getInfluxDBByName(InfluxDBContants.ALARM);
QueryResult result= query(influxDBModel.getName(), command.toString(), InfluxDBContants.MIDDLEWARE_ALARM);
return 1;
}
@Override
public List<ZkInfo> selectZkInfosByCodition(PageBean page) {
return null;
StringBuffer command=new StringBuffer();
command.append("SELECT hostIp,isLive,time FROM "+InfluxDBContants.ZOOKEEPER_ALARM) .append( " where 1=1") ;
String hostIp=(String)page.getParams().get("hostIP");
int flag=0;
if(StringUtils.isNotBlank(hostIp)){
flag=1;
command.append("where hostIp='"+hostIp+"'");
}
String time=(String)page.getParams().get("time");
if(StringUtils.isNotBlank(hostIp)){
if(flag==1){
command.append(" and time>'"+time+"'");
}else{
command.append(" where time>'"+time+"'");
}
}
String isLive=(String)page.getParams().get("isLive");
if(StringUtils.isNotBlank(hostIp)){
if(flag==1){
command.append(" and time="+isLive);
}else{
command.append("where and time="+isLive);
}
}
command.append("' ORDER BY time DESC LIMIT " + page.getPageSize() + " OFFSET " + page.getStartIndex());
InfluxDBModel influxDBModel= inluxDBSingle.getInfluxDBByName(InfluxDBContants.ALARM);
QueryResult result= query(influxDBModel.getName(), command.toString(), InfluxDBContants.MIDDLEWARE_ALARM);
return null;
}
}
... ...
... ... @@ -38,17 +38,13 @@
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<exclusions>
<exclusion>
<artifactId>log4j</artifactId>
<groupId>log4j</groupId>
</exclusion>
<exclusion>
<artifactId>slf4j-log4j12</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
</dependency>
</dependencies>
</project>
\ No newline at end of file
... ...
... ... @@ -3,7 +3,10 @@ package com.monitor.middleware.zookeeper.service.iml;
import com.monitor.influxdb.mapper.IZkMapper;
import com.monitor.influxdb.model.ZkInfo;
import com.monitor.middleware.zookeeper.service.IZkMonitorService;
import org.apache.zookeeper.ZooKeeper;
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.RetryOneTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
... ... @@ -42,12 +45,7 @@ public class ZkMonitorServiceImpl implements IZkMonitorService {
List<ZkInfo> zkList=new ArrayList<ZkInfo>();
ZkInfo zk=null;
for(String ip:ipList){
int result=1;
try {
result=checkConnection(ip);
} catch (Exception e) {
result=0;
}
int result=checkConnection1(ip);
zookeeperMapper.insert(new ZkInfo(ip,result));
}
... ... @@ -55,12 +53,19 @@ public class ZkMonitorServiceImpl implements IZkMonitorService {
}
private int checkConnection(String ip) throws Exception{
ZooKeeper zk = new ZooKeeper("192.168.102.205:2181", 2000,null);
// 获取某路径下所有节点
List<String> children = zk.getChildren("/", false);
// log.info("checkConnection is param { },size is { }",ip,null==children?0:children.size());
return 1;
private int checkConnection1(String ip){
try {
RetryPolicy retryPolicy=new RetryOneTime(1000);
CuratorFramework client = CuratorFrameworkFactory.newClient(ip+":2181", retryPolicy);
client.start();
List<String> children = client.getChildren().forPath("/");
} catch (Exception e) {
return 0;
}
return 1;
}
}
... ...
... ... @@ -4,7 +4,6 @@ import com.monitor.middleware.zookeeper.service.IZkMonitorService;
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.Component;
/**
... ... @@ -18,7 +17,7 @@ public class ZookeeperMonitorTask {
@Autowired
IZkMonitorService zkMonitorService;
@Scheduled(fixedRate=20000)
//@Scheduled(fixedRate=20000)
public void zookeeperMonitor() {
zkMonitorService.zookeeperMonitor();
}
... ...