Skip to content

Commit

Permalink
update: mdns, monitor 프로세스 종료되면 에러 내용을 담은 ERROR 수준 로그를 한 번만 남기도록 함
Browse files Browse the repository at this point in the history
  • Loading branch information
inferior3x committed Dec 4, 2024
1 parent 5d6fdc7 commit d19e1bf
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,15 @@ public String readErrorLine(){
}
}

public String readErrorLines(){
StringBuilder sb = new StringBuilder();
String line;
while((line = readErrorLine()) != null){
sb.append(line).append("\n");
}
return sb.toString();
}

public boolean isAlive(){
return this.process.isAlive();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

//TODO: 에러 로그 어떻게 관리할지 생각. 일단 TransientProcess라서 구현 안함
@Slf4j
@Component
public class ArpLogWriter {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

//TODO: tshark가 종료되지 않더라도 오류가 발생할 수 있으려나? 이 경우에도 로깅이 필요하긴 함
@Slf4j
@Component
public class MdnsLogWriter {
private final Map<ManagedInfo, MdnsLogProcess> processes;
private final Map<ManagedInfo, Boolean> wasDead;
private final MdnsLogParser parser;
private final ManagedLogDAO dao;
private final String sudoPassword;
Expand All @@ -25,11 +26,13 @@ public MdnsLogWriter(ManagedLogDAO dao, MdnsLogParser parser, NetworkConfig conf
this.dao = dao;
this.parser = parser;
this.sudoPassword = sudoPassword;
this.processes = config.getMdnsList().parallelStream()
.collect(Collectors.toMap(
managedInfo -> managedInfo,
managedInfo -> new MdnsLogProcess(managedInfo, sudoPassword)
));
this.processes = new HashMap<>();
this.wasDead = new HashMap<>();
config.getMdnsList().parallelStream()
.forEach(managedInfo -> {
this.processes.put(managedInfo, new MdnsLogProcess(managedInfo, sudoPassword));
this.wasDead.put(managedInfo, false);
});
}

@Scheduled(initialDelay = 10000, fixedDelay = 10000)
Expand All @@ -39,7 +42,10 @@ private void writeLogs() {
ManagedInfo managedInfo = entry.getKey();
MdnsLogProcess process = entry.getValue();
boolean alive = process.isAlive();
if (!alive) log.error("[managed - mdns({})] dead", managedInfo.ssid());
if (!alive && wasDead.get(managedInfo).equals(Boolean.FALSE)) {
wasDead.put(managedInfo, true);
log.error("[managed - mdns({})] dead :\n{}", managedInfo.ssid(), process.readErrorLines());
}
return alive;})
.flatMap(entry -> {
ManagedInfo managedInfo = entry.getKey();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
@Component
public class MonitorLogWriter {
private MonitorLogProcess process; //교체될 수 있으므로 final X
private Boolean wasDead = false;
private final MonitorLogParser parser;
private final MonitorLogDAO repo;
private final String sudoPassword;
Expand All @@ -27,7 +28,10 @@ public MonitorLogWriter(MonitorLogParser parser, MonitorLogDAO repo, NetworkConf
@Scheduled(initialDelay = 10000, fixedDelay = 10000)
private void saveLogs(){
if (!process.isAlive()) {
log.error("[monitor] dead");
if (wasDead.equals(Boolean.FALSE)) {
log.error("[monitor] dead:\n{}", process.readErrorLines());
wasDead = true;
}
return;
}
Set<String> macs = new HashSet<>();
Expand Down

0 comments on commit d19e1bf

Please sign in to comment.