11package root .core .repository .implement ;
22
33import java .io .InputStream ;
4+ import java .time .LocalDate ;
45import java .util .ArrayList ;
56import java .util .Arrays ;
67import java .util .List ;
7- import java .util .Locale ;
88import java .util .StringTokenizer ;
9- import java .util .regex .Pattern ;
109
1110import org .apache .commons .io .IOUtils ;
1211
1312import com .jcraft .jsch .Channel ;
1413import com .jcraft .jsch .JSchException ;
1514import com .jcraft .jsch .Session ;
1615
16+ import lombok .extern .slf4j .Slf4j ;
1717import root .common .server .implement .JschServer ;
1818import root .core .domain .AlertLog ;
1919import root .core .domain .AlertLogCommand ;
2424import root .utils .NumberUnitUtils ;
2525import root .utils .NumberUnitUtils .Unit ;
2626
27+ @ Slf4j
2728public class ServerCheckRepositoryImpl implements ServerCheckRepository {
2829 private JschServer jsch ;
29-
30+
3031 public ServerCheckRepositoryImpl (JschServer jsch ) {
3132 this .jsch = jsch ;
3233 }
33-
34+
3435 @ Override
3536 public String getServerName () {
3637 return jsch .getServerName ();
3738 }
38-
39+
3940 @ Override
4041 public Session getSession () {
41- return jsch .getSession ();
42+ return jsch .getSession ();
4243 }
43-
44+
4445 @ Override
4546 public Session connectSession (Session session ) {
4647 try {
4748 session .connect ();
4849 } catch (JSchException e ) {
49- e . printStackTrace ( );
50+ log . error ( e . getMessage () );
5051 }
5152 return session ;
5253 }
53-
54+
5455 @ Override
5556 public void disConnectSession (Session session ) {
56- if (session .isConnected () == true && session != null ) {
57+ if (session .isConnected () == true && session != null ) {
5758 session .disconnect ();
5859 }
5960 }
60-
61+
6162 @ Override
6263 public int getAlertLogFileLineCount (AlertLogCommand alc ) {
6364 int fileLineCnt = 0 ;
@@ -70,12 +71,12 @@ public int getAlertLogFileLineCount(AlertLogCommand alc) {
7071 fileLineCnt = Integer .parseInt (result .trim ());
7172 jsch .disConnectChannel (channel );
7273 } catch (Exception e ) {
73- e . printStackTrace ( );
74- }
75-
74+ log . error ( e . getMessage () );
75+ }
76+
7677 return fileLineCnt ;
7778 }
78-
79+
7980 @ Override
8081 public String checkAlertLog (AlertLogCommand alc ) {
8182 String result = "" ;
@@ -87,124 +88,88 @@ public String checkAlertLog(AlertLogCommand alc) {
8788 result = IOUtils .toString (in , "UTF-8" );
8889 jsch .disConnectChannel (channel );
8990 } catch (Exception e ) {
90- e . printStackTrace ( );
91- }
92-
91+ log . error ( e . getMessage () );
92+ }
93+
9394 return result ;
9495 }
95-
96+
9697 @ Override
9798 public AlertLog checkAlertLogDuringPeriod (AlertLogCommand alc , String startDate , String endDate ) {
9899 AlertLog alertLog = new AlertLog ();
99- int alertLogFileLineCnt = this .getAlertLogFileLineCount (alc );
100- String fullAlertLogString = this .checkAlertLog (alc );
101- String dateFormat = alc .getDateFormat ();
102- String dateFormatRegex = alc .getDateFormatRegex ();
103- int dateLength = dateFormat .equals ("yyyy-MM-dd" ) ? 11 : 24 ;
104-
105- try {
106-
107- // 조회시작일자의 로그를 모두 포함하도록 readLine 수를 점진적으로 늘리면서 읽는다.
108- while (true ) {
109- String [] lines = fullAlertLogString .split ("\n " );
110100
111- // 현재 Read Line 수가 파일 최대 Line 수를 초과했을 시, 파일 전체를 읽고 반환한다.
112- if (lines .length >= alertLogFileLineCnt ) {
113- break ;
114- }
115-
116- // 조회한 로그 내에서 가장 처음으로 나타나는 로그의 기록일자를 얻어낸다.
117- String logDate = "" ;
118- for (String line : lines ) {
119- String linePrefix = line .substring (0 , line .length () < dateLength ? line .length () : dateLength );
120- if (Pattern .matches ("^" +dateFormatRegex , linePrefix )) {
121- logDate = DateUtils .convertDateFormat (dateFormat , "yyyy-MM-dd" , linePrefix , Locale .ENGLISH );
122- break ;
123- }
124- }
125-
126- // 조회시작일자와 로그의 처음 기록일자를 비교한다.
127- long diffTime = DateUtils .getDateDiffTime ("yyyy-MM-dd" , logDate , startDate );
128- if (diffTime >= 0 ) { // 조회 Line 수를 더 늘려서 다시 조회
129- alc .setReadLine (String .valueOf (Integer .parseInt (alc .getReadLine ()) * 2 ));
130- fullAlertLogString = this .checkAlertLog (alc );
131- } else {
132- break ;
133- }
134- }
135-
101+ String fullAlertLogString = getAlertLogStringFromCertainDate (alc , startDate );
102+
103+ try {
136104 // 조회기간동안의 로그만을 취하여 StringBuffer에 저장한다.
137105 String [] lines = fullAlertLogString .split ("\n " );
138-
106+
139107 boolean isStartDate = false ;
140108 boolean isEndDate = false ;
109+
141110 int readStartIndex = 0 ;
142111 int readEndIndex = lines .length ;
143- int realNumberOfReadLine = 0 ;
112+
144113 String logTimeStamp = "" ;
145114 List <String > logContents = new ArrayList <>();
146115 StringBuffer sb = new StringBuffer ();
147-
148- for (int i = 0 ; i < lines .length ; i ++) {
116+
117+ for (int i = 0 ; i < lines .length ; i ++) {
149118 String line = lines [i ];
150-
119+
151120 // 조회시작일자 찾기
152- if (isStartDate == false ) {
153- String linePrefix = line .substring (0 , line .length () < dateLength ? line .length () : dateLength );
154- if (Pattern .matches ("^" +dateFormatRegex , linePrefix )) {
155- linePrefix = DateUtils .convertDateFormat (dateFormat , "yyyy-MM-dd" , linePrefix , Locale .ENGLISH );
156- if (linePrefix .startsWith (startDate )) {
157- isStartDate = true ;
158- readStartIndex = i ;
159- }
121+ if (!isStartDate ) {
122+ LocalDate parsedDate = DateUtils .parse (line );
123+ if (parsedDate != null && DateUtils .convertDateFormat ("yyyy-MM-dd" , parsedDate ).equals (startDate )) {
124+ isStartDate = true ;
125+ readStartIndex = i ;
126+ break ;
160127 }
161- }
162-
128+ }
129+
163130 // 로그 저장 시작 & 조회종료일자 찾기
164- if (isStartDate == true ) {
165- String linePrefix = line .substring (0 , line .length () < dateLength ? line .length () : dateLength );
166- if (Pattern .matches ("^" +dateFormatRegex , linePrefix )) { // LogTimeStamp Line
167- // 조회종료일자인지 확인
168- linePrefix = DateUtils .convertDateFormat (dateFormat , "yyyy-MM-dd" , linePrefix , Locale .ENGLISH );
169- if (linePrefix .startsWith (DateUtils .addDate (endDate , 0 , 0 , 1 ))) {
131+ if (isStartDate ) {
132+ LocalDate parsedDate = DateUtils .parse (line );
133+ if (parsedDate != null ) { // Log TimeStamp Line
134+ String logDate = DateUtils .convertDateFormat ("yyyy-MM-dd" , parsedDate );
135+ if (logDate .startsWith (DateUtils .addDate (endDate , 0 , 0 , 1 ))) {
170136 isEndDate = true ;
171137 readEndIndex = i ;
172138 }
173-
174- if (i == readStartIndex ) {
139+
140+ if (i == readStartIndex ) {
175141 logTimeStamp = line ;
176- }
177- if (i != readStartIndex ) {
142+ } else {
178143 alertLog .addLog (new Log (logTimeStamp , logContents ));
179144 logContents = new ArrayList <>();
180145 logTimeStamp = line ;
181146 }
182147 } else { // Log Content Line
183148 logContents .add (line );
184149 }
185-
150+
186151 // 로그 저장 중지
187- if ( isEndDate == false ) {
188- sb .append (line );
152+ if (! isEndDate ) {
153+ sb .append (line );
189154 } else {
190155 break ;
191156 }
192157 }
193158 }
159+
194160 // 종료 후 마지막 로그 추가하기
195161 alertLog .addLog (new Log (logTimeStamp , logContents ));
196162 alertLog .setFullLogString (sb .toString ());
197163
198- realNumberOfReadLine = readEndIndex - readStartIndex ;
199- System .out .println ("\t ▶ Alert Log READ LINE: " + realNumberOfReadLine + "/" + alc .getReadLine ());
164+ log .info ("\t ▶ Alert Log READ LINE: " + (readEndIndex - readStartIndex ) + "/" + alc .getReadLine ());
200165
201166 } catch (Exception e ) {
202- e . printStackTrace ( );
203- }
204-
167+ log . error ( e . getMessage () );
168+ }
169+
205170 return alertLog ;
206171 }
207-
172+
208173 @ Override
209174 public List <OSDiskUsage > checkOSDiskUsage () {
210175 List <OSDiskUsage > list = new ArrayList <>();
@@ -216,15 +181,14 @@ public List<OSDiskUsage> checkOSDiskUsage() {
216181 String result = IOUtils .toString (in , "UTF-8" );
217182 list = stringToOsDiskUsageList (result );
218183 jsch .disConnectChannel (channel );
219- }
220- catch (Exception e ) {
221- e .printStackTrace ();
184+ } catch (Exception e ) {
185+ log .error (e .getMessage ());
222186 }
223-
187+
224188 return list ;
225189 }
226-
227- public List <OSDiskUsage > stringToOsDiskUsageList (String result ) {
190+
191+ public List <OSDiskUsage > stringToOsDiskUsageList (String result ) {
228192 StringTokenizer st = new StringTokenizer (result );
229193 List <String > header = Arrays
230194 .asList (new String [] { "Filesystem" , "1024-blocks" , "Used" , "Available" , "Capacity" , "Mounted on" });
@@ -234,12 +198,12 @@ public List<OSDiskUsage> stringToOsDiskUsageList (String result) {
234198 int index = 0 ;
235199
236200 OSDiskUsage row = new OSDiskUsage ();
237- while (st .hasMoreElements ()) {
201+ while (st .hasMoreElements ()) {
238202 String next = st .nextToken ();
239- if (!isHeader ) {
203+ if (!isHeader ) {
240204 String headerName = header .get (index ++);
241-
242- switch (headerName ) {
205+
206+ switch (headerName ) {
243207 case "Filesystem" :
244208 row .setFileSystem (next );
245209 break ;
@@ -248,7 +212,7 @@ public List<OSDiskUsage> stringToOsDiskUsageList (String result) {
248212 Double .valueOf (next .substring (0 , next .indexOf ("K" )))));
249213 break ;
250214 case "Used" :
251- row .setUsedSpace (NumberUnitUtils .toByteValue (Unit .KiloByte ,
215+ row .setUsedSpace (NumberUnitUtils .toByteValue (Unit .KiloByte ,
252216 Double .valueOf (next .substring (0 , next .indexOf ("K" )))));
253217 break ;
254218 case "Available" :
@@ -262,16 +226,53 @@ public List<OSDiskUsage> stringToOsDiskUsageList (String result) {
262226 row .setMountedOn (next );
263227 break ;
264228 }
265-
266- if (index == 6 ) {
229+
230+ if (index == 6 ) {
267231 list .add (row );
268232 row = new OSDiskUsage ();
269233 index = 0 ;
270234 }
271235 }
272- if (next .equals ("on" )) isHeader = false ;
236+ if (next .equals ("on" ))
237+ isHeader = false ;
273238 }
274-
239+
275240 return list ;
276241 }
242+
243+ private String getAlertLogStringFromCertainDate (AlertLogCommand alc , String startDate ) {
244+ int alertLogFileLineCnt = this .getAlertLogFileLineCount (alc );
245+ String fullAlertLogString = this .checkAlertLog (alc );
246+
247+ // 조회시작일자의 로그를 모두 포함하도록 readLine 수를 점진적으로 늘리면서 읽는다.
248+ while (true ) {
249+ String [] lines = fullAlertLogString .split ("\n " );
250+
251+ // 현재 Read Line 수가 파일 최대 Line 수를 초과했을 시, 파일 전체를 읽고 반환한다.
252+ if (lines .length >= alertLogFileLineCnt ) {
253+ break ;
254+ }
255+
256+ // 조회한 로그 내에서 가장 처음으로 나타나는 로그의 기록일자를 얻어낸다.
257+ String logDate = "" ;
258+ for (String line : lines ) {
259+ LocalDate parsedDate = DateUtils .parse (line );
260+ if (parsedDate != null ) {
261+ logDate = DateUtils .convertDateFormat ("yyyy-MM-dd" , parsedDate );
262+ break ;
263+ }
264+ }
265+
266+ // 조회시작일자와 로그의 처음 기록일자를 비교한다.
267+ long diffTime = DateUtils .getDateDiffTime ("yyyy-MM-dd" , logDate , startDate );
268+ if (diffTime >= 0 ) { // 조회 Line 수를 더 늘려서 다시 조회
269+ alc .setReadLine (String .valueOf (Integer .parseInt (alc .getReadLine ()) * 2 ));
270+ fullAlertLogString = this .checkAlertLog (alc );
271+ } else {
272+ break ;
273+ }
274+ }
275+
276+ return fullAlertLogString ;
277+ }
277278}
0 commit comments