Authored by jack.xue

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

@@ -12,7 +12,9 @@ import org.springframework.web.bind.annotation.RequestMapping; @@ -12,7 +12,9 @@ import org.springframework.web.bind.annotation.RequestMapping;
12 import org.springframework.web.bind.annotation.ResponseBody; 12 import org.springframework.web.bind.annotation.ResponseBody;
13 13
14 import com.model.ZkConfig; 14 import com.model.ZkConfig;
  15 +import com.model.ZkConfigAll;
15 import com.monitor.cmdb.service.IZkMoitorService; 16 import com.monitor.cmdb.service.IZkMoitorService;
  17 +import com.monitor.model.request.ZkTreeAllReq;
16 import com.monitor.model.request.ZkTreeReq; 18 import com.monitor.model.request.ZkTreeReq;
17 import com.monitor.model.response.BaseResponse; 19 import com.monitor.model.response.BaseResponse;
18 import com.monitor.model.response.PageResponse; 20 import com.monitor.model.response.PageResponse;
@@ -44,7 +46,12 @@ public class ZkMonitorCtrl { @@ -44,7 +46,12 @@ public class ZkMonitorCtrl {
44 return new BaseResponse<Object>(response); 46 return new BaseResponse<Object>(response);
45 } 47 }
46 48
47 - 49 + /**
  50 + * 查询选中Ip下的根节点
  51 + * @param req
  52 + * @return
  53 + * @throws Exception
  54 + */
48 @RequestMapping("/getZkMonitorTree") 55 @RequestMapping("/getZkMonitorTree")
49 @ResponseBody 56 @ResponseBody
50 public BaseResponse<PageResponse<ZkConfig>> getZkMonitorTree(@RequestBody ZkTreeReq req) throws Exception { 57 public BaseResponse<PageResponse<ZkConfig>> getZkMonitorTree(@RequestBody ZkTreeReq req) throws Exception {
@@ -60,6 +67,58 @@ public class ZkMonitorCtrl { @@ -60,6 +67,58 @@ public class ZkMonitorCtrl {
60 response.setTotal(list.getTotal()); 67 response.setTotal(list.getTotal());
61 return new BaseResponse<PageResponse<ZkConfig>>(response); 68 return new BaseResponse<PageResponse<ZkConfig>>(response);
62 } 69 }
  70 +
  71 + /**
  72 + * 查询跟节点下的所有子节点的路径和值
  73 + * @param req
  74 + * @return
  75 + * @throws Exception
  76 + */
  77 + @RequestMapping("/getZkMonitorDetail")
  78 + @ResponseBody
  79 + public BaseResponse<PageResponse<ZkConfigAll>> getZkMonitorDetail(@RequestBody ZkTreeAllReq req) throws Exception {
  80 + log.debug("getZkMonitorTree with req is {}",req);
  81 + PageResponse<ZkConfigAll> list = zkMoitorService.getZkMonitorDetail(req);
  82 + if(list==null){
  83 + return new BaseResponse<PageResponse<ZkConfigAll>>();
  84 + }
  85 + PageResponse<ZkConfigAll> response = new PageResponse<ZkConfigAll>();
  86 + response.setCurrentPage(list.getCurrentPage());
  87 + response.setRows(list.getRows());
  88 + response.setPageSize(list.getPageSize());
  89 + response.setTotal(list.getTotal());
  90 + return new BaseResponse<PageResponse<ZkConfigAll>>(response);
  91 + }
  92 +
  93 + /**
  94 + * 修改某个子节点的值
  95 + * @param req
  96 + * @return
  97 + * @throws Exception
  98 + */
  99 + @RequestMapping("/editZkMonitorDetail")
  100 + @ResponseBody
  101 + public BaseResponse<Object> editZkMonitorDetail(@RequestBody ZkTreeAllReq req) throws Exception {
  102 + log.debug("editZkMonitorDetail with req is {}",req);
  103 + int result = zkMoitorService.editZkMonitorDetail(req);
  104 + return new BaseResponse<Object>(result);
  105 +
  106 + }
  107 +
  108 + /**
  109 + * 修改根节点的路径
  110 + * @param req
  111 + * @return
  112 + * @throws Exception
  113 + */
  114 + @RequestMapping("/editZkMonitorRoot")
  115 + @ResponseBody
  116 + public BaseResponse<Object> editZkMonitorRoot(@RequestBody ZkTreeReq req) throws Exception {
  117 + log.debug("editZkMonitorRoot with req is {}",req);
  118 + int result = zkMoitorService.editZkMonitorRoot(req);
  119 + return new BaseResponse<Object>(result);
  120 +
  121 + }
63 122
64 } 123 }
65 124
@@ -3,6 +3,8 @@ package com.monitor.cmdb.service; @@ -3,6 +3,8 @@ package com.monitor.cmdb.service;
3 import java.util.Map; 3 import java.util.Map;
4 4
5 import com.model.ZkConfig; 5 import com.model.ZkConfig;
  6 +import com.model.ZkConfigAll;
  7 +import com.monitor.model.request.ZkTreeAllReq;
