|
1 | 1 | package root.core.repository.implement; |
2 | 2 |
|
| 3 | +import java.io.IOException; |
3 | 4 | import java.io.InputStream; |
4 | 5 | import java.time.LocalDate; |
5 | 6 | import java.util.ArrayList; |
|
10 | 11 | import org.apache.commons.io.IOUtils; |
11 | 12 |
|
12 | 13 | import com.jcraft.jsch.Channel; |
| 14 | +import com.jcraft.jsch.JSchException; |
13 | 15 | import com.jcraft.jsch.Session; |
14 | 16 |
|
15 | 17 | import lombok.extern.slf4j.Slf4j; |
@@ -40,33 +42,22 @@ public String getServerName() { |
40 | 42 | public int getAlertLogFileLineCount(AlertLogCommand alc) { |
41 | 43 | int fileLineCnt = 0; |
42 | 44 | try { |
43 | | - Session session = jsch.getSession(); |
44 | | - session.connect(); |
45 | | - Channel channel = jsch.openExecChannel(session, "cat " + alc.getReadFilePath() + " | wc -l"); |
46 | | - InputStream in = jsch.connectChannel(channel); |
47 | | - String result = IOUtils.toString(in, "UTF-8"); |
48 | | - fileLineCnt = Integer.parseInt(result.trim()); |
49 | | - jsch.disConnectChannel(channel); |
50 | | - jsch.disConnect(session); |
| 45 | + String command = "cat " + alc.getReadFilePath() + " | wc -l"; |
| 46 | + String executeResult = executeCommand(command); |
| 47 | + fileLineCnt = Integer.parseInt(executeResult.trim()); |
51 | 48 | } catch (Exception e) { |
52 | 49 | log.error(e.getMessage()); |
53 | 50 | } |
54 | 51 |
|
55 | 52 | return fileLineCnt; |
56 | 53 | } |
57 | | - |
| 54 | + |
58 | 55 | @Override |
59 | 56 | public String checkAlertLog(AlertLogCommand alc) { |
60 | 57 | String result = ""; |
61 | 58 | try { |
62 | | - Session session = jsch.getSession(); |
63 | | - session.connect(); |
64 | 59 | String command = "tail -" + alc.getReadLine() + " " + alc.getReadFilePath(); |
65 | | - Channel channel = jsch.openExecChannel(session, command); |
66 | | - InputStream in = jsch.connectChannel(channel); |
67 | | - result = IOUtils.toString(in, "UTF-8"); |
68 | | - jsch.disConnectChannel(channel); |
69 | | - jsch.disConnect(session); |
| 60 | + result = executeCommand(command); |
70 | 61 | } catch (Exception e) { |
71 | 62 | log.error(e.getMessage()); |
72 | 63 | } |
@@ -261,4 +252,18 @@ private String getAlertLogStringFromCertainDate(AlertLogCommand alc, String star |
261 | 252 |
|
262 | 253 | return fullAlertLogString; |
263 | 254 | } |
| 255 | + |
| 256 | + private String executeCommand(String command) throws JSchException, IOException { |
| 257 | + String result = null; |
| 258 | + |
| 259 | + Session session = jsch.getSession(); |
| 260 | + session.connect(); |
| 261 | + Channel channel = jsch.openExecChannel(session, command); |
| 262 | + InputStream in = jsch.connectChannel(channel); |
| 263 | + result = IOUtils.toString(in, "UTF-8"); |
| 264 | + jsch.disConnectChannel(channel); |
| 265 | + jsch.disConnect(session); |
| 266 | + |
| 267 | + return result; |
| 268 | + } |
264 | 269 | } |
0 commit comments