Skip to content

Commit be87940

Browse files
authored
Merge pull request #211 from Dokyeongyun/ft-220410-windowServerMonitoring
Ft 220410 window server monitoring
2 parents 53dab1f + ce2bb7c commit be87940

File tree

9 files changed

+459
-87
lines changed

9 files changed

+459
-87
lines changed

pom.xml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,13 +180,21 @@
180180
<scope>test</scope>
181181
</dependency>
182182

183-
<!-- mockito -->
183+
<!-- mockito-core -->
184184
<dependency>
185185
<groupId>org.mockito</groupId>
186186
<artifactId>mockito-core</artifactId>
187187
<version>4.3.1</version>
188188
<scope>test</scope>
189189
</dependency>
190+
191+
<!-- mockito-inline -->
192+
<dependency>
193+
<groupId>org.mockito</groupId>
194+
<artifactId>mockito-inline</artifactId>
195+
<version>4.3.1</version>
196+
<scope>test</scope>
197+
</dependency>
190198

191199
<!-- logback -->
192200
<dependency>

src/main/java/root/common/server/implement/JschServer.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package root.common.server.implement;
22

3+
import java.io.IOException;
34
import java.io.InputStream;
45
import java.util.Properties;
56

7+
import org.apache.commons.io.IOUtils;
8+
69
import com.jcraft.jsch.Channel;
710
import com.jcraft.jsch.ChannelExec;
811
import com.jcraft.jsch.JSch;
@@ -97,6 +100,10 @@ public Channel openExecChannel(Session session, String command) {
97100
}
98101
return channel;
99102
}
103+
104+
private Channel openExecChannel(String command) {
105+
return openExecChannel(session, command);
106+
}
100107

