Skip to content

Commit 2433140

Browse files
authored
Merge pull request #219 from Dokyeongyun/ft-220417-alertLogMonitoringByServerOS
Ft 220417 alert log monitoring by server os
2 parents f60b6be + af73062 commit 2433140

File tree

7 files changed

+155
-277
lines changed

7 files changed

+155
-277
lines changed

src/main/java/root/core/repository/constracts/ServerMonitoringRepository.java

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

33
import java.util.List;
44

5-
import root.core.domain.AlertLog;
65
import root.core.domain.AlertLogCommand;
76
import root.core.domain.OSDiskUsage;
87

@@ -13,7 +12,5 @@ public interface ServerMonitoringRepository {
1312

1413
String checkAlertLog(AlertLogCommand alc);
1514

16-
AlertLog checkAlertLogDuringPeriod(AlertLogCommand alc, String startDate, String endDate);
17-
1815
List<OSDiskUsage> checkOSDiskUsage();
1916
}
Lines changed: 0 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package root.core.repository.implement;
22

33
import java.io.InputStream;
4-
import java.time.LocalDate;
54
import java.util.ArrayList;
65
import java.util.Arrays;
76
import java.util.List;
@@ -14,12 +13,9 @@
1413

1514
import lombok.extern.slf4j.Slf4j;
1615
import root.common.server.implement.JschServer;
17-
import root.core.domain.AlertLog;
1816
import root.core.domain.AlertLogCommand;
19-
import root.core.domain.Log;
2017
import root.core.domain.OSDiskUsage;
2118
import root.core.repository.constracts.ServerMonitoringRepository;
22-
import root.utils.DateUtils;
2319
import root.utils.NumberUnitUtils;
2420
import root.utils.NumberUnitUtils.Unit;
2521

@@ -63,83 +59,6 @@ public String checkAlertLog(AlertLogCommand alc) {
6359
return result;
6460
}
6561

66-
@Override
67-
public AlertLog checkAlertLogDuringPeriod(AlertLogCommand alc, String startDate, String endDate) {
68-
AlertLog alertLog = new AlertLog();
69-
70-
String fullAlertLogString = getAlertLogStringFromCertainDate(alc, startDate);
71-
72-
try {
73-
// 조회기간동안의 로그만을 취하여 StringBuffer에 저장한다.
74-
String[] lines = fullAlertLogString.split("\n");
75-
76-
boolean isStartDate = false;
77-
boolean isEndDate = false;
78-
79-
int readStartIndex = 0;
80-
int readEndIndex = lines.length;
81-
82-
String logTimeStamp = "";
83-
List<String> logContents = new ArrayList<>();
84-
StringBuffer sb = new StringBuffer();
85-
86-
for (int i = 0; i < lines.length; i++) {
87-
String line = lines[i];
88-
89-
// 조회시작일자 찾기
90-
if (!isStartDate) {
91-
LocalDate parsedDate = DateUtils.parse(line);
92-
if (parsedDate != null && DateUtils.convertDateFormat("yyyy-MM-dd", parsedDate).equals(startDate)) {
93-
isStartDate = true;
94-
readStartIndex = i;
95-
}
96-
}
97-
98-
// 로그 저장 시작 & 조회종료일자 찾기
99-
if (isStartDate) {
100-
LocalDate parsedDate = DateUtils.parse(line);
101-
if (parsedDate != null) { // Log TimeStamp Line
102-
String logDate = DateUtils.convertDateFormat("yyyy-MM-dd", parsedDate);
103-
if (logDate.startsWith(DateUtils.addDate(endDate, 0, 0, 1))) {
104-
isEndDate = true;
105-
readEndIndex = i;
106-
}
107-
108-
if (i == readStartIndex) {
109-
logTimeStamp = line;
110-
}
111-
112-
if (i != readStartIndex && !isEndDate) {
113-
alertLog.addLog(new Log(logTimeStamp, logContents));
114-
logContents = new ArrayList<>();
115-
logTimeStamp = line;
116-
}
117-
} else { // Log Content Line
118-
logContents.add(line);
119-
}
120-
121-
// 로그 저장 중지
122-
if (!isEndDate) {
123-
sb.append(line);
124-
} else {
125-
break;
126-
}
127-
}
128-
}
129-
130-
// 종료 후 마지막 로그 추가하기
131-
alertLog.addLog(new Log(logTimeStamp, logContents));
132-
alertLog.setFullLogString(sb.toString());
133-
134-
log.info("\t▶ Alert Log READ LINE: " + (readEndIndex - readStartIndex) + "/" + alc.getReadLine());
135-
136-
} catch (Exception e) {
137-
log.error(e.getMessage());
138-
}
139-
140-
return alertLog;
141-
}
142-
14362
@Override
14463
public List<OSDiskUsage> checkOSDiskUsage() {
14564
List<OSDiskUsage> list = new ArrayList<>();
@@ -210,44 +129,4 @@ public List<OSDiskUsage> stringToOsDiskUsageList(String result) {
210129

211130
return list;
212131
}
213-
214-
private String getAlertLogStringFromCertainDate(AlertLogCommand alc, String startDate) {
215-
int alertLogFileLineCnt = this.getAlertLogFileLineCount(alc);
216-
String fullAlertLogString = this.checkAlertLog(alc);
217-
218-
// 조회시작일자의 로그를 모두 포함하도록 readLine 수를 점진적으로 늘리면서 읽는다.
219-
while (true) {
220-
String[] lines = fullAlertLogString.split("\n");
221-
222-
// 현재 Read Line 수가 파일 최대 Line 수를 초과했을 시, 파일 전체를 읽고 반환한다.
223-
if (lines.length >= alertLogFileLineCnt) {
224-
break;
225-
}
226-
227-
// 조회한 로그 내에서 가장 처음으로 나타나는 로그의 기록일자를 얻어낸다.
228-
String logDate = "";
229-
for (String line : lines) {
230-
LocalDate parsedDate = DateUtils.parse(line);
231-
if (parsedDate != null) {
232-
logDate = DateUtils.convertDateFormat("yyyy-MM-dd", parsedDate);
233-
break;
234-
}
235-
}
236-
237-
if(logDate == null || logDate.equals("")) {
238-
break;
239-
}
240-
241-
// 조회시작일자와 로그의 처음 기록일자를 비교한다.
242-
long diffTime = DateUtils.getDateDiffTime("yyyy-MM-dd", logDate, startDate);
243-
if (diffTime >= 0) { // 조회 Line 수를 더 늘려서 다시 조회
244-
alc.setReadLine(alc.getReadLine() * 2);
245-
fullAlertLogString = this.checkAlertLog(alc);
246-
} else {
247-
break;
248-
}
249-
}
250-
251-
return fullAlertLogString;
252-
}
253132
}
Lines changed: 11 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
package root.core.repository.implement;
22

