Skip to content

Commit 81edd7d

Browse files
authored
Merge pull request #212 from Dokyeongyun/develop
2022.03.28. ~ 2022.04.13. Merge
2 parents 09275db + be87940 commit 81edd7d

37 files changed

+1816
-832
lines changed

pom.xml

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -156,13 +156,6 @@
156156
<artifactId>fontawesomefx-fontawesome</artifactId>
157157
<version>4.7.0-9.1.2</version>
158158
</dependency>
159-
160-
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
161-
<dependency>
162-
<groupId>log4j</groupId>
163-
<artifactId>log4j</artifactId>
164-
<version>1.2.17</version>
165-
</dependency>
166159

167160
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
168161
<dependency>
@@ -186,6 +179,29 @@
186179
<version>5.7.0</version>
187180
<scope>test</scope>
188181
</dependency>
182+
183+
<!-- mockito-core -->
184+
<dependency>
185+
<groupId>org.mockito</groupId>
186+
<artifactId>mockito-core</artifactId>
187+
<version>4.3.1</version>
188+
<scope>test</scope>
189+
</dependency>
190+
191+
<!-- mockito-inline -->
192+
<dependency>
193+
<groupId>org.mockito</groupId>
194+
<artifactId>mockito-inline</artifactId>
195+
<version>4.3.1</version>
196+
<scope>test</scope>
197+
</dependency>
189198

199+
<!-- logback -->
200+
<dependency>
201+
<groupId>ch.qos.logback</groupId>
202+
<artifactId>logback-classic</artifactId>
203+
<version>1.2.3</version>
204+
</dependency>
205+
190206
</dependencies>
191207
</project>

src/main/java/root/applications/ConsoleApp.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,21 @@
1515
import root.common.server.implement.JschServer;
1616
import root.core.batch.DBCheckBatch;
1717
import root.core.batch.ServerCheckBatch;
18-
import root.core.domain.AlertLogCommandPeriod;
1918
import root.core.domain.JdbcConnectionInfo;
2019
import root.core.domain.JschConnectionInfo;
2120
import root.core.repository.constracts.DBCheckRepository;
2221
import root.core.repository.constracts.PropertyRepository;
23-
import root.core.repository.constracts.ServerCheckRepository;
22+
import root.core.repository.constracts.ServerMonitoringRepository;
2423
import root.core.repository.implement.DBCheckRepositoryImpl;
24+
import root.core.repository.implement.LinuxServerMonitoringRepository;
2525
import root.core.repository.implement.PropertyRepositoryImpl;
2626
import root.core.repository.implement.ReportFileRepo;
27-
import root.core.repository.implement.ServerCheckRepositoryImpl;
2827
import root.core.service.contracts.PropertyService;
2928
import root.core.service.implement.FilePropertyService;
3029
import root.core.usecase.constracts.DBCheckUsecase;
31-
import root.core.usecase.constracts.ServerCheckUsecase;
30+
import root.core.usecase.constracts.ServerMonitoringUsecase;
3231
import root.core.usecase.implement.DBCheckUsecaseImpl;
33-
import root.core.usecase.implement.ServerCheckUsecaseImpl;
32+
import root.core.usecase.implement.ServerMonitoringUsecaseImpl;
3433
import root.utils.CsvUtils;
3534
import root.utils.DateUtils;
3635
import root.utils.PatternUtils;
@@ -163,13 +162,13 @@ public static void main(String[] args) throws IOException {
163162
System.out.println("■ [ " + jsch.getServerName() + " Monitoring Start ]\n");
164163
JschServer server = new JschServer(jsch);
165164
server.init();
166-
ServerCheckRepository repo = new ServerCheckRepositoryImpl(server);
167-
ServerCheckUsecase usecase = new ServerCheckUsecaseImpl(repo, ReportFileRepo.getInstance());
165+
ServerMonitoringRepository repo = new LinuxServerMonitoringRepository(server);
166+
ServerMonitoringUsecase usecase = new ServerMonitoringUsecaseImpl(repo, ReportFileRepo.getInstance());
168167
ServerCheckBatch serverBatch = new ServerCheckBatch(usecase);
169168

170-
AlertLogCommandPeriod alcp = new AlertLogCommandPeriod(jsch.getAlc(),
171-
DateUtils.addDate(DateUtils.getToday("yyyy-MM-dd"), 0, 0, -1), DateUtils.getToday("yyyy-MM-dd"));
172-
serverBatch.startBatchAlertLogCheckDuringPeriod(alcp);
169+
String startDate = DateUtils.addDate(DateUtils.getToday("yyyy-MM-dd"), 0, 0, -1);
170+
String endDate = DateUtils.getToday("yyyy-MM-dd");
171+
serverBatch.startBatchAlertLogCheckDuringPeriod(jsch.getAlc(), startDate, endDate);
173172
serverBatch.startBatchOSDiskUsageCheck();
174173
System.out.println("■ [ " + jsch.getServerName() + " Monitoring End ]\n\n");
175174
}