6 import com.monitor.model.request.ZkTreeReq; 8 import com.monitor.model.request.ZkTreeReq;
7 import com.monitor.model.response.PageResponse; 9 import com.monitor.model.response.PageResponse;
8 10
@@ -14,4 +16,10 @@ public interface IZkMoitorService { @@ -14,4 +16,10 @@ public interface IZkMoitorService {
14 Map<String,Object> getZkMonitorRecords(); 16 Map<String,Object> getZkMonitorRecords();
15 17
16 PageResponse<ZkConfig> getZkMonitorTree(ZkTreeReq req); 18 PageResponse<ZkConfig> getZkMonitorTree(ZkTreeReq req);
  19 +
  20 + PageResponse<ZkConfigAll> getZkMonitorDetail(ZkTreeAllReq req);
  21 +
  22 + int editZkMonitorDetail(ZkTreeAllReq req);
  23 +
  24 + int editZkMonitorRoot(ZkTreeReq req);
17 } 25 }
@@ -18,9 +18,11 @@ import org.springframework.util.CollectionUtils; @@ -18,9 +18,11 @@ import org.springframework.util.CollectionUtils;
18 18
19 import com.model.MObjectInfo; 19 import com.model.MObjectInfo;
20 import com.model.ZkConfig; 20 import com.model.ZkConfig;
  21 +import com.model.ZkConfigAll;
21 import com.monitor.cmdb.service.IZkMoitorService; 22 import com.monitor.cmdb.service.IZkMoitorService;
22 import com.monitor.influxdb.mapper.IZkMapper; 23 import com.monitor.influxdb.mapper.IZkMapper;
23 import com.monitor.influxdb.model.ZkInfo; 24 import com.monitor.influxdb.model.ZkInfo;
  25 +import com.monitor.model.request.ZkTreeAllReq;
