1010import org .apache .commons .io .IOUtils ;
1111
1212import com .jcraft .jsch .Channel ;
13- import com .jcraft .jsch .JSchException ;
1413import com .jcraft .jsch .Session ;
1514
1615import lombok .extern .slf4j .Slf4j ;
@@ -37,39 +36,18 @@ public String getServerName() {
3736 return jsch .getServerName ();
3837 }
3938
40- @ Override
41- public Session getSession () {
42- return jsch .getSession ();
43- }
44-
45- @ Override
46- public Session connectSession (Session session ) {
47- try {
48- session .connect ();
49- } catch (JSchException e ) {
50- log .error (e .getMessage ());
51- }
52- return session ;
53- }
54-
55- @ Override
56- public void disConnectSession (Session session ) {
57- if (session .isConnected () == true && session != null ) {
58- session .disconnect ();
59- }
60- }
61-
6239 @ Override
6340 public int getAlertLogFileLineCount (AlertLogCommand alc ) {
6441 int fileLineCnt = 0 ;
6542 try {
66- Session session = this .getSession ();
67- session = this . connectSession ( session );
43+ Session session = jsch .getSession ();
44+ session . connect ( );
6845 Channel channel = jsch .openExecChannel (session , "cat " + alc .getReadFilePath () + " | wc -l" );
6946 InputStream in = jsch .connectChannel (channel );
7047 String result = IOUtils .toString (in , "UTF-8" );
7148 fileLineCnt = Integer .parseInt (result .trim ());
7249 jsch .disConnectChannel (channel );
50+ jsch .disConnect (session );
7351 } catch (Exception e ) {
7452 log .error (e .getMessage ());
7553 }
@@ -81,13 +59,14 @@ public int getAlertLogFileLineCount(AlertLogCommand alc) {
8159 public String checkAlertLog (AlertLogCommand alc ) {
8260 String result = "" ;
8361 try {
84- Session session = this .getSession ();
85- session = this . connectSession ( session );
62+ Session session = jsch .getSession ();
63+ session . connect ( );
8664 String command = "tail -" + alc .getReadLine () + " " + alc .getReadFilePath ();
8765 Channel channel = jsch .openExecChannel (session , command );
8866 InputStream in = jsch .connectChannel (channel );
8967 result = IOUtils .toString (in , "UTF-8" );
9068 jsch .disConnectChannel (channel );
69+ jsch .disConnect (session );
9170 } catch (Exception e ) {
9271 log .error (e .getMessage ());
9372 }
@@ -176,13 +155,14 @@ public AlertLog checkAlertLogDuringPeriod(AlertLogCommand alc, String startDate,
176155 public List <OSDiskUsage > checkOSDiskUsage () {
177156 List <OSDiskUsage > list = new ArrayList <>();
178157 try {
179- Session session = this .getSession ();
180- session = this . connectSession ( session );
158+ Session session = jsch .getSession ();
159+ session . connect ( );
181160 Channel channel = jsch .openExecChannel (session , "df --block-size=K -P" );
182161 InputStream in = jsch .connectChannel (channel );
183162 String result = IOUtils .toString (in , "UTF-8" );
184163 list = stringToOsDiskUsageList (result );
185164 jsch .disConnectChannel (channel );
165+ jsch .disConnect (session );
186166 } catch (Exception e ) {
187167 log .error (e .getMessage ());
188168 }
0 commit comments