src/main/java/root/applications/CsvReportSeparatorApp.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010
import java.util.regex.Pattern;
1111

1212
import root.core.repository.constracts.ReportRepository;
13-
import root.core.repository.implement.ReportRepositoryImpl;
13+
import root.core.repository.implement.ReportFileRepo;
1414
import root.utils.DateUtils;
1515

1616
public class CsvReportSeparatorApp {
1717

18-
public static ReportRepository reportRepository = ReportRepositoryImpl.getInstance();
18+
public static ReportRepository reportRepository = ReportFileRepo.getInstance();
1919

2020
public static void main(String[] args) throws IOException {
2121
/*

src/main/java/root/applications/Program.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,18 @@
44
import javafx.scene.Parent;
55
import javafx.scene.Scene;
66
import javafx.stage.Stage;
7+
import lombok.extern.slf4j.Slf4j;
78
import root.core.repository.constracts.PropertyRepository;
89
import root.core.repository.implement.PropertyRepositoryImpl;
910
import root.javafx.Controller.HomeController;
1011
import root.javafx.Controller.LeftMenuController;
1112
import root.javafx.DI.DependencyInjection;
1213

14+
@Slf4j
1315
public class Program extends Application {
1416

1517
public static Stage stage;
16-
18+
1719
PropertyRepository propRepo = PropertyRepositoryImpl.getInstance();
1820

1921
@Override
@@ -36,16 +38,21 @@ public void start(Stage primaryStage) throws Exception {
3638
}
3739

3840
public static void main(String[] args) {
39-
if(System.getProperty("resourceBaseDir") == null) {
40-
System.setProperty("resourceBaseDir", "");
41+
if (System.getProperty("resourceBaseDir") == null) {
42+
System.setProperty("resourceBaseDir", "");
4143
}
4244

45+
log.info("Start GUI Program");
4346
launch(args);
4447
}
4548

4649
private void setUpDependecyInjector() {
4750
// save the factory in the injector
48-
DependencyInjection.addInjectionMethod(HomeController.class, param -> {return new HomeController();});
49-
DependencyInjection.addInjectionMethod(LeftMenuController.class, param -> {return new LeftMenuController();});
51+
DependencyInjection.addInjectionMethod(HomeController.class, param -> {
52+
return new HomeController();
53+
});
54+
DependencyInjection.addInjectionMethod(LeftMenuController.class, param -> {
55+
return new LeftMenuController();
56+
});
5057
}
5158
}
Lines changed: 81 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
package root.common.server.implement;
22

3+
import java.io.IOException;
34
import java.io.InputStream;
45
import java.util.Properties;
56

7+
import org.apache.commons.io.IOUtils;
8+
69
import com.jcraft.jsch.Channel;
710
import com.jcraft.jsch.ChannelExec;
811
import com.jcraft.jsch.JSch;
912
import com.jcraft.jsch.JSchException;
1013
import com.jcraft.jsch.Session;
1114

15+
import lombok.extern.slf4j.Slf4j;
1216
import root.core.domain.JschConnectionInfo;
1317

18+
@Slf4j
1419
public class JschServer {
1520
private JSch jsch;
1621
private Session session;
@@ -23,77 +28,128 @@ public JschServer(JschConnectionInfo jschConnectionInfo) {
2328
public void init() {
2429
jsch = new JSch();
2530
session = null;
26-
31+
2732
try {
2833
session = jsch.getSession(jschConnectionInfo.getUserName(), jschConnectionInfo.getHost(),
2934
Integer.valueOf(jschConnectionInfo.getPort()));
3035
session.setPassword(jschConnectionInfo.getPassword());
31-
36+
3237
Properties config = new Properties();
3338
config.put("StrictHostKeyChecking", "no"); // 호스트 정보를 검사하지 않는다.
39+
config.put("PreferredAuthentications", "password");
3440
session.setConfig(config);
35-
41+
3642
} catch (JSchException e) {
37-
System.out.println("JSch Session Creation Faild!");
38-
e.printStackTrace();
43+
log.error(e.getMessage());
3944
}
4045
}
41-
46+
4247
public Session getSession() {
43-
if(session == null) {
48+
if (session == null) {
4449
return null;
4550
}
4651
return session;
4752
}
48-
49-
public Session connect(Session session) {
53+
54+
public Session connect(Session session) throws JSchException {
55+
if(session == null) {
56+
throw new NullPointerException("Session is null");
57+
}
58+
59+
if(session.isConnected()) {
60+
return session;
61+
}
62+
5063
try {
5164
session.connect();
5265
} catch (JSchException e) {
53-
System.out.println("JSch Connection Faild!");
54-
e.printStackTrace();
66+
log.error(e.getMessage());
67+
throw e;
5568
}
69+
5670
return session;
5771
}
58-
72+
5973
public void disConnect(Session session) {
74+
if(session == null) {
75+
throw new NullPointerException("Session is null");
76+
}
77+
6078
session.disconnect();
6179
}
62-
80+
6381
public Channel openExecChannel(Session session, String command) {
82+
if(session == null) {
83+
init();
84+
try {
85+
session = this.connect(this.getSession());
86+
} catch (JSchException e) {
87+
log.error(e.getMessage());
88+
}
89+
}
90+
6491
Channel channel = null;
6592
try {
6693
channel = session.openChannel("exec");
67-
//채널접속
68-
ChannelExec channelExec = (ChannelExec) channel; //명령 전송 채널사용
69-
channelExec.setPty(true);
70-
channelExec.setCommand(command);
94+
// 채널접속
95+
ChannelExec channelExec = (ChannelExec) channel; // 명령 전송 채널사용
96+
// channelExec.setPty(true);
97+
channelExec.setCommand(command);
7198
} catch (JSchException e) {
72-
System.out.println("Channel Open Faild!");
73-
// e.printStackTrace();
99+
log.error(e.getMessage());
74100
}
75101
return channel;
76102
}
77103

104+
private Channel openExecChannel(String command) {
105+
return openExecChannel(session, command);
106+
}
107+
78108
public InputStream connectChannel(Channel channel) {
79109
InputStream in = null;
80110
try {
81111
// CallBack
82112
in = channel.getInputStream();
83-
((ChannelExec) channel).setErrStream(System.err);
84-
113+
((ChannelExec) channel).setErrStream(System.err);
114+
85115
channel.connect();
86116
} catch (Exception e) {
87-
System.out.println("Channel Connect Failed!");
117+
log.error(e.getMessage());
88118
}
89119
return in;
90120
}
91-
121+
92122
public void disConnectChannel(Channel channel) {
93-
channel.disconnect();
123+
channel.disconnect();
94124
}
95-
125+
96126
public String getServerName() {
97127
return this.jschConnectionInfo.getServerName();
98128
}
129+
130+
public String executeCommand(String command) throws JSchException, IOException {
131+
log.debug(command);
132+
Channel channel = openExecChannel(command);
133+
InputStream in = connectChannel(channel);
134+
String result = IOUtils.toString(in, "UTF-8");
135+
disConnectChannel(channel);
136+
disConnect(session);
137+
return result.trim();
138+
}
139+
140+
public static boolean validateConn(Session session) {
141+
if (session == null) {
142+
log.error("JSch session is null");
143+
return false;
144+
}
145+
146+
try {
147+
session.connect(3000);
148+
} catch (JSchException e) {
149+
log.error(e.getMessage());
150+
return false;
151+
}
152+
153+
return session.isConnected();
154+
}
99155
}

src/main/java/root/core/batch/ServerCheckBatch.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
package root.core.batch;
22

33
import root.core.domain.AlertLogCommand;
4-
import root.core.domain.AlertLogCommandPeriod;
5-
import root.core.usecase.constracts.ServerCheckUsecase;
4+
import root.core.usecase.constracts.ServerMonitoringUsecase;
65

76
public class ServerCheckBatch {
8-
private ServerCheckUsecase serverCheckUsecase;
7+
private ServerMonitoringUsecase serverCheckUsecase;
98

10-
public ServerCheckBatch(ServerCheckUsecase serverCheckUsecase) {
9+
public ServerCheckBatch(ServerMonitoringUsecase serverCheckUsecase) {
1110
this.serverCheckUsecase = serverCheckUsecase;
1211
}
1312

@@ -19,9 +18,9 @@ public void startBatchAlertLogCheck(AlertLogCommand alc) {
1918
}
2019
}
2120

22-
public void startBatchAlertLogCheckDuringPeriod(AlertLogCommandPeriod alcp) {
21+
public void startBatchAlertLogCheckDuringPeriod(AlertLogCommand alc, String startDate, String endDate) {
2322
try {
24-
this.serverCheckUsecase.printAlertLogDuringPeriod(alcp);
23+
this.serverCheckUsecase.printAlertLogDuringPeriod(alc, startDate, endDate);
2524
}catch(Exception e) {
2625
e.printStackTrace();
2726
}

src/main/java/root/core/domain/AlertLogCommand.java

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,27 @@
66
@NoArgsConstructor
77
@Data
88
public class AlertLogCommand {
9-
private String readCommand;
10-
private String readLine;
9+
private int readLine;
1110
private String readFilePath;
1211
private String dateFormat;
1312
private String dateFormatRegex;
1413
private String[] catchErrorMsg;
1514

16-
public AlertLogCommand(String readCommand, String readLine, String readFilePath) {
17-
this.readCommand = readCommand;
15+
public AlertLogCommand(int readLine, String readFilePath) {
1816
this.readLine = readLine;
1917
this.readFilePath = readFilePath;
2018
}
2119

22-
public AlertLogCommand(String readCommand, String readLine, String readFilePath, String dateFormat, String dateFormatRegex) {
23-
this.readCommand = readCommand;
20+
public AlertLogCommand(int readLine, String readFilePath, String dateFormat, String dateFormatRegex) {
2421
this.readLine = readLine;
2522
this.readFilePath = readFilePath;
2623
this.dateFormat = dateFormat;
2724
this.dateFormatRegex = dateFormatRegex;
2825
}
2926

30-
public AlertLogCommand(String readCommand, String readLine, String readFilePath, String... catchErrorMsg) {
31-
this.readCommand = readCommand;
27+
public AlertLogCommand(int readLine, String readFilePath, String... catchErrorMsg) {
3228
this.readLine = readLine;
3329
this.readFilePath = readFilePath;
3430
this.catchErrorMsg = catchErrorMsg;
3531
}
36-
37-
public String getCommand() {
38-
return this.getReadCommand() + " -" + this.getReadLine() + " " + this.getReadFilePath();
39-
}
4032
}

0 commit comments

Comments
 (0)