11package root .core .repository .implement ;
22
3- import java .time .LocalDate ;
4- import java .util .ArrayList ;
53import java .util .List ;
4+ import java .util .StringTokenizer ;
65
76import lombok .extern .slf4j .Slf4j ;
87import root .common .server .implement .JschServer ;
9- import root .core .domain .AlertLog ;
108import root .core .domain .AlertLogCommand ;
11- import root .core .domain .Log ;
129import root .core .domain .OSDiskUsage ;
1310import root .core .repository .constracts .ServerMonitoringRepository ;
14- import root .utils .DateUtils ;
1511
1612@ Slf4j
1713public 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