Authored by simba

update

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