24 import com.monitor.model.request.ZkTreeReq; 26 import com.monitor.model.request.ZkTreeReq;
25 import com.monitor.model.response.PageResponse; 27 import com.monitor.model.response.PageResponse;
26 import com.monitor.mysql.mapper.MObjectInfoMapper; 28 import com.monitor.mysql.mapper.MObjectInfoMapper;
@@ -83,7 +85,7 @@ public class ZkMoitorServiceImpl implements IZkMoitorService { @@ -83,7 +85,7 @@ public class ZkMoitorServiceImpl implements IZkMoitorService {
83 String rootString = ""; 85 String rootString = "";
84 int page=0; 86 int page=0;
85 RetryPolicy retryPolicy = new RetryOneTime(1000); 87 RetryPolicy retryPolicy = new RetryOneTime(1000);
86 - CuratorFramework client = CuratorFrameworkFactory.newClient("192.168.102.205"+ ":2181", 5 * 1000, 5 * 1000, retryPolicy); 88 + CuratorFramework client = CuratorFrameworkFactory.newClient(req.getIp()+ ":2181", 5 * 1000, 5 * 1000, retryPolicy);
87 client.start(); 89 client.start();
88 try { 90 try {
89 List<String> children = client.getChildren().forPath(root); 91 List<String> children = client.getChildren().forPath(root);
@@ -93,6 +95,7 @@ public class ZkMoitorServiceImpl implements IZkMoitorService { @@ -93,6 +95,7 @@ public class ZkMoitorServiceImpl implements IZkMoitorService {
93 rootString = root+"/"+chi; 95 rootString = root+"/"+chi;
94 zkConfig.setName(chi); 96 zkConfig.setName(chi);
95 zkConfig.setRoot(rootString); 97 zkConfig.setRoot(rootString);
  98 + zkConfig.setIp(req.getIp());
96 list.add(zkConfig); 99 list.add(zkConfig);
97 page++; 100 page++;
98 } 101 }
@@ -103,11 +106,160 @@ public class ZkMoitorServiceImpl implements IZkMoitorService { @@ -103,11 +106,160 @@ public class ZkMoitorServiceImpl implements IZkMoitorService {
103 response.setRows(list); 106 response.setRows(list);
104 107
105 } catch (Exception e) { 108 } catch (Exception e) {
  109 + logger.error("getZkMonitorTree fail with ip is {}",req.getIp());
106 e.printStackTrace(); 110 e.printStackTrace();
107 } finally { 111 } finally {
108 client.close(); 112 client.close();
109 } 113 }
110 return response; 114 return response;
111 } 115 }
  116 +
  117 + @Override
  118 + public PageResponse<ZkConfigAll> getZkMonitorDetail(ZkTreeAllReq req) {
  119 +
  120 + PageResponse<ZkConfigAll> response = new PageResponse<ZkConfigAll>();
  121 + List<ZkConfigAll> list = new ArrayList<ZkConfigAll>();
  122 + int page=0;
  123 + RetryPolicy retryPolicy = new RetryOneTime(1000);
  124 + CuratorFramework client = CuratorFrameworkFactory.newClient(req.getIp()+ ":2181", 5 * 1000, 5 * 1000, retryPolicy);
  125 + client.start();
  126 + try {
  127 + list=getAllChildren(req.getZkPath(),client,req.getIp());
  128 + for(ZkConfigAll zk:list){
  129 + zk.getCurrentPage();
  130 + page++;
  131 + }
  132 + if (list.size()>10&&req.getCurrentPage()*10<list.size()) {
  133 + list=list.subList(req.getCurrentPage()*10-10, req.getCurrentPage()*10);
  134 + }else if (req.getCurrentPage()*10>list.size()) {
  135 + list=list.subList(req.getCurrentPage()*10-10, list.size());
  136 + }
  137 + response.setCurrentPage(req.getCurrentPage());
  138 + response.setPageSize(req.getPageSize());
  139 + response.setTotal(page);
  140 + response.setRows(list);
  141 + } catch (Exception e) {
  142 + logger.error("getZkMonitorDetail fail with ip is {} and path is {}",req.getIp(),req.getZkPath());
  143 + e.printStackTrace();
  144 + }finally {
  145 + client.close();
  146 + }
  147 + return response;
  148 +
  149 + }
  150 +
  151 +
  152 + public List<ZkConfigAll> getAllChildren(String parentPath,CuratorFramework client,String ip) {
  153 + List<ZkConfigAll> childList=new ArrayList<ZkConfigAll>();
  154 + try {
  155 + //取该路径下的所有子节点
  156 + List<String> childNodeNames=client.getChildren().forPath(parentPath);
  157 + for(String nodeName: childNodeNames){
  158 + //拼接路径
  159 + String nodePath=parentPath.concat("/").concat(nodeName);
  160 +
  161 + ZkConfigAll zkNode=new ZkConfigAll();
  162 + zkNode.setZkPath(nodePath);
  163 + zkNode.setIp(ip);
  164 + String data =new String(client.getData().forPath(nodePath),"UTF-8");
  165 + zkNode.setZkValue(data);
  166 + List<String> liString = client.getChildren().forPath(nodePath);
  167 + if(CollectionUtils.isEmpty(liString)){
  168 + childList.add(zkNode);
  169 + }else {
  170 + //递归
  171 + childList.addAll(getAllChildren(nodePath, client,ip));
  172 + }
  173 + }
  174 +
  175 + } catch (Exception e) {
  176 + e.printStackTrace();
  177 + }
  178 + return childList;
  179 + }
  180 +
  181 + @Override
  182 + public int editZkMonitorDetail(ZkTreeAllReq req) {
  183 + int result = 0;
  184 + RetryPolicy retryPolicy = new RetryOneTime(1000);
  185 + CuratorFramework client = CuratorFrameworkFactory.newClient(req.getIp()+ ":2181", 5 * 1000, 5 * 1000, retryPolicy);
  186 + client.start();
  187 + try {
  188 + byte[] data = req.getZkValue().getBytes();
  189 + client.setData().forPath(req.getZkPath(), data);
  190 +
  191 + String newData = new String(client.getData().forPath(req.getZkPath()));
  192 + if(newData.equals(req.getZkValue())){
  193 + result=1;
  194 + }
  195 +
  196 + } catch (Exception e) {
  197 + logger.error("editZkMonitorDetail fail with data is {}",req.getZkValue());
  198 + e.printStackTrace();
  199 + }finally {
  200 + client.close();
  201 + }
  202 + return result;
  203 + }
  204 +
  205 + @Override
  206 + public int editZkMonitorRoot(ZkTreeReq req) {
  207 + int result = 0;
  208 + RetryPolicy retryPolicy = new RetryOneTime(1000);
  209 + CuratorFramework client = CuratorFrameworkFactory.newClient(req.getIp()+ ":2181", 5 * 1000, 5 * 1000, retryPolicy);
  210 + client.start();
  211 + try {
  212 + List<ZkConfigAll> childList=getAllChildren(req,client);
  213 + //将原来节点的值重新创建到新的节点上
  214 + client.create().creatingParentContainersIfNeeded().forPath(req.getRoot(),client.getData().forPath(req.getOldRoot()));
  215 +
  216 + for(ZkConfigAll zkNode:childList)
  217 + {
  218 +
  219 + String path=zkNode.getZkPath().replaceFirst(req.getOldRoot(), req.getRoot());
  220 +
  221 + client.create().forPath(path,zkNode.getZkValue().getBytes());
  222 +
  223 + }
  224 +
  225 + client.delete().deletingChildrenIfNeeded().forPath(req.getOldRoot());
  226 +
  227 + result =1;
  228 +
  229 + } catch (Exception e) {
  230 + logger.error("editZkMonitorRoot fail with oldRoot is {}",req.getOldRoot());
  231 + } finally {
  232 + client.close();
  233 + }
  234 + return result;
  235 + }
  236 +
  237 +
  238 + public List<ZkConfigAll> getAllChildren(ZkTreeReq req,CuratorFramework client) {
  239 + List<ZkConfigAll> childList=new ArrayList<ZkConfigAll>();
  240 +
  241 + try {
  242 + //取该路径下的所有子节点
  243 + List<String> childNodeNames=client.getChildren().forPath(req.getOldRoot());
  244 +
  245 + for(String nodeName: childNodeNames)
  246 + {
  247 + //拼接路径
  248 + String nodePath=req.getOldRoot().concat("/").concat(nodeName);
  249 +
  250 + ZkConfigAll zkNode=new ZkConfigAll();
  251 + zkNode.setZkPath(nodePath);
  252 + String data =new String(client.getData().forPath(nodePath),"UTF-8");
  253 + zkNode.setZkValue(data);
  254 + childList.add(zkNode);
  255 + //递归
  256 + childList.addAll(getAllChildren(req,client));
  257 + }
  258 + } catch (Exception e) {
  259 + logger.error("getAllChildren fail");
  260 + e.printStackTrace();
  261 + }
  262 + return childList;
  263 + }
112 264
113 } 265 }
@@ -4,6 +4,8 @@ import com.alibaba.fastjson.JSONObject; @@ -4,6 +4,8 @@ import com.alibaba.fastjson.JSONObject;
4 import com.model.MObjectInfo; 4 import com.model.MObjectInfo;
5 import com.model.RedisMonitor; 5 import com.model.RedisMonitor;
6 import com.model.TypeInfo; 6 import com.model.TypeInfo;
  7 +import com.monitor.common.config.SnsMobileConfig;
  8 +import com.monitor.common.service.AlarmMsgService;
