Merge branch 'master' of http://git.yoho.cn/ops/monitor-service
Showing
9 changed files
with
146 additions
and
7 deletions
@@ -112,3 +112,5 @@ monitor-service-middleware/.settings/org.eclipse.core.resources.prefs | @@ -112,3 +112,5 @@ monitor-service-middleware/.settings/org.eclipse.core.resources.prefs | ||
112 | monitor-service-middleware/.settings/org.eclipse.jdt.core.prefs | 112 | monitor-service-middleware/.settings/org.eclipse.jdt.core.prefs |
113 | monitor-service-middleware/.settings/org.eclipse.m2e.core.prefs | 113 | monitor-service-middleware/.settings/org.eclipse.m2e.core.prefs |
114 | monitor-service-influxdb/src/main/java/com/monitor/influxdb/QueryResultUtil.java | 114 | monitor-service-influxdb/src/main/java/com/monitor/influxdb/QueryResultUtil.java |
115 | +monitor-service-middleware/.settings/org.eclipse.wst.common.component | ||
116 | +monitor-service-middleware/.settings/org.eclipse.wst.common.project.facet.core.xml |
@@ -92,6 +92,12 @@ | @@ -92,6 +92,12 @@ | ||
92 | <version>2.5</version> | 92 | <version>2.5</version> |
93 | <scope>provided</scope> | 93 | <scope>provided</scope> |
94 | </dependency> | 94 | </dependency> |
95 | + | ||
96 | + <dependency> | ||
97 | + <groupId>com.jcraft</groupId> | ||
98 | + <artifactId>jsch</artifactId> | ||
99 | + <version>0.1.53</version> | ||
100 | + </dependency> | ||
95 | </dependencies> | 101 | </dependencies> |
96 | 102 | ||
97 | </project> | 103 | </project> |
1 | +package com.monitor.common.util; | ||
2 | + | ||
3 | +import com.jcraft.jsch.ChannelExec; | ||
4 | +import com.jcraft.jsch.JSch; | ||
5 | +import com.jcraft.jsch.JSchException; | ||
6 | +import com.jcraft.jsch.Session; | ||
7 | + | ||
8 | +import java.io.BufferedReader; | ||
9 | +import java.io.IOException; | ||
10 | +import java.io.InputStream; | ||
11 | +import java.io.InputStreamReader; | ||
12 | + | ||
13 | + | ||
14 | +public class SSHHelper { | ||
15 | + | ||
16 | + /** | ||
17 | + * 远程 执行命令并返回结果调用过程 是同步的(执行完才会返回) | ||
18 | + * @param host 主机名 | ||
19 | + * @param user 用户名 | ||
20 | + * @param psw 密码 | ||
21 | + * @param port 端口 | ||
22 | + * @param command 命令 | ||
23 | + * @return | ||
24 | + */ | ||
25 | + public static String exec(String host,String user,String psw,int port,String command){ | ||
26 | + String result=""; | ||
27 | + Session session =null; | ||
28 | + ChannelExec openChannel =null; | ||
29 | + try { | ||
30 | + JSch jsch=new JSch(); | ||
31 | + session = jsch.getSession(user, host, port); | ||
32 | + java.util.Properties config = new java.util.Properties(); | ||
33 | + config.put("StrictHostKeyChecking", "no"); | ||
34 | + session.setConfig(config); | ||
35 | + session.setPassword(psw); | ||
36 | + session.connect(); | ||
37 | + openChannel = (ChannelExec) session.openChannel("exec"); | ||
38 | + openChannel.setCommand(command); | ||
39 | + int exitStatus = openChannel.getExitStatus(); | ||
40 | + System.out.println(exitStatus); | ||
41 | + openChannel.connect(); | ||
42 | + InputStream in = openChannel.getInputStream(); | ||
43 | + BufferedReader reader = new BufferedReader(new InputStreamReader(in)); | ||
44 | + String buf = null; | ||
45 | + while ((buf = reader.readLine()) != null) { | ||
46 | + result+= new String(buf.getBytes("gbk"),"UTF-8")+" <br>\r\n"; | ||
47 | + } | ||
48 | + } catch (JSchException | IOException e) { | ||
49 | + result+=e.getMessage(); | ||
50 | + }finally{ | ||
51 | + if(openChannel!=null&&!openChannel.isClosed()){ | ||
52 | + openChannel.disconnect(); | ||
53 | + } | ||
54 | + if(session!=null&&session.isConnected()){ | ||
55 | + session.disconnect(); | ||
56 | + } | ||
57 | + } | ||
58 | + return result; | ||
59 | + } | ||
60 | + | ||
61 | + | ||
62 | + | ||
63 | + public static void main(String args[]){ | ||
64 | + 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;"); | ||
65 | + System.out.println(exec); | ||
66 | + System.out.println("********************************"); | ||
67 | + System.out.println("********************************"+exec.indexOf("used_memory:")); | ||
68 | + System.out.println("********************************"+exec.indexOf("used_memory")); | ||
69 | + } | ||
70 | +} |
1 | +package com.monitor.middleware.redis.service.impl; | ||
2 | + | ||
3 | +import java.util.ArrayList; | ||
4 | +import java.util.List; | ||
5 | +import org.slf4j.Logger; | ||
6 | +import org.slf4j.LoggerFactory; | ||
7 | +import org.springframework.beans.factory.annotation.Autowired; | ||
8 | + | ||
9 | +import com.monitor.influxdb.mapper.IRedisMapper; | ||
10 | +import com.monitor.middleware.redis.service.IRedisMonitorService; | ||
11 | + | ||
12 | +public class RedisMonitorServiceImpl implements IRedisMonitorService { | ||
13 | + | ||
14 | + Logger log = LoggerFactory.getLogger(RedisMonitorServiceImpl.class); | ||
15 | + | ||
16 | + @Autowired | ||
17 | + IRedisMapper redisMapper; | ||
18 | + | ||
19 | + @Override | ||
20 | + public void redisMonitor() { | ||
21 | + | ||
22 | + | ||
23 | + List<String> list = new ArrayList<String>(); | ||
24 | + | ||
25 | + | ||
26 | + | ||
27 | + } | ||
28 | + | ||
29 | +} |
monitor-service-middleware/src/main/java/com/monitor/middleware/redis/task/RedisMonitorTask.java
0 → 100644
1 | +package com.monitor.middleware.redis.task; | ||
2 | + | ||
3 | +import org.springframework.beans.factory.annotation.Autowired; | ||
4 | +import org.springframework.stereotype.Component; | ||
5 | + | ||
6 | +import com.monitor.middleware.redis.service.IRedisMonitorService; | ||
7 | + | ||
8 | + | ||
9 | + | ||
10 | +@Component | ||
11 | +public class RedisMonitorTask { | ||
12 | + | ||
13 | + @Autowired | ||
14 | + private IRedisMonitorService redisMonitorService; | ||
15 | + | ||
16 | + public void redisMonitor(){ | ||
17 | + redisMonitorService.redisMonitor(); | ||
18 | + } | ||
19 | + | ||
20 | +} |
-
Please register or login to post a comment