3-
import java.time.LocalDate;
4-
import java.util.ArrayList;
53
import java.util.List;
4+
import java.util.StringTokenizer;
65

76
import lombok.extern.slf4j.Slf4j;
87
import root.common.server.implement.JschServer;
9-
import root.core.domain.AlertLog;
108
import root.core.domain.AlertLogCommand;
11-
import root.core.domain.Log;
129
import root.core.domain.OSDiskUsage;
1310
import root.core.repository.constracts.ServerMonitoringRepository;
14-
import root.utils.DateUtils;
1511

1612
@Slf4j
1713
public class WindowServerMonitoringRepository implements ServerMonitoringRepository {
@@ -33,7 +29,14 @@ public int getAlertLogFileLineCount(AlertLogCommand alc) {
3329
try {
3430
String command = String.format("find /v /c \"\" %s", alc.getReadFilePath());
3531
String executeResult = jsch.executeCommand(command);
36-
fileLineCnt = Integer.parseInt(executeResult);
32+
StringTokenizer st = new StringTokenizer(executeResult);
33+
String lastToken = "0";
34+
while (st.hasMoreTokens()) {
35+
lastToken = st.nextToken();
36+
}
37+
38+
fileLineCnt = Integer.parseInt(lastToken);
39+
log.debug(String.format("alert log file line count: %s, %d", alc.getReadFilePath(), fileLineCnt));
3740
} catch (Exception e) {
3841
log.error(e.getMessage());
3942
}
@@ -45,135 +48,18 @@ public int getAlertLogFileLineCount(AlertLogCommand alc) {
4548
public String checkAlertLog(AlertLogCommand alc) {
4649
String result = "";
4750
try {
48-
String command = String.format("tail %d %s", alc.getReadLine(), alc.getReadFilePath());
51+
String command = String.format("tail -%d %s", alc.getReadLine(), alc.getReadFilePath());
4952
result = jsch.executeCommand(command);
5053
} catch (Exception e) {
5154
log.error(e.getMessage());
5255
}
53-
56+
log.debug(alc.toString());
5457
return result;
5558
}
5659

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-
13460
@Override
13561
public List<OSDiskUsage> checkOSDiskUsage() {
13662
// TODO Auto-generated method stub
13763
return null;
13864
}
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-
}
17965
}

0 commit comments

Comments
 (0)