7 import com.monitor.common.util.HttpRestClient; 9 import com.monitor.common.util.HttpRestClient;
8 import com.monitor.common.util.RedisCommonUtil; 10 import com.monitor.common.util.RedisCommonUtil;
9 import com.monitor.common.util.TelnetUtils; 11 import com.monitor.common.util.TelnetUtils;
@@ -43,13 +45,19 @@ public class RedisMonitorHandleServiceImpl implements IRedisMonitorHandleService @@ -43,13 +45,19 @@ public class RedisMonitorHandleServiceImpl implements IRedisMonitorHandleService
43 45
44 @Autowired 46 @Autowired
45 RedisMonitorMapper redisMonitorMapper; 47 RedisMonitorMapper redisMonitorMapper;
  48 +
  49 + @Autowired
  50 + public AlarmMsgService alarmMsgService;
  51 +
  52 + @Autowired
  53 + private SnsMobileConfig snsMobileConfig;
46 54
47 @Override 55 @Override
48 public void redisMonitor() { 56 public void redisMonitor() {
49 List<RedisMonitor> redisInfoList=new ArrayList<RedisMonitor>(); 57 List<RedisMonitor> redisInfoList=new ArrayList<RedisMonitor>();
50 - int twemproxyAlarmCount=0;  
51 - int redisAlarmCount=0;  
52 - int slaveAramCount=0; 58 + List<String> twemproxyAlarmList=new ArrayList<String>();
  59 + List<String> redisAlarmList=new ArrayList<String>();
  60 + List<String> slaveAlarmList=new ArrayList<String>();
53 61
54 TypeInfo typeInfo=mTypeInfoMapper.selectTypeInfoByName("redis"); 62 TypeInfo typeInfo=mTypeInfoMapper.selectTypeInfoByName("redis");
55 if(typeInfo==null){ 63 if(typeInfo==null){
@@ -101,7 +109,6 @@ public class RedisMonitorHandleServiceImpl implements IRedisMonitorHandleService @@ -101,7 +109,6 @@ public class RedisMonitorHandleServiceImpl implements IRedisMonitorHandleService
101 String result= TelnetUtils.getResult(obj.getMoHostIp(),Integer.valueOf(ports[0])); 109 String result= TelnetUtils.getResult(obj.getMoHostIp(),Integer.valueOf(ports[0]));
102 redisMonitor = new RedisMonitor(); 110 redisMonitor = new RedisMonitor();
103 if(StringUtils.isNotBlank(result)){ 111 if(StringUtils.isNotBlank(result)){
104 - paramMonitor.append("1,");  
105 boolean proxyFlag=RedisCommonUtil.getRedisIsSlave(obj.getMoHostIp(),Integer.valueOf(ports[1])); 112 boolean proxyFlag=RedisCommonUtil.getRedisIsSlave(obj.getMoHostIp(),Integer.valueOf(ports[1]));
106 if(proxyFlag){ 113 if(proxyFlag){
107 paramMonitor.append("探测成功;"); 114 paramMonitor.append("探测成功;");
@@ -134,9 +141,9 @@ public class RedisMonitorHandleServiceImpl implements IRedisMonitorHandleService @@ -134,9 +141,9 @@ public class RedisMonitorHandleServiceImpl implements IRedisMonitorHandleService
134 redisMonitor.setParamMonitor(paramMonitor.toString()); 141 redisMonitor.setParamMonitor(paramMonitor.toString());
135 } 142 }
136 }else{ 143 }else{
137 - twemproxyAlarmCount++; 144 + twemproxyAlarmList.add("失败:"+obj.getMoHostIp()+":"+ports[0]);
138 redisMonitor.setIsFailed(0); 145 redisMonitor.setIsFailed(0);
139 - redisMonitor.setParamMonitor("0,"); 146 + redisMonitor.setParamMonitor(obj.getMoHostIp()+":"+obj.getMoTags());
140 } 147 }
141 redisMonitor.setNodeFrom(redisTweproxyMap.get(obj.getMoTypeId())); 148 redisMonitor.setNodeFrom(redisTweproxyMap.get(obj.getMoTypeId()));
142 redisMonitor.setNodeTo(obj.getMoHostIp()+":"+ports[1]); 149 redisMonitor.setNodeTo(obj.getMoHostIp()+":"+ports[1]);
@@ -166,7 +173,7 @@ public class RedisMonitorHandleServiceImpl implements IRedisMonitorHandleService @@ -166,7 +173,7 @@ public class RedisMonitorHandleServiceImpl implements IRedisMonitorHandleService
166 result= RedisCommonUtil.getRedisInfo(ipConfig[0], Integer.valueOf(ipConfig[1])); 173 result= RedisCommonUtil.getRedisInfo(ipConfig[0], Integer.valueOf(ipConfig[1]));
167 paramMonitor=new StringBuffer(); 174 paramMonitor=new StringBuffer();
168 if(null==result){ 175 if(null==result){
169 - redisAlarmCount++; 176 + redisAlarmList.add(ipConfig[0]+":"+ipConfig[1]+",");
170 paramMonitor.append("0,"); 177 paramMonitor.append("0,");
171 }else{ 178 }else{
172 isFailed=1; 179 isFailed=1;
@@ -200,6 +207,7 @@ public class RedisMonitorHandleServiceImpl implements IRedisMonitorHandleService @@ -200,6 +207,7 @@ public class RedisMonitorHandleServiceImpl implements IRedisMonitorHandleService
200 if(flag){ 207 if(flag){
201 msg="OK"; 208 msg="OK";
202 }else{ 209 }else{
  210 + slaveAlarmList.add("主"+result.get("master_host").toString()+":"+result.get("master_port").toString()+",从"+ ipConfig[0]+":"+ipConfig[1]+",");
203 msg="ERROR"; 211 msg="ERROR";
204 } 212 }
205 redisInfoList.add(new RedisMonitor(ipConfig[0]+":"+ipConfig[1],result.get("master_host").toString()+":"+result.get("master_port").toString(),2,isFailed,1,Integer.valueOf(ipConfig[2]),msg)); 213 redisInfoList.add(new RedisMonitor(ipConfig[0]+":"+ipConfig[1],result.get("master_host").toString()+":"+result.get("master_port").toString(),2,isFailed,1,Integer.valueOf(ipConfig[2]),msg));
@@ -219,5 +227,39 @@ public class RedisMonitorHandleServiceImpl implements IRedisMonitorHandleService @@ -219,5 +227,39 @@ public class RedisMonitorHandleServiceImpl implements IRedisMonitorHandleService
219 redisMonitorMapper.deleteAllRedisMonitor(); 227 redisMonitorMapper.deleteAllRedisMonitor();
220 redisMonitorMapper.bachInsertRedisMonitor(redisInfoList); 228 redisMonitorMapper.bachInsertRedisMonitor(redisInfoList);
221 } 229 }
  230 +
  231 + /**********************************************************************
  232 + *4、告警
  233 + ***********************************************************************/
  234 + StringBuffer alrarmMsg=new StringBuffer("");
  235 + if(!CollectionUtils.isEmpty(twemproxyAlarmList)){
  236 + alrarmMsg.append("twemproxy:");
  237 + for(String str:twemproxyAlarmList){
  238 + alrarmMsg.append(str);
  239 + }
  240 + alrarmMsg.append("失败;");
  241 + }
  242 +
  243 + if(!CollectionUtils.isEmpty(redisAlarmList)){
  244 + alrarmMsg.append("redis:");
  245 + for(String str:redisAlarmList){
  246 + alrarmMsg.append(str);
  247 + }
  248 + alrarmMsg.append("失败;");
  249 + }
  250 +
  251 + if(!CollectionUtils.isEmpty(slaveAlarmList)){
  252 + alrarmMsg.append("主从:");
  253 + for(String str:slaveAlarmList){
  254 + alrarmMsg.append(str);
  255 + }
  256 + alrarmMsg.append("失败;");
  257 + }
  258 +
  259 + log.info("sms redis start info {}",alrarmMsg.toString());
  260 + if(StringUtils.isNotBlank(alrarmMsg.toString())) {
  261 + alrarmMsg.append("请您及时查看!");
  262 + alarmMsgService.sendSms("redis", alrarmMsg.toString(), snsMobileConfig.getBaseMobile());
  263 + }
222 } 264 }
223 } 265 }
  1 +package com.monitor.model.request;
  2 +
  3 +import lombok.Data;
  4 +
  5 +import com.monitor.model.page.PageRequest;
  6 +
  7 +@Data
  8 +public class ZkTreeAllReq extends PageRequest{
  9 +
  10 + private String ip;
  11 +
  12 + private String zkPath;
  13 +
  14 + private String zkValue;
  15 +}
