Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

여러가지 작업... #58

Merged
merged 12 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class ManagedLog extends BaseEntity {
@EmbeddedId
private LogId logId;

@Column(nullable = false)
private String ssid;

private String deviceName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,21 @@

@Getter
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
public class ManagedLog {
public final class ManagedLog {
@EqualsAndHashCode.Include
private final String mac;
@EqualsAndHashCode.Include
private final String ip;
private final String deviceName;
@Setter //Parser에선 로그로부터 ssid를 얻을 수 없기 때문에 추후에 설정할 수 있도록 함
private String ssid;
private final LocalDateTime createdAt;
//같은 기기더라도 맥, 아이피가 다른 로그들이 발생 가능.
//따라서 마지막에 발생한 로그를 알아내기 위해 ms는 버려지는 CURRENT_TIMESTAMP 대신 로그에서 발생 시각 추출
private final LocalDateTime createdAt = LocalDateTime.now();

public ManagedLog(String mac, String ip, String deviceName) {
this.mac = mac;
this.ip = ip;
this.deviceName = deviceName;
this.createdAt = LocalDateTime.now();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Collection;
import java.util.List;
import lombok.RequiredArgsConstructor;
Expand All @@ -12,28 +13,25 @@

@Component
@RequiredArgsConstructor
public class ManagedLogDAO {
public final class ManagedLogDAO {

private final JdbcTemplate jdbcTemplate;

public void insertAll(Collection<ManagedLog> logs) {
if(logs.size() > 0) {
List<ManagedLog> logList = logs.stream().toList();

String sql = new StringBuilder()
.append("INSERT INTO " + "managed_log")
.append("(managed_log_mac, created_date, updated_date, managed_log_device_name, managed_log_ip, managed_log_wifi_ssid)")
.append("values (?, now(), now(), ?, ?, ?)")
.append(" ON DUPLICATE KEY UPDATE updated_date = CURRENT_TIMESTAMP")
.toString();

try{
batchExecute(sql, logList);
} catch(DuplicateKeyException e){
System.err.println("[ERROR] Duplicate key: "+ e.getMessage());
} catch (Exception e){
System.err.println("[ERROR] Unexpected error: "+ e.getMessage());
}
if (logs.isEmpty()) return;
List<ManagedLog> logList = logs.stream().toList();

String sql = "INSERT INTO managed_log " +
"(mac, created_at, updated_at, device_name, ip, ssid) " +
"values (?, ?, ?, ?, ?, ?) " +
"ON DUPLICATE KEY UPDATE updated_at = ?";

try{
batchExecute(sql, logList);
} catch(DuplicateKeyException e){
System.err.println("[ERROR] Duplicate key: "+ e.getMessage());
} catch (Exception e){
System.err.println("[ERROR] Unexpected error: "+ e.getMessage());
}

}
Expand All @@ -42,11 +40,18 @@ private void batchExecute(String sql, List<ManagedLog> logList){
jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
@Override
public void setValues(PreparedStatement preparedStatement, int i) throws SQLException {
ManagedLog networkLog = logList.get(i);
preparedStatement.setString(1, networkLog.getMac());
preparedStatement.setString(2, networkLog.getDeviceName());
preparedStatement.setString(3, networkLog.getIp());
preparedStatement.setString(4, networkLog.getSsid());
ManagedLog networkLog = logList.get(i);
//같은 기기더라도 맥, 아이피가 다른 로그들이 발생 가능.
//따라서 마지막에 발생한 로그를 알아내기 위해 ms는 버려지는 CURRENT_TIMESTAMP 대신 로그에서 발생 시각 추출
String logTime = Timestamp.valueOf(networkLog.getCreatedAt()).toString();
preparedStatement.setString(1, networkLog.getMac());
preparedStatement.setString(2, logTime);
preparedStatement.setString(3, logTime);
preparedStatement.setString(4, networkLog.getDeviceName());
preparedStatement.setString(5, networkLog.getIp());
preparedStatement.setString(6, networkLog.getSsid());
preparedStatement.setString(7, logTime);

}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import org.springframework.stereotype.Component;

@Component
public class ArpLogParser {
public final class ArpLogParser {

private final String macRegex = "^([0-9A-Fa-f]{2}([-:])(?=[0-9A-Fa-f]{2}([-:]))[0-9A-Fa-f]{2}([-:])){5}[0-9A-Fa-f]{2}$|^[0-9A-Fa-f]{4}(\\.[0-9A-Fa-f]{4}){2}$";
private final String ipRegex = "^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import org.springframework.stereotype.Component;

@Component
public class ArpLogProcess {
public final class ArpLogProcess {

private final String command;
private final String password;
Expand All @@ -30,7 +30,7 @@ public ArpLogProcess(
}

// 단발성 프로세스 이므로, start 를 호출하면 arp 로그를 받아오는 프로세스를 실행
public Set<String> start(){
public Set<String> run(){
Set<String> logs = new HashSet<>();

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.whoz_in.log_writer.managed.ManagedLogDAO;
import java.util.Collections;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
import org.springframework.scheduling.annotation.Scheduled;
Expand All @@ -30,24 +29,18 @@ public ArpLogWriter(ManagedLogDAO managedLogDAO,

@Scheduled(fixedRate = 5000)
private void scan() {
//TODO: 비동기 제거
CompletableFuture<Set<String>> logCollect =
CompletableFuture.supplyAsync(arpLogProcess::start);

Set<String> result = logCollect.join();
logs.addAll(result);
logs.addAll(arpLogProcess.run());

save();
}

//TODO: LogEntity들 저장
private void save(){
Set<ManagedLog> managedLogs = logs.stream()
.filter(arpLogParser::validate)
.map(arpLogParser::parse)
.collect(Collectors.toSet());

System.out.println(String.format("[arp] 저장할 로그 개수 : %d", managedLogs.size()));
System.out.println(String.format("[managed - arp] 저장할 로그 개수 : %d", managedLogs.size()));

managedLogDAO.insertAll(managedLogs);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import com.whoz_in.log_writer.managed.ManagedLog;
import com.whoz_in.log_writer.managed.ManagedLogDAO;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
Expand All @@ -28,23 +30,28 @@ public MdnsLogWriter(ManagedLogDAO managedLogDAO, MdnsLogParser parser, MdnsConf

@Scheduled(fixedRate = 10000)
private void writeLogs() {
Set<ManagedLog> logs = new HashSet<>();
Map<ManagedLog, ManagedLog> logs = new HashMap<>();
this.processes.parallelStream()
.forEach(process-> {
String line;
for(;;) {
try {
line = process.readLine();
if (line == null) return;
parser.parse(line).ifPresent(logs::add);
parser.parse(line).ifPresent(
log -> {
logs.put(log, log);
//TODO: ssid 설정
}
);
} catch (Exception e){
e.printStackTrace();
}
}
});
System.out.println("[managed] 저장할 로그 개수 : " + logs.size());
System.out.println("[managed - mdns] 저장할 로그 개수 : " + logs.size());

managedLogDAO.insertAll(logs);
managedLogDAO.insertAll(logs.values());

logs.clear();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class MonitorLogDAO {
public void upsertAll(Collection<String> macs) {
if (macs.isEmpty()) return;

//monitor 로그는 발생했는지가 중요하기 때문에 ms가 버려지는 CURRENT_TIMESTAMP를 써도 괜찮음
String sql = "INSERT INTO monitor_log (mac, created_at, updated_at) VALUES (?, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP) ON DUPLICATE KEY UPDATE updated_at = CURRENT_TIMESTAMP";
jdbcTemplate.batchUpdate(sql, macs, macs.size(),
(ps, mac) -> ps.setString(1, mac));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,8 @@ private void saveLogs(){
e.printStackTrace();
}
}

System.out.println("[monitor] 저장할 mac 개수: " + macs.size());
macs.remove("");
System.out.println("[monitor] 저장할 mac 개수: " + macs.size());
repo.upsertAll(macs);
macs.clear();
}
Expand Down

This file was deleted.

This file was deleted.