101108
public InputStream connectChannel(Channel channel) {
102109
InputStream in = null;
@@ -120,6 +127,16 @@ public String getServerName() {
120127
return this.jschConnectionInfo.getServerName();
121128
}
122129

130+
public String executeCommand(String command) throws JSchException, IOException {
131+
log.debug(command);
132+
Channel channel = openExecChannel(command);
133+
InputStream in = connectChannel(channel);
134+
String result = IOUtils.toString(in, "UTF-8");
135+
disConnectChannel(channel);
136+
disConnect(session);
137+
return result.trim();
138+
}
139+
123140
public static boolean validateConn(Session session) {
124141
if (session == null) {
125142
log.error("JSch session is null");

src/main/java/root/core/domain/AlertLogCommand.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,25 @@
66
@NoArgsConstructor
77
@Data
88
public class AlertLogCommand {
9-
private String readLine;
9+
private int readLine;
1010
private String readFilePath;
1111
private String dateFormat;
1212
private String dateFormatRegex;
1313
private String[] catchErrorMsg;
1414

15-
public AlertLogCommand(String readLine, String readFilePath) {
15+
public AlertLogCommand(int readLine, String readFilePath) {
1616
this.readLine = readLine;
1717
this.readFilePath = readFilePath;
1818
}
1919

20-
public AlertLogCommand(String readLine, String readFilePath, String dateFormat, String dateFormatRegex) {
20+
public AlertLogCommand(int readLine, String readFilePath, String dateFormat, String dateFormatRegex) {
2121
this.readLine = readLine;
2222
this.readFilePath = readFilePath;
2323
this.dateFormat = dateFormat;
2424
this.dateFormatRegex = dateFormatRegex;
2525
}
2626

27-
public AlertLogCommand(String readLine, String readFilePath, String... catchErrorMsg) {
27+
public AlertLogCommand(int readLine, String readFilePath, String... catchErrorMsg) {
2828
this.readLine = readLine;
2929
this.readFilePath = readFilePath;
3030
this.catchErrorMsg = catchErrorMsg;

src/main/java/root/core/repository/implement/LinuxServerMonitoringRepository.java

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package root.core.repository.implement;
22

3-
import java.io.IOException;
43
import java.io.InputStream;
54
import java.time.LocalDate;
65
import java.util.ArrayList;
@@ -11,7 +10,6 @@
1110
import org.apache.commons.io.IOUtils;
1211

1312
import com.jcraft.jsch.Channel;
14-
import com.jcraft.jsch.JSchException;
1513
import com.jcraft.jsch.Session;
1614

1715
import lombok.extern.slf4j.Slf4j;
@@ -42,9 +40,9 @@ public String getServerName() {
4240
public int getAlertLogFileLineCount(AlertLogCommand alc) {
4341
int fileLineCnt = 0;
4442
try {
45-
String command = "cat " + alc.getReadFilePath() + " | wc -l";
46-
String executeResult = executeCommand(command);
47-
fileLineCnt = Integer.parseInt(executeResult.trim());
43+
String command = String.format("cat %s | wc -l", alc.getReadFilePath());
44+
String executeResult = jsch.executeCommand(command);
45+
fileLineCnt = Integer.parseInt(executeResult);
4846
} catch (Exception e) {
4947
log.error(e.getMessage());
5048
}
@@ -56,8 +54,8 @@ public int getAlertLogFileLineCount(AlertLogCommand alc) {
5654
public String checkAlertLog(AlertLogCommand alc) {
5755
String result = "";
5856
try {
59-
String command = "tail -" + alc.getReadLine() + " " + alc.getReadFilePath();
60-
result = executeCommand(command);
57+
String command = String.format("tail -%d %s", alc.getReadLine(), alc.getReadFilePath());
58+
result = jsch.executeCommand(command);
6159
} catch (Exception e) {
6260
log.error(e.getMessage());
6361
}
@@ -243,7 +241,7 @@ private String getAlertLogStringFromCertainDate(AlertLogCommand alc, String star
243241
// 조회시작일자와 로그의 처음 기록일자를 비교한다.
244242
long diffTime = DateUtils.getDateDiffTime("yyyy-MM-dd", logDate, startDate);
245243
if (diffTime >= 0) { // 조회 Line 수를 더 늘려서 다시 조회
246-
alc.setReadLine(String.valueOf(Integer.parseInt(alc.getReadLine()) * 2));
244+
alc.setReadLine(alc.getReadLine() * 2);
247245
fullAlertLogString = this.checkAlertLog(alc);
248246
} else {
249247
break;
@@ -252,18 +250,4 @@ private String getAlertLogStringFromCertainDate(AlertLogCommand alc, String star
252250

253251
return fullAlertLogString;
254252
}
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-
}
269253
}

src/main/java/root/core/repository/implement/PropertyRepositoryImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,7 @@ public JschConnectionInfo getJschConnectionInfo(String serverName) {
546546
@Override
547547
public AlertLogCommand getAlertLogCommand(String serverName) {
548548
String alertLogFilePath = connInfoConfig.getString(serverName + ".server.alertlog.filepath");
549-
String alertLogReadLine = connInfoConfig.getString(serverName + ".server.alertlog.readline");
549+
int alertLogReadLine = connInfoConfig.getInt(serverName + ".server.alertlog.readline");
550550
String alertLogDateFormat = connInfoConfig.getString(serverName + ".server.alertlog.dateformat");
551551
String alertLogDateFormatRegex = connInfoConfig.getString(serverName + ".server.alertlog.dateformatregex");
552552
AlertLogCommand alc = new AlertLogCommand(alertLogReadLine, alertLogFilePath, alertLogDateFormat,
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
package root.core.repository.implement;
2+
3+
import java.time.LocalDate;
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
7+
import lombok.extern.slf4j.Slf4j;
8+
import root.common.server.implement.JschServer;
9+
import root.core.domain.AlertLog;
10+
import root.core.domain.AlertLogCommand;
11+
import root.core.domain.Log;
12+
import root.core.domain.OSDiskUsage;
13+
import root.core.repository.constracts.ServerMonitoringRepository;
14+
import root.utils.DateUtils;
15+
16+
@Slf4j
17+
public class WindowServerMonitoringRepository implements ServerMonitoringRepository {
18+
private JschServer jsch;
19+
20+
public WindowServerMonitoringRepository(JschServer jsch) {
21+
this.jsch = jsch;
22+
}
23+
24+
@Override
25+
public String getServerName() {
26+
return jsch.getServerName();
27+
}
28+
29+
@Override
30+
public int getAlertLogFileLineCount(AlertLogCommand alc) {
31+
int fileLineCnt = 0;
32+
33+
try {
34+
String command = String.format("find /v /c \"\" %s", alc.getReadFilePath());
35+
String executeResult = jsch.executeCommand(command);
36+
fileLineCnt = Integer.parseInt(executeResult);
37+
} catch (Exception e) {
38+
log.error(e.getMessage());
39+
}
40+
41+
return fileLineCnt;
42+
}
43+
44+
@Override
45+
public String checkAlertLog(AlertLogCommand alc) {
46+
String result = "";
47+
try {
48+
String command = String.format("tail %d %s", alc.getReadLine(), alc.getReadFilePath());
49+
result = jsch.executeCommand(command);
50+
} catch (Exception e) {
51+
log.error(e.getMessage());
52+
}
53+
54+
return result;
55+
}
56+
57+
@Override
58+
public AlertLog checkAlertLogDuringPeriod(AlertLogCommand alc, String startDate, String endDate) {
59+
AlertLog alertLog = new AlertLog();
60+
61+
String fullAlertLogString = getAlertLogStringFromCertainDate(alc, startDate);
62+
63+
try {
64+
// 조회기간동안의 로그만을 취하여 StringBuffer에 저장한다.
65+
String[] lines = fullAlertLogString.split("\n");
66+
67+
boolean isStartDate = false;
68+
boolean isEndDate = false;
69+
70+
int readStartIndex = 0;
71+
int readEndIndex = lines.length;
72+
73+
String logTimeStamp = "";
74+
List<String> logContents = new ArrayList<>();
75+
StringBuffer sb = new StringBuffer();
76+
77+
for (int i = 0; i < lines.length; i++) {
78+
String line = lines[i];
79+
80+
// 조회시작일자 찾기
81+
if (!isStartDate) {
82+
LocalDate parsedDate = DateUtils.parse(line);
83+
if (parsedDate != null && DateUtils.convertDateFormat("yyyy-MM-dd", parsedDate).equals(startDate)) {
84+
isStartDate = true;
85+
readStartIndex = i;
86+
}
87+
}
88+
89+
// 로그 저장 시작 & 조회종료일자 찾기
90+
if (isStartDate) {
91+
LocalDate parsedDate = DateUtils.parse(line);
92+
if (parsedDate != null) { // Log TimeStamp Line
93+
String logDate = DateUtils.convertDateFormat("yyyy-MM-dd", parsedDate);
94+
if (logDate.startsWith(DateUtils.addDate(endDate, 0, 0, 1))) {
95+
isEndDate = true;
96+
readEndIndex = i;
97+
}
98+
99+
if (i == readStartIndex) {
100+
logTimeStamp = line;
101+
}
102+
103+
if (i != readStartIndex && !isEndDate) {
104+
alertLog.addLog(new Log(logTimeStamp, logContents));
105+
logContents = new ArrayList<>();
106+
logTimeStamp = line;
107+
}
108+
} else { // Log Content Line
109+
logContents.add(line);
110+
}
111+
112+
// 로그 저장 중지
113+
if (!isEndDate) {
114+
sb.append(line);
115+
} else {
116+
break;
117+
}
118+
}
119+
}
120+
121+
// 종료 후 마지막 로그 추가하기
122+
alertLog.addLog(new Log(logTimeStamp, logContents));
123+
alertLog.setFullLogString(sb.toString());
124+
125+
log.info("\t▶ Alert Log READ LINE: " + (readEndIndex - readStartIndex) + "/" + alc.getReadLine());
126+
127+
} catch (Exception e) {
128+
log.error(e.getMessage());
129+
}
130+
131+
return alertLog;
132+
}
133+
134+
@Override
135+
public List<OSDiskUsage> checkOSDiskUsage() {
136+
// TODO Auto-generated method stub
137+
return null;
138+
}
139+
140+
private String getAlertLogStringFromCertainDate(AlertLogCommand alc, String startDate) {
141+
int alertLogFileLineCnt = getAlertLogFileLineCount(alc);
142+
String fullAlertLogString = checkAlertLog(alc);
143+
144+
// 조회시작일자의 로그를 모두 포함하도록 readLine 수를 점진적으로 늘리면서 읽는다.
145+
while (true) {
146+
String[] lines = fullAlertLogString.split("\n");
147+
148+
// 현재 Read Line 수가 파일 최대 Line 수를 초과했을 시, 파일 전체를 읽고 반환한다.
149+
if (lines.length >= alertLogFileLineCnt) {
150+
break;
151+
}
152+
153+
// 조회한 로그 내에서 가장 처음으로 나타나는 로그의 기록일자를 얻어낸다.
154+
String logDate = "";
155+
for (String line : lines) {
156+
LocalDate parsedDate = DateUtils.parse(line);
157+
if (parsedDate != null) {
158+
logDate = DateUtils.convertDateFormat("yyyy-MM-dd", parsedDate);
159+
break;
160+
}
161+
}
162+
163+
if(logDate == null || logDate.equals("")) {
164+
break;
165+
}
166+
167+
// 조회시작일자와 로그의 처음 기록일자를 비교한다.
168+
long diffTime = DateUtils.getDateDiffTime("yyyy-MM-dd", logDate, startDate);
169+
if (diffTime >= 0) { // 조회 Line 수를 더 늘려서 다시 조회
170+
alc.setReadLine((alc.getReadLine()) * 2);
171+
fullAlertLogString = checkAlertLog(alc);
172+
} else {
173+
break;
174+
}
175+
}
176+
177+
return fullAlertLogString;
178+
}
179+
}

src/test/java/root/common/server/implement/JschServerTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
44
import static org.junit.jupiter.api.Assertions.assertFalse;
5+
import static org.junit.jupiter.api.Assertions.assertNotEquals;
56
import static org.junit.jupiter.api.Assertions.assertNotNull;
67
import static org.junit.jupiter.api.Assertions.assertNull;
78
import static org.junit.jupiter.api.Assertions.assertThrows;
@@ -157,6 +158,18 @@ public void testGetServerName() {
157158
String serverName = jsch.getServerName();
158159
assertEquals(serverName, "DKY SERVER");
159160
}
161+
162+
@Test
163+
public void testExecuteCommand_EchoCommand() throws JSchException, IOException {
164+
String result = jsch.executeCommand("echo 1");
165+
assertEquals(result, "1");
166+
}
167+
168+
@Test
169+
public void testExecuteCommand_TailCommand() throws JSchException, IOException {
170+
String result = jsch.executeCommand("tail -500 C://Users/aserv/Desktop/alert_DB.log");
171+
assertNotEquals(result, "");
172+
}
160173

161174
@Test
162175
public void testValidateConn_Valid() {

0 commit comments

Comments
 (0)