@@ -12,4 +12,6 @@ public class ZkTreeReq extends PageRequest{ @@ -12,4 +12,6 @@ public class ZkTreeReq extends PageRequest{
12 private String name; 12 private String name;
13 13
14 private String root; 14 private String root;
  15 +
  16 + private String oldRoot;
15 } 17 }
  1 +package com.model;
  2 +
  3 +import lombok.Data;
  4 +
  5 +/**
  6 + * Created by zhengyouwei on 2016/7/14.
  7 + */
  8 +@Data
  9 +public class AuthModule {
  10 +
  11 + private int id;
  12 +
  13 + private String moduleName;
  14 +
  15 + private int moduleLevel;
  16 +
  17 +}
  1 +package com.model;
  2 +
  3 +import lombok.Data;
  4 +
  5 +/**
  6 + * Created by zhengyouwei on 2016/7/13.
  7 + */
  8 +@Data
  9 +public class User {
  10 +
  11 + private int id;
  12 +
  13 + private String name;
  14 +
  15 + private String pwd;
  16 +
  17 + private int level;
  18 +
  19 +}
@@ -7,7 +7,8 @@ import com.monitor.model.page.PageRequest; @@ -7,7 +7,8 @@ import com.monitor.model.page.PageRequest;
7 import lombok.Data; 7 import lombok.Data;
8 @Data 8 @Data
9 public class ZkConfig extends PageRequest implements Serializable{ 9 public class ZkConfig extends PageRequest implements Serializable{
10 - 10 +
  11 + private String ip;
11 12
12 private String name; 13 private String name;
13 14
  1 +package com.model;
  2 +
  3 +import java.io.Serializable;
  4 +
  5 +import lombok.Data;
  6 +
  7 +import com.monitor.model.page.PageRequest;
  8 +@Data
  9 +public class ZkConfigAll extends PageRequest implements Serializable {
  10 +
  11 + private String ip;
  12 +
  13 + private String zkValue;
  14 +
  15 + private String zkPath;
  16 +
  17 +}
  1 +package com.monitor.mysql.mapper;
  2 +
  3 +import com.model.AuthModule;
  4 +import com.model.User;
  5 +import org.apache.ibatis.annotations.Param;
  6 +
  7 +import java.util.List;
  8 +
  9 +/**
  10 + * Created by zhengyouwei on 2016/7/13.
  11 + */
  12 +public interface AuthModuleMapper {
  13 +
  14 + int insert(AuthModule authModule);
  15 +
  16 + int updateLevel(AuthModule authModule);
  17 +
  18 + List<AuthModule> selectAll();
  19 +
  20 + AuthModule selectByName(@Param("moduleName") String moduleName);
  21 +
  22 + AuthModule selectById(@Param("id") int id);
  23 +
  24 +
  25 +}
  1 +package com.monitor.mysql.mapper;
  2 +
  3 +import com.model.User;
  4 +import org.apache.ibatis.annotations.Param;
  5 +
  6 +import java.util.List;
  7 +
  8 +/**
  9 + * Created by zhengyouwei on 2016/7/13.
  10 + */
  11 +public interface UserMapper {
  12 +
  13 + int insert(User user);
  14 +
  15 + int updatePwd(User user);
  16 +
  17 + int updateLevel(User user);
  18 +
  19 + int deleteByName(@Param("name") String name);
  20 +
  21 + int deleteById(@Param("id") int id);
  22 +
  23 + List<User> selectAll();
  24 +
  25 + User selectByName(@Param("name") String name);
  26 +
  27 + User selectById(@Param("id") int id);
  28 +
  29 +
  30 +}
  1 +<?xml version="1.0" encoding="UTF-8" ?>
  2 +<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
  3 +<mapper namespace="com.monitor.mysql.mapper.AuthModuleMapper" >
  4 + <resultMap id="BaseResultMap" type="com.model.AuthModule" >
  5 + <id column="id" property="id" jdbcType="INTEGER" />
  6 + <result column="module_name" property="moduleName" jdbcType="VARCHAR" />
  7 + <result column="module_level" property="moduleLevel" jdbcType="INTEGER" />
  8 + </resultMap>
  9 + <sql id="Base_Column_List" >
  10 + id, module_name, module_level
  11 + </sql>
  12 +
  13 + <select id="selectById" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
  14 + select
  15 + <include refid="Base_Column_List" />
  16 + from auth_module
  17 + where id = #{id,jdbcType=INTEGER}
  18 + </select>
  19 +
  20 + <select id="selectByName" resultMap="BaseResultMap" parameterType="java.lang.String" >
  21 + select
  22 + <include refid="Base_Column_List" />
  23 + from auth_module
  24 + where module_name = #{moduleName,jdbcType=VARCHAR}
  25 + </select>
  26 +
  27 + <select id="selectAll" resultMap="BaseResultMap">
  28 + select
  29 + <include refid="Base_Column_List" />
  30 + from auth_module
  31 + </select>
  32 +
  33 + <insert id="insert" parameterType="com.model.AuthModule" >
  34 + insert into auth_module
  35 + (module_name,module_level, create_time, update_time)
  36 + values
  37 + (#{moduleName,jdbcType=VARCHAR},#{moduleLevel,jdbcType=INTEGER}, now(),now())
  38 + </insert>
  39 +
  40 + <update id="updateLevel" parameterType="com.model.AuthModule" >
  41 + update auth_module
  42 + set module_level = #{moduleLevel,jdbcType=VARCHAR},
  43 + update_time = now()
  44 + where id = #{id,jdbcType=INTEGER}
  45 + </update>
  46 +
  47 +</mapper>
  1 +<?xml version="1.0" encoding="UTF-8" ?>
  2 +<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
  3 +<mapper namespace="com.monitor.mysql.mapper.UserMapper" >
  4 + <resultMap id="BaseResultMap" type="com.model.User" >
  5 + <id column="id" property="id" jdbcType="INTEGER" />
  6 + <result column="name" property="name" jdbcType="VARCHAR" />
  7 + <result column="pwd" property="pwd" jdbcType="VARCHAR" />
  8 + <result column="level" property="level" jdbcType="INTEGER" />
  9 + </resultMap>
  10 +
  11 + <sql id="Base_Column_List" >
  12 + id, name, pwd, level
  13 + </sql>
  14 +
  15 + <select id="selectById" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
  16 + select
  17 + <include refid="Base_Column_List" />
  18 + from user
  19 + where id = #{id,jdbcType=INTEGER}
  20 + </select>
  21 +
  22 + <select id="selectByName" resultMap="BaseResultMap" parameterType="java.lang.String" >
  23 + select
  24 + <include refid="Base_Column_List" />
  25 + from user
  26 + where name = #{name,jdbcType=VARCHAR}
  27 + </select>
  28 +
  29 + <select id="selectAll" resultMap="BaseResultMap">
  30 + select
  31 + <include refid="Base_Column_List" />
  32 + from user
  33 + </select>
  34 +
  35 + <delete id="deleteById" parameterType="java.lang.Integer" >
  36 + delete from user
  37 + where id = #{id,jdbcType=INTEGER}
  38 + </delete>
  39 +
  40 + <delete id="deleteByName" parameterType="java.lang.String" >
  41 + delete from user
  42 + where name = #{name,jdbcType=VARCHAR}
  43 + </delete>
  44 +
  45 + <insert id="insert" parameterType="com.model.User" >
  46 + insert into user
  47 + (name,pwd,level, create_time, update_time)
  48 + values
  49 + (#{name,jdbcType=VARCHAR},#{pwd,jdbcType=VARCHAR},#{level,jdbcType=INTEGER}, now(),now())
  50 + </insert>
  51 +
  52 + <update id="updatePwd" parameterType="com.model.User" >
  53 + update user
  54 + set pwd = #{pwd,jdbcType=VARCHAR},
  55 + update_time = now()
  56 + where id = #{id,jdbcType=INTEGER}
  57 + </update>
  58 +
  59 + <update id="updateLevel" parameterType="com.model.User" >
  60 + update user
  61 + set level = #{level,jdbcType=INTEGER},
  62 + update_time = now()
  63 + where id = #{id,jdbcType=INTEGER}
  64 + </update>
  65 +
  66 +</mapper>
  1 +<?xml version="1.0" encoding="UTF-8"?>
  2 +<project xmlns="http://maven.apache.org/POM/4.0.0"
  3 + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4 + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5 + <parent>
  6 + <artifactId>monitor-service-parent</artifactId>
  7 + <groupId>monitor-service</groupId>
  8 + <version>1.0-SNAPSHOT</version>
  9 + </parent>
  10 + <modelVersion>4.0.0</modelVersion>
  11 +
  12 + <artifactId>monitor-service-user</artifactId>
  13 + <version>1.0-SNAPSHOT</version>
  14 +
  15 + <dependencies>
  16 +
  17 + <!--项目内部依赖-->
  18 + <dependency>
  19 + <groupId>monitor-service</groupId>
  20 + <artifactId>monitor-service-common</artifactId>
  21 + </dependency>
  22 + <dependency>
  23 + <groupId>monitor-service</groupId>
  24 + <artifactId>monitor-service-mysql</artifactId>
  25 + </dependency>
  26 + <dependency>
  27 + <groupId>monitor-service</groupId>
  28 + <artifactId>monitor-service-influxdb</artifactId>
  29 + </dependency>
  30 + <dependency>
  31 + <groupId>monitor-service</groupId>
  32 + <artifactId>monitor-service-model</artifactId>
  33 + </dependency>
  34 +</dependencies>
  35 +</project>
  1 +package com.monitor.user.ctrl;
  2 +
  3 +import com.model.AuthModule;
  4 +import com.model.User;
  5 +import com.monitor.model.response.BaseResponse;
  6 +import com.monitor.mysql.mapper.AuthModuleMapper;
  7 +import org.slf4j.Logger;
  8 +import org.slf4j.LoggerFactory;
  9 +import org.springframework.beans.factory.annotation.Autowired;
  10 +import org.springframework.stereotype.Controller;
  11 +import org.springframework.util.CollectionUtils;
  12 +import org.springframework.web.bind.annotation.RequestBody;
  13 +import org.springframework.web.bind.annotation.RequestMapping;
  14 +import org.springframework.web.bind.annotation.ResponseBody;
  15 +
  16 +import java.util.List;
  17 +
  18 +@Controller
  19 +@RequestMapping("module")
  20 +public class ModuleCtrl {
  21 +
  22 + Logger log = LoggerFactory.getLogger(ModuleCtrl.class);
  23 +
  24 + @Autowired
  25 + AuthModuleMapper authModuleMapper;
  26 +
  27 + @RequestMapping("/getAll")
  28 + @ResponseBody
  29 + public BaseResponse getAll() {
  30 + try{
  31 + List<AuthModule> list=authModuleMapper.selectAll();
  32 +
  33 + if (list == null || CollectionUtils.isEmpty(list)) {
  34 + return new BaseResponse<List<User>>();
  35 + }
  36 +
  37 + return new BaseResponse<List<AuthModule>>(list);
  38 + }catch (Exception e){
  39 + log.error("getAll error",e);
  40 + return null;
  41 + }
  42 + }
  43 +
  44 + @RequestMapping("/getAuthModuleByName")
  45 + @ResponseBody
  46 + public BaseResponse getAuthModuleByName(String name) {
  47 + try{
  48 + AuthModule authModule=authModuleMapper.selectByName(name);
  49 + return new BaseResponse<AuthModule>(authModule);
  50 + }catch (Exception e){
  51 + log.error("getAuthModuleByName error",e);
  52 + return null;
  53 + }
  54 + }
  55 +
  56 + @RequestMapping("/getAuthModuleById")
  57 + @ResponseBody
  58 + public BaseResponse getAuthModuleById(int id) {
  59 + try{
  60 + AuthModule authModule=authModuleMapper.selectById(id);
  61 + return new BaseResponse<AuthModule>(authModule);
  62 + }catch (Exception e){
  63 + log.error("getAuthModuleById error",e);
  64 + return null;
  65 + }
  66 + }
  67 +
  68 + @RequestMapping("/insert")
  69 + @ResponseBody
  70 + public BaseResponse insert(@RequestBody AuthModule authModule) {
  71 + try{
  72 + int result = authModuleMapper.insert(authModule);
  73 + return new BaseResponse<Integer>(result);
  74 + }catch (Exception e){
  75 + log.error("getUserById error",e);
  76 + return null;
  77 + }
  78 + }
  79 +
  80 + @RequestMapping("/updateLevel")
  81 + @ResponseBody
  82 + public BaseResponse updateLevel(@RequestBody AuthModule authModule) {
  83 + try{
  84 + int result = authModuleMapper.updateLevel( authModule);
  85 + return new BaseResponse<Integer>(result);
  86 + }catch (Exception e){
  87 + log.error("updateLevel error",e);
  88 + return null;
  89 + }
  90 + }
  91 +
  92 +}
  93 +
  1 +package com.monitor.user.ctrl;
  2 +
  3 +import com.model.User;
  4 +import com.monitor.model.response.BaseResponse;
  5 +import com.monitor.mysql.mapper.UserMapper;
  6 +import org.slf4j.Logger;
  7 +import org.slf4j.LoggerFactory;
  8 +import org.springframework.beans.factory.annotation.Autowired;
  9 +import org.springframework.stereotype.Controller;
  10 +import org.springframework.util.CollectionUtils;
  11 +import org.springframework.web.bind.annotation.RequestBody;
  12 +import org.springframework.web.bind.annotation.RequestMapping;
  13 +import org.springframework.web.bind.annotation.ResponseBody;
  14 +
  15 +import java.util.List;
  16 +
  17 +@Controller
  18 +@RequestMapping("user")
  19 +public class UserCtrl {
  20 +
  21 + Logger log = LoggerFactory.getLogger(UserCtrl.class);
  22 +
  23 + @Autowired
  24 + UserMapper userMapper;
  25 +
  26 + @RequestMapping("/getAllUser")
  27 + @ResponseBody
  28 + public BaseResponse getAllUser() {
  29 + try{
  30 + List<User> list=userMapper.selectAll();
  31 +
  32 + if (list == null || CollectionUtils.isEmpty(list)) {
  33 + return new BaseResponse<List<User>>();
  34 + }
  35 +
  36 + return new BaseResponse<List<User>>(list);
  37 + }catch (Exception e){
  38 + log.error("getAllUser error",e);
  39 + return null;
  40 + }
  41 + }
  42 +
  43 + @RequestMapping("/getUserByName")
  44 + @ResponseBody
  45 + public BaseResponse getUserByName(String name) {
  46 + try{
  47 + User user=userMapper.selectByName(name);
  48 + return new BaseResponse<User>(user);
  49 + }catch (Exception e){
  50 + log.error("getUserByName error",e);
  51 + return null;
  52 + }
  53 + }
  54 +
  55 + @RequestMapping("/getUserById")
  56 + @ResponseBody
  57 + public BaseResponse getUserById(int id) {
  58 + try{
  59 + User user=userMapper.selectById(id);
  60 + return new BaseResponse<User>(user);
  61 + }catch (Exception e){
  62 + log.error("getUserById error",e);
  63 + return null;
  64 + }
  65 + }
  66 +
  67 + @RequestMapping("/insert")
  68 + @ResponseBody
  69 + public BaseResponse insert(@RequestBody User user) {
  70 + try{
  71 + int result = userMapper.insert(user);
  72 + return new BaseResponse<Integer>(result);
  73 + }catch (Exception e){
  74 + log.error("getUserById error",e);
  75 + return null;
  76 + }
  77 + }
  78 +
  79 + @RequestMapping("/updatePwd")
  80 + @ResponseBody
  81 + public BaseResponse updatePwd(@RequestBody User user) {
  82 + try{
  83 + int result = userMapper.updatePwd(user);
  84 + return new BaseResponse<Integer>(result);
  85 + }catch (Exception e){
  86 + log.error("getUserById error",e);
  87 + return null;
  88 + }
  89 + }
  90 +
  91 +
  92 + @RequestMapping("/updateLevel")
  93 + @ResponseBody
  94 + public BaseResponse updateAuth(@RequestBody User user) {
  95 + try{
  96 + int result = userMapper.updateLevel(user);
  97 + return new BaseResponse<Integer>(result);
  98 + }catch (Exception e){
  99 + log.error("getUserById error",e);
  100 + return null;
  101 + }
  102 + }
  103 +
  104 +
  105 + @RequestMapping("/deleteByName")
  106 + @ResponseBody
  107 + public BaseResponse deleteByName(String name) {
  108 + try{
  109 + int result = userMapper.deleteByName(name);
  110 + return new BaseResponse<Integer>(result);
  111 + }catch (Exception e){
  112 + log.error("getUserById error",e);
  113 + return null;
  114 + }
  115 + }
  116 +
  117 + @RequestMapping("/deleteById")
  118 + @ResponseBody
  119 + public BaseResponse deleteById(int id) {
  120 + try{
  121 + int result = userMapper.deleteById(id);
  122 + return new BaseResponse<Integer>(result);
  123 + }catch (Exception e){
  124 + log.error("getUserById error",e);
  125 + return null;
  126 + }
  127 + }
  128 +}
  129 +
@@ -43,6 +43,10 @@ @@ -43,6 +43,10 @@
43 <artifactId>monitor-service-switch</artifactId> 43 <artifactId>monitor-service-switch</artifactId>
44 </dependency> 44 </dependency>
45 <dependency> 45 <dependency>
  46 + <groupId>monitor-service</groupId>
  47 + <artifactId>monitor-service-user</artifactId>
  48 + </dependency>
  49 + <dependency>
46 <groupId>junit</groupId> 50 <groupId>junit</groupId>
47 <artifactId>junit</artifactId> 51 <artifactId>junit</artifactId>
48 <version>4.11</version> 52 <version>4.11</version>
@@ -110,6 +110,11 @@ @@ -110,6 +110,11 @@
110 <version>${project-version}</version> 110 <version>${project-version}</version>
111 </dependency> 111 </dependency>
112 <dependency> 112 <dependency>
  113 + <groupId>monitor-service</groupId>
  114 + <artifactId>monitor-service-user</artifactId>
  115 + <version>${project-version}</version>
  116 + </dependency>
  117 + <dependency>
113 <groupId>org.projectlombok</groupId> 118 <groupId>org.projectlombok</groupId>
114 <artifactId>lombok</artifactId> 119 <artifactId>lombok</artifactId>
115 <version>${lombok-version}</version> 120 <version>${lombok-version}</version>
@@ -137,6 +142,7 @@ @@ -137,6 +142,7 @@
137 <module>monitor-service-cmdb</module> 142 <module>monitor-service-cmdb</module>
138 <module>monitor-service-model</module> 143 <module>monitor-service-model</module>
139 <module>monitor-service-middleware</module> 144 <module>monitor-service-middleware</module>
  145 + <module>monitor-service-user</module>
140 </modules> 146 </modules>
141 147
142 148