Skip to content

Commit 1b283e7

Browse files
committed
Fix: Fix window server alertlog monitoring error
1 parent f60b6be commit 1b283e7

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed

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

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.time.LocalDate;
44
import java.util.ArrayList;
55
import java.util.List;
6+
import java.util.StringTokenizer;
67

78
import lombok.extern.slf4j.Slf4j;
89
import root.common.server.implement.JschServer;
@@ -33,7 +34,14 @@ public int getAlertLogFileLineCount(AlertLogCommand alc) {
3334
try {
3435
String command = String.format("find /v /c \"\" %s", alc.getReadFilePath());
3536
String executeResult = jsch.executeCommand(command);
36-
fileLineCnt = Integer.parseInt(executeResult);
37+
StringTokenizer st = new StringTokenizer(executeResult);
38+
String lastToken = "0";
39+
while (st.hasMoreTokens()) {
40+
lastToken = st.nextToken();
41+
}
42+
43+
fileLineCnt = Integer.parseInt(lastToken);
44+
log.debug(String.format("alert log file line count: %s, %d", alc.getReadFilePath(), fileLineCnt));
3745
} catch (Exception e) {
3846
log.error(e.getMessage());
3947
}
@@ -45,12 +53,13 @@ public int getAlertLogFileLineCount(AlertLogCommand alc) {
4553
public String checkAlertLog(AlertLogCommand alc) {
4654
String result = "";
4755
try {
48-
String command = String.format("tail %d %s", alc.getReadLine(), alc.getReadFilePath());
56+
String command = String.format("tail -%d %s", alc.getReadLine(), alc.getReadFilePath());
4957
result = jsch.executeCommand(command);
5058
} catch (Exception e) {
5159
log.error(e.getMessage());
5260
}
5361

62+
log.debug(alc.toString());
5463
return result;
5564
}
5665

@@ -62,7 +71,7 @@ public AlertLog checkAlertLogDuringPeriod(AlertLogCommand alc, String startDate,
6271

6372
try {
6473
// 조회기간동안의 로그만을 취하여 StringBuffer에 저장한다.
65-
String[] lines = fullAlertLogString.split("\n");
74+
String[] lines = fullAlertLogString.split(System.lineSeparator());
6675

6776
boolean isStartDate = false;
6877
boolean isEndDate = false;
@@ -76,13 +85,15 @@ public AlertLog checkAlertLogDuringPeriod(AlertLogCommand alc, String startDate,
7685

7786
for (int i = 0; i < lines.length; i++) {
7887
String line = lines[i];
79-
8088
// 조회시작일자 찾기
8189
if (!isStartDate) {
8290
LocalDate parsedDate = DateUtils.parse(line);
83-
if (parsedDate != null && DateUtils.convertDateFormat("yyyy-MM-dd", parsedDate).equals(startDate)) {
84-
isStartDate = true;
85-
readStartIndex = i;
91+
if (parsedDate != null) {
92+
String parsedDateString = DateUtils.convertDateFormat("yyyy-MM-dd", parsedDate);
93+
if (DateUtils.getDateDiffTime("yyyy-MM-dd", parsedDateString, startDate) >= 0) {
94+
isStartDate = true;
95+
readStartIndex = i;
96+
}
8697
}
8798
}
8899

@@ -98,7 +109,7 @@ public AlertLog checkAlertLogDuringPeriod(AlertLogCommand alc, String startDate,
98109

99110
if (i == readStartIndex) {
100111
logTimeStamp = line;
101-
}
112+
}
102113

103114
if (i != readStartIndex && !isEndDate) {
104115
alertLog.addLog(new Log(logTimeStamp, logContents));
@@ -127,7 +138,6 @@ public AlertLog checkAlertLogDuringPeriod(AlertLogCommand alc, String startDate,
127138
} catch (Exception e) {
128139
log.error(e.getMessage());
129140
}
130-
131141
return alertLog;
132142
}
133143

@@ -143,7 +153,7 @@ private String getAlertLogStringFromCertainDate(AlertLogCommand alc, String star
143153

144154
// 조회시작일자의 로그를 모두 포함하도록 readLine 수를 점진적으로 늘리면서 읽는다.
145155
while (true) {
146-
String[] lines = fullAlertLogString.split("\n");
156+
String[] lines = fullAlertLogString.split(System.lineSeparator());
147157

148158
// 현재 Read Line 수가 파일 최대 Line 수를 초과했을 시, 파일 전체를 읽고 반환한다.
149159
if (lines.length >= alertLogFileLineCnt) {
@@ -159,8 +169,8 @@ private String getAlertLogStringFromCertainDate(AlertLogCommand alc, String star
159169
break;
160170
}
161171
}
162-
163-
if(logDate == null || logDate.equals("")) {
172+
173+
if (logDate == null || logDate.equals("")) {
164174
break;
165175
}
166176

src/main/java/root/core/usecase/implement/ServerMonitoringUsecaseImpl.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import dnl.utils.text.table.TextTable;
1919
import dnl.utils.text.table.csv.CsvTableModel;
20+
import lombok.extern.slf4j.Slf4j;
2021
import root.core.domain.AlertLog;
2122
import root.core.domain.AlertLogCommand;
2223
import root.core.domain.Log;
@@ -30,6 +31,7 @@
3031
import root.utils.DateUtils;
3132
import root.utils.ExcelSheet;
3233

34+
@Slf4j
3335
public class ServerMonitoringUsecaseImpl implements ServerMonitoringUsecase {
3436
private ServerMonitoringRepository serverCheckRepository;
3537
private ReportRepository reportRepository;
@@ -264,7 +266,9 @@ public List<OSDiskUsage> getCurrentOSDiskUsage() {
264266

265267
@Override
266268
public AlertLog getAlertLogDuringPeriod(AlertLogCommand alc, String startDate, String endDate) {
269+
log.debug(String.format("alert log file monitoring, %s (%s ~ %s)", alc.getReadFilePath(), startDate, endDate));
267270
AlertLog result = serverCheckRepository.checkAlertLogDuringPeriod(alc, startDate, endDate);
271+
log.debug(result.toString());
268272
return result;
269273
}
270274
}

0 commit comments

Comments
 (0)