Skip to content

Commit f30b9e1

Browse files
committed
Monitoring alertLog file in Window server OS
1 parent 9a1dc42 commit f30b9e1

File tree

2 files changed

+347
-0
lines changed

2 files changed

+347
-0
lines changed
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("Get-Content %s -Tail %d", alc.getReadFilePath(), alc.getReadLine());
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+
}
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
package root.core.repository.implement;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertNotNull;
5+
import static org.junit.jupiter.api.Assertions.assertNull;
6+
import static org.mockito.Mockito.mock;
7+
import static org.mockito.Mockito.when;
8+
9+
import java.io.IOException;
10+
11+
import org.junit.jupiter.api.BeforeAll;
12+
import org.junit.jupiter.api.BeforeEach;
13+
import org.junit.jupiter.api.Test;
14+
15+
import com.jcraft.jsch.JSchException;
16+
17+
import root.common.server.implement.JschServer;
18+
import root.core.domain.AlertLog;
19+
import root.core.domain.AlertLogCommand;
20+
import root.core.repository.constracts.ServerMonitoringRepository;
21+
22+
public class WindowServerMonitoringRepositoryTest {
23+
24+
public JschServer jschServer;
25+
public ServerMonitoringRepository repo;
26+
public static String alertLogString = "";
27+
public static String[] alertLogLines;
28+
29+
@BeforeAll
30+
public static void before() {
31+
StringBuffer sb = new StringBuffer();
32+
sb.append("2022-03-23T13:38:35.065184+09:00").append("\n");
33+
sb.append("Thread 1 advanced to log sequence 7606 (LGWR switch)").append("\n");
34+
sb.append(" Current log# 5 seq# 7606 mem# 0: +REDO/1003346093").append("\n");
35+
sb.append("2022-03-24T13:39:00.180206+09:00").append("\n");
36+
sb.append("Archived Log entry 13398 added for T-1.S-7605 ID 0x8155080b LAD:1").append("\n");
37+
sb.append("2022-03-25T14:24:57.291344+09:00").append("\n");
38+
sb.append("Session (223,56276): RECO logon successful: Inbound connection from client").append("\n");
39+
sb.append("Session (223,56276): RECO logon successful: DB Logon User: RECO, ").append("\n");
40+
sb.append("Session (223,56276): RECO logon successful: Client IP Address: -").append("\n");
41+
sb.append("2022-03-26T18:04:53.572965+09:00").append("\n");
42+
sb.append("Thread 1 advanced to log sequence 7607 (LGWR switch)").append("\n");
43+
sb.append(" Current log# 1 seq# 7607 mem# 0: +REDO/DBERP/ONLINELOG/group_1.257.966360593").append("\n");
44+
sb.append("2022-03-27T18:05:16.929231+09:00").append("\n");
45+
sb.append("Archived Log entry 13400 added for T-1.S-7606 ID 0x8155080b LAD:1").append("\n");
46+
sb.append("2022-03-28T00:02:21.629284+09:00").append("\n");
47+
sb.append("TABLE SYS.WRI$_OPTSTAT_HISTHEAD_HISTORY: ADDED INTERVAL PARTITION SYS_P38333)").append("\n");
48+
sb.append("TABLE SYS.WRI$_OPTSTAT_HISTGRM_HISTORY: ADDED INTERVAL PARTITION SYS_P38336)").append("\n");
49+
sb.append("2022-03-28T01:00:18.971650+09:00").append("\n");
50+
sb.append("ALTER SYSTEM ARCHIVE LOG").append("\n");
51+
sb.append("2022-03-29T01:00:18.980968+09:00").append("\n");
52+
sb.append("Thread 1 advanced to log sequence 7608 (LGWR switch)").append("\n");
53+
sb.append(" Current log# 6 seq# 7608 mem# 0: +REDO/1003346037").append("\n");
54+
sb.append("2022-03-30T01:00:38.903080+09:00").append("\n");
55+
sb.append("Archived Log entry 13401 added for T-1.S-7607 ID 0x8155080b LAD:1").append("\n");
56+
sb.append("2022-03-31T02:56:51.984291+09:00").append("\n");
57+
sb.append("Starting control autobackup").append("\n");
58+
alertLogString = sb.toString();
59+
60+
alertLogLines = alertLogString.split("\n");
61+
}
62+
63+
@BeforeEach
64+
public void setup() {
65+
jschServer = mock(JschServer.class);
66+
repo = new WindowServerMonitoringRepository(jschServer);
67+
}
68+
69+
@Test
70+
public void testGetServerName_ServerNameIsNull() {
71+
when(jschServer.getServerName()).thenReturn(null);
72+
String result = repo.getServerName();
73+
assertNull(result);
74+
}
75+
76+
@Test
77+
public void testGetServerName_ServerNameIsNotNull() {
78+
when(jschServer.getServerName()).thenReturn("DKY SERVER");
79+
String result = repo.getServerName();
80+
assertNotNull(result);
81+
}
82+
83+
@Test
84+
public void testGetAlertLogFileLineCount() throws JSchException, IOException {
85+
// Arrange
86+
AlertLogCommand alc = new AlertLogCommand();
87+
alc.setReadFilePath("C:\\alert_DKYDB.log");
88+
89+
String command = String.format("find /v /c \"\" %s", alc.getReadFilePath());
90+
when(jschServer.executeCommand(command)).thenReturn(String.valueOf(alertLogLines.length));
91+
92+
// Act
93+
int lineCount = repo.getAlertLogFileLineCount(alc);
94+
95+
// Assert
96+
assertEquals(lineCount, alertLogLines.length);
97+
}
98+
99+
@Test
100+
public void testCheckAlertLog() throws JSchException, IOException {
101+
// Arrange
102+
AlertLogCommand alc = new AlertLogCommand();
103+
alc.setReadLine(10);
104+
alc.setReadFilePath("C:\\alert_DKYDB.log");
105+
106+
String command = String.format("Get-Content %s -Tail %d", alc.getReadFilePath(), alc.getReadLine());
107+
when(jschServer.executeCommand(command)).thenReturn(alertLogString);
108+
109+
// Act
110+
String result = repo.checkAlertLog(alc);
111+
112+
// Assert
113+
assertEquals(result, alertLogString);
114+
}
115+
116+
@Test
117+
public void testCheckAlertLogDuringPeriod() throws JSchException, IOException {
118+
// Arrange
119+
AlertLogCommand alc = new AlertLogCommand();
120+
alc.setReadLine(10);
121+
alc.setReadFilePath("C:\\alert_DKYDB.log");
122+
123+
String command1 = String.format("find /v /c \"\" %s", alc.getReadFilePath());
124+
when(jschServer.executeCommand(command1)).thenReturn(String.valueOf(alertLogLines.length));
125+
126+
String command2 = String.format("Get-Content %s -Tail %d", alc.getReadFilePath(), alc.getReadLine());
127+
when(jschServer.executeCommand(command2)).thenReturn(alertLogString);
128+
129+
// Act
130+
AlertLog alertLog = repo.checkAlertLogDuringPeriod(alc, "2022-03-24", "2022-03-29");
131+
132+
// Assert
133+
assertEquals(alertLog.getTotalLineCount(), 12);
134+
assertEquals(alertLog.getAlertLogs().size(), 7);
135+
}
136+
137+
@Test
138+
public void testCheckAlertLogDuringPeriod_ReadLineBiggerThenTotalLineCnt() throws JSchException, IOException {
139+
// Arrange
140+
AlertLogCommand alc = new AlertLogCommand();
141+
alc.setReadLine(20);
142+
alc.setReadFilePath("C:\\alert_DKYDB.log");
143+
144+
String command1 = String.format("find /v /c \"\" %s", alc.getReadFilePath());
145+
when(jschServer.executeCommand(command1)).thenReturn("26");
146+
147+
String command2 = String.format("Get-Content %s -Tail %d", alc.getReadFilePath(), alc.getReadLine());
148+
StringBuilder builder = new StringBuilder();
149+
for (int i = 0; i < Math.min(alertLogLines.length, alc.getReadLine()); i++) {
150+
builder.append(alertLogLines[i]).append("\n");
151+
}
152+
when(jschServer.executeCommand(command2)).thenReturn(builder.toString());
153+
154+
String command3 = String.format("Get-Content %s -Tail %d", alc.getReadFilePath(), alc.getReadLine() * 2);
155+
builder = new StringBuilder();
156+
for (int i = 0; i < Math.min(alertLogLines.length, alc.getReadLine() * 2); i++) {
157+
builder.append(alertLogLines[i]).append("\n");
158+
}
159+
when(jschServer.executeCommand(command3)).thenReturn(builder.toString());
160+
161+
// Act
162+
AlertLog alertLog = repo.checkAlertLogDuringPeriod(alc, "2022-03-23", "2022-03-24");
163+
164+
// Assert
165+
assertEquals(alertLog.getTotalLineCount(), 3);
166+
assertEquals(alertLog.getAlertLogs().size(), 2);
167+
}
168+
}

0 commit comments

Comments
 (0)