Authored by simba

update

package com.monitor.cmdb.ctrl;
import com.model.HostInfo;
import com.monitor.cmdb.service.IZkMoitorService;
import com.monitor.influxdb.model.ZkInfo;
import com.monitor.model.request.ZkInfoReq;
import com.monitor.model.response.BaseResponse;
import com.monitor.model.response.PageResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* Created by yoho on 2016/6/14.
* 查询机器信息
*/
@Controller
@RequestMapping("zkMonitor")
public class ZkMonitorCtrl {
Logger log = LoggerFactory.getLogger(ZkMonitorCtrl.class);
@Autowired
IZkMoitorService zkMoitorService;
@RequestMapping("/getZkMonitorRecords")
@ResponseBody
public BaseResponse<PageResponse<ZkInfo>> getZkMonitorRecords(@RequestBody ZkInfoReq req) throws Exception {
log.info("getHostInfos with param is {}", req);
// 查询列表
PageResponse<ZkInfo> responseBO = zkMoitorService.getZkMonitorRecords(req);
if (responseBO == null || CollectionUtils.isEmpty(responseBO.getRows())) {
return new BaseResponse<PageResponse<ZkInfo>>();
}
PageResponse<ZkInfo> response = new PageResponse<ZkInfo>();
response.setCurrentPage(responseBO.getCurrentPage());
response.setRows(responseBO.getRows());
response.setPageSize(responseBO.getPageSize());
response.setTotal(responseBO.getTotal());
log.info("getHostInfos success and total={}", response.getTotal());
return new BaseResponse<PageResponse<ZkInfo>>(response);
}
}
... ...
package com.monitor.cmdb.service;
import com.monitor.influxdb.model.ZkInfo;
import com.monitor.model.request.ZkInfoReq;
import com.monitor.model.response.PageResponse;
/**
* Created by yoho on 2016/6/22.
*/
public interface IZkMoitorService {
PageResponse<ZkInfo> getZkMonitorRecords(ZkInfoReq req);
}
... ...
package com.monitor.cmdb.service.impl;
import com.monitor.cmdb.service.IZkMoitorService;
import com.monitor.influxdb.mapper.IZkMapper;
import com.monitor.influxdb.model.ZkInfo;
import com.monitor.model.domain.PageBean;
import com.monitor.model.request.ZkInfoReq;
import com.monitor.model.response.PageResponse;
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.List;
/**
* Created by yoho on 2016/6/22.
*/
@Service
public class ZkMoitorServiceImpl implements IZkMoitorService {
Logger logger = LoggerFactory.getLogger(HostInfoServiceImpl.class);
@Autowired
IZkMapper zkMapper;
@Override
public PageResponse<ZkInfo> getZkMonitorRecords(ZkInfoReq req) {
logger.info("getHostInfos with param is {}", req);
// 组装分页对象
PageBean page = PageBean.initPageInfo(req.getCurrentPage(),
req.getPageSize(), req);
// 先查询符合条件的总数量
int total = zkMapper.selectCountByCodition(page);
logger.info("selectUserTotal num is {}, with param is {}", total,
req);
// 数量为0 直接返回
if (total == 0) {
// 返回初始page对象
return null;
}
// 获取列表
List<ZkInfo> zkInfos = zkMapper.selectZkInfosByCodition(page);
if (CollectionUtils.isEmpty(zkInfos)) {
logger.debug("getHostInfos is null with param is {}", req);
return null;
}
PageResponse<ZkInfo> response = new PageResponse<ZkInfo>();
response.setCurrentPage(req.getCurrentPage());
response.setPageSize(req.getPageSize());
response.setTotal(total);
response.setRows(zkInfos);
return response;
}
}
... ...
package com.monitor.influxdb.mapper;
import com.monitor.influxdb.model.ZkInfo;
import com.monitor.model.domain.PageBean;
import java.util.List;
/**
* Created by yoho on 2016/6/21.
*/
public interface IZkMapper {
public void insert(ZkInfo zkInfo);
public int selectCountByCodition(PageBean page);
List<ZkInfo> selectZkInfosByCodition(PageBean page);
}
... ...
package com.monitor.influxdb.mapper.impl;
import com.monitor.common.contants.InfluxDBContants;
import com.monitor.influxdb.InfluxDBQuery;
import com.monitor.influxdb.InluxDBSingle;
import com.monitor.influxdb.mapper.IZookeeperMapper;
import com.monitor.influxdb.model.ZookeeperInfo;
import org.influxdb.dto.BatchPoints;
import com.monitor.influxdb.mapper.IZkMapper;
import com.monitor.influxdb.model.ZkInfo;
import com.monitor.model.domain.PageBean;
import org.influxdb.dto.Point;
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;
... ... @@ -16,15 +18,16 @@ import java.util.concurrent.TimeUnit;
* Created by yoho on 2016/6/21.
*/
@Component
public class ZookeeperMapper implements IZookeeperMapper{
public class ZkMapper extends InfluxDBQuery implements IZkMapper {
@Autowired
private InluxDBSingle inluxDBSingle;
Random random = new Random();
@Override
public void insert(ZookeeperInfo zkInfo) {
public void insert(ZkInfo zkInfo) {
Point point = Point.measurement(InfluxDBContants.ZOOKEEPER_ALARM)
.addField("id", zkInfo.getId())
... ... @@ -36,4 +39,22 @@ public class ZookeeperMapper implements IZookeeperMapper{
.write(InfluxDBContants.MIDDLEWARE_ALARM, "default", point);
}
@Override
public int selectCountByCodition(PageBean page) {
String command = "SELECT COUNT(id) FROM "+InfluxDBContants.ZOOKEEPER_ALARM;
return query(influxDBName, command, InfluxDBContants.ZOOKEEPER_ALARM,
"ZkMapper", "selectCountByCodition");
}
@Override
public List<ZkInfo> selectZkInfosByCodition(PageBean page) {
String command="SELECT id, hostIp, isLive from "+InfluxDBContants.ZOOKEEPER_ALARM+
" ORDER BY time DESC LIMIT " + page.getParams().get("pageSize") +
" OFFSET " + page.getParams().get("startIndex");
return query(influxDBName, command, InfluxDBContants.ZOOKEEPER_ALARM,
"ZkMapper", "selectZkInfosByCodition");
}
}
... ...
... ... @@ -6,18 +6,18 @@ import lombok.Data;
* Created by yoho on 2016/6/21.
*/
@Data
public class ZookeeperInfo {
public class ZkInfo {
private int id;
private String hostIp;
private int isLive; //0是不活动,1是活动
private String redcordTime;
public ZookeeperInfo() {
public ZkInfo() {
super();
}
public ZookeeperInfo(String hostIp, int isLive) {
public ZkInfo(String hostIp, int isLive) {
super();
this.hostIp = hostIp;
this.isLive = isLive;
... ...
package com.monitor.influxdb.mapper;
import com.monitor.influxdb.model.ZookeeperInfo;
package com.monitor.middleware.zookeeper.service;
/**
* Created by yoho on 2016/6/21.
*/
public interface IZookeeperMapper {
public interface IZkMonitorService {
public void zookeeperMonitor();
public void insert(ZookeeperInfo zkInfo);
}
... ...
package com.monitor.middleware.zookeeper.service;
package com.monitor.middleware.zookeeper.service.iml;
import com.monitor.influxdb.mapper.IZookeeperMapper;
import com.monitor.influxdb.model.ZookeeperInfo;
import com.monitor.influxdb.mapper.IZkMapper;
import com.monitor.influxdb.model.ZkInfo;
import com.monitor.middleware.zookeeper.service.IZkMonitorService;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooKeeper;
import org.slf4j.Logger;
... ... @@ -17,12 +18,12 @@ import java.util.List;
* Created by yoho on 2016/6/21.
*/
@Service
public class IZookeeperMonitorService {
public class ZkMonitorServiceImpl implements IZkMonitorService {
Logger log = LoggerFactory.getLogger(IZookeeperMonitorService.class);
Logger log = LoggerFactory.getLogger(ZkMonitorServiceImpl.class);
@Autowired
IZookeeperMapper zookeeperMapper;
IZkMapper zookeeperMapper;
public void zookeeperMonitor(){
log.info("task start...");
... ... @@ -38,8 +39,8 @@ public class IZookeeperMonitorService {
ipList.add("10.66.4.8");
ipList.add("10.66.4.9");
List<ZookeeperInfo> zkList=new ArrayList<ZookeeperInfo>();
ZookeeperInfo zk=null;
List<ZkInfo> zkList=new ArrayList<ZkInfo>();
ZkInfo zk=null;
for(String ip:ipList){
int result=1;
try {
... ... @@ -47,7 +48,7 @@ public class IZookeeperMonitorService {
} catch (Exception e) {
result=0;
}
zookeeperMapper.insert(new ZookeeperInfo(ip,result));
zookeeperMapper.insert(new ZkInfo(ip,result));
}
log.info("task end...");
... ...
package com.monitor.middleware.zookeeper.task;
import com.monitor.influxdb.model.ZookeeperInfo;
import com.monitor.influxdb.model.ZkInfo;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooKeeper;
import org.slf4j.Logger;
... ... @@ -36,7 +36,7 @@ public class ZookeeperMonitorTask {
ipList.add("10.66.4.8");
ipList.add("10.66.4.9");
List<ZookeeperInfo> zkList=new ArrayList<ZookeeperInfo>();
List<ZkInfo> zkList=new ArrayList<ZkInfo>();
for(String ip:ipList){
boolean result=true;
try {
... ...
package com.monitor.model.request;
import com.monitor.model.page.PageRequest;
import lombok.Data;
/**
* Created by yoho on 2016/6/14.
*/
@Data
public class ZkInfoReq extends PageRequest {
private String hostIp;
private String recordTime;
}
... ...